Skip to content

Commit

Permalink
tx: switch to earlier v validation and throwing before v common/EIP-1…
Browse files Browse the repository at this point in the history
…55 determination logic
  • Loading branch information
holgerd77 committed May 31, 2022
1 parent 98e7f36 commit a74572e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
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)) {
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

0 comments on commit a74572e

Please sign in to comment.