From e364e2b619a102cddf7e974e2f485fb2cda8a496 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 30 Jun 2016 08:17:04 -0700 Subject: [PATCH] refactor sighash. --- lib/bcoin/coin.js | 4 ++-- lib/bcoin/tx.js | 35 +++++++++++++---------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 31449ad0..0838447d 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -61,8 +61,8 @@ Coin.prototype.fromOptions = function fromOptions(options) { assert(utils.isNumber(options.height)); assert(utils.isNumber(options.value)); assert(typeof options.coinbase === 'boolean'); - assert(!options.hash || typeof options.hash === 'string'); - assert(!options.index || utils.isNumber(options.index)); + assert(options.hash == null || typeof options.hash === 'string'); + assert(options.index == null || utils.isNumber(options.index)); this.version = options.version; this.height = options.height; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 6327feb3..606dbd37 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -460,6 +460,15 @@ TX.prototype.hasWitness = function hasWitness() { */ TX.prototype.signatureHash = function signatureHash(index, prev, type, version) { + if (typeof index !== 'number') + index = this.inputs.indexOf(index); + + if (typeof type === 'string') + type = constants.hashType[type.toUpperCase()]; + + assert(index >= 0 && index < this.inputs.length); + assert(prev instanceof Script); + // Traditional sighashing if (version === 0) return this.signatureHashV0(index, prev, type); @@ -483,19 +492,7 @@ TX.prototype.signatureHash = function signatureHash(index, prev, type, version) TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) { var i, input, output; var p = new BufferWriter(); - var hashType; - - if (typeof index !== 'number') - index = this.inputs.indexOf(index); - - if (typeof type === 'string') - type = constants.hashType[type.toUpperCase()]; - - assert(index >= 0 && index < this.inputs.length); - assert(prev instanceof Script); - - // Get the unmasked hash type. - hashType = type & 0x1f; + var hashType = type & 0x1f; if (hashType === constants.hashType.SINGLE) { // Bitcoind used to return 1 as an error code: @@ -610,15 +607,6 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) { var p = new BufferWriter(); var i, hashPrevouts, hashSequence, hashOutputs; - if (typeof index !== 'number') - index = this.inputs.indexOf(index); - - if (typeof type === 'string') - type = constants.hashType[type.toUpperCase()]; - - assert(index >= 0 && index < this.inputs.length); - assert(prev instanceof Script); - if (!(type & constants.hashType.ANYONECANPAY)) { if (this._hashPrevouts) { hashPrevouts = this._hashPrevouts; @@ -2206,6 +2194,9 @@ TX.prototype.frameWitness = function frameWitness(writer) { p.writeU32(this.locktime); + if (witnessSize === this.inputs.length) + throw new Error('Cannot serialize empty-witness tx.'); + this._lastWitnessSize = witnessSize + 2; if (!writer)