Skip to content

Commit 405cadf

Browse files
committed
use type AccessListEIP2930ValuesArray and some tidying up
1 parent 2b13383 commit 405cadf

File tree

7 files changed

+32
-28
lines changed

7 files changed

+32
-28
lines changed

packages/block/src/block.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,14 @@ export class Block {
149149
}
150150

151151
/**
152-
* Returns a Buffer Array of the raw Buffers of this block, in order.
152+
* Returns a Buffer Array of the raw Buffers of this block, in order.
153153
*/
154154
raw(): BlockBuffer {
155155
return [
156156
this.header.raw(),
157-
this.transactions.map((tx) => <Buffer[]>tx.raw()),
157+
this.transactions.map((tx) =>
158+
'transactionType' in tx && tx.transactionType > 0 ? tx.serialize() : tx.raw()
159+
) as Buffer[],
158160
this.uncleHeaders.map((uh) => uh.raw()),
159161
]
160162
}

packages/block/src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export interface BlockData {
100100
export type BlockBuffer = [BlockHeaderBuffer, TransactionsBuffer, UncleHeadersBuffer]
101101
export type BlockHeaderBuffer = Buffer[]
102102
export type BlockBodyBuffer = [TransactionsBuffer, UncleHeadersBuffer]
103-
export type TransactionsBuffer = Buffer[][]
103+
/**
104+
* TransactionsBuffer can be an array of serialized txs for Typed Transactions or an array of Buffer Arrays for legacy transactions.
105+
*/
106+
export type TransactionsBuffer = Buffer[][] | Buffer[]
104107
export type UncleHeadersBuffer = Buffer[][]
105108

106109
/**

packages/tx/src/baseTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ecsign,
99
publicToAddress,
1010
} from 'ethereumjs-util'
11-
import { TxData, TxOptions, JsonTx } from './types'
11+
import { TxData, TxOptions, JsonTx, AccessListEIP2930ValuesArray } from './types'
1212

1313
export abstract class BaseTransaction<TransactionObject> {
1414
public readonly nonce: BN
@@ -114,7 +114,7 @@ export abstract class BaseTransaction<TransactionObject> {
114114
/**
115115
* Returns a Buffer Array of the raw Buffers of this transaction, in order.
116116
*/
117-
abstract raw(): Buffer[]
117+
abstract raw(): Buffer[] | AccessListEIP2930ValuesArray
118118

119119
/**
120120
* Returns the encoding of the transaction.

packages/tx/src/eip2930Transaction.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
7373
}
7474

7575
const values = rlp.decode(serialized.slice(1))
76+
7677
if (!Array.isArray(values)) {
7778
throw new Error('Invalid serialized tx input: must be array')
7879
}
7980

80-
return AccessListEIP2930Transaction.fromValuesArray(values, opts)
81+
return AccessListEIP2930Transaction.fromValuesArray(values as any, opts)
8182
}
8283

8384
/**
@@ -99,16 +100,15 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
99100
* The format is:
100101
* chainId, nonce, gasPrice, gasLimit, to, value, data, access_list, yParity (v), senderR (r), senderS (s)
101102
*/
102-
public static fromValuesArray(values: (Buffer | AccessListBuffer)[], opts: TxOptions = {}) {
103+
public static fromValuesArray(values: AccessListEIP2930ValuesArray, opts: TxOptions = {}) {
103104
if (values.length !== 8 && values.length !== 11) {
104105
throw new Error(
105106
'Invalid EIP-2930 transaction. Only expecting 8 values (for unsigned tx) or 11 values (for signed tx).'
106107
)
107108
}
108109

109-
const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = <
110-
AccessListEIP2930ValuesArray
111-
>values
110+
const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = values
111+
112112
const emptyBuffer = Buffer.from([])
113113

114114
return new AccessListEIP2930Transaction(
@@ -147,7 +147,6 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
147147
let usedAccessList
148148
if (accessList && isAccessList(accessList)) {
149149
this.AccessListJSON = accessList
150-
151150
const newAccessList: AccessListBuffer = []
152151

153152
for (let i = 0; i < accessList.length; i++) {
@@ -248,7 +247,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
248247
*
249248
* Use `serialize()` to add to block data for `Block.fromValuesArray()`.
250249
*/
251-
raw(): Buffer[] {
250+
raw(): AccessListEIP2930ValuesArray {
252251
return [
253252
bnToRlp(this.chainId),
254253
bnToRlp(this.nonce),
@@ -257,7 +256,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
257256
this.to !== undefined ? this.to.buf : Buffer.from([]),
258257
bnToRlp(this.value),
259258
this.data,
260-
<any>this.accessList,
259+
this.accessList,
261260
this.v !== undefined ? bnToRlp(this.v) : Buffer.from([]),
262261
this.r !== undefined ? bnToRlp(this.r) : Buffer.from([]),
263262
this.s !== undefined ? bnToRlp(this.s) : Buffer.from([]),
@@ -268,15 +267,16 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
268267
* Returns the serialized encoding of the transaction.
269268
*/
270269
serialize(): Buffer {
271-
return Buffer.concat([Buffer.from('01', 'hex'), rlp.encode(this.raw())])
270+
const base = this.raw()
271+
return Buffer.concat([Buffer.from('01', 'hex'), rlp.encode(base as any)])
272272
}
273273

274274
/**
275275
* Computes a sha3-256 hash of the serialized unsigned tx, which is used to sign the transaction.
276276
*/
277277
getMessageToSign() {
278278
const base = this.raw().slice(0, 8)
279-
return keccak256(Buffer.concat([Buffer.from('01', 'hex'), rlp.encode(base)]))
279+
return keccak256(Buffer.concat([Buffer.from('01', 'hex'), rlp.encode(base as any)]))
280280
}
281281

282282
/**

packages/tx/src/transactionFactory.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,8 @@ export default class TransactionFactory {
8181
if (Buffer.isBuffer(data)) {
8282
return this.fromSerializedData(data, txOptions)
8383
} else if (Array.isArray(data)) {
84-
if (data.length === 6 || data.length === 9) {
85-
// It is a legacy transaction
86-
return Transaction.fromValuesArray(data, txOptions)
87-
} else if (data.length === 8 || data.length === 11) {
88-
// It is an Access List Transaction
89-
return AccessListEIP2930Transaction.fromValuesArray(data, txOptions)
90-
} else {
91-
throw new Error('Cannot decode transaction: unknown array length')
92-
}
84+
// It is a legacy transaction
85+
return Transaction.fromValuesArray(data, txOptions)
9386
} else {
9487
throw new Error('Cannot decode transaction: unknown type input')
9588
}

packages/tx/src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@ export interface TxOptions {
3131
freeze?: boolean
3232
}
3333

34+
/*
35+
* Access List types
36+
*/
37+
3438
export type AccessListItem = {
3539
address: string
3640
storageKeys: string[]
3741
}
3842

43+
/*
44+
* An Access List as a tuple of [address: Buffer, storageKeys: Buffer[]]
45+
*/
3946
export type AccessListBufferItem = [Buffer, Buffer[]]
40-
41-
export type AccessList = AccessListItem[]
4247
export type AccessListBuffer = AccessListBufferItem[]
48+
export type AccessList = AccessListItem[]
4349

4450
export function isAccessListBuffer(
4551
input: AccessListBuffer | AccessList

packages/tx/test/base.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ tape('[BaseTransaction]', function (t) {
8989
`${txType.name}: tx should not be frozen when freeze deactivated in options`
9090
)
9191

92-
tx = txType.class.fromValuesArray(txType.values, { common })
92+
tx = txType.class.fromValuesArray(txType.values as any, { common })
9393
st.ok(Object.isFrozen(tx), `${txType.name}: tx should be frozen by default`)
9494

95-
tx = txType.class.fromValuesArray(txType.values, { common, freeze: false })
95+
tx = txType.class.fromValuesArray(txType.values as any, { common, freeze: false })
9696
st.ok(
9797
!Object.isFrozen(tx),
9898
`${txType.name}: tx should not be frozen when freeze deactivated in options`

0 commit comments

Comments
 (0)