From bdb54ac52bd58b535aaff0fb366bc91828ecaa21 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 24 Aug 2021 14:54:15 -0300 Subject: [PATCH] docs: added cookbook entry to compute raw transaction (#1857). --- docs.wrm/api/providers/types.wrm | 4 +++- docs.wrm/cookbook/index.wrm | 1 + docs.wrm/cookbook/transactions.wrm | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 docs.wrm/cookbook/transactions.wrm diff --git a/docs.wrm/api/providers/types.wrm b/docs.wrm/api/providers/types.wrm index 698a3b950..7e66c3ffb 100644 --- a/docs.wrm/api/providers/types.wrm +++ b/docs.wrm/api/providers/types.wrm @@ -266,7 +266,9 @@ The number of blocks that have been mined (including the initial block) since th transaction was mined. _property: transaction.raw => string<[[DataHexString]]> -The serialized transaction. +The serialized transaction. This may be null as some backends do not +rpopulate it. If this is required, it can be computed from a **TransactionResponse** +object using [this cookbook recipe](cookbook--compute-raw-transaction). _property: transaction.wait([ confirms = 1 ]) => Promise<[[providers-TransactionReceipt]]> Resolves to the [[providers-TransactionReceipt]] once the transaction diff --git a/docs.wrm/cookbook/index.wrm b/docs.wrm/cookbook/index.wrm index 1a7d732cf..f142a603e 100644 --- a/docs.wrm/cookbook/index.wrm +++ b/docs.wrm/cookbook/index.wrm @@ -6,4 +6,5 @@ snippets of code that are in general useful. _toc: react-native + transactions diff --git a/docs.wrm/cookbook/transactions.wrm b/docs.wrm/cookbook/transactions.wrm new file mode 100644 index 000000000..ca2716486 --- /dev/null +++ b/docs.wrm/cookbook/transactions.wrm @@ -0,0 +1,24 @@ +_section: Transactions @ + +_subsection: Compute the raw transaction @ + +_code: @lang + +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 maxFeePerGer 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; +}