Skip to content

Commit

Permalink
Merge pull request #1077 from ethereumjs/fix-tx-isSigned-fromValuesArray
Browse files Browse the repository at this point in the history
Tx: fix assignment of v,r,s in fromValuesArray
  • Loading branch information
holgerd77 authored Feb 12, 2021
2 parents 22c8a4a + 87c0ae6 commit 97c30f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
31 changes: 6 additions & 25 deletions packages/tx/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export default class Transaction {
to: to && to.length > 0 ? new Address(to) : undefined,
value: new BN(value),
data: data ?? emptyBuffer,
v: !v?.equals(emptyBuffer) ? new BN(v) : undefined,
r: !r?.equals(emptyBuffer) ? new BN(r) : undefined,
s: !s?.equals(emptyBuffer) ? new BN(s) : undefined,
v: v && !v.equals(emptyBuffer) ? new BN(v) : undefined,
r: r && !r.equals(emptyBuffer) ? new BN(r) : undefined,
s: s && !s.equals(emptyBuffer) ? new BN(s) : undefined,
},
opts
)
Expand Down Expand Up @@ -135,19 +135,7 @@ export default class Transaction {
* Computes a sha3-256 hash of the serialized tx
*/
hash(): Buffer {
const values = [
bnToRlp(this.nonce),
bnToRlp(this.gasPrice),
bnToRlp(this.gasLimit),
this.to !== undefined ? this.to.buf : Buffer.from([]),
bnToRlp(this.value),
this.data,
this.v ? bnToRlp(this.v) : Buffer.from([]),
this.r ? bnToRlp(this.r) : Buffer.from([]),
this.s ? bnToRlp(this.s) : Buffer.from([]),
]

return rlphash(values)
return rlphash(this.raw())
}

getMessageToSign() {
Expand Down Expand Up @@ -383,14 +371,7 @@ export default class Transaction {
}

private _getMessageToSign(withEIP155: boolean) {
const values = [
bnToRlp(this.nonce),
bnToRlp(this.gasPrice),
bnToRlp(this.gasLimit),
this.to !== undefined ? this.to.buf : Buffer.from([]),
bnToRlp(this.value),
this.data,
]
const values = this.raw().slice(0, 6)

if (withEIP155) {
values.push(toBuffer(this.getChainId()))
Expand All @@ -405,7 +386,7 @@ export default class Transaction {
* Validates tx's `v` value
*/
private _validateTxV(v: BN | undefined): void {
if (v === undefined || v.toNumber() === 0) {
if (v === undefined) {
return
}

Expand Down
6 changes: 6 additions & 0 deletions packages/tx/test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ tape('[Transaction]: Basic functions', function (t) {
st.ok(tx.isSigned())
tx = Transaction.fromRlpSerializedTx(rawSigned)
st.ok(tx.isSigned())

const signedValues = (rlp.decode(rawSigned) as any) as Buffer[]
tx = Transaction.fromValuesArray(signedValues)
st.ok(tx.isSigned())
tx = Transaction.fromValuesArray(signedValues.slice(0, 6))
st.notOk(tx.isSigned())
st.end()
})

Expand Down

0 comments on commit 97c30f1

Please sign in to comment.