Skip to content

Commit 97c30f1

Browse files
authored
Merge pull request #1077 from ethereumjs/fix-tx-isSigned-fromValuesArray
Tx: fix assignment of v,r,s in fromValuesArray
2 parents 22c8a4a + 87c0ae6 commit 97c30f1

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

packages/tx/src/transaction.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export default class Transaction {
6969
to: to && to.length > 0 ? new Address(to) : undefined,
7070
value: new BN(value),
7171
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,
72+
v: v && !v.equals(emptyBuffer) ? new BN(v) : undefined,
73+
r: r && !r.equals(emptyBuffer) ? new BN(r) : undefined,
74+
s: s && !s.equals(emptyBuffer) ? new BN(s) : undefined,
7575
},
7676
opts
7777
)
@@ -135,19 +135,7 @@ export default class Transaction {
135135
* Computes a sha3-256 hash of the serialized tx
136136
*/
137137
hash(): Buffer {
138-
const values = [
139-
bnToRlp(this.nonce),
140-
bnToRlp(this.gasPrice),
141-
bnToRlp(this.gasLimit),
142-
this.to !== undefined ? this.to.buf : Buffer.from([]),
143-
bnToRlp(this.value),
144-
this.data,
145-
this.v ? bnToRlp(this.v) : Buffer.from([]),
146-
this.r ? bnToRlp(this.r) : Buffer.from([]),
147-
this.s ? bnToRlp(this.s) : Buffer.from([]),
148-
]
149-
150-
return rlphash(values)
138+
return rlphash(this.raw())
151139
}
152140

153141
getMessageToSign() {
@@ -383,14 +371,7 @@ export default class Transaction {
383371
}
384372

385373
private _getMessageToSign(withEIP155: boolean) {
386-
const values = [
387-
bnToRlp(this.nonce),
388-
bnToRlp(this.gasPrice),
389-
bnToRlp(this.gasLimit),
390-
this.to !== undefined ? this.to.buf : Buffer.from([]),
391-
bnToRlp(this.value),
392-
this.data,
393-
]
374+
const values = this.raw().slice(0, 6)
394375

395376
if (withEIP155) {
396377
values.push(toBuffer(this.getChainId()))
@@ -405,7 +386,7 @@ export default class Transaction {
405386
* Validates tx's `v` value
406387
*/
407388
private _validateTxV(v: BN | undefined): void {
408-
if (v === undefined || v.toNumber() === 0) {
389+
if (v === undefined) {
409390
return
410391
}
411392

packages/tx/test/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ tape('[Transaction]: Basic functions', function (t) {
380380
st.ok(tx.isSigned())
381381
tx = Transaction.fromRlpSerializedTx(rawSigned)
382382
st.ok(tx.isSigned())
383+
384+
const signedValues = (rlp.decode(rawSigned) as any) as Buffer[]
385+
tx = Transaction.fromValuesArray(signedValues)
386+
st.ok(tx.isSigned())
387+
tx = Transaction.fromValuesArray(signedValues.slice(0, 6))
388+
st.notOk(tx.isSigned())
383389
st.end()
384390
})
385391

0 commit comments

Comments
 (0)