tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
413 stars 271 forks source link

`Failed to sign transaction by v2: Invalid transaction provided` when signing a hash message in browser #447

Closed alienzhangyw closed 9 months ago

alienzhangyw commented 9 months ago

I have a hashed message to be signed: 0x4a3264ece439ed1a76ba692380cce39da2eb570b78b0572259fe8c0e528f30e6, According to TronWeb doc: https://tronweb.network/docu/docs/Sign%20and%20Verify%20Message

If you only want to sign a hash, you should convert your hash string to an Bytes Array first using TronWeb.utils.ethersUtils.arrayify utility function.

So I use TronWeb.utils.ethersUtils.arrayify to turn it to a Uint8Array with a length of 32. Then pass the Uint8Array to TronLinkAdapter.signMessage. Then I got this error from TronLink browser plugin: Failed to sign transaction by v2: Invalid transaction provided. image

Please tell me what is wrong.

svein1010 commented 9 months ago

It seems that it's a problem of TronLink Extension. I tried with my own tronWeb instance and it works fine. Please construct a new tronWeb instance and try the signMessageV2 method.

alienzhangyw commented 9 months ago

It seems that it's a problem of TronLink Extension. I tried with my own tronWeb instance and it works fine. Please construct a new tronWeb instance and try the signMessageV2 method.

signMessageV2 works fine in Nodejs environment. But shouldn't I use the TronLink injected tronWeb instance in browser? I'm developing a DAPP. I need to connect to user's wallet then sign messages and transactions.

svein1010 commented 9 months ago

Can you just sign the hash as a plain string? The following codes work fine.

const str = '0x4a3264ece439ed1a76ba692380cce39da2eb570b78b0572259fe8c0e528f30e6';
const res =  await tronWeb.trx.signMessageV2(str);
const verifiedAddress = await tronWeb.trx.verifyMessageV2(str, res);
console.log('signed message: ', res, 'is valid: ', verifiedAddress === tronWeb.defaultAddress.base58);

It seems that TronLink does not process signMessageV2() correctly as same as TronWeb library. Also you can open an issue in TronLink developer community.

alienzhangyw commented 9 months ago

Can you just sign the hash as a plain string? The following codes work fine.

const str = '0x4a3264ece439ed1a76ba692380cce39da2eb570b78b0572259fe8c0e528f30e6';
const res =  await tronWeb.trx.signMessageV2(str);
const verifiedAddress = await tronWeb.trx.verifyMessageV2(str, res);
console.log('signed message: ', res, 'is valid: ', verifiedAddress === tronWeb.defaultAddress.base58);

It seems that TronLink does not process signMessageV2() correctly as same as TronWeb library. Also you can open an issue in TronLink developer community.

This is ok but we're verifing signature in smart contract, which can only treat the hash as byte32, not string type. Actually the hash itself is generated by the contract, if I treat it as a normal string in front-end, then the signature verification in contract will be failed.

svein1010 commented 9 months ago

We focus on TronWeb library. You could contact TronLink community to report this issue

alienzhangyw commented 9 months ago

I also reported this in the TronLink dev TG group. But nobody answers except some frauds.

alienzhangyw commented 9 months ago

Now I'm using signMessageV1