From 2b5c1369da717d442dc65dd26dc9d9a6581cd8f4 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 31 Mar 2016 02:08:08 -0700 Subject: [PATCH] refactor. strict isscripthash. --- lib/bcoin/script.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 17f13565..bbc3006c 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -865,7 +865,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version n2 = Script.num(stack.pop(), flags); n1 = Script.num(stack.pop(), flags); val = n2.cmp(n1) <= 0 && n1.cmp(n3) < 0; - stack.push(val.cmpn(0) !== 0 ? STACK_TRUE : STACK_FALSE); + stack.push(val ? STACK_TRUE : STACK_FALSE); break; } @@ -1124,7 +1124,7 @@ Script.checkSequence = function checkSequence(sequence, tx, i) { var txSequence = tx.inputs[i].sequence; var locktimeMask, txSequenceMasked, sequenceMasked; - if ((tx.version >>> 0) < 2) + if (tx.version < 2) return false; if (txSequence & constants.sequenceLocktimeDisableFlag) @@ -1370,6 +1370,13 @@ Script.createWitnessProgram = function createWitnessProgram(version, data) { return new Script([version === 0 ? 0 : version + 0x50, data]); }; +Script.prototype.createCommitment = function createCommitment(hash) { + return new Script([ + opcodes.OP_RETURN, + Buffer.concat([new Buffer([0xaa, 0x21, 0xa9, 0xed]), hash]) + ]); +}; + Script.prototype.getRedeem = function getRedeem() { if (!this.redeem) this.redeem = Script.getRedeem(this.code); @@ -1660,6 +1667,21 @@ Script.prototype.isMultisig = function isMultisig(keys) { Script.prototype.isScripthash = function isScripthash(hash) { var res; + // Bitcoind requires strict + // minimaldata encoding for scripthash. + if (this.raw) { + if (hash) { + if (!Script.isHash(this.code[1])) + return false; + if (!utils.isEqual(this.code[1], hash)) + return false; + } + return this.raw.length === 23 + && this.raw[0] === opcodes.OP_HASH160 + && this.raw[1] === 0x14 + && this.raw[22] === opcodes.OP_EQUAL; + } + if (this.code.length !== 3) return false; @@ -1700,13 +1722,6 @@ Script.prototype.isCommitment = function isCommitment() { && utils.readU32BE(this.code[1], 0) === 0xaa21a9ed; }; -Script.prototype.createCommitment = function createCommitment(hash) { - return new Script([ - opcodes.OP_RETURN, - Buffer.concat([new Buffer([0xaa, 0x21, 0xa9, 0xed]), hash]) - ]); -}; - Script.prototype.getCommitmentHash = function getCommitmentHash() { if (!this.isCommitment()) return;