andelf / tronpy

TRON Python Client Library.
MIT License
204 stars 96 forks source link

verify message tronlink #51

Closed smartDev22 closed 1 year ago

smartDev22 commented 2 years ago

Could you show an example of message verify in tronpy? I sign messages like this: const original_message = "hello" const signedtxn = await tronWeb.trx.sign(original_message);

mvshvets commented 2 years ago

Now I'm also faced with a difficulty here.

The signature is done on the frontend. Next, I try to verify its authenticity using a public key:

public_key = PublicKey(bytes.fromhex(address))
public_key.verify_msg(body.nonce.encode(), Signature(body.sign.encode()))

My code fails on public_key = PublicKey(...

File "/Users/maksim/Projects/rc/backend/./routes/http/tron/tron.py", line 39, in verify
    public_key = PublicKey(bytes.fromhex(body.address))
  File "/Users/maksim/.local/share/virtualenvs/backend-QY53YioO/lib/python3.9/site-packages/tronpy/keys/__init__.py", line 153, in __init__
    raise BadKey
tronpy.exceptions.BadKey

What am I doing wrong?

MrNaif2018 commented 1 year ago
from tronpy import keys

def normalize_address(self, address):
    return keys.to_base58check_address(address)

def verifymessage(address, signature, message):
        return keys.PublicKey.recover_from_msg(
            message.encode("utf-8"), keys.Signature.fromhex(signature)
        ).to_base58check_address() == normalize_address(address)

https://github.com/bitcartcc/bitcart/blob/11e80ef0aec82a05ceaeca38649b184e46c354d8/daemons/trx.py#L299-L303