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

Commit

Permalink
Correctly support signing and serializing of EIP155 valid transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
danjm committed Apr 23, 2019
1 parent 7d84d02 commit 1583bea
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Transaction {
}, {
name: 'v',
allowZero: true,
default: new Buffer([0x1c])
default: new Buffer([opts.chain || opts.common ? this._common.chainId() : 0x1c])
}, {
name: 'r',
length: 32,
Expand Down Expand Up @@ -184,10 +184,15 @@ 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 onEIP155BlockOrLater = this._common.gteHardfork('spuriousDragon')
const v = ethUtil.bufferToInt(this.v)
const onEIP155BlockOrLater = this._common.gteHardfork('spuriousDragon')
const vAndChainIdMeetEIP155Conditions = v === this._chainId * 2 + 35 || v === this._chainId * 2 + 36
if (vAndChainIdMeetEIP155Conditions && onEIP155BlockOrLater) {
const meetsAllEIP155Conditions = vAndChainIdMeetEIP155Conditions && onEIP155BlockOrLater

const unsigned = !this.r.length && !this.s.length
const seeksReplayProtection = this._chainId > 0

if (unsigned && seeksReplayProtection || !unsigned && meetsAllEIP155Conditions) {
const raw = this.raw.slice()
this.v = this._chainId
this.r = 0
Expand Down Expand Up @@ -229,8 +234,8 @@ class Transaction {
* @return {Buffer}
*/
getSenderPublicKey () {
if (!this._senderPubKey || !this._senderPubKey.length) {
if (!this.verifySignature()) throw new Error('Invalid Signature')
if (!this.verifySignature()) {
throw new Error('Invalid Signature')
}
return this._senderPubKey
}
Expand Down

0 comments on commit 1583bea

Please sign in to comment.