Skip to content

Commit

Permalink
remove unused code from Script. Update it to do not use scriptSig pla…
Browse files Browse the repository at this point in the history
…ceholders
  • Loading branch information
matiu committed Apr 23, 2014
1 parent e8ebd2e commit 56d81bc
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 73 deletions.
72 changes: 28 additions & 44 deletions lib/Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

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.countMissingSignatures = function() {
if (this.isMultiSig()) {
log.debug("Can not count missing signatures on normal Multisig script");
return null;
}

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() {
Expand Down Expand Up @@ -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(' ');
Expand Down
8 changes: 7 additions & 1 deletion lib/TransactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -581,15 +582,21 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
var signaturesAdded = 0;

for(var j=0; j<l && scriptSig.countMissingSignatures(); j++) {
//console.log('[TransactionBuilder.js] pubkey [j]',j, pubkeys[j].toString('hex')); //TODO
var wk = this._findWalletKey(walletKeyMap, {pubKeyBuf: pubkeys[j]});
if (!wk) continue;
//console.log('[TransactionBuilder.js.585] wk pubkey: PRIO:',j, wk.privKey.public.toString('hex')); //TODO

var newScriptSig = this._updateMultiSig(wk, scriptSig, txSigHash, nreq);
if (newScriptSig) {
scriptSig = newScriptSig;
signaturesAdded++;
}
}
if (!scriptSig.countMissingSignatures()) {

console.log('########## DONE!!!'); //TODO
}

return {
isFullySigned: scriptSig.countMissingSignatures() === 0,
Expand Down Expand Up @@ -926,7 +933,6 @@ TransactionBuilder.prototype.merge = function(b, ignoreConflictingSignatures) {
throw new Error('mismatch at TransactionBuilder NTXID');

this._mergeTx(b.tx, ignoreConflictingSignatures);
// TODO UPDATE: signaturesAdded, inputsSigned
}
};

Expand Down
3 changes: 2 additions & 1 deletion test/data/unspentSign.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@
"cMyBgowsyrJRufoKWob73rMQB1PBqDdwFt8z4TJ6APN2HkmX1Ttm",
"cN9yZCom6hAZpHtCp8ovE1zFa7RqDf3Cr4W6AwH2tp59Jjh9JcXu"
],
"comment": "script pubkey can be obtained from: bitcoind createrawtransaction '[{\"txid\": \"2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1\",\"vout\":1}]' '{\"2NFW3ja1tdza4b1WTyG9fkz6cBtRf4qEFBh\":0.08}' and then decoding the generated transaction hex using bitcoind decoderawtransaction",
"unspentP2sh": [
{
"address": "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6",
"scriptPubKey": "a91432d272ce8a9b482b363408a0b1dd28123d59c63387",
"scriptPubKey": "a914dc0623476aefb049066b09b0147a022e6eb8429187",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
"vout": 1,
"amount": 1,
Expand Down
47 changes: 26 additions & 21 deletions test/test.Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,15 @@ describe('Script', function() {
script.finishedMultiSig().should.equal(true);
});
it('should report that this scripSig has not finished being signed', function() {
var scriptHex = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff50100004c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae';
//decoded: 3045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff501 0 0 5221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae
var scriptHex = '00483045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b014c69522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae';
//decoded: 0 3045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b01 522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae
//meaning: sig place_holder place_holder serialized_script
var scriptBuf = new Buffer(scriptHex, 'hex');
var script = new Script(scriptBuf);
script.finishedMultiSig().should.equal(false);
});
});

describe('#removePlaceHolders', function() {
it('should remove place holders from this script', function() {
var scriptHex = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff50100004c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae';
//decoded: 3045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff501 0 0 5221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae
//meaning: sig place_holder place_holder serialized_script
var scriptBuf = new Buffer(scriptHex, 'hex');
var script = new Script(scriptBuf);
script.removePlaceHolders();

var scriptHex2 = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff5014c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae';
//decoded: 3045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff501 5221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae
//meaning: sig serialized_script
var scriptBuf2 = new Buffer(scriptHex2, 'hex');
var script2 = new Script(scriptBuf2);

script.buffer.toString('hex').should.equal(script2.buffer.toString('hex'));
});
});

describe('prependOp0', function() {
it('should prepend the script with OP_0', function() {
var scriptHex = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff50100004c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae';
Expand Down Expand Up @@ -149,5 +130,29 @@ describe('Script', function() {
});


describe('#countMissingSignatures', function() {
Script = ScriptModule;
it('should count missing signature in empty scripts', function() {
var s = new Script();
s.countMissingSignatures().should.equal(1);
});
it('should count missing signatures p2sh 2-3 1 missing', function() {
// from https://gist.github.com/matiu/11182987
var b = new Buffer('00483045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b014c69522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae', 'hex');
var s = new Script(b);
s.countMissingSignatures().should.equal(1);
});

it('should count missing signatures p2sh 2-3 0 missing', function() {
// from https://gist.github.com/matiu/11182987
var b = new Buffer('00483045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b01483045022100a505aff6a1d9cc14d0658a99ebcf1901b5c9f9e6408055fa9b9da443c80bfdb602207f0391c98abecc93bc3b353c55ada4d3fb6d4bab48fd63ae184df1af367cee46014c69522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae', 'hex');
var s = new Script(b);
s.countMissingSignatures().should.equal(0);
});


});



});
56 changes: 50 additions & 6 deletions test/test.TransactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,18 +682,31 @@ describe('TransactionBuilder', function() {
};
};

//
// bitcoind createmultisig 3 '["03197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d" , "0380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127" , "0392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed03", "03a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e3" , "03e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e4" ]'
//
// =>
//
// {
// "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)
Expand All @@ -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();
});
});


Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 56d81bc

Please sign in to comment.