remove unused code from Script. Update it to do not use scriptSig placeholders

This commit is contained in:
Matias Alejo Garcia 2014-04-22 13:50:41 -03:00
parent e8ebd2e332
commit 56d81bc0df
5 changed files with 113 additions and 73 deletions

View File

@ -119,34 +119,37 @@ Script.prototype.isMultiSig = function() {
this.chunks[this.chunks.length - 1] == OP_CHECKMULTISIG); this.chunks[this.chunks.length - 1] == OP_CHECKMULTISIG);
}; };
Script.prototype.finishedMultiSig = function() { Script.prototype.countMissingSignatures = function() {
var nsigs = 0; if (this.isMultiSig()) {
for (var i = 0; i < this.chunks.length - 1; i++) log.debug("Can not count missing signatures on normal Multisig script");
if (this.chunks[i] !== 0) return null;
nsigs++; }
var serializedScript = this.chunks[this.chunks.length - 1]; // P2SH?
var script = new Script(serializedScript); var l =this.chunks.length;
var nreq = script.chunks[0] - 80; //see OP_2-OP_16 if (isSmallIntOp(this.chunks[0]) && this.chunks[0] ===0) {
var redeemScript = new Script(this.chunks[l-1]);
if (nsigs == nreq) if (!isSmallIntOp(redeemScript.chunks[0])) {
return true; log.debug("Unrecognized script type");
else ret = null;
return false; }
else {
var nreq = redeemScript.chunks[0] - 80; //see OP_2-OP_16
ret = nreq - (l - 2); // 2-> marked 0 + redeemScript
}
}
// p2pubkey or p2pubkeyhash
else {
if (buffertools.compare(this.getBuffer(), util.EMPTY_BUFFER) === 0) {
ret = 1;
}
}
return ret;
}; };
Script.prototype.removePlaceHolders = function() {
var chunks = []; Script.prototype.finishedMultiSig = function() {
for (var i in this.chunks) { return this.countMissingSignatures() === 0;
if (this.chunks.hasOwnProperty(i)) {
var chunk = this.chunks[i];
if (chunk != 0)
chunks.push(chunk);
}
}
this.chunks = chunks;
this.updateBuffer();
return this;
}; };
Script.prototype.prependOp0 = function() { Script.prototype.prependOp0 = function() {
@ -517,25 +520,6 @@ Script.prototype.toHumanReadable = function() {
return s; 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) { Script.stringToBuffer = function(s) {
var buf = new Put(); var buf = new Put();
var split = s.split(' '); var split = s.split(' ');

View File

@ -550,6 +550,7 @@ TransactionBuilder.prototype._updateMultiSig = function(wk, scriptSig, txSigHash
if (this._isSignedWithKey(wk,scriptSig, txSigHash, nreq)) if (this._isSignedWithKey(wk,scriptSig, txSigHash, nreq))
return null; return null;
console.log('[TransactionBuilder.js.552] ', wk.privKey.public.toString('hex')); //TODO
// Find an empty slot and sign // Find an empty slot and sign
for(var i=1; i<=nreq; i++) { for(var i=1; i<=nreq; i++) {
var chunk = scriptSig.chunks[i]; var chunk = scriptSig.chunks[i];
@ -581,8 +582,10 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
var signaturesAdded = 0; var signaturesAdded = 0;
for(var j=0; j<l && scriptSig.countMissingSignatures(); j++) { 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]}); var wk = this._findWalletKey(walletKeyMap, {pubKeyBuf: pubkeys[j]});
if (!wk) continue; 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); var newScriptSig = this._updateMultiSig(wk, scriptSig, txSigHash, nreq);
if (newScriptSig) { if (newScriptSig) {
@ -590,6 +593,10 @@ TransactionBuilder.prototype._signMultiSig = function(walletKeyMap, input, txSig
signaturesAdded++; signaturesAdded++;
} }
} }
if (!scriptSig.countMissingSignatures()) {
console.log('########## DONE!!!'); //TODO
}
return { return {
isFullySigned: scriptSig.countMissingSignatures() === 0, isFullySigned: scriptSig.countMissingSignatures() === 0,
@ -926,7 +933,6 @@ TransactionBuilder.prototype.merge = function(b, ignoreConflictingSignatures) {
throw new Error('mismatch at TransactionBuilder NTXID'); throw new Error('mismatch at TransactionBuilder NTXID');
this._mergeTx(b.tx, ignoreConflictingSignatures); this._mergeTx(b.tx, ignoreConflictingSignatures);
// TODO UPDATE: signaturesAdded, inputsSigned
} }
}; };

View File

@ -66,10 +66,11 @@
"cMyBgowsyrJRufoKWob73rMQB1PBqDdwFt8z4TJ6APN2HkmX1Ttm", "cMyBgowsyrJRufoKWob73rMQB1PBqDdwFt8z4TJ6APN2HkmX1Ttm",
"cN9yZCom6hAZpHtCp8ovE1zFa7RqDf3Cr4W6AwH2tp59Jjh9JcXu" "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": [ "unspentP2sh": [
{ {
"address": "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6", "address": "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6",
"scriptPubKey": "a91432d272ce8a9b482b363408a0b1dd28123d59c63387", "scriptPubKey": "a914dc0623476aefb049066b09b0147a022e6eb8429187",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1", "txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
"vout": 1, "vout": 1,
"amount": 1, "amount": 1,

View File

@ -44,8 +44,8 @@ describe('Script', function() {
script.finishedMultiSig().should.equal(true); script.finishedMultiSig().should.equal(true);
}); });
it('should report that this scripSig has not finished being signed', function() { it('should report that this scripSig has not finished being signed', function() {
var scriptHex = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff50100004c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae'; var scriptHex = '00483045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b014c69522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae';
//decoded: 3045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff501 0 0 5221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae //decoded: 0 3045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b01 522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae
//meaning: sig place_holder place_holder serialized_script //meaning: sig place_holder place_holder serialized_script
var scriptBuf = new Buffer(scriptHex, 'hex'); var scriptBuf = new Buffer(scriptHex, 'hex');
var script = new Script(scriptBuf); var script = new Script(scriptBuf);
@ -53,25 +53,6 @@ describe('Script', function() {
}); });
}); });
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() { describe('prependOp0', function() {
it('should prepend the script with OP_0', function() { it('should prepend the script with OP_0', function() {
var scriptHex = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff50100004c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae'; var scriptHex = '483045022002da7fae9b98615115b7e9a4f1d9efd581463b670f91ec6404a14cb6fc9c4531022100ff449d72ba4e72deb4317267e2d38cec9fd2f58a9afa39c9f5e35f5678694ff50100004c695221033e4cc6b6ee8d8ce3335fed6d4917b2bbbac0f5743b2ced101ba036f95c51e59421023147410ce15e0a31c2bb970cdf01517266dc2c9182f5938636ed363cfd4cc3ae210342a3c8a4b20c7a122a011a07063df04e4c5ad520a1302a2a66e174fd9b0d4ea453ae';
@ -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);
});
});
}); });

