copay/test/test.Wallet.js

214 lines
6.0 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 copay = copay || require('../copay');
2014-04-16 17:10:04 -07:00
var Wallet = require('../js/models/core/Wallet');
var Storage= require('./mocks/FakeStorage');
2014-04-24 19:42:59 -07:00
var Network= require('./mocks/FakeNetwork');
2014-04-16 17:10:04 -07:00
var Blockchain= copay.Insight;
2014-04-15 11:50:22 -07:00
var addCopayers = function (w) {
for(var i=0; i<4; i++) {
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-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
2014-04-16 17:10:04 -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
2014-04-30 08:58:40 -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;
2014-04-16 17:10:04 -07:00
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-04-17 13:01:31 -07:00
c.publicKeyRing.addCopayer(c.privateKey.getExtendedPublicKeyString());
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);
c.networkName = config.networkName;
c.verbose = config.verbose;
return new Wallet(c);
}
it('should create an instance', function () {
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');
b.toString('hex').length.should.equal(16);
2014-04-15 11:50:22 -07:00
});
2014-04-16 17:10:04 -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
});
2014-04-15 14:23:35 -07:00
var unspentTest = [
{
"address": "dummy",
"scriptPubKey": "dummy",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
"vout": 1,
"amount": 10,
"confirmations":7
}
];
2014-04-17 13:01:31 -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;
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.getExtendedPublicKeyString():null);
2014-04-15 14:23:35 -07:00
}
else
pkr.addCopayer();
}
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-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-16 17:10:04 -07:00
w.createTxSync(
2014-04-15 14:23:35 -07:00
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
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);
2014-04-18 15:28:28 -07:00
Object.keys(t.txps[k].signedBy).length.should.equal(1);
Object.keys(t.txps[k].seenBy).length.should.equal(1);
2014-04-15 14:23:35 -07:00
});
2014-04-18 07:19:39 -07:00
it('#addressIsOwn', function () {
var w = createW2();
var l = w.getAddressesStr();
for (var i=0; i<l.length; i++)
w.addressIsOwn(l[i]).should.equal(true);
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-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(
2014-04-15 14:23:35 -07:00
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
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);
2014-04-19 07:30:55 -07:00
( 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);
});
2014-04-30 08:58:40 -07:00
it('#getSecret decodeSecret', function () {
var w = createW2();
var id = w.getMyCopayerId();
var nk = w.netKey;
var sb= w.getSecret();
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();
});
2014-04-10 13:57:41 -07:00
});