From 8e1de3179716d44ab3643a0f2abcfc9205bd848c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Jul 2014 17:33:40 -0300 Subject: [PATCH] test passing. --- lib/TransactionBuilder.js | 43 +++++++++++++++++---------------- test/test.TransactionBuilder.js | 8 ++---- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/TransactionBuilder.js b/lib/TransactionBuilder.js index 9b34376..cc34fff 100644 --- a/lib/TransactionBuilder.js +++ b/lib/TransactionBuilder.js @@ -897,15 +897,10 @@ TransactionBuilder.prototype._mergeInputSigP2sh = function(input, s0, s1) { } } -console.log('[TransactionBuilder.js.887:diff:]',diff); //TODO // Add signatures for (var j in diff) { var newSig = diff[j]; var order = this._getNewSignatureOrder(newSig.prio, s0, p2sh.txSigHash, pubkeys); - - if (this._getSighashType(newSig.chunk) !== this.signhash) - throw new Error('signhash type mismatch at merge'); - s0.chunks.splice(order + 1, 0, newSig.chunk); } s0.updateBuffer(); @@ -918,12 +913,27 @@ TransactionBuilder.prototype._getSighashType = function(sig) { return sig[sig.length-1]; }; + +TransactionBuilder.prototype._checkSignHash = function(s1) { + var l = s1.chunks.length-1; + + for(var i=0; i 2) || (l1 < 2 && l0 > 2))) throw new Error('TX sig types mismatch in merge'); -console.log('[TransactionBuilder.js.935]', l0, l1); //TODO - if ((!l0 && !l1) || (l0 && !l1) || (!l0 && l1)) + if ((!l0 && !l1) || (l0 && !l1)) + return s0buf; + + this._checkSignHash(s1); + + if ((!l0 && l1)) return s1buf; - -console.log('[TransactionBuilder.js.940]'); //TODO - // Get the pubkeys var input = this.inputMap[index]; var type = input.scriptPubKey.classify(); -console.log('[TransactionBuilder.js.941]'); //TODO //p2pubkey or p2pubkeyhash if (type === Script.TX_PUBKEYHASH || type === Script.TX_PUBKEY) { var s = new Script(s1buf); - - if (this._getSighashType(s.chunks[0]) !== this.signhash) - throw new Error('signhash type mismatch at merge'); - log.debug('Merging two signed inputs type:' + input.scriptPubKey.getRawOutType() + '. Signatures differs. Using the first version.'); return s0buf; @@ -960,8 +966,6 @@ console.log('[TransactionBuilder.js.941]'); //TODO throw new Error('Script type:' + input.scriptPubKey.getRawOutType() + 'not supported at #merge'); } -console.log('[TransactionBuilder.js.957]'); //TODO - return this._mergeInputSigP2sh(input, s0, s1); }; @@ -969,7 +973,6 @@ console.log('[TransactionBuilder.js.957]'); //TODO TransactionBuilder.prototype._mergeTx = function(tx) { var v0 = this.tx; var v1 = tx; -console.log('[TransactionBuilder.js.966:var:]'); //TODO var l = v0.ins.length; if (l !== v1.ins.length) @@ -977,7 +980,6 @@ console.log('[TransactionBuilder.js.966:var:]'); //TODO this.inputsSigned = 0; for (var i = 0; i < l; i++) { -console.log('[TransactionBuilder.js.974:for:]',i); //TODO var i0 = v0.ins[i]; var i1 = v1.ins[i]; @@ -987,7 +989,6 @@ console.log('[TransactionBuilder.js.974:for:]',i); //TODO if (buffertools.compare(i0.o, i1.o) !== 0) throw new Error('TX .o in mismatch in merge. Input:', i); -console.log('[TransactionBuilder.js.984]'); //TODO i0.s = this._mergeInputSig(i, i0.s, i1.s); this.vanilla.scriptSig[i]= i0.s.toString('hex'); diff --git a/test/test.TransactionBuilder.js b/test/test.TransactionBuilder.js index e7f88d4..e164b85 100644 --- a/test/test.TransactionBuilder.js +++ b/test/test.TransactionBuilder.js @@ -1108,21 +1108,17 @@ describe('TransactionBuilder', function() { }); - it.only('should check signhash in p2sh/merge', function() { + it('should check signhash in p2sh/merge', function() { var b = getP2shBuilder(1); var k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(0,1); var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2); - var k3 = testdata.dataUnspentSign.keyStringsP2sh.slice(2,3); b.isFullySigned().should.equal(false); b.sign(k1); var tx = b.build(); tx.isComplete().should.equal(false); - b = TransactionBuilder.fromObj(b.toObj()); var b2 = getP2shBuilder(1, {signhash: bitcore.Transaction.SIGHASH_NONE }); b2.sign(k2); - -console.log('[test.TransactionBuilder.js.1124]'); //TODO (function() { b2.merge(b)}).should.throw(); }); @@ -1139,7 +1135,7 @@ console.log('[test.TransactionBuilder.js.1124]'); //TODO var b2 = getP2shBuilder(1, {remainderOut: {address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE' }}); b2.sign(k2); - (function() { b2.merge(b)}).should.throw('asdds'); + (function() { b2.merge(b)}).should.throw('NTXID'); });