iexbase / tron-api-python

A Python API for interacting with Tron (TRX)
https://tronapi-for-python.readthedocs.io
MIT License
166 stars 84 forks source link

Verify message Tronlink #45

Closed smartDev22 closed 5 years ago

smartDev22 commented 5 years ago

Good afternoon, show me please an example of verification of the message signed tronlink. After signing I get a string of this type:

0x267363b76ac81738e22272c01e212483402f43474f8ac175420d563703470ce74486c6188f9da9ef27bad17ed7ef63797d11337be8b147fe1093de62cca66a7f1c

пытаюсь делать так: tron.trx.verify_message(message=tron.toHex('123'), signed_message='0x267363b76ac81738e22272c01e212483402f43474f8ac175420d563703470ce74486c6188f9da9ef27bad17ed7ef63797d11337be8b147fe1093de62cca66a7f1c', address='TAQByCrjDDWrMWdDJqTFs5sPnvGb85q1cy') but in response I get an error

"Unsupported type: The primitive argument must be one of: bytes," TypeError: Unsupported type: The primitive argument must be one of: bytes,bytearray, int or bool and not str

serderovsh commented 5 years ago
message = tron.toHex(text='hello')
signed = tron.trx.sign(message)

# verify
tron.trx.verify_message(message, signed)

https://github.com/iexbase/tron-api-python/blob/master/tronapi/trx.py#L656

smartDev22 commented 5 years ago

Using your code I get an error:

Traceback (most recent call last): File "<input>", line 1, in <module> File "/*****/python3.6/site-packages/tronapi/trx.py", line 541, in sign message_hash = self.tron.sha3( AttributeError: 'Tron' object has no attribute 'sha3'

here is the complete code that i use:

from tronapi import Tron
full_node = 'https://api.shasta.trongrid.io'
solidity_node = 'https://api.shasta.trongrid.io'
event_server = 'https://api.shasta.trongrid.io'
tron = Tron(full_node=full_node,
        solidity_node=solidity_node,
        event_server=event_server)
message = tron.toHex ( text = ' hello ' )
signed = tron.trx.sign(message)
serderovsh commented 5 years ago

https://github.com/iexbase/tron-api-python/commit/e1e11e55575cee9fe39f7da8f049195a1182308a

oldnote commented 5 years ago

Confirmation of the message signature always fails in my case. Used following code:

Code ```python TRON_SETTINGS = { 'full_node': 'https://api.shasta.trongrid.io', 'solidity_node': 'https://api.shasta.trongrid.io', 'event_server': 'https://api.shasta.trongrid.io', } tron = tronapi.Tron(**TRON_SETTINGS) account = tron.create_account # XXX: create_account is property. tron.private_key = account.private_key tron.default_address = account.address.base58 message = tron.toHex(text='hello') signed = tron.trx.sign(message) tron.trx.verify_message(message, signed) ```

Always getting ValueError: Signature does not match.

oldnote commented 5 years ago

Always getting ValueError: Signature does not match.

I guess there's a bug either in trx.sign either in trx.verify_message method.

trx.verify_message have following code for header:

        ...
        # Determine which header to attach to the message
        # before encrypting or decrypting
        header = TRX_MESSAGE_HEADER if use_tron else ETH_MESSAGE_HEADER

        message_hash = self.tron.keccak(text=header+message)
        ...

When trx.sign one line more header += str(len(transaction)):

        ...
            # Determine which header to attach to the message
            # before encrypting or decrypting
            header = TRX_MESSAGE_HEADER if use_tron else ETH_MESSAGE_HEADER
            header += str(len(transaction))

            message_hash = self.tron.keccak(text=header+transaction)
       ...

Adding header += str(len(message)) string into verify_message fixes the problem.

oldnote commented 5 years ago

Created separate issue for this - https://github.com/iexbase/tron-api-python/issues/49.