Skip to content

Commit c91b566

Browse files
ScottyPoiholgerd77
authored andcommitted
Tx: Throw when hash() is called on unsigned legacy transaction (#1894)
* tx: edit legacyTransaction to throw when hash() called on unsigned tx * tx: adapt legacy.spec test to throw on unsigned tx.hash() * tx: edit tests which call hash() on unsigned tx
1 parent ac053e1 commit c91b566

File tree

3 files changed

+10
-40
lines changed

3 files changed

+10
-40
lines changed

packages/tx/src/legacyTransaction.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,10 @@ export default class Transaction extends BaseTransaction<Transaction> {
258258
* Use {@link Transaction.getMessageToSign} to get a tx hash for the purpose of signing.
259259
*/
260260
hash(): Buffer {
261-
// In contrast to the tx type transaction implementations the `hash()` function
262-
// for the legacy tx does not throw if the tx is not signed.
263-
// This has been considered for inclusion but lead to unexpected backwards
264-
// compatibility problems (no concrete reference found, needs validation).
265-
//
266-
// For context see also https://github.com/ethereumjs/ethereumjs-monorepo/pull/1445,
267-
// September, 2021 as well as work done before.
268-
//
269-
// This should be updated along the next major version release by adding:
270-
//
271-
//if (!this.isSigned()) {
272-
// const msg = this._errorMsg('Cannot call hash method if transaction is not signed')
273-
// throw new Error(msg)
274-
//}
261+
if (!this.isSigned()) {
262+
const msg = this._errorMsg('Cannot call hash method if transaction is not signed')
263+
throw new Error(msg)
264+
}
275265

276266
if (Object.isFrozen(this)) {
277267
if (!this.cache.hash) {

packages/tx/test/inputValue.spec.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import tape from 'tape'
2-
import {
3-
Address,
4-
AddressLike,
5-
BigIntLike,
6-
BufferLike,
7-
bufferToHex,
8-
toBuffer,
9-
} from 'ethereumjs-util'
2+
import { Address, AddressLike, BigIntLike, BufferLike, toBuffer } from 'ethereumjs-util'
103
import Common, { Chain, Hardfork } from '@ethereumjs/common'
114
import { Transaction } from '../src'
125

@@ -112,12 +105,10 @@ tape('[Transaction Input Values]', function (t) {
112105
const legacyTxData = generateCombinations({
113106
options,
114107
})
115-
const expectedHash = Transaction.fromTxData(legacyTxData[0]).hash()
116108
const randomSample = getRandomSubarray(legacyTxData, 100)
117109
for (const txData of randomSample) {
118110
const tx = Transaction.fromTxData(txData, { common })
119-
const hash = tx.hash()
120-
st.deepEqual(hash, expectedHash, `correct tx hash (0x${bufferToHex(hash)})`)
111+
t.throws(() => tx.hash(), 'tx.hash() throws if tx is unsigned')
121112
}
122113
st.end()
123114
})
@@ -133,14 +124,11 @@ tape('[Transaction Input Values]', function (t) {
133124
const eip1559TxData = generateCombinations({
134125
options,
135126
})
136-
const expectedHash = Transaction.fromTxData(eip1559TxData[0]).hash()
137127
const randomSample = getRandomSubarray(eip1559TxData, 100)
138128

139129
for (const txData of randomSample) {
140130
const tx = Transaction.fromTxData(txData, { common })
141-
const hash = tx.hash()
142-
143-
st.deepEqual(hash, expectedHash, `correct tx hash (0x${bufferToHex(hash)})`)
131+
t.throws(() => tx.hash(), 'tx.hash() should throw if unsigned')
144132
}
145133
st.end()
146134
})

packages/tx/test/legacy.spec.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,13 @@ tape('[Transaction]', function (t) {
230230
chain: Chain.Mainnet,
231231
hardfork: Hardfork.TangerineWhistle,
232232
})
233-
// Test currently commented out, see comment on legacy tx hash() function
234-
/*let tx = Transaction.fromValuesArray(txFixtures[3].raw.slice(0, 6).map(toBuffer), {
233+
234+
let tx = Transaction.fromValuesArray(txFixtures[3].raw.slice(0, 6).map(toBuffer), {
235235
common,
236236
})
237237
st.throws(() => {
238238
tx.hash()
239-
}, 'should throw calling hash with unsigned tx')*/
240-
let tx = Transaction.fromValuesArray(txFixtures[3].raw.map(toBuffer), {
241-
common,
242-
freeze: false,
243-
})
244-
st.deepEqual(
245-
tx.hash(),
246-
Buffer.from('375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa', 'hex')
247-
)
239+
}, 'should throw calling hash with unsigned tx')
248240
tx = Transaction.fromValuesArray(txFixtures[3].raw.map(toBuffer), {
249241
common,
250242
})

0 commit comments

Comments
 (0)