copay/test/test.Wallet.js

165 lines
4.2 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');
var Network= copay.WebRTC;
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-16 17:10:04 -07:00
var createW = function () {
var c = JSON.parse(JSON.stringify(config));
2014-04-14 14:30:08 -07:00
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-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-16 17:10:04 -07:00
var w = createW();
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;
var tx = t.txps[0].builder.build();
should.exist(tx);
tx.isComplete().should.equal(false);
Object.keys(t.txps[0].signedBy).length.should.equal(1);
Object.keys(t.txps[0].seenBy).length.should.equal(1);
});
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;
var tx = t.txps[0].builder.build();
should.exist(tx);
tx.isComplete().should.equal(false);
tx.countInputMissingSignatures(0).should.equal(2);
( t.txps[0].signedBy[w.privateKey.id] - ts > 0).should.equal(true);
( t.txps[0].seenBy[w.privateKey.id] - ts > 0).should.equal(true);
}
}
});
2014-04-10 13:57:41 -07:00
});