From e68bab4ab367b7cdf44327f9ee4fd473b2c854de Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 18 Feb 2016 01:46:29 -0800 Subject: [PATCH] make scripthash input test less insane. --- lib/bcoin/script.js | 55 ++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 63f4b75b..3574d513 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1978,8 +1978,8 @@ script.isMultisigInput = function isMultisigInput(s, keys, tx, i) { return true; }; -script.isScripthashInput = function isScripthashInput(s, data, strict) { - var raw, redeem; +script.isScripthashInput = function isScripthashInput(s, redeem) { + var raw; // Grab the raw redeem script. raw = s[s.length - 1]; @@ -1994,9 +1994,20 @@ script.isScripthashInput = function isScripthashInput(s, data, strict) { if (!Array.isArray(raw)) return false; - // If the last data element is a valid - // signature or key, it's _extremely_ - // unlikely this is a scripthash. + // Check data against last array in case + // a raw redeem script was passed in. + if (redeem) + return utils.isEqual(redeem, raw); + + // Testing for scripthash inputs requires + // some evil magic to work. We do it by + // ruling things _out_. This test will not + // be correct 100% of the time. We rule + // out that the last data element is: a + // null dummy, a valid signature, a valid + // key, and we ensure that it is at least + // a script that does not use undefined + // opcodes. if (script.isDummy(raw)) return false; @@ -2006,40 +2017,8 @@ script.isScripthashInput = function isScripthashInput(s, data, strict) { if (script.isKeyEncoding(raw)) return false; - // Ensure this is a valid encoded script - // if (!script.isEncoded(raw)) - // return false; - - // Check data against last array in case - // a raw redeem script was passed in. - if (data && utils.isEqual(data, raw)) - return true; - - // Return here if we do not want to check - // against standard transaction types. - if (!strict) - return true; - - // P2SH redeem scripts can be nonstandard: make - // it easier for other functions to parse this. - redeem = script.decode(raw); - - // Get the "real" scriptSig - s = s.slice(0, -1); - - // Do some sanity checking on the inputs - if (!script.isPubkeyInput(s) - && !script.isPubkeyhashInput(s) - && !script.isMultisigInput(s)) { + if (!script.isEncoded(raw)) return false; - } - - // Test against all other script types - if (!script.isPubkey(redeem, data) - && !script.isPubkeyhash(redeem, data) - && !script.isMultisig(redeem, data)) { - return false; - } return true; };