-1

This question is regarding raw ether transactions. My understanding is that the transaction is serialized to recursive length prefix(RLP), then this string is hashed using keccak256. This will be the hash of the transaction that will also be signed. 

I did a basic transaction and send it to Ganache. Then I took the raw transaction and deserialized using ethers js. I re-serialized the transaction and hashed it but the new hash is not matching the original one. 

Here is the code for this:

const ethers = require("ethers");
let ser = "0xf868800182520894ffffa0d1a599267bdf128b9e2ff3b429ef693177872386f26fc1000080820a95a00328648e71c665c09108f74166d9f6943113f722d356374d813b919f3d86f478a04e7bcd3f80afaf9684f76dfbc19ccf438544297905044fae1640a70e08296e8d";

let tx = ethers.utils.parseTransaction(ser);
console.log("Hash tx:   " + tx.hash);

delete tx["v"];
delete tx["r"];
delete tx["s"];
delete tx["from"];
delete tx["hash"];

let serialized = ethers.utils.serializeTransaction(tx);
let s = ethers.utils.keccak256(serialized);
console.log("Hash:      " + s);

The output:

Hash tx:   0xa1488cd317757f21bf3811f1d95ac0f08c6c55042d7250facaa3bd8e402c6b8e
Hash:      0x24868d74c8e983efd7924b81dcf58cf31f95946767f3e20ccac2e53e7b8f30fa
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • can you try with web3.utils.soliditysha3 : https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#soliditysha3 – Yilmaz Nov 24 '22 at 19:15
  • different libraries use different algorithms. I used `web3.utils.soliditysha3` before with no issue – Yilmaz Nov 24 '22 at 19:16
  • it really should be that case because this would make the blockchain indeterministic. I still belive I'm doing something wrong here – Liviu Stirb Nov 24 '22 at 20:14
  • https://ethereum.stackexchange.com/questions/29139/how-does-solidity-tightly-packed-arguments-work-in-sha256 – Yilmaz Nov 24 '22 at 22:08
  • if you try another library and still get a mismatch, that time you will be sure that you are doing something wrong – Yilmaz Nov 24 '22 at 22:09
  • I'm sure I'm doing something wrong. tried with erb3j and the hash matches the ethers, so it is definitely something in my approach – Liviu Stirb Nov 25 '22 at 10:22

1 Answers1

0

I figured it out. The hash of the transaction is the hash of the signed and serialized transaction.

The following code revealed the right hash:

let signedTx = await signer.signTransaction(tx);

const hash1 = ethers.utils.keccak256(signedTx);
console.log('hash1', hash1);
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40