diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 9ba33c20..27e73a6f 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -93,27 +93,43 @@ Input.prototype.getType = function getType() { /** * Get the redeem script. Will attempt to resolve nested - * redeem scripts if witnesspubkeyhash is behind a scripthash. + * redeem scripts if witnessscripthash is behind a scripthash. * @returns {Script?} Redeem script. */ Input.prototype.getRedeem = function getRedeem() { - var redeem = this.script; - var type; + var redeem, prev; if (this.isCoinbase()) return; - type = this.getType(); + if (!this.coin) { + if (this.script.isScripthashInput()) { + redeem = this.script.getRedeem(); - if (type === 'scripthash') { - redeem = redeem.getRedeem(); - if (!redeem) - return; + if (redeem && redeem.isWitnessScripthash()) + redeem = this.witness.getRedeem(); + + return redeem; + } + + if (this.witness.isScripthashInput()) + return this.witness.getRedeem(); + + return; } - if (redeem.isWitnessScripthash()) - redeem = this.witness.getRedeem(); + prev = this.coin.script; + + if (prev.isScripthash()) { + prev = this.script.getRedeem(); + redeem = prev; + } + + if (prev && prev.isWitnessScripthash()) { + prev = this.witness.getRedeem(); + redeem = prev; + } return redeem; }; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index f46bd7ba..4492a823 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -162,7 +162,7 @@ Witness.prototype.isMultisigInput = function isMultisigInput() { */ Witness.prototype.isScripthashInput = function isScripthashInput() { - return Script.isScripthashInput(this.items, true); + return Script.isScripthashInput(this.items); }; /** @@ -2241,15 +2241,6 @@ Script.prototype.getRedeem = function getRedeem() { Script.getRedeem = function getRedeem(code) { var redeem = code[code.length - 1]; - if (typeof redeem === 'number') { - if (redeem > opcodes.OP_16) - return; - return new Script([redeem]); - } - - if (Script.isBadPush(redeem)) - return; - if (!Buffer.isBuffer(redeem)) return; @@ -2389,7 +2380,7 @@ Script.getInputAddress = function getInputAddress(code, isWitness) { if (Script.isMultisigInput(code, isWitness)) return; - if (Script.isScripthashInput(code, isWitness)) { + if (Script.isScripthashInput(code)) { if (isWitness) { return bcoin.address.compileData( code[code.length - 1], @@ -2455,7 +2446,7 @@ Script.getInputHash = function getInputHash(isWitness) { if (Script.isMultisigInput(code, isWitness)) return; - if (Script.isScripthashInput(code, isWitness)) { + if (Script.isScripthashInput(code)) { return isWitness ? utils.sha256(code[code.length - 1]).toString('hex') : utils.ripesha(code[code.length - 1]).toString('hex') @@ -2769,7 +2760,7 @@ Script.getInputType = function getInputType(code, isWitness) { var type = (Script.isPubkeyInput(code) && 'pubkey') || (Script.isPubkeyhashInput(code) && 'pubkeyhash') || (Script.isMultisigInput(code, isWitness) && 'multisig') - || (Script.isScripthashInput(code, isWitness) && 'scripthash') + || (Script.isScripthashInput(code) && 'scripthash') || 'unknown'; if (isWitness) { @@ -2929,7 +2920,7 @@ Script.isMultisigInput = function isMultisigInput(code, isWitness) { // We need to rule out scripthash // because it may look like multisig. - if (Script.isScripthashInput(code, isWitness)) + if (Script.isScripthashInput(code)) return false; if (code.length < 3) @@ -2961,22 +2952,12 @@ Script.prototype.isScripthashInput = function isScripthashInput() { return Script.isScripthashInput(this.code); }; -Script.isScripthashInput = function isScripthashInput(code, isWitness) { +Script.isScripthashInput = function isScripthashInput(code) { var raw; // Grab the raw redeem script. raw = code[code.length - 1]; - // Need at least one data element with - // the redeem script. NOTE: NOT THE CASE FOR SEGWIT! - if (isWitness) { - if (code.length < 1) - return false; - } else { - if (code.length < 2) - return false; - } - // Last data element should be an array // for the redeem script. if (!Buffer.isBuffer(raw)) @@ -3552,7 +3533,7 @@ Script.prototype.getScripthashSigops = function getScripthashSigops(input) { return 0; } - if (typeof op === 'number') + if (!Buffer.isBuffer(op)) return 0; redeem = new Script(op); @@ -3617,7 +3598,7 @@ Script.getWitnessSigops = function getWitnessSigops(input, output, witness, flag // does not check the return value of GetOp. if (output.isScripthash() && input.isPushOnly()) { redeem = input.getRedeem(); - if (redeem.isWitnessProgram()) + if (redeem && redeem.isWitnessProgram()) return Script.witnessSigops(redeem.getWitnessProgram(), witness, flags); }