2014-04-09 13:28:35 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var chai = chai || require('chai');
|
|
|
|
var should = chai.should();
|
|
|
|
var bitcore = bitcore || require('bitcore');
|
|
|
|
var Transaction = bitcore.Transaction;
|
|
|
|
var buffertools = bitcore.buffertools;
|
|
|
|
var WalletKey = bitcore.WalletKey;
|
|
|
|
var Key = bitcore.Key;
|
|
|
|
var bignum = bitcore.bignum;
|
2014-04-10 21:09:42 -07:00
|
|
|
var Script = bitcore.Script;
|
2014-04-15 14:23:35 -07:00
|
|
|
var Builder = bitcore.TransactionBuilder;
|
2014-04-10 21:09:42 -07:00
|
|
|
var util = bitcore.util;
|
2014-04-09 13:28:35 -07:00
|
|
|
var networks = bitcore.networks;
|
|
|
|
var copay = copay || require('../copay');
|
|
|
|
var fakeStorage = copay.FakeStorage;
|
2014-04-09 16:37:14 -07:00
|
|
|
var PrivateKey = copay.PrivateKey || require('../js/models/PrivateKey');
|
2014-04-09 13:28:35 -07:00
|
|
|
var TxProposals = copay.TxProposals || require('../js/models/TxProposal');
|
2014-04-14 11:31:10 -07:00
|
|
|
var PublicKeyRing = is_browser ? copay.PublicKeyRing :
|
|
|
|
require('soop').load('../js/models/core/PublicKeyRing', {Storage: fakeStorage});
|
|
|
|
var is_browser = (typeof process.versions === 'undefined')
|
2014-04-09 13:28:35 -07:00
|
|
|
|
|
|
|
var config = {
|
|
|
|
networkName:'livenet',
|
|
|
|
};
|
|
|
|
|
|
|
|
var unspentTest = [
|
|
|
|
{
|
|
|
|
"address": "dummy",
|
|
|
|
"scriptPubKey": "dummy",
|
|
|
|
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
|
|
|
|
"vout": 1,
|
|
|
|
"amount": 10,
|
|
|
|
"confirmations":7
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2014-04-10 21:09:42 -07:00
|
|
|
var createPKR = function (bip32s) {
|
2014-04-09 13:28:35 -07:00
|
|
|
var w = new PublicKeyRing(config);
|
|
|
|
should.exist(w);
|
|
|
|
|
|
|
|
for(var i=0; i<5; i++) {
|
|
|
|
if (bip32s) {
|
|
|
|
var b=bip32s[i];
|
2014-05-29 12:17:17 -07:00
|
|
|
w.addCopayer(b?b.deriveBIP45Branch().extendedPublicKeyString():null);
|
2014-04-09 13:28:35 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
w.addCopayer();
|
|
|
|
}
|
2014-04-09 22:16:57 -07:00
|
|
|
w.generateAddress(true);
|
|
|
|
w.generateAddress(true);
|
|
|
|
w.generateAddress(true);
|
|
|
|
w.generateAddress(false);
|
|
|
|
w.generateAddress(false);
|
|
|
|
w.generateAddress(false);
|
|
|
|
//3x3 indexes
|
2014-04-09 13:28:35 -07:00
|
|
|
|
|
|
|
return w;
|
|
|
|
};
|
|
|
|
|
2014-04-21 16:28:57 -07:00
|
|
|
var vopts = {
|
|
|
|
verifyP2SH: true,
|
|
|
|
dontVerifyStrictEnc: true
|
|
|
|
};
|
|
|
|
|
2014-04-09 13:28:35 -07:00
|
|
|
|
|
|
|
describe('TxProposals model', function() {
|
|
|
|
|
2014-04-21 16:28:57 -07:00
|
|
|
it('verify TXs', function (done) {
|
|
|
|
|
|
|
|
var priv = new PrivateKey(config);
|
|
|
|
var priv2 = new PrivateKey(config);
|
|
|
|
var priv3 = new PrivateKey(config);
|
2014-05-29 12:17:17 -07:00
|
|
|
var ts = Date.now();
|
2014-04-21 16:28:57 -07:00
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
|
|
|
var pkr = createPKR([priv, priv2, priv3]);
|
|
|
|
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
|
|
|
|
|
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w.add(createTx(
|
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
|
|
|
'123456789',
|
|
|
|
unspentTest,
|
|
|
|
opts,
|
|
|
|
priv,
|
|
|
|
pkr
|
|
|
|
));
|
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var b = w.txps[k].builder;
|
|
|
|
var tx = b.build();
|
|
|
|
tx.isComplete().should.equal(false);
|
2014-06-04 09:44:32 -07:00
|
|
|
b.sign(priv2.getAll(pkr.indexes.getReceiveIndex(), pkr.indexes.getChangeIndex()) );
|
|
|
|
b.sign(priv3.getAll(pkr.indexes.getReceiveIndex(), pkr.indexes.getChangeIndex()) );
|
2014-04-21 16:28:57 -07:00
|
|
|
tx = b.build();
|
|
|
|
tx.isComplete().should.equal(true);
|
|
|
|
|
|
|
|
var s = new Script(new Buffer(unspentTest[0].scriptPubKey,'hex'));
|
|
|
|
|
|
|
|
tx.verifyInput(0,s, {
|
|
|
|
verifyP2SH: true,
|
|
|
|
dontVerifyStrictEnc: true
|
|
|
|
}, function(err, results){
|
2014-04-22 18:07:18 -07:00
|
|
|
should.not.exist(err);
|
|
|
|
results.should.equal(true);
|
2014-04-21 16:28:57 -07:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-04-22 18:07:18 -07:00
|
|
|
it('should create an instance', function () {
|
2014-04-09 13:28:35 -07:00
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName
|
|
|
|
});
|
|
|
|
should.exist(w);
|
|
|
|
w.network.name.should.equal('livenet');
|
|
|
|
});
|
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
function createTx(toAddress, amountSatStr, utxos, opts, priv, pkr) {
|
|
|
|
opts = opts || {};
|
2014-04-09 13:28:35 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
var amountSat = bitcore.bignum(amountSatStr);
|
2014-04-09 13:28:35 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
if(! pkr.isComplete() ) {
|
|
|
|
throw new Error('publicKeyRing is not complete');
|
|
|
|
}
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
if (!opts.remainderOut) {
|
|
|
|
opts.remainderOut ={ address: pkr.generateAddress(true).toString() };
|
|
|
|
};
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
var b = new Builder(opts)
|
|
|
|
.setUnspent(utxos)
|
|
|
|
.setHashToScriptMap(pkr.getRedeemScriptMap())
|
|
|
|
.setOutputs([{address: toAddress, amountSat: amountSat}])
|
|
|
|
;
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
var signRet;
|
|
|
|
if (priv) {
|
2014-06-04 09:44:32 -07:00
|
|
|
b.sign( priv.getAll(pkr.indexes.getReceiveIndex(), pkr.indexes.getChangeIndex()) );
|
2014-04-15 14:23:35 -07:00
|
|
|
}
|
|
|
|
var me = {};
|
|
|
|
if (priv) me[priv.id] = Date.now();
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
return {
|
|
|
|
signedBy: priv && b.signaturesAdded ? me : {},
|
|
|
|
seenBy: priv ? me : {},
|
|
|
|
builder: b,
|
|
|
|
};
|
|
|
|
};
|
2014-04-09 13:28:35 -07:00
|
|
|
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-22 18:07:18 -07:00
|
|
|
it('#getUsedUnspend', function () {
|
2014-04-21 08:00:14 -07:00
|
|
|
var priv = new PrivateKey(config);
|
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
|
|
|
var start = new Date().getTime();
|
|
|
|
var pkr=createPKR([priv]);
|
|
|
|
var ts = Date.now();
|
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w.add(createTx(
|
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
|
|
|
'123456789',
|
|
|
|
unspentTest,
|
|
|
|
{},
|
|
|
|
priv,
|
|
|
|
pkr
|
|
|
|
));
|
2014-06-05 11:43:43 -07:00
|
|
|
var uu = w.getUsedUnspent();
|
|
|
|
var uuk = Object.keys(uu);
|
|
|
|
uuk.length.should.equal(1);
|
|
|
|
uuk[0].split(',')[0].should.equal(unspentTest[0].txid);
|
2014-04-21 08:00:14 -07:00
|
|
|
});
|
|
|
|
|
2014-06-09 06:30:37 -07:00
|
|
|
it.skip('#merge with self', function () {
|
2014-04-10 21:09:42 -07:00
|
|
|
var priv = new PrivateKey(config);
|
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-17 11:04:39 -07:00
|
|
|
var start = new Date().getTime();
|
2014-04-17 13:01:31 -07:00
|
|
|
var pkr=createPKR([priv]);
|
2014-04-10 21:09:42 -07:00
|
|
|
var ts = Date.now();
|
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
{},
|
|
|
|
priv,
|
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-10 21:09:42 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
w.merge(w);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps).length.should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-22 18:07:18 -07:00
|
|
|
it('#merge, merge signatures case 1', function () {
|
2014-04-10 21:09:42 -07:00
|
|
|
var priv2 = new PrivateKey(config);
|
|
|
|
var priv = new PrivateKey(config);
|
|
|
|
var ts = Date.now();
|
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
2014-04-17 13:01:31 -07:00
|
|
|
var pkr = createPKR([priv]);
|
2014-04-10 21:09:42 -07:00
|
|
|
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
|
|
|
|
|
|
|
|
|
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv2,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps[k].signedBy).length.should.equal(0);
|
|
|
|
Object.keys(w.txps[k].seenBy).length.should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
var w2 = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
publicKeyRing: w.publicKeyRing,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w2.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var tx = w2.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
(w2.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w2.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
w.merge(w2);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps).length.should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
var _dumpChunks = function (scriptSig, label) {
|
|
|
|
console.log('## DUMP: ' + label + ' ##');
|
|
|
|
for(var i=0; i<scriptSig.chunks.length; i++) {
|
|
|
|
console.log('\tCHUNK ', i, scriptSig.chunks[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-22 18:07:18 -07:00
|
|
|
it('#merge, merge signatures case 2', function () {
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
var priv = new PrivateKey(config);
|
|
|
|
var priv2 = new PrivateKey(config);
|
|
|
|
var priv3 = new PrivateKey(config);
|
|
|
|
var ts = Date.now();
|
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
2014-04-17 13:01:31 -07:00
|
|
|
var pkr = createPKR([priv, priv2]);
|
2014-04-10 21:09:42 -07:00
|
|
|
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
|
|
|
|
|
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
|
|
|
|
w.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv3,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps[k].signedBy).length.should.equal(0);
|
|
|
|
Object.keys(w.txps[k].seenBy).length.should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
var w2 = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
|
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w2.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w2.txps)[0];
|
|
|
|
tx = w2.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
(w2.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w2.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
w.merge(w2);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps).length.should.equal(1);
|
2014-04-13 07:00:58 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
var w3 = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
publicKeyRing: pkr,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w3.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv2,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
tx = w3.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
(w3.txps[k].signedBy[priv2.id] - ts > 0).should.equal(true);
|
|
|
|
(w3.txps[k].seenBy[priv2.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
w.merge(w3);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps).length.should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].signedBy[priv2.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv2.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(1);
|
2014-04-10 21:09:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-09 07:19:38 -07:00
|
|
|
it.skip('#merge, merge signatures case 3', function () {
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
var priv = new PrivateKey(config);
|
|
|
|
var priv2 = new PrivateKey(config);
|
|
|
|
var priv3 = new PrivateKey(config);
|
|
|
|
var ts = Date.now();
|
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
2014-04-17 13:01:31 -07:00
|
|
|
var pkr = createPKR([priv, priv2, priv3]);
|
2014-04-10 21:09:42 -07:00
|
|
|
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
|
|
|
|
|
|
|
|
var w = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
var w2 = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w2.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv2,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
var tx = w2.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w2.txps[k].signedBy[priv2.id] - ts > 0).should.equal(true);
|
|
|
|
(w2.txps[k].seenBy[priv2.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
var w3 = new TxProposals({
|
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w3.add(createTx(
|
2014-04-10 21:09:42 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-10 21:09:42 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
opts,
|
2014-04-10 21:09:42 -07:00
|
|
|
priv3,
|
2014-04-15 14:23:35 -07:00
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
var tx = w3.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w3.txps[k].signedBy[priv3.id] - ts > 0).should.equal(true);
|
|
|
|
(w3.txps[k].seenBy[priv3.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
w.merge(w2);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps).length.should.equal(1);
|
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(1);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv2.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].signedBy[priv2.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
w.merge(w3);
|
2014-04-18 15:28:28 -07:00
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-13 07:00:58 -07:00
|
|
|
tx.isComplete().should.equal(true);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(0);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w.txps).length.should.equal(1);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv2.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv3.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].signedBy[priv2.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].signedBy[priv3.id] - ts > 0).should.equal(true);
|
2014-04-10 21:09:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-22 18:07:18 -07:00
|
|
|
it('#toObj #fromObj roundtrip', function () {
|
2014-04-09 19:04:22 -07:00
|
|
|
|
|
|
|
var priv = new PrivateKey(config);
|
2014-04-17 13:01:31 -07:00
|
|
|
var pkr = createPKR([priv]);
|
2014-04-09 19:04:22 -07:00
|
|
|
var w = new TxProposals({
|
2014-04-11 09:26:36 -07:00
|
|
|
walletId: 'qwerty',
|
2014-04-09 19:04:22 -07:00
|
|
|
networkName: config.networkName,
|
|
|
|
});
|
2014-04-09 22:16:57 -07:00
|
|
|
var ts = Date.now();
|
|
|
|
var isChange=0;
|
|
|
|
var index=0;
|
2014-04-09 13:28:35 -07:00
|
|
|
|
2014-04-15 14:23:35 -07:00
|
|
|
unspentTest[0].address = pkr.getAddress(index, isChange).toString();
|
|
|
|
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange);
|
|
|
|
w.add(createTx(
|
2014-04-09 22:16:57 -07:00
|
|
|
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
|
2014-04-11 09:26:36 -07:00
|
|
|
'123456789',
|
2014-04-09 22:16:57 -07:00
|
|
|
unspentTest,
|
2014-04-15 14:23:35 -07:00
|
|
|
{},
|
|
|
|
priv,
|
|
|
|
pkr
|
|
|
|
));
|
2014-04-18 15:28:28 -07:00
|
|
|
var k = Object.keys(w.txps)[0];
|
|
|
|
var tx = w.txps[k].builder.build();
|
2014-04-09 22:16:57 -07:00
|
|
|
tx.isComplete().should.equal(false);
|
|
|
|
tx.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
2014-04-09 22:16:57 -07:00
|
|
|
|
|
|
|
var o = w.toObj();
|
|
|
|
should.exist(o);
|
2014-04-10 21:09:42 -07:00
|
|
|
o.txps.length.should.equal(1);
|
2014-04-13 07:00:58 -07:00
|
|
|
should.exist(o.txps[0]);
|
2014-04-10 21:09:42 -07:00
|
|
|
should.exist(o.txps[0].signedBy);
|
|
|
|
should.exist(o.txps[0].seenBy);
|
2014-04-13 07:00:58 -07:00
|
|
|
should.exist(o.txps[0].builderObj);
|
|
|
|
should.exist(o.txps[0].builderObj.valueInSat);
|
2014-04-10 21:09:42 -07:00
|
|
|
should.exist(o.txps[0].signedBy[priv.id]);
|
2014-04-09 22:16:57 -07:00
|
|
|
|
2014-04-18 15:28:28 -07:00
|
|
|
var o2 = JSON.parse(JSON.stringify(o));
|
|
|
|
var w2 = TxProposals.fromObj(o2);
|
2014-04-11 09:26:36 -07:00
|
|
|
w2.walletId.should.equal(w.walletId);
|
2014-04-18 15:28:28 -07:00
|
|
|
var tx2 = w2.txps[k].builder.build();
|
2014-04-09 22:16:57 -07:00
|
|
|
tx2.isComplete().should.equal(false);
|
|
|
|
tx2.countInputMissingSignatures(0).should.equal(2);
|
2014-04-18 15:28:28 -07:00
|
|
|
(w2.txps[k].signedBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
(w2.txps[k].seenBy[priv.id] - ts > 0).should.equal(true);
|
|
|
|
should.exist(w2.txps[k].builder);
|
|
|
|
should.exist(w2.txps[k].builder.valueInSat);
|
2014-04-13 07:00:58 -07:00
|
|
|
|
2014-04-11 09:26:36 -07:00
|
|
|
w2.merge(w);
|
2014-04-18 15:28:28 -07:00
|
|
|
Object.keys(w2.txps).length.should.equal(1);
|
2014-04-09 13:28:35 -07:00
|
|
|
});
|
2014-04-21 16:28:57 -07:00
|
|
|
|
|
|
|
|
2014-04-09 13:28:35 -07:00
|
|
|
});
|