Skip to content

Commit 83e591a

Browse files
authored
Merge pull request #1042 from ethereumjs/tx-fix-isSigned
Fix isSigned returning true for unsigned txs
2 parents 5953e09 + 7a804e8 commit 83e591a

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

packages/tx/src/transaction.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,19 @@ export default class Transaction {
5959

6060
const [nonce, gasPrice, gasLimit, to, value, data, v, r, s] = values
6161

62+
const emptyBuffer = Buffer.from([])
63+
6264
return new Transaction(
6365
{
6466
nonce: new BN(nonce),
6567
gasPrice: new BN(gasPrice),
6668
gasLimit: new BN(gasLimit),
6769
to: to && to.length > 0 ? new Address(to) : undefined,
6870
value: new BN(value),
69-
data: data || Buffer.from([]),
70-
v: v ? new BN(v) : undefined,
71-
r: r ? new BN(r) : undefined,
72-
s: s ? new BN(s) : undefined,
71+
data: data ?? emptyBuffer,
72+
v: !v?.equals(emptyBuffer) ? new BN(v) : undefined,
73+
r: !r?.equals(emptyBuffer) ? new BN(r) : undefined,
74+
s: !s?.equals(emptyBuffer) ? new BN(s) : undefined,
7375
},
7476
opts
7577
)
@@ -89,9 +91,9 @@ export default class Transaction {
8991
this.to = to ? new Address(toBuffer(to)) : undefined
9092
this.value = new BN(toBuffer(value))
9193
this.data = toBuffer(data)
92-
this.v = new BN(toBuffer(v))
93-
this.r = new BN(toBuffer(r))
94-
this.s = new BN(toBuffer(s))
94+
this.v = v ? new BN(toBuffer(v)) : undefined
95+
this.r = r ? new BN(toBuffer(r)) : undefined
96+
this.s = s ? new BN(toBuffer(s)) : undefined
9597

9698
const validateCannotExceedMaxInteger = {
9799
nonce: this.nonce,

packages/tx/test/api.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,43 @@ tape('[Transaction]: Basic functions', function (t) {
335335
st.end()
336336
})
337337

338+
t.test('returns correct values for isSigned', function (st) {
339+
let tx = Transaction.fromTxData({})
340+
st.notOk(tx.isSigned())
341+
342+
const txData: TxData = {
343+
data: '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',
344+
gasLimit: '0x15f90',
345+
gasPrice: '0x1',
346+
nonce: '0x01',
347+
to: '0xd9024df085d09398ec76fbed18cac0e1149f50dc',
348+
value: '0x0',
349+
}
350+
const privateKey = Buffer.from(
351+
'4646464646464646464646464646464646464646464646464646464646464646',
352+
'hex'
353+
)
354+
tx = Transaction.fromTxData(txData)
355+
st.notOk(tx.isSigned())
356+
tx = tx.sign(privateKey)
357+
st.ok(tx.isSigned())
358+
359+
tx = new Transaction(txData)
360+
st.notOk(tx.isSigned())
361+
const rawUnsigned = tx.serialize()
362+
tx = tx.sign(privateKey)
363+
const rawSigned = tx.serialize()
364+
st.ok(tx.isSigned())
365+
366+
tx = Transaction.fromRlpSerializedTx(rawUnsigned)
367+
st.notOk(tx.isSigned())
368+
tx = tx.sign(privateKey)
369+
st.ok(tx.isSigned())
370+
tx = Transaction.fromRlpSerializedTx(rawSigned)
371+
st.ok(tx.isSigned())
372+
st.end()
373+
})
374+
338375
t.test(
339376
'throws when creating a a transaction with incompatible chainid and v value',
340377
function (st) {

packages/vm/tests/api/runTx.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tape('runTx', (t) => {
3333
t.test('should fail to run without signature', async (st) => {
3434
const tx = getTransaction(false)
3535
shouldFail(st, suite.runTx({ tx }), (e: Error) =>
36-
st.ok(e.message.includes('Invalid Signature'), 'should fail with appropriate error')
36+
st.ok(e.message.includes('not signed'), 'should fail with appropriate error')
3737
)
3838
st.end()
3939
})

0 commit comments

Comments
 (0)