From fdc72b20625fed6de4e05af3060a5af37d15f37d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 12 Jan 2016 01:13:22 -0800 Subject: [PATCH] let isMultisig handle m and n validation. --- lib/bcoin/script.js | 24 ++++++++++++------------ lib/bcoin/tx.js | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index b5d83760..80fec05b 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1097,17 +1097,6 @@ script.isMultisig = function isMultisig(s, keys) { if (s[s.length - 1] !== 'checkmultisig') return false; - m = s[0]; - - if (Array.isArray(m)) { - if (m.length !== 1) - return false; - m = m[0]; - } - - if (!(m >= 1 && m <= 15)) - return false; - n = s[s.length - 2]; if (Array.isArray(n)) { @@ -1116,7 +1105,18 @@ script.isMultisig = function isMultisig(s, keys) { n = n[0]; } - if (!(n >= m && n <= 15)) + if (!(n >= 1 && n <= 15)) + return false; + + m = s[0]; + + if (Array.isArray(m)) { + if (m.length !== 1) + return false; + m = m[0]; + } + + if (!(m >= 1 && m <= n)) return false; if (n + 3 !== s.length) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index d9daab58..6858a33f 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -198,7 +198,7 @@ TX.prototype.scriptInput = function scriptInput(index, pub, redeem) { // Grab `n` value (number of keys). n = s[s.length - 2]; if (Array.isArray(n)) - n = n[0] || 0; + n = n[0]; // Fill script with `n` signature slots. for (i = 0; i < n; i++) @@ -281,15 +281,15 @@ TX.prototype.signInput = function signInput(index, key, type) { // Grab `m` value (number of sigs required). m = s[0]; if (Array.isArray(m)) - m = m[0] || 0; + m = m[0]; // Grab `n` value (number of keys). n = s[s.length - 2]; - if (Array.isArray(m)) - n = n[0] || 0; + if (Array.isArray(n)) + n = n[0]; // Something is very wrong here. Abort. - if (m < 1 || m > 15 || n < m || n > 15 || len - 1 > n) + if (len - 1 > n) return; // Count the number of current signatures. @@ -480,11 +480,11 @@ TX.prototype.scriptOutput = function scriptOutput(index, options) { m = options.m || keys.length; n = options.n || keys.length; - assert(m >= 1 && m <= n); - if (options.scripthash) - assert(n >= 1 && n <= 15); - else - assert(n >= 1 && n <= 3); + if (!(m >= 1 && m <= n)) + return; + + if (!(n >= 1 && n <= (options.scripthash ? 15 : 3))) + return; script = bcoin.script.redeem(keys, m, n); } else if (bcoin.wallet.validateAddress(options.address, 'scripthash')) { @@ -689,7 +689,7 @@ TX.prototype.maxSize = function maxSize() { // Get the previous m value: m = s[0]; if (Array.isArray(m)) - m = m[0] || 0; + m = m[0]; assert(m >= 1 && m <= 3); // OP_0 size += 1;