copay/test/test.txproposal.js

157 lines
4.6 KiB
JavaScript
Raw Normal View History

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 BIP32 = bitcore.BIP32;
var bignum = bitcore.bignum;
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');
var PublicKeyRing = (typeof process.versions === 'undefined') ? copay.PublicKeyRing :
require('soop').load('../js/models/PublicKeyRing', {Storage: fakeStorage});
var config = {
networkName:'livenet',
};
var unspentTest = [
{
"address": "dummy",
"scriptPubKey": "dummy",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
"vout": 1,
"amount": 10,
"confirmations":7
}
];
var createW = function (bip32s) {
var w = new PublicKeyRing(config);
should.exist(w);
for(var i=0; i<5; i++) {
if (bip32s) {
var b=bip32s[i];
2014-04-09 19:07:21 -07:00
w.addCopayer(b?b.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;
};
describe('TxProposals model', function() {
it('should create an instance', function () {
var w = new TxProposals({
networkName: config.networkName
});
should.exist(w);
w.network.name.should.equal('livenet');
});
it('#create', function () {
var w = new TxProposals({
networkName: config.networkName,
publicKeyRing: createW(),
});
should.exist(w);
w.network.name.should.equal('livenet');
unspentTest[0].address = w.publicKeyRing.getAddress(1, true);
unspentTest[0].scriptPubKey = w.publicKeyRing.getRedeemScript(1, true).getBuffer();
2014-04-09 16:37:14 -07:00
var tx = w.create(
2014-04-09 13:28:35 -07:00
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
bignum('123456789'),
unspentTest
);
2014-04-09 16:37:14 -07:00
should.exist(tx);
tx.isComplete().should.equal(false);
2014-04-09 13:28:35 -07:00
});
2014-04-09 19:04:22 -07:00
it('#create. Signing with derivate keys', function () {
2014-04-09 13:28:35 -07:00
2014-04-09 16:37:14 -07:00
var priv = new PrivateKey(config);
2014-04-09 13:28:35 -07:00
var w = new TxProposals({
networkName: config.networkName,
2014-04-09 16:37:14 -07:00
publicKeyRing: createW([priv.getBIP32()]),
2014-04-09 13:28:35 -07:00
});
2014-04-09 22:16:57 -07:00
var ts = Date.now();
2014-04-09 13:28:35 -07:00
for (var isChange=0; isChange<2; isChange++) {
for (var index=0; index<3; index++) {
unspentTest[0].address = w.publicKeyRing.getAddress(index, isChange);
unspentTest[0].scriptPubKey = w.publicKeyRing.getRedeemScript(index, isChange).getBuffer();
2014-04-09 16:37:14 -07:00
var tx = w.create(
2014-04-09 13:28:35 -07:00
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
bignum('123456789'),
unspentTest,
2014-04-09 22:16:57 -07:00
priv
2014-04-09 19:04:22 -07:00
);
should.exist(tx);
tx.isComplete().should.equal(false);
tx.countInputMissingSignatures(0).should.equal(2);
2014-04-09 22:16:57 -07:00
(w.txs[0].signedBy[priv.id] - ts > 0).should.equal(true);
(w.txs[0].seenBy[priv.id] - ts > 0).should.equal(true);
2014-04-09 19:04:22 -07:00
}
}
});
2014-04-09 22:16:57 -07:00
it('#toObj #fromObj roundtrip', function () {
2014-04-09 19:04:22 -07:00
var priv = new PrivateKey(config);
var w = new TxProposals({
networkName: config.networkName,
publicKeyRing: createW([priv.getBIP32()]),
});
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-09 22:16:57 -07:00
unspentTest[0].address = w.publicKeyRing.getAddress(index, isChange);
unspentTest[0].scriptPubKey = w.publicKeyRing.getRedeemScript(index, isChange).getBuffer();
var tx = w.create(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
bignum('123456789'),
unspentTest,
priv
);
tx.isComplete().should.equal(false);
tx.countInputMissingSignatures(0).should.equal(2);
(w.txs[0].signedBy[priv.id] - ts > 0).should.equal(true);
(w.txs[0].seenBy[priv.id] - ts > 0).should.equal(true);
var o = w.toObj();
should.exist(o);
o.txs.length.should.equal(1);
should.exist(o.txs[0].txHex);
should.exist(o.txs[0].signedBy);
should.exist(o.txs[0].seenBy);
should.exist(o.txs[0].signedBy[priv.id]);
var w2 = TxProposals.fromObj(o);
var tx2 = w2.txs[0].tx;
tx2.isComplete().should.equal(false);
tx2.countInputMissingSignatures(0).should.equal(2);
(w2.txs[0].signedBy[priv.id] - ts > 0).should.equal(true);
(w2.txs[0].seenBy[priv.id] - ts > 0).should.equal(true);
2014-04-09 13:28:35 -07:00
});
});