From 56d81bc0dfce3c0b9b713f836a09aea54fa22b91 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 22 Apr 2014 13:50:41 -0300 Subject: [PATCH] remove unused code from Script. Update it to do not use scriptSig placeholders --- lib/Script.js | 72 +++++++++++++-------------------- lib/TransactionBuilder.js | 8 +++- test/data/unspentSign.json | 3 +- test/test.Script.js | 47 +++++++++++---------- test/test.TransactionBuilder.js | 56 ++++++++++++++++++++++--- 5 files changed, 113 insertions(+), 73 deletions(-) diff --git a/lib/Script.js b/lib/Script.js index d76e3b8..fcba36b 100644 --- a/lib/Script.js +++ b/lib/Script.js @@ -119,34 +119,37 @@ Script.prototype.isMultiSig = function() { this.chunks[this.chunks.length - 1] == OP_CHECKMULTISIG); }; -Script.prototype.finishedMultiSig = function() { - var nsigs = 0; - for (var i = 0; i < this.chunks.length - 1; i++) - if (this.chunks[i] !== 0) - nsigs++; +Script.prototype.countMissingSignatures = function() { + if (this.isMultiSig()) { + log.debug("Can not count missing signatures on normal Multisig script"); + return null; + } - var serializedScript = this.chunks[this.chunks.length - 1]; - var script = new Script(serializedScript); - var nreq = script.chunks[0] - 80; //see OP_2-OP_16 - - if (nsigs == nreq) - return true; - else - return false; -}; - -Script.prototype.removePlaceHolders = function() { - var chunks = []; - for (var i in this.chunks) { - if (this.chunks.hasOwnProperty(i)) { - var chunk = this.chunks[i]; - if (chunk != 0) - chunks.push(chunk); + // P2SH? + var l =this.chunks.length; + if (isSmallIntOp(this.chunks[0]) && this.chunks[0] ===0) { + var redeemScript = new Script(this.chunks[l-1]); + if (!isSmallIntOp(redeemScript.chunks[0])) { + log.debug("Unrecognized script type"); + ret = null; + } + else { + var nreq = redeemScript.chunks[0] - 80; //see OP_2-OP_16 + ret = nreq - (l - 2); // 2-> marked 0 + redeemScript } } - this.chunks = chunks; - this.updateBuffer(); - return this; + // p2pubkey or p2pubkeyhash + else { + if (buffertools.compare(this.getBuffer(), util.EMPTY_BUFFER) === 0) { + ret = 1; + } + } + return ret; +}; + + +Script.prototype.finishedMultiSig = function() { + return this.countMissingSignatures() === 0; }; Script.prototype.prependOp0 = function() { @@ -517,25 +520,6 @@ Script.prototype.toHumanReadable = function() { return s; }; -Script.prototype.countMissingSignatures = function() { - var ret = 0; - if (!Buffer.isBuffer(this.chunks[0]) && this.chunks[0] ===0) { - // Multisig, skip first 0x0 - for (var i = 1; i < this.chunks.length; i++) { - if (this.chunks[i]===0 - || buffertools.compare(this.chunks[i], util.EMPTY_BUFFER) === 0){ - ret++; - } - } - } - else { - if (buffertools.compare(this.getBuffer(), util.EMPTY_BUFFER) === 0) { - ret = 1; - } - } - return ret; -}; - Script.stringToBuffer = function(s) { var buf = new Put(); var split = s.split(' '); diff --git a/lib/TransactionBuilder.js b/lib/TransactionBuilder.js index ca27dcc..0466e59 100644 --- a/lib/TransactionBuilder.js +++ b/lib/TransactionBuilder.js @@ -550,6 +550,7 @@ TransactionBuilder.prototype._updateMultiSig = function(wk, scriptSig, txSigHash if (this._isSignedWithKey(wk,scriptSig, txSigHash, nreq)) return null; +console.log('[TransactionBuilder.js.552] ', wk.privKey.public.toString('hex')); //TODO // Find an empty slot and sign for(var i=1; i<=nreq; i++) { var chunk = scriptSig.chunks[i]; @@ -581,8 +582,10 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig var signaturesAdded = 0; for(var j=0; j +// +// { +// "address" : "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6", +// "redeemScript" : "532103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e32103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e455ae" +// } +// var getP2shBuilder = function(setMap) { var network = 'testnet'; var opts = { remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, }; var data = getInfoForP2sh(); +console.log('[test.TransactionBuilder.js.700:data:]',data); //TODO +process.exit(1); // multisig p2sh var p2shOpts = {nreq:3, pubkeys:data.pubkeys}; var info = TransactionBuilder.infoForP2sh(p2shOpts, network); +console.log('[test.TransactionBuilder.js.693:info:]',info, p2shOpts); //TODO var outs = outs || [{ - address: info.address, + address: 'mon1Hqs3jqKTtRSnRwJ3pRYMFos9WYfKb5', amount: 0.08 }]; var b = new TransactionBuilder(opts) @@ -713,14 +726,45 @@ describe('TransactionBuilder', function() { (function() {b.sign(testdata.dataUnspentSign.keyStringsP2sh);}).should.throw(); }); - it('should sign a p2sh/multisign tx', function() { - var b = getP2shBuilder(1); - b.sign(testdata.dataUnspentSign.keyStringsP2sh); + it('should sign a p2sh/multisig tx right order', function(done) { + var b = getP2shBuilder(1); + b.sign([testdata.dataUnspentSign.keyStringsP2sh[3]]); + b.sign([testdata.dataUnspentSign.keyStringsP2sh[1]]); + b.sign([testdata.dataUnspentSign.keyStringsP2sh[2]]); b.isFullySigned().should.equal(true); var tx = b.build(); tx.ins.length.should.equal(1); tx.outs.length.should.equal(2); tx.isComplete().should.equal(true); + + var shex = testdata.dataUnspentSign.unspentP2sh[0].scriptPubKey; + var s = new Script(new Buffer(shex,'hex')); + tx.verifyInput(0,s, vopts, function(err, results){ + should.exist(results); + results.should.equal(true); + should.not.exist(err); + done(); + }); + }); + + it('should failed to verify a p2sh/multisig tx wrong order', function(done) { + var b = getP2shBuilder(1); + b.sign([testdata.dataUnspentSign.keyStringsP2sh[1]]); + b.sign([testdata.dataUnspentSign.keyStringsP2sh[2]]); + b.sign([testdata.dataUnspentSign.keyStringsP2sh[3]]); + b.isFullySigned().should.equal(true); + var tx = b.build(); + tx.ins.length.should.equal(1); + tx.outs.length.should.equal(2); + tx.isComplete().should.equal(true); + + var shex = testdata.dataUnspentSign.unspentP2sh[0].scriptPubKey; + var s = new Script(new Buffer(shex,'hex')); + tx.verifyInput(0,s, vopts, function(err, results){ + should.not.exist(results); + should.exist(err); + done(); + }); }); @@ -953,9 +997,9 @@ describe('TransactionBuilder', function() { it('#merge p2sh/steps', function(done) { var b = getP2shBuilder(1); - var k3 = testdata.dataUnspentSign.keyStringsP2sh.slice(0,1); + var k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(0,1); var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2); - var k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(2,3); + var k3 = testdata.dataUnspentSign.keyStringsP2sh.slice(2,3); b.isFullySigned().should.equal(false); b.signaturesAdded.should.equal(0); b.sign(k1);