diff --git a/src/script.js b/src/script.js index 2c6fbea..6add3f1 100644 --- a/src/script.js +++ b/src/script.js @@ -317,7 +317,15 @@ Script.createPubKeyHashScriptSig = function(signature, pubKey) { } // OP_0 [signatures ...] -Script.createMultisigScriptSig = function(signatures) { +Script.createMultisigScriptSig = function(signatures, scriptPubKey) { + if (scriptPubKey) { + assert(isMultisig.call(scriptPubKey)) + + var m = scriptPubKey.chunks[0] + var k = m - (Opcode.map.OP_1 - 1) + assert(k <= signatures.length, 'Not enough signatures provided') + } + var inScript = new Script() inScript.writeOp(Opcode.map.OP_0) @@ -335,18 +343,6 @@ Script.createP2SHScriptSig = function(scriptSig, scriptPubKey) { return inScript } -// [signatures ...] {m [pubKeys ...] n OP_CHECKSIG} -Script.createP2SHMultisigScriptSig = function(signatures, scriptPubKey) { - assert(isMultisig.call(scriptPubKey)) - - var m = scriptPubKey.chunks[0] - var k = m - (Opcode.map.OP_1 - 1) - assert(k <= signatures.length, 'Not enough signatures provided') - - var scriptSig = Script.createMultisigScriptSig(signatures) - return Script.createP2SHScriptSig(scriptSig, scriptPubKey) -} - Script.prototype.clone = function() { return new Script(this.buffer) } diff --git a/test/script.js b/test/script.js index faf4bb8..218e971 100644 --- a/test/script.js +++ b/test/script.js @@ -121,9 +121,19 @@ describe('Script', function() { it('should create a valid P2SH multisig scriptSig', function() { var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys) - var actual = Script.createP2SHMultisigScriptSig(signatures, redeemScript) + var redeemScriptSig = Script.createMultisigScriptSig(signatures) - assert.equal(b2h(actual.buffer), expected) + var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript) + + assert.equal(b2h(scriptSig.buffer), expected) + }) + + it('should throw on not enough signatures', function() { + var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys) + + assert.throws(function() { + Script.createMultisigScriptSig(signatures.slice(1), redeemScript) + }) }) }) }) diff --git a/test/transaction.js b/test/transaction.js index 62e0525..d4e1e13 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -264,7 +264,8 @@ describe('Transaction', function() { return tx.signScriptSig(0, redeemScript, privKey) }) - var scriptSig = Script.createP2SHMultisigScriptSig(signatures, redeemScript) + var redeemScriptSig = Script.createMultisigScriptSig(signatures) + var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript) tx.setScriptSig(0, scriptSig) signatures.forEach(function(sig, i){