Skip to content

Commit

Permalink
tx: decode EIP2930 transactions from block
Browse files Browse the repository at this point in the history
tx: fix undefined v/r/s/yParity values
  • Loading branch information
jochem-brouwer committed Jan 28, 2021
1 parent d058ac0 commit e846a70
Show file tree
Hide file tree
Showing 16 changed files with 3,244 additions and 11 deletions.
11 changes: 6 additions & 5 deletions packages/tx/src/EIP2930Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ export class EIP2930Transaction extends BaseTransaction<JsonEIP2930Tx, EIP2930Tr
value: new BN(value),
data: data ?? emptyBuffer,
accessList: accessList ?? emptyBuffer,
yParity: !yParity?.equals(emptyBuffer)
? parseInt(yParity.toString('hex'), 16)
: undefined,
r: !r?.equals(emptyBuffer) ? new BN(r) : undefined,
s: !s?.equals(emptyBuffer) ? new BN(s) : undefined,
yParity:
yParity !== undefined && !yParity.equals(emptyBuffer)
? parseInt(yParity.toString('hex'), 16)
: undefined,
r: r !== undefined && !r?.equals(emptyBuffer) ? new BN(r) : undefined,
s: s !== undefined && !s?.equals(emptyBuffer) ? new BN(s) : undefined,
},
opts ?? {}
)
Expand Down
25 changes: 25 additions & 0 deletions packages/tx/src/transactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export default class TransactionFactory {
// It is not possible to instantiate a TransactionFactory object.
private constructor() {}

/**
* This method tries to decode `raw` data. It is somewhat equivalent to `fromRlpSerializedTx`.
* However, it could be that the data is not directly RLP-encoded (it is a Typed Transaction)
* @param rawData - The raw data buffer
* @param transactionOptions - The transaction options
*/
public static fromRawData(rawData: Buffer, transactionOptions: TxOptions): Transaction {
const common = transactionOptions.common ?? DEFAULT_COMMON
if (rawData[0] <= 0x7f) {
Expand Down Expand Up @@ -38,6 +44,25 @@ export default class TransactionFactory {
}
}

/**
* When decoding a BlockBody, in the transactions field, a field is either:
* A Buffer (a TypedTransaction - encoded as TransactionType || rlp(TransactionPayload))
* A Buffer[] (LegacyTransaction)
* This method returns the right transaction.
* @param rawData - Either a Buffer or a Buffer[]
* @param transactionOptions - The transaction options
*/
public static fromBlockBodyData(rawData: Buffer | Buffer[], transactionOptions: TxOptions) {
if (Buffer.isBuffer(rawData)) {
return this.fromRawData(rawData, transactionOptions)
} else if (Array.isArray(rawData)) {
// It is a LegacyTransaction
return LegacyTransaction.fromValuesArray(rawData, transactionOptions)
} else {
throw new Error('Cannot decode transaction: unknown type input')
}
}

/**
* This helper method allows one to retrieve the class which matches the transactionID
* If transactionID is undefined, return the LegacyTransaction class.
Expand Down
6 changes: 3 additions & 3 deletions packages/tx/src/unsignedLegacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export class LegacyTransaction extends BaseTransaction<JsonLegacyTx, LegacyTrans
to: to && to.length > 0 ? new Address(to) : undefined,
value: new BN(value),
data: data ?? emptyBuffer,
v: !v?.equals(emptyBuffer) ? new BN(v) : undefined,
r: !r?.equals(emptyBuffer) ? new BN(r) : undefined,
s: !s?.equals(emptyBuffer) ? new BN(s) : undefined,
v: v !== undefined && !v?.equals(emptyBuffer) ? new BN(v) : undefined,
r: r !== undefined && !r?.equals(emptyBuffer) ? new BN(r) : undefined,
s: s !== undefined && !s?.equals(emptyBuffer) ? new BN(s) : undefined,
},
opts
)
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/test/eip2930.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Common from '@ethereumjs/common'
import { privateToAddress } from 'ethereumjs-util'
import { privateToAddress, rlp } from 'ethereumjs-util'
import tape from 'tape'
import { EIP2930Transaction } from '../src'
import { EIP2930Transaction, TransactionFactory } from '../src'

const pKey = Buffer.from('4646464646464646464646464646464646464646464646464646464646464646', 'hex')
const address = privateToAddress(pKey)
Expand Down
42 changes: 42 additions & 0 deletions packages/tx/test/json/eip2930/acl_block_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"rlp": "f90263f901f5a0151b04645af991f513d5e11f8ed62e12b73f080af9c7a6a3d98fd6b1503f23faa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a0968408b8a2f170bc78230cc2fac3881120a93f7392cf26b609566d8648abfd79a0e8137f2b67ac4680d8103f8b1cd7c05c49f56e4df464b0a79253679f38df9ab4a07f53535270d749f41b9d783aa1abcfceb73cc2a16ecde789cd3bc97a42fbda2ab901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018347e7c48262740a80a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f868f866800a8301e24194000000000000000000000000000000000000aaaa808086f2ded8deec88a0e7545c5664e63873d7aeb4bb5f92152c3366a9095c9140e5f8453af0998194bea04d24143ac9bc97c0aa07a7074c12ab60484d83d3b447b1e5e6ba5cbf421675bfc0",
"json": {
"header": {
"parentHash": "0x151b04645af991f513d5e11f8ed62e12b73f080af9c7a6a3d98fd6b1503f23fa",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0x0100000000000000000000000000000000000000",
"stateRoot": "0x968408b8a2f170bc78230cc2fac3881120a93f7392cf26b609566d8648abfd79",
"transactionsRoot": "0xe8137f2b67ac4680d8103f8b1cd7c05c49f56e4df464b0a79253679f38df9ab4",
"receiptsRoot": "0x7f53535270d749f41b9d783aa1abcfceb73cc2a16ecde789cd3bc97a42fbda2a",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x20000",
"number": "0x1",
"gasLimit": "0x47e7c4",
"gasUsed": "0x6274",
"timestamp": "0xa",
"extraData": "0x",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"hash": "0x9aa9cdb2afc4ebf893c32b67dedf1dea4291673bf4e80d3227a4e7f8d0feaf68"
},
"transactions": [
{
"type": "0x0",
"chainId": null,
"nonce": "0x0",
"gasPrice": "0xa",
"gas": "0x1e241",
"to": "0x000000000000000000000000000000000000aaaa",
"value": "0x0",
"input": "0x",
"accessList": null,
"v": "0xf2ded8deec88",
"r": "0xe7545c5664e63873d7aeb4bb5f92152c3366a9095c9140e5f8453af0998194be",
"s": "0x4d24143ac9bc97c0aa07a7074c12ab60484d83d3b447b1e5e6ba5cbf421675bf",
"hash": "0x7e122173adcc02b9a3c94da426f65c7d46e724e8e3fdb16be73d5e0405e280b6"
}
],
"uncles": null,
"receipts": null
}
}
65 changes: 65 additions & 0 deletions packages/tx/test/json/eip2930/acl_block_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"rlp": "f9032df901f5a09aa9cdb2afc4ebf893c32b67dedf1dea4291673bf4e80d3227a4e7f8d0feaf68a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a0fb70f6c0512e7979903607bba8a4f1b18c2dd1060a8eff1b777a2182d05af524a0dfcad019a36acdf0f3fac6b3db02bf1d70012a0bd3eadabea87b856be9a6f1cca0a7a57ba954834e69b55d9115305f14f7faff9beb83443f7bb5526585848c6aefb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000028347e7c482dd201480a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f90131b8c701f8c486796f6c6f7632010a8301e24194000000000000000000000000000000000000aaaa8080f85bf859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000001a0a9efa6b5fd3c2f210c4ec7232058d8dd30c93a456b4f6074429fb09bbd73eb4fa070632eff8bf0933381917c9161a98494e34fc7b836ee150445cae9ab151d267bf866020a8301e24194000000000000000000000000000000000000aaaa808086f2ded8deec88a055a39da3ce02c75e976ad84f608771080811a5ef322c6b6536c362f716c60985a018ab5df4198436eacd0e991f59bdeb8da9a75c678067db6b1a4d07881555c7a8c0",
"json": {
"header": {
"parentHash": "0x9aa9cdb2afc4ebf893c32b67dedf1dea4291673bf4e80d3227a4e7f8d0feaf68",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0x0100000000000000000000000000000000000000",
"stateRoot": "0xfb70f6c0512e7979903607bba8a4f1b18c2dd1060a8eff1b777a2182d05af524",
"transactionsRoot": "0xdfcad019a36acdf0f3fac6b3db02bf1d70012a0bd3eadabea87b856be9a6f1cc",
"receiptsRoot": "0xa7a57ba954834e69b55d9115305f14f7faff9beb83443f7bb5526585848c6aef",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x20000",
"number": "0x2",
"gasLimit": "0x47e7c4",
"gasUsed": "0xdd20",
"timestamp": "0x14",
"extraData": "0x",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"hash": "0xe1807e7366284bc72d34c8f02318f81c6598454dbff4c6cdd71e872cd8d5748c"
},
"transactions": [
{
"type": "0x1",
"chainId": "0x796f6c6f7632",
"nonce": "0x1",
"gasPrice": "0xa",
"gas": "0x1e241",
"to": "0x000000000000000000000000000000000000aaaa",
"value": "0x0",
"input": "0x",
"accessList": [
{
"address": "0x0000000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
}
],
"v": "0x1",
"r": "0xa9efa6b5fd3c2f210c4ec7232058d8dd30c93a456b4f6074429fb09bbd73eb4f",
"s": "0x70632eff8bf0933381917c9161a98494e34fc7b836ee150445cae9ab151d267b",
"hash": "0x1916413ac8d1d5ee4eda786e5fe29509a109910f800894e0e18eb84abb7564cb"
},
{
"type": "0x0",
"chainId": null,
"nonce": "0x2",
"gasPrice": "0xa",
"gas": "0x1e241",
"to": "0x000000000000000000000000000000000000aaaa",
"value": "0x0",
"input": "0x",
"accessList": null,
"v": "0xf2ded8deec88",
"r": "0x55a39da3ce02c75e976ad84f608771080811a5ef322c6b6536c362f716c60985",
"s": "0x18ab5df4198436eacd0e991f59bdeb8da9a75c678067db6b1a4d07881555c7a8",
"hash": "0x54c6cfb51cbbe2c562e17a0d6ee1324f1b50013a9b9d47589cedcf0ec4ac342a"
}
],
"uncles": null,
"receipts": null
}
}
102 changes: 102 additions & 0 deletions packages/tx/test/json/eip2930/acl_block_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"rlp": "f904b1f901f6a0e1807e7366284bc72d34c8f02318f81c6598454dbff4c6cdd71e872cd8d5748ca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a025e5b4b7668760c0aa01d44c061d77e24b53f6d247d3e1d85a91e23646c1d679a0be53a36dd90786b4cd30fb0f3677ad3ce5f804a5615872a7b0512b6be82639a2a076946d9f034a6657c6832a6ad4df3ef0ddf3fa9f3f8be98ea64880fb23bd23e3b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000038347e7c48301883c1e80a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f902b4b9012301f9011f86796f6c6f7632030a8301e24194000000000000000000000000000000000000aaaa8080f8b6f859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000f859940100000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000000a0010000000000000000000000000000000000000000000000000000000000000001a0635c44b8110989ef84c77de11a377f19cbd84660559ed76b37ba0f7fdee1fb40a012e1576d168301614720c68f1a9e9d5ed20e23622dab6c3587b11177155d59f4b9012301f9011f86796f6c6f7632040a8301e24194000000000000000000000000000000000000aaaa8080f8b6f859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000f859940100000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000000a0010000000000000000000000000000000000000000000000000000000000000001a00380e446bf3992ee1df44b8177a11e948dc52cc3faa8eae059a52b7183126c0fa05206f0f35648d3b8f23092d4d7bdccb926967d840a269cf65b706a8a359b9029f866050a8301e24194000000000000000000000000000000000000aaaa808086f2ded8deec87a0ea5de382dbf422aa1bd156547a2bc3a9357d745a68cdc9203b14812307068567a0388f7425b873ae204d43d91f4997ca41d2af6759be49538b174a06f67d5741b5c0",
"json": {
"header": {
"parentHash": "0xe1807e7366284bc72d34c8f02318f81c6598454dbff4c6cdd71e872cd8d5748c",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0x0100000000000000000000000000000000000000",
"stateRoot": "0x25e5b4b7668760c0aa01d44c061d77e24b53f6d247d3e1d85a91e23646c1d679",
"transactionsRoot": "0xbe53a36dd90786b4cd30fb0f3677ad3ce5f804a5615872a7b0512b6be82639a2",
"receiptsRoot": "0x76946d9f034a6657c6832a6ad4df3ef0ddf3fa9f3f8be98ea64880fb23bd23e3",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x20000",
"number": "0x3",
"gasLimit": "0x47e7c4",
"gasUsed": "0x1883c",
"timestamp": "0x1e",
"extraData": "0x",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"hash": "0x1615129d2aa7b437cd40e3aa1a5794af9e40b07b9b017014e4123cefeef785ce"
},
"transactions": [
{
"type": "0x1",
"chainId": "0x796f6c6f7632",
"nonce": "0x3",
"gasPrice": "0xa",
"gas": "0x1e241",
"to": "0x000000000000000000000000000000000000aaaa",
"value": "0x0",
"input": "0x",
"accessList": [
{
"address": "0x0000000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
},
{
"address": "0x0100000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0100000000000000000000000000000000000000000000000000000000000000"
]
}
],
"v": "0x1",
"r": "0x635c44b8110989ef84c77de11a377f19cbd84660559ed76b37ba0f7fdee1fb40",
"s": "0x12e1576d168301614720c68f1a9e9d5ed20e23622dab6c3587b11177155d59f4",
"hash": "0x54471f503901f2c2fe392e43df0377a11f9a1cd9cde0ec5c68c89eac1723e02a"
},
{
"type": "0x1",
"chainId": "0x796f6c6f7632",
"nonce": "0x4",
"gasPrice": "0xa",
"gas": "0x1e241",
"to": "0x000000000000000000000000000000000000aaaa",
"value": "0x0",
"input": "0x",
"accessList": [
{
"address": "0x0000000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
},
{
"address": "0x0100000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0100000000000000000000000000000000000000000000000000000000000000"
]
}
],
"v": "0x1",
"r": "0x380e446bf3992ee1df44b8177a11e948dc52cc3faa8eae059a52b7183126c0f",
"s": "0x5206f0f35648d3b8f23092d4d7bdccb926967d840a269cf65b706a8a359b9029",
"hash": "0xe3a1354e84d22ebbca0c55751937b075083f0f0e3a47c65fc7249d86f872f268"
},
{
"type": "0x0",
"chainId": null,
"nonce": "0x5",
"gasPrice": "0xa",
"gas": "0x1e241",
"to": "0x000000000000000000000000000000000000aaaa",
"value": "0x0",
"input": "0x",
"accessList": null,
"v": "0xf2ded8deec87",
"r": "0xea5de382dbf422aa1bd156547a2bc3a9357d745a68cdc9203b14812307068567",
"s": "0x388f7425b873ae204d43d91f4997ca41d2af6759be49538b174a06f67d5741b5",
"hash": "0x44b1e9999ac02a3b0daf1391be16bb00a262079706418697094bab5f37b522d7"
}
],
"uncles": null,
"receipts": null
}
}
Loading

0 comments on commit e846a70

Please sign in to comment.