copay/test/test.Wallet.js

244 lines
6.5 KiB
JavaScript
Raw Normal View History

2014-04-10 13:57:41 -07:00
'use strict';
2014-04-14 14:30:08 -07:00
var chai = chai || require('chai');
var should = chai.should();
var sinon = require('sinon');
2014-04-14 14:30:08 -07:00
var copay = copay || require('../copay');
2014-04-16 17:10:04 -07:00
var Wallet = require('../js/models/core/Wallet');
var Storage = require('./mocks/FakeStorage');
var Network = require('./mocks/FakeNetwork');
var Blockchain = copay.Insight;
2014-04-15 11:50:22 -07:00
var addCopayers = function(w) {
for (var i = 0; i < 4; i++) {
2014-04-15 11:50:22 -07:00
w.publicKeyRing.addCopayer();
}
};
2014-04-10 13:57:41 -07:00
describe('Wallet model', function() {
2014-04-16 17:10:04 -07:00
2014-04-14 14:30:08 -07:00
var config = {
2014-04-16 17:10:04 -07:00
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: 1,
2014-06-03 08:40:28 -07:00
reconnectDelay: 100,
2014-04-15 14:23:35 -07:00
blockchain: {
host: 'test.insight.is',
port: 80
},
2014-04-16 17:10:04 -07:00
networkName: 'testnet',
2014-04-14 14:30:08 -07:00
};
2014-04-10 13:57:41 -07:00
it('should fail to create an instance', function() {
(function() {
new Wallet(config)
}).should.
throw ();
2014-04-10 13:57:41 -07:00
});
2014-04-14 14:30:08 -07:00
var createW = function(netKey) {
2014-04-16 17:10:04 -07:00
var c = JSON.parse(JSON.stringify(config));
2014-04-30 08:58:40 -07:00
if (netKey) c.netKey = netKey;
c.privateKey = new copay.PrivateKey({
networkName: c.networkName
});
2014-04-15 11:50:22 -07:00
2014-04-16 17:10:04 -07:00
c.publicKeyRing = new copay.PublicKeyRing({
networkName: c.networkName,
requiredCopayers: c.requiredCopayers,
totalCopayers: c.totalCopayers,
});
2014-05-29 13:18:55 -07:00
var copayerEPK = c.privateKey.deriveBIP45Branch().extendedPublicKeyString()
c.publicKeyRing.addCopayer(copayerEPK);
2014-04-15 11:50:22 -07:00
2014-04-16 17:10:04 -07:00
c.txProposals = new copay.TxProposals({
networkName: c.networkName,
});
c.storage = new Storage(config.storage);
c.network = new Network(config.network);
c.blockchain = new Blockchain(config.blockchain);
2014-04-16 17:10:04 -07:00
c.networkName = config.networkName;
c.verbose = config.verbose;
c.version = '0.0.1';
2014-04-16 17:10:04 -07:00
return new Wallet(c);
}
it('should create an instance', function() {
2014-04-16 17:10:04 -07:00
var w = createW();
should.exist(w);
w.publicKeyRing.walletId.should.equal(w.id);
w.txProposals.walletId.should.equal(w.id);
w.requiredCopayers.should.equal(3);
2014-04-15 11:50:22 -07:00
should.exist(w.id);
should.exist(w.publicKeyRing);
should.exist(w.privateKey);
should.exist(w.txProposals);
2014-04-30 08:58:40 -07:00
should.exist(w.netKey);
var b = new Buffer(w.netKey, 'base64');
2014-04-30 08:58:40 -07:00
b.toString('hex').length.should.equal(16);
2014-04-15 11:50:22 -07:00
});
it('should provide some basic features', function() {
2014-04-15 11:50:22 -07:00
var opts = {};
2014-04-16 17:10:04 -07:00
var w = createW();
2014-04-15 11:50:22 -07:00
addCopayers(w);
w.publicKeyRing.generateAddress(false);
w.publicKeyRing.isComplete().should.equal(true);
2014-04-14 14:30:08 -07:00
});
var unspentTest = [{
2014-04-15 14:23:35 -07:00
"address": "dummy",
"scriptPubKey": "dummy",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
"vout": 1,
"amount": 10,
"confirmations": 7
}];
2014-04-15 14:23:35 -07:00
var createW2 = function(privateKeys) {
2014-04-30 08:58:40 -07:00
var netKey = 'T0FbU2JLby0=';
var w = createW(netKey);
2014-04-15 14:23:35 -07:00
should.exist(w);
var pkr = w.publicKeyRing;
2014-04-15 14:23:35 -07:00
for (var i = 0; i < 4; i++) {
2014-04-17 13:01:31 -07:00
if (privateKeys) {
var k = privateKeys[i];
pkr.addCopayer(k ? k.deriveBIP45Branch().extendedPublicKeyString() : null);
2014-05-29 13:18:55 -07:00
} else {
2014-04-15 14:23:35 -07:00
pkr.addCopayer();
2014-05-29 13:18:55 -07:00
}
2014-04-15 14:23:35 -07:00
}
pkr.generateAddress(true);
pkr.generateAddress(true);
pkr.generateAddress(true);
pkr.generateAddress(false);
pkr.generateAddress(false);
pkr.generateAddress(false);
//3x3 indexes
return w;
};
it('#create, 1 sign', function() {
2014-04-15 14:23:35 -07:00
2014-04-16 17:10:04 -07:00
var w = createW2();
2014-04-15 14:23:35 -07:00
unspentTest[0].address = w.publicKeyRing.getAddress(1, true).toString();
unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(1, true);
2014-04-15 14:23:35 -07:00
2014-05-29 13:18:55 -07:00
var ntxid = w.createTxSync(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
2014-04-15 14:23:35 -07:00
unspentTest
);
var t = w.txProposals;
2014-05-29 13:18:55 -07:00
var txp = t.txps[ntxid];
var tx = txp.builder.build();
2014-04-15 14:23:35 -07:00
should.exist(tx);
tx.isComplete().should.equal(false);
2014-05-29 13:18:55 -07:00
Object.keys(txp.seenBy).length.should.equal(1);
Object.keys(txp.signedBy).length.should.equal(1);
2014-04-15 14:23:35 -07:00
});
it('#addressIsOwn', function() {
2014-04-18 07:19:39 -07:00
var w = createW2();
var l = w.getAddressesStr();
for (var i = 0; i < l.length; i++)
2014-04-18 07:19:39 -07:00
w.addressIsOwn(l[i]).should.equal(true);
2014-05-01 07:04:21 -07:00
w.addressIsOwn(l[0], {
excludeMain: true
}).should.equal(false);
2014-05-01 07:04:21 -07:00
2014-04-18 07:19:39 -07:00
w.addressIsOwn('mmHqhvTVbxgJTnePa7cfweSRjBCy9bQQXJ').should.equal(false);
w.addressIsOwn('mgtUfP9sTJ6vPLoBxZLPEccGpcjNVryaCX').should.equal(false);
});
2014-04-15 14:23:35 -07:00
it('#create. Signing with derivate keys', function() {
2014-04-15 14:23:35 -07:00
2014-04-16 17:10:04 -07:00
var w = createW2();
2014-04-15 14:23:35 -07:00
var ts = Date.now();
for (var isChange = 0; isChange < 2; isChange++) {
for (var index = 0; index < 3; index++) {
unspentTest[0].address = w.publicKeyRing.getAddress(index, isChange).toString();
unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(index, isChange);
2014-04-16 17:10:04 -07:00
w.createTxSync(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
2014-04-15 14:23:35 -07:00
unspentTest
);
var t = w.txProposals;
2014-04-18 15:28:28 -07:00
var k = Object.keys(t.txps)[0];
var tx = t.txps[k].builder.build();
2014-04-15 14:23:35 -07:00
should.exist(tx);
tx.isComplete().should.equal(false);
tx.countInputMissingSignatures(0).should.equal(2);
(t.txps[k].signedBy[w.privateKey.getId()] - ts > 0).should.equal(true);
(t.txps[k].seenBy[w.privateKey.getId()] - ts > 0).should.equal(true);
2014-04-15 14:23:35 -07:00
}
}
});
it('#fromObj #toObj round trip', function() {
var w = createW2();
var o = w.toObj();
o = JSON.parse(JSON.stringify(o));
var w2 = Wallet.fromObj(o,
new Storage(config.storage),
new Network(config.network),
new Blockchain(config.blockchain));
should.exist(w2);
w2.publicKeyRing.requiredCopayers.should.equal(w.publicKeyRing.requiredCopayers);
should.exist(w2.publicKeyRing.getCopayerId);
should.exist(w2.txProposals.toObj);
should.exist(w2.privateKey.toObj);
});
it('#getSecret decodeSecret', function() {
2014-04-30 08:58:40 -07:00
var w = createW2();
var id = w.getMyCopayerId();
var nk = w.netKey;
var sb = w.getSecret();
2014-04-30 08:58:40 -07:00
should.exist(sb);
var s = Wallet.decodeSecret(sb);
s.pubKey.should.equal(id);
s.netKey.should.equal(nk);
});
it('decodeSecret check', function() {
(function() {
Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoKM');
}).should.not.
throw ();
(function() {
Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoK');
}).should.
throw ();
(function() {
Wallet.decodeSecret('12345');
}).should.
throw ();
});
it('call reconnect after interval', function(done) {
var w = createW2();
var spy = sinon.spy(w, 'scheduleConnect');
w.netStart();
setTimeout(function() {
sinon.assert.callCount(spy, 10);
done();
}, 1000);
2014-04-30 08:58:40 -07:00
});
2014-04-10 13:57:41 -07:00
});