Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP1559 + EIP2718 support #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/ethObject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { encode, toHex } = require('eth-util-lite')

class EthObject extends Array{

set objtype(typeIn) {
this.type = typeIn;
}
constructor(fields, raw){
//raw array
super(...raw)
Expand All @@ -27,8 +29,14 @@ class EthObject extends Array{
get: function(){ return toHex(this.buffer) },
});
Object.defineProperty(this, 'raw', {
// only commit to data from fields that exist, since we have multiple transaction formats now we will be missing data depending on the tx type
// so we need to skip so the root's are correctly calculated still
get: function(){
return this.fields.map((field, i)=>{ return this[i] })
return this.fields.filter((field, i) => {
return this[i] !== undefined;
}).map((field, i) => {
return this[i];
})
},
});
Object.defineProperty(this, 'object', {
Expand All @@ -50,8 +58,22 @@ class EthObject extends Array{
}

// aliases
serialize(){ return this.buffer }
toBuffer() { return this.buffer }
serialize(){
// if type is defined, concat type and RLP buffer seperately (for receipts/transactions following EIP2718)
if(this.type) {
return Buffer.concat([this.type, this.buffer ])
} else {
return this.buffer
}
}
toBuffer(){
// if type is defined, concat type and RLP buffer seperately (for receipts/transactions following EIP2718)
if(this.type) {
return Buffer.concat([this.type, this.buffer ])
} else {
return this.buffer
}
}
toHex(){ return this.hex }
toObject(){ return this.object }
toString(){ return this.json }
Expand Down
10 changes: 8 additions & 2 deletions src/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Header extends EthObject{
'extraData',
'mixHash',
'nonce',
'baseFeePerGas',
]}

constructor(raw = this.NULL){
Expand All @@ -31,7 +32,7 @@ class Header extends EthObject{
static fromObject(rpcResult){ return this.fromRpc(rpcResult) }
static fromRpc(rpcResult){
if(rpcResult){
return new this([
let data = [
toBuffer(rpcResult.parentHash),
toBuffer(rpcResult.sha3Uncles) || KECCAK256_RLP_ARRAY,
toBuffer(rpcResult.miner),
Expand All @@ -47,7 +48,12 @@ class Header extends EthObject{
toBuffer(rpcResult.extraData),
toBuffer(rpcResult.mixHash),
toBuffer(rpcResult.nonce)
])
]
// https://eips.ethereum.org/EIPS/eip-1559
if(rpcResult.baseFeePerGas){
data.push(toBuffer(rpcResult.baseFeePerGas));
}
return new this(data)
Comment on lines +51 to +56
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good here

}else{
return new this()
}
Expand Down
25 changes: 22 additions & 3 deletions src/receipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,40 @@ class Receipt extends EthObject{
super(Receipt.fields, raw)
}

static fromBuffer(buf){ return buf ? new Receipt(decode(buf)) : new Receipt() }
static fromHex(hex){ return hex ? new Receipt(decode(hex)) : new Receipt() }
static fromBuffer(buf){
if(buf) {
// marker for EIP2718 TX Envelope type - anything under is type followed by RLP, over is standard RLP
if(buf[0] > 0x7f) {
return new Receipt(decode(buf))
} else {
let receipt = new Receipt(decode(buf.slice(1)))
receipt.objtype = toBuffer(buf[0])
return receipt
}
} else {
return new Receipt()
}
}
static fromHex(hex){
return this.fromBuffer(toBuffer(hex))
}
static fromRaw(raw){ return new Receipt(raw) }
static fromObject(rpcResult){ return Receipt.fromRpc(rpcResult) }
static fromRpc(rpcResult){
let logs = []
for (var i = 0; i < rpcResult.logs.length; i++) {
logs.push(Log.fromRpc(rpcResult.logs[i]))
}
return new Receipt([
let receipt = new Receipt([
toBuffer(rpcResult.status || rpcResult.root),
toBuffer(rpcResult.cumulativeGasUsed),
toBuffer(rpcResult.logsBloom),
logs
])
if(rpcResult.type) {
receipt.objtype = toBuffer(rpcResult.type)
}
return receipt
}
}

Expand Down
103 changes: 89 additions & 14 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ const EthObject = require('./ethObject')
class Transaction extends EthObject{

static get fields(){ return [
'chainId',
'nonce',
'gasPrice',
'maxPriorityFeePerGas',
'maxFeePerGas',
'gasLimit',
'to',
'value',
'data',
'accessList',
'v',
'r',
's',
Comment on lines 6 to 18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a problem here because these new fields knock the ordering off when generating objects that dont include them

Copy link
Author

@sidhujag sidhujag Oct 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this code on proofs using eth mainnet which has a collection of legacy/version 1/version 2 transactions, it does create properly, can you give example where it would break?

Comment on lines +7 to 18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll need to have some conditionals here for backwards compatibility.

Copy link
Author

@sidhujag sidhujag Oct 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it only encode fields that are set in the object and skips over the ones that aren't? The proofs are valid across all the tx types.

Expand All @@ -18,23 +21,95 @@ class Transaction extends EthObject{
constructor(raw = Transaction.NULL){
super(Transaction.fields, raw)
}
static fromBuffer(buf){
if(buf) {
// marker for EIP2718 TX Envelope type - anything under is type followed by RLP, over is standard RLP
if(buf[0] > 0x7f) {
return new Transaction(decode(buf))
} else {
let tx = new Transaction(decode(buf.slice(1)))
tx.objtype = toBuffer(buf[0])
return tx
}
} else {
return new Transaction()
}
}

static fromBuffer(buf){ return buf ? new Transaction(decode(buf)) : new Transaction() }
static fromHex(hex){ return hex ? new Transaction(decode(hex)) : new Transaction() }
static fromHex(hex){
return this.fromBuffer(toBuffer(hex))
}
static fromRaw(raw){ return new Transaction(raw) }
static fromObject(rpcResult){ return Transaction.fromRpc(rpcResult) }
static fromRpc(rpcResult){
return new Transaction([
toBuffer(rpcResult.nonce),
toBuffer(rpcResult.gasPrice),
toBuffer(rpcResult.gas || rpcResult.gasLimit),
toBuffer(rpcResult.to),
toBuffer(rpcResult.value),
toBuffer(rpcResult.input || rpcResult.data),
toBuffer(rpcResult.v),
toBuffer(rpcResult.r),
toBuffer(rpcResult.s)
])
// see definitions: https://eips.ethereum.org/EIPS/eip-1559
// https://eips.ethereum.org/EIPS/eip-2718 1559-payload
if(rpcResult.type == "0x2") {
let accessList = []
for (var i = 0; i < rpcResult.accessList.length; i++) {
var listObjAtIndex = rpcResult.accessList[i]
var storageKeys = []
for (var j = 0; j < listObjAtIndex.storageKeys.length; j++) {
storageKeys.push(toBuffer(listObjAtIndex.storageKeys[j]))
}
accessList.push([toBuffer(listObjAtIndex.address), storageKeys])
}
let tx = new Transaction([
toBuffer(rpcResult.chainId),
toBuffer(rpcResult.nonce),
toBuffer(rpcResult.maxPriorityFeePerGas),
toBuffer(rpcResult.maxFeePerGas),
toBuffer(rpcResult.gas || rpcResult.gasLimit),
toBuffer(rpcResult.to),
toBuffer(rpcResult.value),
toBuffer(rpcResult.input || rpcResult.data),
accessList,
toBuffer(rpcResult.v),
toBuffer(rpcResult.r),
toBuffer(rpcResult.s)
])
tx.objtype = toBuffer(rpcResult.type)
return tx
// https://eips.ethereum.org/EIPS/eip-2718 2930-payload
} else if(rpcResult.type == "0x1") {
let accessList = []
for (var i = 0; i < rpcResult.accessList.length; i++) {
var listObjAtIndex = rpcResult.accessList[i]
var storageKeys = []
for (var j = 0; j < listObjAtIndex.storageKeys.length; j++) {
storageKeys.push(toBuffer(listObjAtIndex.storageKeys[j]))
}
accessList.push([toBuffer(listObjAtIndex.address), storageKeys])
}
let tx = new Transaction([
toBuffer(rpcResult.chainId),
toBuffer(rpcResult.nonce),
toBuffer(rpcResult.gasPrice),
toBuffer(rpcResult.gas || rpcResult.gasLimit),
toBuffer(rpcResult.to),
toBuffer(rpcResult.value),
toBuffer(rpcResult.input || rpcResult.data),
accessList,
toBuffer(rpcResult.v),
toBuffer(rpcResult.r),
toBuffer(rpcResult.s)
])
tx.objtype = toBuffer(rpcResult.type)
return tx
}
else {
return new Transaction([
toBuffer(rpcResult.nonce),
toBuffer(rpcResult.gasPrice),
toBuffer(rpcResult.gas || rpcResult.gasLimit),
toBuffer(rpcResult.to),
toBuffer(rpcResult.value),
toBuffer(rpcResult.input || rpcResult.data),
toBuffer(rpcResult.v),
toBuffer(rpcResult.r),
toBuffer(rpcResult.s)
])
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.