add signatureAdded counter

This commit is contained in:
Matias Alejo Garcia 2014-04-10 19:43:28 -03:00
parent a8f5f9fcb8
commit d507e7f3d5
2 changed files with 13 additions and 4 deletions

View File

@ -110,6 +110,7 @@ function TransactionBuilder(opts) {
this.tx = {}; this.tx = {};
this.inputsSigned= 0; this.inputsSigned= 0;
this.signaturesAdded= 0;
return this; return this;
} }
@ -482,7 +483,7 @@ TransactionBuilder.prototype._signPubKey = function(walletKeyMap, input, txSigHa
var scriptSig = new Script(); var scriptSig = new Script();
scriptSig.chunks.push(sig); scriptSig.chunks.push(sig);
scriptSig.updateBuffer(); scriptSig.updateBuffer();
return {isFullySigned: true, signaturesAdded: true, script: scriptSig.getBuffer()}; return {isFullySigned: true, signaturesAdded: 1, script: scriptSig.getBuffer()};
}; };
TransactionBuilder.prototype._signPubKeyHash = function(walletKeyMap, input, txSigHash) { TransactionBuilder.prototype._signPubKeyHash = function(walletKeyMap, input, txSigHash) {
@ -501,7 +502,7 @@ TransactionBuilder.prototype._signPubKeyHash = function(walletKeyMap, input, txS
scriptSig.chunks.push(sig); scriptSig.chunks.push(sig);
scriptSig.chunks.push(wk.privKey.public); scriptSig.chunks.push(wk.privKey.public);
scriptSig.updateBuffer(); scriptSig.updateBuffer();
return {isFullySigned: true, signaturesAdded: true, script: scriptSig.getBuffer()}; return {isFullySigned: true, signaturesAdded: 1, script: scriptSig.getBuffer()};
}; };
// FOR TESTING // FOR TESTING
@ -577,7 +578,7 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
originalScriptBuf = this.tx.ins[input.i].s; originalScriptBuf = this.tx.ins[input.i].s;
var scriptSig = new Script (originalScriptBuf); var scriptSig = new Script (originalScriptBuf);
var signaturesAdded = false; var signaturesAdded = 0;
for(var j=0; j<l && scriptSig.countMissingSignatures(); j++) { for(var j=0; j<l && scriptSig.countMissingSignatures(); j++) {
var wk = this._findWalletKey(walletKeyMap, {pubKeyBuf: pubkeys[j]}); var wk = this._findWalletKey(walletKeyMap, {pubKeyBuf: pubkeys[j]});
@ -586,7 +587,7 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
var newScriptSig = this._updateMultiSig(wk, scriptSig, txSigHash, nreq); var newScriptSig = this._updateMultiSig(wk, scriptSig, txSigHash, nreq);
if (newScriptSig) { if (newScriptSig) {
scriptSig = newScriptSig; scriptSig = newScriptSig;
signaturesAdded = true; signaturesAdded++;
} }
} }
@ -694,6 +695,7 @@ TransactionBuilder.prototype.sign = function(keys) {
if (ret && ret.script) { if (ret && ret.script) {
tx.ins[i].s = ret.script; tx.ins[i].s = ret.script;
if (ret.isFullySigned) this.inputsSigned++; if (ret.isFullySigned) this.inputsSigned++;
if (ret.signaturesAdded) this.signaturesAdded +=ret.signaturesAdded;
} }
} }
return this; return this;

View File

@ -605,36 +605,43 @@ describe('TransactionBuilder', function() {
var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2); var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2);
var k5 = testdata.dataUnspentSign.keyStringsP2sh.slice(4,5); var k5 = testdata.dataUnspentSign.keyStringsP2sh.slice(4,5);
b.isFullySigned().should.equal(false); b.isFullySigned().should.equal(false);
b.signaturesAdded.should.equal(0);
b.sign(k1); b.sign(k1);
b.isFullySigned().should.equal(false); b.isFullySigned().should.equal(false);
b.signaturesAdded.should.equal(1);
var tx = b.build(); var tx = b.build();
tx.ins.length.should.equal(1); tx.ins.length.should.equal(1);
tx.outs.length.should.equal(2); tx.outs.length.should.equal(2);
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(1);
// Sign with the same // Sign with the same
b.sign(k1); b.sign(k1);
b.isFullySigned().should.equal(false); b.isFullySigned().should.equal(false);
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(1);
// Sign with k5 // Sign with k5
b.sign(k5); b.sign(k5);
/// ///
b.isFullySigned().should.equal(false); b.isFullySigned().should.equal(false);
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(2);
// Sign with same // Sign with same
b.sign(k5); b.sign(k5);
b.isFullySigned().should.equal(false); b.isFullySigned().should.equal(false);
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(2);
// Sign k2 // Sign k2
b.sign(k2); b.sign(k2);
b.isFullySigned().should.equal(true); b.isFullySigned().should.equal(true);
tx.isComplete().should.equal(true); tx.isComplete().should.equal(true);
b.signaturesAdded.should.equal(3);
}); });
it('should sign in steps a p2sh/p2pubkeyhash tx', function() { it('should sign in steps a p2sh/p2pubkeyhash tx', function() {