Skip to content

Commit

Permalink
tx: added hack for tx.sign() for the case that we have got a legacy t…
Browse files Browse the repository at this point in the history
…x after spuriousDragon with a non-EIP155 conforming signature and want to recreate the signature
  • Loading branch information
holgerd77 committed Jun 23, 2021
1 parent 505c856 commit 1c29006
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion packages/tx/src/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,34 @@ export abstract class BaseTransaction<TransactionObject> {
if (privateKey.length !== 32) {
throw new Error('Private key must be 32 bytes in length.')
}

// Hack for the constellation that we have got a legacy tx after spuriousDragon with a non-EIP155 conforming signature
// and want to recreate a signature (where EIP155 should be applied)
// Leaving this hack lets the legacy.spec.ts -> sign(), verifySignature() test fail
// 2021-06-23
let hackApplied = false
if (
this.type === 0 &&
this.common.gteHardfork('spuriousDragon') &&
!this.supports(Capabilities.EIP155ReplayProtection)
) {
this.activeCapabilities.push(Capabilities.EIP155ReplayProtection)
hackApplied = true
}

const msgHash = this.getMessageToSign(true)
const { v, r, s } = ecsign(msgHash, privateKey)
return this._processSignature(v, r, s)
const tx = this._processSignature(v, r, s)

// Hack part 2
if (hackApplied) {
const index = this.activeCapabilities.indexOf(Capabilities.EIP155ReplayProtection)
if (index > -1) {
this.activeCapabilities.splice(index, 1)
}
}

return tx
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ export default class Transaction extends BaseTransaction<Transaction> {
// hash nine elements, with v replaced by CHAIN_ID, r = 0 and s = 0.
const v = this.v!
const chainIdDoubled = this.common.chainIdBN().muln(2)

// v and chain ID meet EIP-155 conditions
if(v.eq(chainIdDoubled.addn(35)) || v.eq(chainIdDoubled.addn(36))) {
if (v.eq(chainIdDoubled.addn(35)) || v.eq(chainIdDoubled.addn(36))) {
this.activeCapabilities.push(Capabilities.EIP155ReplayProtection)
}
}
Expand Down

0 comments on commit 1c29006

Please sign in to comment.