Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tx: fix assignment of v,r,s in fromValuesArray #1077

Merged
merged 4 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work if v is 0. This is problematic in EIP-2930.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I am not sure if there exists legacy transactions with a v value of 0... They do exist on EIP-2930 transactions, so it can also be "fixed" there, but I am generally more in favor to be correct here; if one inputs any value (also for r and s) which is 0, then it should create a BN(0).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah hm okay, then maybe I shouldn't remove the validation for v to be zero in this commit? 14581b0

I think I'd prefer that tx v3 continues to support legacy transactions only, and then v4 can support EIP-2930. These accidental BN(0) bugs are not fun and I don't think v,r,s should ever be initialized with BN(0), only undefined for unsigned transactions, otherwise we need to add more checks around the library (like in isSigned). What do you think?

Copy link
Member

@holgerd77 holgerd77 Feb 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I am not sure if there exists legacy transactions with a v value of 0... They do exist on EIP-2930

Are you maybe mixing this up with this v - 27 thing (never understood that completely), I can't imagine that EIP-2930 is changing on the signature format?

Maybe @alcuadrado can have a short look here as well

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