trustwallet / wallet-core

Cross-platform, cross-blockchain wallet library.
https://developer.trustwallet.com/wallet-core
Apache License 2.0
2.81k stars 1.58k forks source link

wasm Ethereum gasPrice bug #2736

Closed icebergtsn closed 1 year ago

icebergtsn commented 1 year ago

Describe the bug I have a bug in signing on Ethereum, when I use 3b9aca00 as gasPrice there is no problem, but 5 times 12a05f200 has a bug.

To Reproduce testBSC4 = async () => {

const {CoinType, HexCoding, AnySigner} = supportCenter.walletCore;

const input = TW.Ethereum.Proto.SigningInput.create({
  toAddress: '0xd348c59e41e069c650132c0a8492c815158d067c',
  chainId: Buffer.from('01', 'hex'),
  nonce: Buffer.from('38', 'hex'),          //1000000000 //2000000000  //5000000000
  gasPrice: Buffer.from('12a05f200', 'hex'),//3b9aca00 // 77359400 //12a05f200
  gasLimit: Buffer.from('5208', 'hex'),
  transaction: TW.Ethereum.Proto.Transaction.create({
    transfer: TW.Ethereum.Proto.Transaction.Transfer.create({
      //1000000000000000000
      amount: Buffer.from('0de0b6b3a7640000', 'hex'),
    }),
  }),

  privateKey: HexCoding.decode('****'),
});

const encoded = TW.Ethereum.Proto.SigningInput.encode(input).finish()
const signed = AnySigner.sign(encoded, CoinType.smartChain);
const output = TW.Ethereum.Proto.SigningOutput.decode(signed);

const rawTx: TxData = {
  nonce: '0x38',
  gasPrice: '0x12a05f200',
  gasLimit: '0x5208',
  to: '0xd348c59e41e069c650132c0a8492c815158d067c',
  value: '0x0de0b6b3a7640000',
};
const common = Common.custom(
  { chainId: 1 },
  { hardfork: Hardfork.London }
);
const tx = new Transaction(rawTx, { common });

const signTx = tx.sign(Buffer.from('****', 'hex'));
const serializedTx = signTx.serialize();

//trust
console.log(HexCoding.encode(output.encoded))
//ethereumjs/tx
console.log('0x' + serializedTx.toString('hex'));

} }

Expected behavior Changing the price does not affect the signature result, but I am not sure under what circumstances the gasPrice will be affected

Screenshots

WeChatc6f9429f6b054f76eb1d04b31062f449 WeChat833506d7857d5207fb3f7d83677b6bae

现在改变gasPrice为12a05f200

WeChat46746a78efdc3970b29ead9825cc9a4f WeChat5397a9cf77006cb9bb375237d54aee9c

signTx is error

Additional context macOS 12.6

icebergtsn commented 1 year ago

When I use 09C7652400 instead of 9C7652400, the signature is successful and I get the correct result. This problem is also true for other gasPrices. Is there any plan to modify this problem, or how should I correctly regulate the gasPrice? Are there any other parameters that need attention?

WeChat055f85e89ae4c30c0d4eb6b5f510feed

I also found a similar answer in the historical issue #959 #973

Dstoney commented 1 year ago

How do I login to view my balance

icebergtsn commented 1 year ago

How do I login to view my balance

Although I can't understand why you replied under this issue, but do you want to know what assets are in your address? If so, you can use web3.js or ethers.js to follow the rpc documentation for queries.

hewigovens commented 1 year ago

The problem is that Buffer truncates improperly (especially for a number):

╰─$ node
Welcome to Node.js v16.15.1.
Type ".help" for more information.
> var b = Buffer.from('12a05f200', 'hex')
> b
<Buffer 12 a0 5f 20>
> var c = Buffer.from('012a05f200', 'hex')
> c
<Buffer 01 2a 05 f2 00>

You better to have some helper to pad it if needed

hewigovens commented 1 year ago

HexCoding handles it correctly btw

icebergtsn commented 1 year ago

HexCoding handles it correctly btw

Thanks, I'll try it, the code I wrote is found in the test file.

Dstoney commented 1 year ago

Hey am sorry I just don't know what wrong with my wife been Sick I need to get some money out and do some things