ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.87k stars 1.82k forks source link

Error `invalid hexlify value` with transaction type 0 #4566

Open sangnguyen1001 opened 7 months ago

sangnguyen1001 commented 7 months ago

Ethers Version

5.7.2

Search Terms

No response

Describe the Problem

I have a transaction object was populated with type 0:

tx = {"to"=>"0x3f1B32386dE69693F708204BEC1568aB6599E0e6",
 "value"=>"0x16345785d8a0000",
 "data"=>"0x",
 "type"=>0,
 "nonce"=>0,
 "gasLimit"=>{"type"=>"BigNumber", "hex"=>"0x5208"},
 "gasPrice"=>{"type"=>"BigNumber", "hex"=>"0x59682f10"},
 "from"=>"0xc9aAD004e656153e7Ea4633BbF3Dc10BcE7a50fb",
 "chainId"=>80001}

When I sign it (offline) with code below:

      const signer = new ethers.Wallet(privateKey);
      const txRaw = await signer.signTransaction(tx);

Then I get error: {"reason"=>"invalid hexlify value", "code"=>"INVALID_ARGUMENT", "argument"=>"value", "value"=>{"type"=>"BigNumber", "hex"=>"0x59682f10"}}

If I populate transaction using type 2:

{"to"=>"0x3f1B32386dE69693F708204BEC1568aB6599E0e6",
 "value"=>"0x16345785d8a0000",
 "data"=>"0x",
 "type"=>2,
 "nonce"=>0,
 "gasLimit"=>{"type"=>"BigNumber", "hex"=>"0x5208"},
 "gasPrice"=>nil,
 "from"=>"0xc9aAD004e656153e7Ea4633BbF3Dc10BcE7a50fb",
 "maxFeePerGas"=>{"type"=>"BigNumber", "hex"=>"0x59682f20"},
 "maxPriorityFeePerGas"=>{"type"=>"BigNumber", "hex"=>"0x59682f00"},
 "chainId"=>80001}

Then it will be signed ok

Plz help, thanks!

Code Snippet

No response

Contract ABI

No response

Errors

No response

Environment

node.js (v12 or newer)

Environment (Other)

No response

sangnguyen1001 commented 7 months ago

I found it, If I change tx from

tx = {"to"=>"0x3f1B32386dE69693F708204BEC1568aB6599E0e6",
 "value"=>"0x16345785d8a0000",
 "data"=>"0x",
 "type"=>0,
 "nonce"=>0,
 "gasLimit"=>{"type"=>"BigNumber", "hex"=>"0x5208"},
 "gasPrice"=>{"type"=>"BigNumber", "hex"=>"0x59682f10"},
 "from"=>"0xc9aAD004e656153e7Ea4633BbF3Dc10BcE7a50fb",
 "chainId"=>80001}

to

tx = {"to"=>"0x3f1B32386dE69693F708204BEC1568aB6599E0e6",
 "value"=>"0x16345785d8a0000",
 "data"=>"0x",
 "type"=>0,
 "nonce"=>0,
 "gasLimit"=>"0x5208",
 "gasPrice"=>"0x59682f10",
 "from"=>"0xc9aAD004e656153e7Ea4633BbF3Dc10BcE7a50fb",
 "chainId"=>80001}

it will work.

Note that I populate the tx with:

    const provider = new ethers.providers.JsonRpcProvider(nodeUrl);
    const signer = provider.getSigner(fromAddress);

    try {
      const tx = await signer.populateTransaction({
        to: toAddress,
        value: amount,
        data: "0x",
        type: 0
      });