ethers.js/docs.wrm/cookbook/transactions.wrm
2021-10-04 10:58:10 -04:00

25 lines
799 B
Plaintext

_section: Transactions @<cookbook--transactions>
_subsection: Compute the raw transaction @<cookbook--compute-raw-transaction>
_code: @lang<javascript>
function getRawTransaction(tx) {
function addKey(accum, key) {
if (tx[key]) { accum[key] = tx[key]; }
return accum;
}
// Extract the relevant parts of the transaction and signature
const txFields = "accessList chainId data gasPrice gasLimit maxFeePerGas maxPriorityFeePerGas nonce to type value".split(" ");
const sigFields = "v r s".split(" ");
// Seriailze the signed transaction
const raw = utils.serializeTransaction(txFields.reduce(addKey, { }), sigFields.reduce(addKey, { }));
// Double check things went well
if (utils.keccak256(raw) !== tx.hash) { throw new Error("serializing failed!"); }
return raw;
}