1

I'm trying to RLP encode a transaction I get from the mempool to get the signed transaction hex using ethersjs.

Here's how the tx looks like initially:

{
  "blockHash": null,
  "blockNumber": null,
  "from": "0x71a218a64d658a3b4dadd11d0bb50047bf25d1db",
  "gas": "0x186a0",
  "gasPrice": "0x28fa6ae00",
  "hash": "0xcc2eed93b30609bb98372992d4403c1cbc17039fe7e1bef5dc54beb93f6e28d8",
  "input": "0xa9059cbb000000000000000000000000477b8d5ef7c2c42db84deb555419cd817c336b6f00000000000000000000000000000000000000000000000000000000416a5c67",
  "nonce": "0xe",
  "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
  "transactionIndex": null,
  "value": "0x0",
  "type": "0x0",
  "chainId": "0x1",
  "v": "0x26",
  "r": "0x595eca735685a58e520caa7f38854205a300a7979c1c671337372d52c626eab8",
  "s": "0xd80d68a96d8200b38c5f44ad09d843396770a367a7cabce01755896bf717ae6"
}

And I try to encode it like this:

const params = [
      tx.nonce,
      tx.gasPrice,
      tx.gas,
      tx.to,
      tx.value,
      tx.input,
      tx.v,
      tx.r,
      tx.s,
];

const encodedSignedTx = ethers.utils.RLP.encode(params);

However some of the hex strings like nonce and value are not even length so I keep getting hex data is odd-length ... errors.

I tried using hexZeroPad but the length is not fixed. How can I force the hex values to be even without adding too many extra zeroes?

1 Answer 1

0

I ended up using ethers.BigNumber to "normalise" all hex values (except the to address):

const params = [
  ethers.BigNumber.from(tx.nonce)._hex,
  ethers.BigNumber.from(tx.gasPrice)._hex,
  ethers.BigNumber.from(tx.gas)._hex,
  //...
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.