Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Check for EIP155 condition in sign() and extract that check to reusab…
Browse files Browse the repository at this point in the history
…le function.
  • Loading branch information
danjm committed Mar 22, 2019
1 parent 812c99d commit c011704
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const BN = ethUtil.BN
// secp256k1n/2
const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)

const vMeetsEIP155Conditions = (v, _this) => {
const vOptions = [_this._chainId * 2 + 35, _this._chainId * 2 + 35 + 1]
const hardforkGteSpuriousDragon = _this._common.gteHardfork('spuriousDragon')
return hardforkGteSpuriousDragon && vOptions.includes(v)
}

/**
* Creates a new transaction object.
*
Expand Down Expand Up @@ -181,10 +187,8 @@ class Transaction {
// elements (i.e. nonce, gasprice, startgas, to, value, data), hash nine elements, with v replaced by
// CHAIN_ID, r = 0 and s = 0.

const onSpuriousDragonOrLater = this._common.gteHardfork('spuriousDragon')
const v = ethUtil.bufferToInt(this.v)
const vMeetsEIP155Conditions = v === this._chainId * 2 + 35 || v === this._chainId * 2 + 36
if (onSpuriousDragonOrLater && vMeetsEIP155Conditions) {
if (vMeetsEIP155Conditions(v, this)) {
const raw = this.raw.slice()
this.v = this._chainId
this.r = 0
Expand Down Expand Up @@ -245,8 +249,7 @@ class Transaction {

try {
const v = ethUtil.bufferToInt(this.v)
const useChainIdWhileRecoveringPubKey = v >= this._chainId * 2 + 35 && this._common.gteHardfork('spuriousDragon')
this._senderPubKey = ethUtil.ecrecover(msgHash, v, this.r, this.s, useChainIdWhileRecoveringPubKey && this._chainId)
this._senderPubKey = ethUtil.ecrecover(msgHash, v, this.r, this.s, vMeetsEIP155Conditions(v, this) && this._chainId)
} catch (e) {
return false
}
Expand All @@ -261,7 +264,7 @@ class Transaction {
sign (privateKey) {
const msgHash = this.hash(false)
const sig = ethUtil.ecsign(msgHash, privateKey)
if (this._chainId > 0) {
if (vMeetsEIP155Conditions(sig.v, this)) {
sig.v += this._chainId * 2 + 8
}
Object.assign(this, sig)
Expand Down

0 comments on commit c011704

Please sign in to comment.