|
|
@ -16,7 +16,7 @@ var networks = bitcore.networks; |
|
|
|
var testdata = testdata || require('./testdata'); |
|
|
|
|
|
|
|
|
|
|
|
var vopts = { |
|
|
|
var vopts = { |
|
|
|
verifyP2SH: true, |
|
|
|
dontVerifyStrictEnc: true |
|
|
|
}; |
|
|
@ -33,7 +33,10 @@ describe('TransactionBuilder', function() { |
|
|
|
}); |
|
|
|
|
|
|
|
it('should be able to create instance with params', function() { |
|
|
|
var t = new TransactionBuilder({spendUnconfirmed: true, lockTime: 10}); |
|
|
|
var t = new TransactionBuilder({ |
|
|
|
spendUnconfirmed: true, |
|
|
|
lockTime: 10 |
|
|
|
}); |
|
|
|
should.exist(t); |
|
|
|
should.exist(t.lockTime); |
|
|
|
t.spendUnconfirmed.should.equal(true); |
|
|
@ -45,15 +48,17 @@ describe('TransactionBuilder', function() { |
|
|
|
satoshi.should.equal(10000); |
|
|
|
}); |
|
|
|
|
|
|
|
var getBuilder = function (spendUnconfirmed) { |
|
|
|
var t = new TransactionBuilder({spendUnconfirmed: spendUnconfirmed}) |
|
|
|
var getBuilder = function(spendUnconfirmed) { |
|
|
|
var t = new TransactionBuilder({ |
|
|
|
spendUnconfirmed: spendUnconfirmed |
|
|
|
}) |
|
|
|
.setUnspent(testdata.dataUnspent); |
|
|
|
|
|
|
|
return t; |
|
|
|
}; |
|
|
|
|
|
|
|
function f(amount, spendUnconfirmed) { |
|
|
|
spendUnconfirmed = typeof spendUnconfirmed === 'undefined'?true:false; |
|
|
|
spendUnconfirmed = typeof spendUnconfirmed === 'undefined' ? true : false; |
|
|
|
return getBuilder(spendUnconfirmed) |
|
|
|
._selectUnspent(amount * util.COIN).selectedUtxos; |
|
|
|
} |
|
|
@ -75,22 +80,28 @@ describe('TransactionBuilder', function() { |
|
|
|
}); |
|
|
|
|
|
|
|
it('#_selectUnspent should return null if not enough utxos', function() { |
|
|
|
(function() { f(1.12); }).should.throw(); |
|
|
|
(function() { |
|
|
|
f(1.12); |
|
|
|
}).should.throw(); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('#_selectUnspent should check confirmations', function() { |
|
|
|
(function() { f(0.9,false); }).should.throw(); |
|
|
|
(function() { |
|
|
|
f(0.9, false); |
|
|
|
}).should.throw(); |
|
|
|
f(0.9).length.should.equal(3); |
|
|
|
|
|
|
|
f(0.11,false).length.should.equal(2); |
|
|
|
(function() { f(0.111,false); }).should.throw(); |
|
|
|
f(0.11, false).length.should.equal(2); |
|
|
|
(function() { |
|
|
|
f(0.111, false); |
|
|
|
}).should.throw(); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('#_setInputs sets inputs', function() { |
|
|
|
var txobj={}; |
|
|
|
var txobj = {}; |
|
|
|
var b = getBuilder() |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
._selectUnspent(0.1 * util.COIN) |
|
|
@ -102,7 +113,7 @@ describe('TransactionBuilder', function() { |
|
|
|
}); |
|
|
|
|
|
|
|
it('#_setInputMap set inputMap', function() { |
|
|
|
var txobj={}; |
|
|
|
var txobj = {}; |
|
|
|
var b = getBuilder() |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
._selectUnspent(0.1 * util.COIN) |
|
|
@ -113,9 +124,11 @@ describe('TransactionBuilder', function() { |
|
|
|
b.inputMap.length.should.equal(2); |
|
|
|
}); |
|
|
|
|
|
|
|
var getBuilder2 = function (fee) { |
|
|
|
var getBuilder2 = function(fee) { |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
spendUnconfirmed: true, |
|
|
|
}; |
|
|
|
|
|
|
@ -126,7 +139,7 @@ describe('TransactionBuilder', function() { |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
|
|
|
|
return new TransactionBuilder(opts) |
|
|
|
return new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
.setOutputs(outs); |
|
|
|
}; |
|
|
@ -144,7 +157,9 @@ describe('TransactionBuilder', function() { |
|
|
|
var utxos = testdata.dataUnspent; |
|
|
|
|
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
spendUnconfirmed: true, |
|
|
|
}; |
|
|
|
var outs = [{ |
|
|
@ -154,9 +169,9 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
(function() { |
|
|
|
new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
.setOutputs(outs); |
|
|
|
}).should.throw(); |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
.setOutputs(outs); |
|
|
|
}).should.throw(); |
|
|
|
|
|
|
|
var outs2 = [{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
@ -173,9 +188,9 @@ describe('TransactionBuilder', function() { |
|
|
|
opts.spendUnconfirmed = false; |
|
|
|
(function() { |
|
|
|
new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
.setOutputs(outs2); |
|
|
|
}).should.throw(); |
|
|
|
.setUnspent(testdata.dataUnspent) |
|
|
|
.setOutputs(outs2); |
|
|
|
}).should.throw(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should be able to create a tx', function() { |
|
|
@ -218,10 +233,12 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getBuilder3 = function (outs, signhash) { |
|
|
|
var getBuilder3 = function(outs, signhash) { |
|
|
|
|
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
spendUnconfirmed: true, |
|
|
|
signhash: signhash, |
|
|
|
}; |
|
|
@ -231,7 +248,7 @@ describe('TransactionBuilder', function() { |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
|
|
|
|
return new TransactionBuilder(opts) |
|
|
|
return new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspent) |
|
|
|
.setOutputs(outs); |
|
|
|
}; |
|
|
@ -250,8 +267,8 @@ describe('TransactionBuilder', function() { |
|
|
|
tx.outs.length.should.equal(2); |
|
|
|
|
|
|
|
var shex = testdata.dataUnspentSign.unspent[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -261,9 +278,9 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should sign a tx (case 2)', function(done) { |
|
|
|
var b = getBuilder3([{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
.sign(testdata.dataUnspentSign.keyStrings); |
|
|
|
|
|
|
|
b.isFullySigned().should.equal(true); |
|
|
@ -273,8 +290,8 @@ describe('TransactionBuilder', function() { |
|
|
|
tx.outs.length.should.equal(2); |
|
|
|
|
|
|
|
var shex = testdata.dataUnspentSign.unspent[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -290,7 +307,7 @@ describe('TransactionBuilder', function() { |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
|
|
|
|
var b = new TransactionBuilder() |
|
|
|
var b = new TransactionBuilder() |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspent) |
|
|
|
.setOutputs(outs) |
|
|
|
.sign(keys); |
|
|
@ -326,8 +343,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspent[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -362,8 +379,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspent[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -397,8 +414,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspent[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -430,7 +447,7 @@ describe('TransactionBuilder', function() { |
|
|
|
tx.ins.length.should.equal(2); |
|
|
|
|
|
|
|
// outs = 101 outs + 1 remainder
|
|
|
|
tx.outs.length.should.equal(N+1); |
|
|
|
tx.outs.length.should.equal(N + 1); |
|
|
|
|
|
|
|
|
|
|
|
// 3560 bytes tx -> 0.0004
|
|
|
@ -469,7 +486,7 @@ describe('TransactionBuilder', function() { |
|
|
|
// remainder = 1.0101-1.0004 = 0.0097
|
|
|
|
|
|
|
|
// outs = 101 outs + 1 remainder
|
|
|
|
tx.outs.length.should.equal(N+1); |
|
|
|
tx.outs.length.should.equal(N + 1); |
|
|
|
|
|
|
|
|
|
|
|
// 3560 bytes tx -> 0.0004
|
|
|
@ -484,14 +501,16 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should sign a p2pubkey tx', function(done) { |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
}; |
|
|
|
var outs = outs || [{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
|
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspentPubKey) |
|
|
|
.setOutputs(outs) |
|
|
|
.sign(testdata.dataUnspentSign.keyStringsPubKey); |
|
|
@ -504,8 +523,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspentPubKey[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -517,13 +536,15 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should sign a multisig tx', function(done) { |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
}; |
|
|
|
var outs = outs || [{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspentMulti) |
|
|
|
.setOutputs(outs); |
|
|
|
|
|
|
@ -536,8 +557,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspentMulti[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -548,19 +569,21 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should sign a multisig tx in steps (3-5)', function(done) { |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
}; |
|
|
|
var outs = outs || [{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspentMulti) |
|
|
|
.setOutputs(outs); |
|
|
|
|
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsMulti.slice(0,1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsMulti.slice(1,2); |
|
|
|
var k3 = testdata.dataUnspentSign.keyStringsMulti.slice(2,3); |
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsMulti.slice(0, 1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsMulti.slice(1, 2); |
|
|
|
var k3 = testdata.dataUnspentSign.keyStringsMulti.slice(2, 3); |
|
|
|
|
|
|
|
b.sign(k1); |
|
|
|
b.isFullySigned().should.equal(false); |
|
|
@ -576,8 +599,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspentMulti[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -589,19 +612,21 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should count multisig signs (3-5)', function() { |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
}; |
|
|
|
var outs = outs || [{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspentMulti) |
|
|
|
.setOutputs(outs); |
|
|
|
|
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsMulti.slice(0,1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsMulti.slice(1,2); |
|
|
|
var k3 = testdata.dataUnspentSign.keyStringsMulti.slice(2,3); |
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsMulti.slice(0, 1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsMulti.slice(1, 2); |
|
|
|
var k3 = testdata.dataUnspentSign.keyStringsMulti.slice(2, 3); |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
b.isFullySigned().should.equal(false); |
|
|
@ -627,18 +652,20 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should avoid siging with the same key twice multisig signs (3-5)', function(done) { |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
}; |
|
|
|
var outs = outs || [{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspentMulti) |
|
|
|
.setOutputs(outs); |
|
|
|
|
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsMulti.slice(0,1); |
|
|
|
var k23 = testdata.dataUnspentSign.keyStringsMulti.slice(1,3); |
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsMulti.slice(0, 1); |
|
|
|
var k23 = testdata.dataUnspentSign.keyStringsMulti.slice(1, 3); |
|
|
|
var tx = b.build(); |
|
|
|
|
|
|
|
tx.countInputSignatures(0).should.equal(0); |
|
|
@ -657,8 +684,8 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var tx = b.build(); |
|
|
|
var shex = testdata.dataUnspentSign.unspentMulti[0].scriptPubKey; |
|
|
|
var s = new Script(new Buffer(shex,'hex')); |
|
|
|
tx.verifyInput(0,s, vopts, function(err, results){ |
|
|
|
var s = new Script(new Buffer(shex, 'hex')); |
|
|
|
tx.verifyInput(0, s, vopts, function(err, results) { |
|
|
|
should.not.exist(err); |
|
|
|
should.exist(results); |
|
|
|
results.should.equal(true); |
|
|
@ -669,12 +696,16 @@ describe('TransactionBuilder', function() { |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
var getInfoForP2sh = function () { |
|
|
|
var privs = testdata.dataUnspentSign.keyStringsP2sh; |
|
|
|
var getInfoForP2sh = function() { |
|
|
|
var privs = testdata.dataUnspentSign.keyStringsP2sh; |
|
|
|
var pubkeys = []; |
|
|
|
privs.forEach(function(p) { |
|
|
|
var wk = new WalletKey({network: networks.testnet}); |
|
|
|
wk.fromObj({priv: p}); |
|
|
|
var wk = new WalletKey({ |
|
|
|
network: networks.testnet |
|
|
|
}); |
|
|
|
wk.fromObj({ |
|
|
|
priv: p |
|
|
|
}); |
|
|
|
pubkeys.push(bitcore.buffertools.toHex(wk.privKey.public)); |
|
|
|
}); |
|
|
|
|
|
|
@ -684,37 +715,42 @@ describe('TransactionBuilder', function() { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
//
|
|
|
|
// bitcoind createmultisig 3 '["03197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d" , "0380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127" , "0392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed03", "03a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e3" , "03e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e4" ]'
|
|
|
|
//
|
|
|
|
// =>
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "address" : "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6",
|
|
|
|
// "redeemScript" : "532103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e32103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e455ae"
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// bitcoind createmultisig 3 '["03197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d" , "0380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127" , "0392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed03", "03a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e3" , "03e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e4" ]'
|
|
|
|
//
|
|
|
|
// =>
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "address" : "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6",
|
|
|
|
// "redeemScript" : "532103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210380a29968851f93af55e581c43d9ef9294577a439a3ca9fc2bc47d1ca2b3e9127210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103a94351fecc4328bb683bf93a1aa67378374904eac5980c7966723a51897c56e32103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e455ae"
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
var getP2shBuilder = function(setMap, opts) { |
|
|
|
var network = 'testnet'; |
|
|
|
opts = opts || {}; |
|
|
|
opts.remainderOut = opts.remainderOut || {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}; |
|
|
|
opts.remainderOut = opts.remainderOut || { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}; |
|
|
|
|
|
|
|
var data = getInfoForP2sh(); |
|
|
|
// multisig p2sh
|
|
|
|
var p2shOpts = {nreq:3, pubkeys:data.pubkeys}; |
|
|
|
var p2shOpts = { |
|
|
|
nreq: 3, |
|
|
|
pubkeys: data.pubkeys |
|
|
|
}; |
|
|
|
var info = TransactionBuilder.infoForP2sh(p2shOpts, network); |
|
|
|
|
|
|
|
var outs = outs || [{ |
|
|
|
address: 'mon1Hqs3jqKTtRSnRwJ3pRYMFos9WYfKb5', |
|
|
|
amount: 0.08 |
|
|
|
}]; |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent(testdata.dataUnspentSign.unspentP2sh) |
|
|
|
.setOutputs(outs); |
|
|
|
|
|
|
|
if (setMap) { |
|
|
|
var hashMap = {}; |
|
|
|
hashMap[info.address]=info.scriptBufHex; |
|
|
|
hashMap[info.address] = info.scriptBufHex; |
|
|
|
b.setHashToScriptMap(hashMap); |
|
|
|
} |
|
|
|
return b; |
|
|
@ -722,7 +758,9 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should fail to sign a p2sh/multisign tx if none script map was given', function() { |
|
|
|
var b = getP2shBuilder(); |
|
|
|
(function() {b.sign(testdata.dataUnspentSign.keyStringsP2sh);}).should.throw(); |
|
|
|
(function() { |
|
|
|
b.sign(testdata.dataUnspentSign.keyStringsP2sh); |
|
|
|
}).should.throw(); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -734,8 +772,8 @@ describe('TransactionBuilder', function() { |
|
|
|
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){ |
|
|
|
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); |
|
|
@ -744,7 +782,14 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,2,1],[3,1,2]].forEach(function(order) { |
|
|
|
[ |
|
|
|
[1, 2, 3], |
|
|
|
[1, 3, 2], |
|
|
|
[2, 1, 3], |
|
|
|
[2, 3, 1], |
|
|
|
[3, 2, 1], |
|
|
|
[3, 1, 2] |
|
|
|
].forEach(function(order) { |
|
|
|
it('should sign a p2sh/multisig tx in order ' + order.join(','), function(done) { |
|
|
|
var b = getP2shBuilder(1); |
|
|
|
b.sign([testdata.dataUnspentSign.keyStringsP2sh[3]]); |
|
|
@ -757,9 +802,9 @@ describe('TransactionBuilder', function() { |
|
|
|
it('should sign in steps a p2sh/multisign tx', function() { |
|
|
|
var b = getP2shBuilder(1); |
|
|
|
|
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(0,1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2); |
|
|
|
var k5 = testdata.dataUnspentSign.keyStringsP2sh.slice(4,5); |
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(0, 1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1, 2); |
|
|
|
var k5 = testdata.dataUnspentSign.keyStringsP2sh.slice(4, 5); |
|
|
|
b.isFullySigned().should.equal(false); |
|
|
|
|
|
|
|
b.sign(k1); |
|
|
@ -777,7 +822,7 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
// Sign with k5
|
|
|
|
b.sign(k5); |
|
|
|
///
|
|
|
|
///
|
|
|
|
b.isFullySigned().should.equal(false); |
|
|
|
tx.isComplete().should.equal(false); |
|
|
|
|
|
|
@ -797,10 +842,14 @@ describe('TransactionBuilder', function() { |
|
|
|
var priv = 'cMpKwGr5oxEacN95WFKNEq6tTcvi11regFwS3muHvGYVxMPJX8JA'; |
|
|
|
var network = 'testnet'; |
|
|
|
var opts = { |
|
|
|
remainderOut: {address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'}, |
|
|
|
remainderOut: { |
|
|
|
address: 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd' |
|
|
|
}, |
|
|
|
}; |
|
|
|
// p2hash/ p2sh
|
|
|
|
var p2shOpts = {address:'mgwqzy6pF5BSc72vxHBFSnnhNEBcV4TJzV'}; |
|
|
|
var p2shOpts = { |
|
|
|
address: 'mgwqzy6pF5BSc72vxHBFSnnhNEBcV4TJzV' |
|
|
|
}; |
|
|
|
var info = TransactionBuilder.infoForP2sh(p2shOpts, network); |
|
|
|
|
|
|
|
//addr: 2NAwCQ1jPYPrSsyBQvfP6AJ6d6SSxnHsZ4e
|
|
|
@ -812,22 +861,22 @@ describe('TransactionBuilder', function() { |
|
|
|
}]; |
|
|
|
//info.scriptBufHex,
|
|
|
|
|
|
|
|
var s = new Address(info.address).getScriptPubKey() |
|
|
|
.getBuffer().toString('hex'); |
|
|
|
var s = new Address(info.address).getScriptPubKey() |
|
|
|
.getBuffer().toString('hex'); |
|
|
|
|
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
var b = new TransactionBuilder(opts) |
|
|
|
.setUnspent([{ |
|
|
|
"address": info.address, |
|
|
|
"scriptPubKey": s, |
|
|
|
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1", |
|
|
|
"vout": 1, |
|
|
|
"amount": 1, |
|
|
|
"confirmations":7 |
|
|
|
"address": info.address, |
|
|
|
"scriptPubKey": s, |
|
|
|
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1", |
|
|
|
"vout": 1, |
|
|
|
"amount": 1, |
|
|
|
"confirmations": 7 |
|
|
|
}]) |
|
|
|
.setOutputs(outs); |
|
|
|
|
|
|
|
var hashMap = {}; |
|
|
|
hashMap[info.address]=info.scriptBufHex; |
|
|
|
hashMap[info.address] = info.scriptBufHex; |
|
|
|
b.setHashToScriptMap(hashMap); |
|
|
|
b.sign([priv]); |
|
|
|
b.isFullySigned().should.equal(true); |
|
|
@ -841,11 +890,13 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('should check sign parameters', function() { |
|
|
|
var b = getP2shBuilder(1); |
|
|
|
(function() { b.sign(testdata.dataUnspentSign.keyStringsP2sh[0]) }).should.throw('array'); |
|
|
|
(function() { |
|
|
|
b.sign(testdata.dataUnspentSign.keyStringsP2sh[0]) |
|
|
|
}).should.throw('array'); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('serizalization', function(){ |
|
|
|
describe('serizalization', function() { |
|
|
|
|
|
|
|
it('#toObj #fromObj roundtrip', function() { |
|
|
|
var b = getBuilder2(); |
|
|
@ -853,7 +904,7 @@ describe('TransactionBuilder', function() { |
|
|
|
b.isFullySigned().should.equal(false); |
|
|
|
b.getSelectedUnspent().length.should.equal(2); |
|
|
|
|
|
|
|
var data =b.toObj(); |
|
|
|
var data = b.toObj(); |
|
|
|
|
|
|
|
var b2 = TransactionBuilder.fromObj(data); |
|
|
|
var tx = b2.build(); |
|
|
@ -873,9 +924,9 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
var keys = JSON.parse(JSON.stringify(testdata.dataUnspentSign.keyStringsP2sh)); |
|
|
|
|
|
|
|
var k1 = keys.slice(0,1); |
|
|
|
var k2 = keys.slice(1,2); |
|
|
|
var k5 = keys.slice(4,5); |
|
|
|
var k1 = keys.slice(0, 1); |
|
|
|
var k2 = keys.slice(1, 2); |
|
|
|
var k5 = keys.slice(4, 5); |
|
|
|
b.isFullySigned().should.equal(false); |
|
|
|
|
|
|
|
var b2 = TransactionBuilder.fromObj(b.toObj()); |
|
|
@ -930,9 +981,9 @@ describe('TransactionBuilder', function() { |
|
|
|
describe('#merge', function() { |
|
|
|
it('with self', function() { |
|
|
|
var b = getBuilder3([{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
.sign(testdata.dataUnspentSign.keyStrings); |
|
|
|
b.merge(b); |
|
|
|
|
|
|
@ -945,9 +996,9 @@ describe('TransactionBuilder', function() { |
|
|
|
|
|
|
|
it('#merge simple', function() { |
|
|
|
var b = getBuilder3([{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
.sign(testdata.dataUnspentSign.keyStrings); |
|
|
|
|
|
|
|
// merge simple
|
|
|
@ -977,7 +1028,9 @@ describe('TransactionBuilder', function() { |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 15 |
|
|
|
}]); |
|
|
|
(function() {b2.merge(b);}).should.throw('NTXID'); |
|
|
|
(function() { |
|
|
|
b2.merge(b); |
|
|
|
}).should.throw('NTXID'); |
|
|
|
}); |
|
|
|
it('should check addresses', function() { |
|
|
|
// bad out
|
|
|
@ -985,7 +1038,9 @@ describe('TransactionBuilder', function() { |
|
|
|
address: 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz', |
|
|
|
amount: 16 |
|
|
|
}]); |
|
|
|
(function() {b2.merge(b);}).should.throw('NTXID'); |
|
|
|
(function() { |
|
|
|
b2.merge(b); |
|
|
|
}).should.throw('NTXID'); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -1000,10 +1055,12 @@ describe('TransactionBuilder', function() { |
|
|
|
var b2 = getBuilder3([{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 15 |
|
|
|
}], bitcore.Transaction.SIGHASH_NONE); |
|
|
|
}], bitcore.Transaction.SIGHASH_NONE); |
|
|
|
b2.sign(testdata.dataUnspentSign.keyStrings); |
|
|
|
|
|
|
|
(function() {b2.merge(b);}).should.throw('signhash'); |
|
|
|
(function() { |
|
|
|
b2.merge(b); |
|
|
|
}).should.throw('signhash'); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -1011,15 +1068,15 @@ describe('TransactionBuilder', function() { |
|
|
|
// same signature
|
|
|
|
// -> keep first signature
|
|
|
|
var b = getBuilder3([{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
.sign(testdata.dataUnspentSign.keyStrings); |
|
|
|
// merge simple
|
|
|
|
var b2 = getBuilder3([{ |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', |
|
|
|
amount: 16 |
|
|
|
}]) |
|
|
|
.sign(testdata.dataUnspentSign.keyStrings); |
|
|
|
b2.isFullySigned().should.equal(true); |
|
|
|
b2.merge(b); |
|
|
@ -1045,7 +1102,7 @@ describe('TransactionBuilder', function() { |
|
|
|
var k1 = testdata.dataUnspentSign.keyStringsP2sh[0]; |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsP2sh[1]; |
|
|
|
var k3 = testdata.dataUnspentSign.keyStringsP2sh[2]; |
|
|
|
b.sign([k1,k2]); |
|
|
|
b.sign([k1, k2]); |
|
|
|
b2.sign([k3]); |
|
|
|
b.merge(b2); |
|
|
|
var tx = b.build(); |
|
|
@ -1061,7 +1118,7 @@ describe('TransactionBuilder', function() { |
|
|
|
var b2 = getP2shBuilder(1); |
|
|
|
|
|
|
|
b.sign([k1]); |
|
|
|
b2.sign([k1,k2]); |
|
|
|
b2.sign([k1, k2]); |
|
|
|
b2.merge(b); |
|
|
|
var tx = b2.build(); |
|
|
|
tx.countInputSignatures(0).should.equal(2); |
|
|
@ -1110,32 +1167,42 @@ describe('TransactionBuilder', 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 k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(0, 1); |
|
|
|
var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1, 2); |
|
|
|
b.isFullySigned().should.equal(false); |
|
|
|
b.sign(k1); |
|
|
|
var tx = b.build(); |
|
|
|
tx.isComplete().should.equal(false); |
|
|
|
|
|
|
|
var b2 = getP2shBuilder(1, {signhash: bitcore.Transaction.SIGHASH_NONE }); |
|
|
|
var b2 = getP2shBuilder(1, { |
|
|
|
signhash: bitcore.Transaction.SIGHASH_NONE |
|
|
|
}); |
|
|
|
b2.sign(k2); |
|
|
|
(function() { b2.merge(b)}).should.throw(); |
|
|
|
(function() { |
|
|
|
b2.merge(b) |
|
|
|
}).should.throw(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('#merge p2sh/steps change return address', 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); |
|
|
|
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, {remainderOut: {address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE' }}); |
|
|
|
var b2 = getP2shBuilder(1, { |
|
|
|
remainderOut: { |
|
|
|
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE' |
|
|
|
} |
|
|
|
}); |
|
|
|
b2.sign(k2); |
|
|
|
(function() { b2.merge(b)}).should.throw('NTXID'); |
|
|
|
(function() { |
|
|
|
b2.merge(b) |
|
|
|
}).should.throw('NTXID'); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|