Skip to content

Commit

Permalink
add signatureAdded counter
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Apr 10, 2014
1 parent a8f5f9f commit d507e7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 6 additions & 4 deletions TransactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function TransactionBuilder(opts) {

this.tx = {};
this.inputsSigned= 0;
this.signaturesAdded= 0;

return this;
}
Expand Down Expand Up @@ -482,7 +483,7 @@ TransactionBuilder.prototype._signPubKey = function(walletKeyMap, input, txSigHa
var scriptSig = new Script();
scriptSig.chunks.push(sig);
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) {
Expand All @@ -501,7 +502,7 @@ TransactionBuilder.prototype._signPubKeyHash = function(walletKeyMap, input, txS
scriptSig.chunks.push(sig);
scriptSig.chunks.push(wk.privKey.public);
scriptSig.updateBuffer();
return {isFullySigned: true, signaturesAdded: true, script: scriptSig.getBuffer()};
return {isFullySigned: true, signaturesAdded: 1, script: scriptSig.getBuffer()};
};

// FOR TESTING
Expand Down Expand Up @@ -577,7 +578,7 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
originalScriptBuf = this.tx.ins[input.i].s;

var scriptSig = new Script (originalScriptBuf);
var signaturesAdded = false;
var signaturesAdded = 0;

for(var j=0; j<l && scriptSig.countMissingSignatures(); j++) {
var wk = this._findWalletKey(walletKeyMap, {pubKeyBuf: pubkeys[j]});
Expand All @@ -586,7 +587,7 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
var newScriptSig = this._updateMultiSig(wk, scriptSig, txSigHash, nreq);
if (newScriptSig) {
scriptSig = newScriptSig;
signaturesAdded = true;
signaturesAdded++;
}
}

Expand Down Expand Up @@ -694,6 +695,7 @@ TransactionBuilder.prototype.sign = function(keys) {
if (ret && ret.script) {
tx.ins[i].s = ret.script;
if (ret.isFullySigned) this.inputsSigned++;
if (ret.signaturesAdded) this.signaturesAdded +=ret.signaturesAdded;
}
}
return this;
Expand Down
7 changes: 7 additions & 0 deletions test/test.TransactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,36 +605,43 @@ describe('TransactionBuilder', function() {
var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2);
var k5 = testdata.dataUnspentSign.keyStringsP2sh.slice(4,5);
b.isFullySigned().should.equal(false);
b.signaturesAdded.should.equal(0);

b.sign(k1);
b.isFullySigned().should.equal(false);
b.signaturesAdded.should.equal(1);

var tx = b.build();
tx.ins.length.should.equal(1);
tx.outs.length.should.equal(2);
tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(1);

// Sign with the same
b.sign(k1);
b.isFullySigned().should.equal(false);
tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(1);

// Sign with k5
b.sign(k5);
///
b.isFullySigned().should.equal(false);
tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(2);

// Sign with same
b.sign(k5);
b.isFullySigned().should.equal(false);
tx.isComplete().should.equal(false);
b.signaturesAdded.should.equal(2);


// Sign k2
b.sign(k2);
b.isFullySigned().should.equal(true);
tx.isComplete().should.equal(true);
b.signaturesAdded.should.equal(3);
});

it('should sign in steps a p2sh/p2pubkeyhash tx', function() {
Expand Down

0 comments on commit d507e7f

Please sign in to comment.