Wallet/test/test.Wallet.js

173 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-04-10 17:57:41 -03:00
'use strict';
2014-04-14 18:30:08 -03:00
var chai = chai || require('chai');
var should = chai.should();
var copay = copay || require('../copay');
2014-04-16 21:10:04 -03:00
var Wallet = require('../js/models/core/Wallet');
var Storage= require('./mocks/FakeStorage');
var Network= copay.WebRTC;
var Blockchain= copay.Insight;
2014-04-15 15:50:22 -03:00
var addCopayers = function (w) {
for(var i=0; i<4; i++) {
w.publicKeyRing.addCopayer();
}
};
2014-04-10 17:57:41 -03:00
describe('Wallet model', function() {
2014-04-16 21:10:04 -03:00
2014-04-14 18:30:08 -03:00
var config = {
2014-04-16 21:10:04 -03:00
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: 1,
2014-04-15 18:23:35 -03:00
blockchain: {
host: 'test.insight.is',
port: 80
},
2014-04-16 21:10:04 -03:00
networkName: 'testnet',
2014-04-14 18:30:08 -03:00
};
2014-04-10 17:57:41 -03:00
2014-04-16 21:10:04 -03:00
it('should fail to create an instance', function () {
(function(){new Wallet(config)}).should.throw();
2014-04-10 17:57:41 -03:00
});
2014-04-14 18:30:08 -03:00
2014-04-16 21:10:04 -03:00
var createW = function () {
var c = JSON.parse(JSON.stringify(config));
2014-04-14 18:30:08 -03:00
2014-04-16 21:10:04 -03:00
c.privateKey = new copay.PrivateKey({ networkName: c.networkName });
2014-04-15 15:50:22 -03:00
2014-04-16 21:10:04 -03:00
c.publicKeyRing = new copay.PublicKeyRing({
networkName: c.networkName,
requiredCopayers: c.requiredCopayers,
totalCopayers: c.totalCopayers,
});
2014-04-17 17:01:31 -03:00
c.publicKeyRing.addCopayer(c.privateKey.getExtendedPublicKeyString());
2014-04-15 15:50:22 -03:00
2014-04-16 21:10:04 -03: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 15:50:22 -03:00
should.exist(w.id);
should.exist(w.publicKeyRing);
should.exist(w.privateKey);
should.exist(w.txProposals);
});
2014-04-16 21:10:04 -03:00
it('should provide some basic features', function () {
2014-04-15 15:50:22 -03:00
var opts = {};
2014-04-16 21:10:04 -03:00
var w = createW();
2014-04-15 15:50:22 -03:00
addCopayers(w);
w.publicKeyRing.generateAddress(false);
w.publicKeyRing.isComplete().should.equal(true);
2014-04-14 18:30:08 -03:00
});
2014-04-15 18:23:35 -03:00
var unspentTest = [
{
"address": "dummy",
"scriptPubKey": "dummy",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
"vout": 1,
"amount": 10,
"confirmations":7
}
];
2014-04-17 17:01:31 -03:00
var createW2 = function (privateKeys) {
2014-04-16 21:10:04 -03:00
var w = createW();
2014-04-15 18:23:35 -03:00
should.exist(w);
var pkr = w.publicKeyRing;
for(var i=0; i<4; i++) {
2014-04-17 17:01:31 -03:00
if (privateKeys) {
var k=privateKeys[i];
pkr.addCopayer(k?k.getExtendedPublicKeyString():null);
2014-04-15 18:23:35 -03: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 21:10:04 -03:00
var w = createW2();
2014-04-15 18:23:35 -03:00
unspentTest[0].address = w.publicKeyRing.getAddress(1, true).toString();
unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(1, true);
2014-04-16 21:10:04 -03:00
w.createTxSync(
2014-04-15 18:23:35 -03:00
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
unspentTest
);
var t = w.txProposals;
2014-04-18 19:28:28 -03:00
var k = Object.keys(t.txps)[0];
var tx = t.txps[k].builder.build();
2014-04-15 18:23:35 -03:00
should.exist(tx);
tx.isComplete().should.equal(false);
2014-04-18 19:28:28 -03:00
Object.keys(t.txps[k].signedBy).length.should.equal(1);
Object.keys(t.txps[k].seenBy).length.should.equal(1);
2014-04-15 18:23:35 -03:00
});
2014-04-18 11:19:39 -03: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 18:23:35 -03:00
it('#create. Signing with derivate keys', function () {
2014-04-16 21:10:04 -03:00
var w = createW2();
2014-04-15 18:23:35 -03: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 21:10:04 -03:00
w.createTxSync(
2014-04-15 18:23:35 -03:00
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
unspentTest
);
var t = w.txProposals;
2014-04-18 19:28:28 -03:00
var k = Object.keys(t.txps)[0];
var tx = t.txps[k].builder.build();
2014-04-15 18:23:35 -03:00
should.exist(tx);
tx.isComplete().should.equal(false);
tx.countInputMissingSignatures(0).should.equal(2);
2014-04-19 11:30:55 -03: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 18:23:35 -03:00
}
}
});
2014-04-10 17:57:41 -03:00
});