View File

@ -682,18 +682,31 @@ describe('TransactionBuilder', function() {
}; };
}; };
//
// bitcoind createmultisig 3 '["03197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d" , "0380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127" , "0392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed03", "03a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e3" , "03e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e4" ]'
//
// =>
//
// {
// "address" : "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6",
// "redeemScript" : "532103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e32103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e455ae"
// }
//
var getP2shBuilder = function(setMap) { var getP2shBuilder = function(setMap) {
var network = 'testnet'; var network = 'testnet';
var opts = { var opts = {
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'},
}; };
var data = getInfoForP2sh(); var data = getInfoForP2sh();
console.log('[test.TransactionBuilder.js.700:data:]',data); //TODO
process.exit(1);
// multisig p2sh // multisig p2sh
var p2shOpts = {nreq:3, pubkeys:data.pubkeys}; var p2shOpts = {nreq:3, pubkeys:data.pubkeys};
var info = TransactionBuilder.infoForP2sh(p2shOpts, network); var info = TransactionBuilder.infoForP2sh(p2shOpts, network);
console.log('[test.TransactionBuilder.js.693:info:]',info, p2shOpts); //TODO
var outs = outs || [{ var outs = outs || [{
address: info.address, address: 'mon1Hqs3jqKTtRSnRwJ3pRYMFos9WYfKb5',
amount: 0.08 amount: 0.08
}]; }];
var b = new TransactionBuilder(opts) var b = new TransactionBuilder(opts)
@ -713,14 +726,45 @@ describe('TransactionBuilder', function() {
(function() {b.sign(testdata.dataUnspentSign.keyStringsP2sh);}).should.throw(); (function() {b.sign(testdata.dataUnspentSign.keyStringsP2sh);}).should.throw();
}); });
it('should sign a p2sh/multisign tx', function() { it('should sign a p2sh/multisig tx right order', function(done) {
var b = getP2shBuilder(1); var b = getP2shBuilder(1);
b.sign(testdata.dataUnspentSign.keyStringsP2sh); b.sign([testdata.dataUnspentSign.keyStringsP2sh[3]]);
b.sign([testdata.dataUnspentSign.keyStringsP2sh[1]]);
b.sign([testdata.dataUnspentSign.keyStringsP2sh[2]]);
b.isFullySigned().should.equal(true); b.isFullySigned().should.equal(true);
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(true); 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) { it('#merge p2sh/steps', function(done) {
var b = getP2shBuilder(1); 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 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.isFullySigned().should.equal(false);
b.signaturesAdded.should.equal(0); b.signaturesAdded.should.equal(0);
b.sign(k1); b.sign(k1);