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

Util: Allow v to be 0 or 1 for EIP1559 transactions #1905

Merged
merged 5 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 11 additions & 12 deletions packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,6 @@ export default class Transaction extends BaseTransaction<Transaction> {
}
}

if (!this.supports(Capability.EIP155ReplayProtection)) {
// In case of a signed non-EIP155 tx, `ecrecover` does not get a `chainId` provided
// Internally, `ecrecover` will calculate the recovery bit by `v = v - 27`
// This recovery bit is either 0 or 1. Thus, any other value than `v = 27` or `v = 28` will
// throw upon calling `getSenderAddress` here. Thus throw early in the constructor
if (this.v !== undefined) {
if (!this.v.eqn(27) && !this.v.eqn(28)) {
throw new Error(`Non-EIP155 txs need either v = 27 or v = 28, got v = ${this.v}`)
}
}
}

if (this.common.isActivatedEIP(3860)) {
checkMaxInitCodeSize(this.common, this.data.length)
}
Expand Down Expand Up @@ -390,6 +378,17 @@ export default class Transaction extends BaseTransaction<Transaction> {
* Validates tx's `v` value
*/
private _validateTxV(v?: BN, common?: Common): Common {
// Check for valid v values in the scope of a signed legacy tx
if (v !== undefined) {
// v is 1. not matching the EIP-155 chainId included case and...
// v is 2. not matching the classic v=27 or v=28 case
if (v.ltn(37) && !v.eqn(27) && !v.eqn(28)) {
Copy link
Member

Choose a reason for hiding this comment

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

If v = 36 then we get a chainId = 0, right? Why is this not ok?

In case of v = 35, then v - 35 (thus 0, which is odd) means we subtract 36, so we end up with a negative chain id which is obviously wrong. But for 36 this logic seems to work..? Unless chain id 0 is special?

Copy link
Member

Choose a reason for hiding this comment

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

TBH I don't find any reference about chain ID 0 and I'm unsure if this theoretically a valid chain ID. I would rather assume for now that it is not, mainnet starts with 1 and all other chain IDs I came across were some arbitrary higher value. In doubt I would tend to leave "as is", if someone demands we could still add later on.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, I agree

throw new Error(
`Legacy txs need either v = 27/28 or v >= 37 (EIP-155 replay protection), got v = ${v}`
)
}
}

let chainIdBN
// No unsigned tx and EIP-155 activated and chain ID included
if (
Expand Down
5 changes: 4 additions & 1 deletion packages/tx/test/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ tape('[Transaction]', function (t) {
)

t.test(
'constructor: throw on non-EIP155 transactions which have v != 27 and v != 28',
'constructor: throw on legacy transactions which have v != 27 and v != 28 and v < 37',
function (st) {
function getTxData(v: number) {
return {
Expand All @@ -450,8 +450,11 @@ tape('[Transaction]', function (t) {
st.throws(() => Transaction.fromTxData(getTxData(n)))
}
st.throws(() => Transaction.fromTxData(getTxData(29)))
st.throws(() => Transaction.fromTxData(getTxData(36)))

st.doesNotThrow(() => Transaction.fromTxData(getTxData(27)))
st.doesNotThrow(() => Transaction.fromTxData(getTxData(28)))
st.doesNotThrow(() => Transaction.fromTxData(getTxData(37)))
st.end()
}
)
Expand Down