Skip to content

Commit ef17dd1

Browse files
jochem-brouwerholgerd77
authored andcommitted
tx: add support for very high v values
1 parent 310135d commit ef17dd1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

packages/tx/src/legacyTransaction.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
255255
try {
256256
return ecrecover(
257257
msgHash,
258-
v.toNumber(),
258+
v,
259259
bnToRlp(r),
260260
bnToRlp(s),
261261
this._signedTxImplementsEIP155() ? this.getChainId() : undefined
@@ -269,26 +269,25 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
269269
* Validates tx's `v` value
270270
*/
271271
private _validateTxV(v: BN | undefined): void {
272-
if (v === undefined) {
272+
if (v === undefined || v.eqn(0)) {
273273
return
274274
}
275275

276276
if (!this.common.gteHardfork('spuriousDragon')) {
277277
return
278278
}
279279

280-
const vInt = v.toNumber()
281-
282-
if (vInt === 27 || vInt === 28) {
280+
if (v.eqn(27) || v.eqn(28)) {
283281
return
284282
}
285283

286-
const isValidEIP155V =
287-
vInt === this.getChainId() * 2 + 35 || vInt === this.getChainId() * 2 + 36
284+
const chainIdDoubled = new BN(this.getChainId().toString()).imuln(2)
285+
286+
const isValidEIP155V = v.eq(chainIdDoubled.addn(35)) || v.eq(chainIdDoubled.addn(36))
288287

289288
if (!isValidEIP155V) {
290289
throw new Error(
291-
`Incompatible EIP155-based V ${vInt} and chain id ${this.getChainId()}. See the Common parameter of the Transaction constructor to set the chain id.`
290+
`Incompatible EIP155-based V ${v.toString()} and chain id ${this.getChainId()}. See the Common parameter of the Transaction constructor to set the chain id.`
292291
)
293292
}
294293
}
@@ -302,10 +301,12 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
302301

303302
// EIP155 spec:
304303
// If block.number >= 2,675,000 and v = CHAIN_ID * 2 + 35 or v = CHAIN_ID * 2 + 36, then when computing the hash of a transaction for purposes of signing or recovering, instead of hashing only the first six elements (i.e. nonce, gasprice, startgas, to, value, data), hash nine elements, with v replaced by CHAIN_ID, r = 0 and s = 0.
305-
const v = this.v?.toNumber()
304+
const v = this.v!
305+
306+
const chainIdDoubled = new BN(this.getChainId().toString()).imuln(2)
306307

307308
const vAndChainIdMeetEIP155Conditions =
308-
v === this.getChainId() * 2 + 35 || v === this.getChainId() * 2 + 36
309+
v.eq(chainIdDoubled.addn(35)) || v.eq(chainIdDoubled.addn(36))
309310

310311
return vAndChainIdMeetEIP155Conditions && onEIP155BlockOrLater
311312
}

0 commit comments

Comments
 (0)