Unable to verify TronLink/TronWeb.js signed message in Python

1.4k views Asked by At

I am signing a message using TronLink/TronWeb.js but unable to verify it in Python

Python Library I am using https://github.com/iexbase/tron-api-python/

Javascript Code for Signing Message:

const original_message = "557e3517549cf8ed47d8b205654ea2a7"
const signedtxn = await tronWeb.trx.sign(original_message);
// result 0x862e16c28684bed7162e9a1dd34962882d887610de6c775054ffbad989baec65707b2ba898366c02e9f20730bc2daf54bb7e6d33d77c64f8930f8c9365f5993a1b

Python Code for Verifying Signed Message

from tronapi import Tron
from hexbytes import HexBytes

tron = Tron()

class signed_message:
     # signedtxn variable in javascript
    signature = HexBytes('0x862e16c28684bed7162e9a1dd34962882d887610de6c775054ffbad989baec65707b2ba898366c02e9f20730bc2daf54bb7e6d33d77c64f8930f8c9365f5993a1b')

original_message = "557e3517549cf8ed47d8b205654ea2a7"
address = "<!-- Tron Base58 Address in TronLink/TronWeb -->"
tron.trx.verify_message(original_message, signed_message, address=address)

I am getting this error in python

ValueError: Signature does not match

I can verify the message in TronWeb.js but can't in Python

2

There are 2 answers

0
Andelf On

You can use tronpy (yes, I am the author of tronpy.)

PublicKey.verify_msg

the last byte of your signature should be 0x01 or 0x00, which is different from ETH.

0
lovefly On

You can use web3=6.9.0 and tronpy==0.4.0

from eth_typing import ChecksumAddress, Hash32
from typing import cast
from eth_utils.curried import keccak, to_bytes
from eth_account.messages import SignableMessage
from web3.auto import w3
from tronpy.keys import to_base58check_address
def tron_recover_message(signature: bytes = None, text: str = None) -> ChecksumAddress:
    """
    :param signature 0x862e16c28684bed7162e9a1dd34962882d887610de6c775054ffbad989baec65707b2ba898366c02e9f20730bc2daf54bb7e6d33d77c64f8930f8c9365f5993a1b
    :param text sign message
    """
    try:
        signature = hex(int(signature, 16) - 27)
        message_bytes = to_bytes(primitive=None, hexstr=None, text=text)
        signable_message = SignableMessage(
            b"E",
            b"TRON Signed Message:\n32",
            message_bytes,
        )
        message_hash = Hash32(keccak(b"\x19" + signable_message.header + signable_message.body))
        return cast(ChecksumAddress, w3.eth.account._recover_hash(message_hash, None, signature))
    except Exception as e:
        print("error")

signer = to_base58check_address(tron_recover_message(text=original_message, signature=signature))