Skip to content

Commit be27b71

Browse files
tx: simplify getMessageToSign
tx: add fromSerializedTx method tx: remove TODOs tx: add fromSerializedTx to legacy transaction
1 parent 861d1c0 commit be27b71

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

packages/tx/src/eip2930Transaction.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
7171
return new EIP2930Transaction(txData, opts)
7272
}
7373

74-
// Instantiate a transaction from the raw RLP serialized tx. This means that the RLP should start with 0x01.
75-
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
74+
// Instantiate a transaction from the serialized tx. This means that the Buffer should start with 0x01.
75+
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
7676
if (serialized[0] !== 1) {
7777
throw new Error(
7878
`Invalid serialized tx input: not an EIP-2930 transaction (wrong tx type, expected: 1, received: ${serialized[0]}`
@@ -87,6 +87,12 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
8787
return EIP2930Transaction.fromValuesArray(values, opts)
8888
}
8989

90+
// Instantiate a transaction from the serialized tx. This means that the Buffer should start with 0x01.
91+
// Alias of fromSerializedTx
92+
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
93+
return EIP2930Transaction.fromSerializedTx(serialized, opts)
94+
}
95+
9096
// Create a transaction from a values array.
9197
// The format is: chainId, nonce, gasPrice, gasLimit, to, value, data, access_list, [yParity, senderR, senderS]
9298
public static fromValuesArray(values: (Buffer | AccessListBuffer)[], opts: TxOptions = {}) {
@@ -129,8 +135,6 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
129135
throw new Error('EIP-2930 not enabled on Common')
130136
}
131137

132-
// TODO: verify the signature.
133-
134138
// check the type of AccessList. If it's a JSON-type, we have to convert it to a buffer.
135139

136140
let usedAccessList
@@ -217,16 +221,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
217221
}
218222

219223
getMessageToSign() {
220-
const base = [
221-
bnToRlp(this.chainId),
222-
bnToRlp(this.nonce),
223-
bnToRlp(this.gasPrice),
224-
bnToRlp(this.gasLimit),
225-
this.to !== undefined ? this.to.buf : Buffer.from([]),
226-
bnToRlp(this.value),
227-
this.data,
228-
this.accessList,
229-
]
224+
const base = this.raw(true).slice(0, 8)
230225
return keccak256(Buffer.concat([Buffer.from('01', 'hex'), rlp.encode(base)]))
231226
}
232227

@@ -322,7 +317,6 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
322317
}
323318

324319
public hash(): Buffer {
325-
// TODO add decorator
326320
if (!this.isSigned()) {
327321
throw new Error('Cannot call hash method if transaction is not signed')
328322
}

packages/tx/src/legacyTransaction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
4747
return this.fromValuesArray(values, opts)
4848
}
4949

50+
// alias of fromRlpSerializedTx
51+
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
52+
return LegacyTransaction.fromRlpSerializedTx(serialized, opts)
53+
}
54+
5055
public static fromValuesArray(values: Buffer[], opts: TxOptions = {}) {
5156
if (values.length !== 6 && values.length !== 9) {
5257
throw new Error(

packages/tx/test/base.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ tape('[BaseTransaction]', function (t) {
105105
txType.class.fromRlpSerializedTx(tx.serialize()),
106106
`${txType.name}: should do roundtrip serialize() -> fromRlpSerializedTx()`
107107
)
108+
st.ok(
109+
txType.class.fromSerializedTx(tx.serialize()),
110+
`${txType.name}: should do roundtrip serialize() -> fromRlpSerializedTx()`
111+
)
108112
})
109113
}
110114
st.end()

0 commit comments

Comments
 (0)