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
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: