2021-08-24 14:54:15 -03:00
|
|
|
_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
|
2021-10-04 10:58:10 -04:00
|
|
|
const txFields = "accessList chainId data gasPrice gasLimit maxFeePerGas maxPriorityFeePerGas nonce to type value".split(" ");
|
2021-08-24 14:54:15 -03:00
|
|
|
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;
|
|
|
|
}
|