copay/test/test.publickeyring.js

288 lines
6.9 KiB
JavaScript
Raw Normal View History

2014-04-04 11:57:28 -07:00
'use strict';
var chai = chai || require('chai');
var should = chai.should();
2014-04-07 11:31:41 -07:00
var bitcore = bitcore || require('bitcore');
2014-04-04 11:57:28 -07:00
var Address = bitcore.Address;
var buffertools = bitcore.buffertools;
2014-04-07 11:31:41 -07:00
var copay = copay || require('../copay');
2014-04-09 19:04:22 -07:00
var PublicKeyRing = copay.PublicKeyRing;
2014-04-04 11:57:28 -07:00
var aMasterPubKey = 'tprv8ZgxMBicQKsPdSVTiWXEqCCzqRaRr9EAQdn5UVMpT9UHX67Dh1FmzEMbavPumpAicsUm2XvC6NTdcWB89yN5DUWx5HQ7z3KByUg7Ht74VRZ';
var config = {
2014-04-07 20:54:38 -07:00
networkName:'livenet',
2014-04-04 11:57:28 -07:00
};
2014-04-07 20:54:38 -07:00
var createW = function (networkName) {
2014-04-04 11:57:28 -07:00
var config = {
2014-04-07 20:54:38 -07:00
networkName: networkName || 'livenet',
2014-04-04 11:57:28 -07:00
};
var w = new PublicKeyRing(config);
should.exist(w);
var copayers = [];
for(var i=0; i<5; i++) {
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(false);
var newEpk = w.addCopayer();
copayers.push(newEpk);
2014-04-04 11:57:28 -07:00
}
return {w:w, copayers: copayers};
};
describe('PublicKeyRing model', function() {
it('should create an instance (livenet)', function () {
var w = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: config.networkName
2014-04-04 11:57:28 -07:00
});
should.exist(w);
w.network.name.should.equal('livenet');
});
it('should create an instance (testnet)', function () {
var w2 = new PublicKeyRing();
should.exist(w2);
w2.network.name.should.equal('testnet');
});
it('should fail to generate shared pub keys wo extended key', function () {
var w2 = new PublicKeyRing(config);
should.exist(w2);
w2.registeredCopayers().should.equal(0);
2014-04-09 10:30:12 -07:00
w2.isComplete().should.equal(false);
2014-04-04 11:57:28 -07:00
w2.getAddress.bind(false).should.throw();
});
it('should add and check when adding shared pub keys', function () {
var k = createW();
var w = k.w;
var copayers = k.copayers;
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(true);
w.addCopayer.bind().should.throw();
for(var i =0; i<5; i++)
w.addCopayer.bind(copayers[i]).should.throw();
2014-04-04 11:57:28 -07:00
});
2014-04-09 19:04:22 -07:00
it('show be able to tostore and read', function () {
2014-04-04 11:57:28 -07:00
var k = createW();
var w = k.w;
var copayers = k.copayers;
2014-04-04 16:16:15 -07:00
for(var i=0; i<3; i++)
w.generateAddress(true);
for(var i=0; i<5; i++)
w.generateAddress(false);
2014-04-04 11:57:28 -07:00
2014-04-09 19:04:22 -07:00
var data = w.toStore();
should.exist(data);
2014-04-04 11:57:28 -07:00
var ID = w.id;
2014-04-09 19:04:22 -07:00
var w2 = PublicKeyRing.read(data, ID, 'dummy' );
2014-04-09 10:30:12 -07:00
w2.isComplete().should.equal(true);
w2.addCopayer.bind().should.throw();
for(var i =0; i<5; i++)
w2.addCopayer.bind(copayers[i]).should.throw();
2014-04-04 16:16:15 -07:00
w2.changeAddressIndex.should.equal(3);
w2.addressIndex.should.equal(5);
2014-04-04 11:57:28 -07:00
});
it('should generate some p2sh addresses', function () {
var k = createW();
var w = k.w;
for(var isChange=0; isChange<2; isChange++) {
for(var i=0; i<5; i++) {
2014-04-10 21:09:42 -07:00
var a = w.generateAddress(isChange);
2014-04-04 11:57:28 -07:00
a.isValid().should.equal(true);
a.isScript().should.equal(true);
a.network().name.should.equal('livenet');
2014-04-04 15:59:00 -07:00
if (i>1) {
2014-04-10 21:09:42 -07:00
w.getAddress(i-1,isChange).toString().should
.not.equal(w.getAddress(i-2,isChange).toString());
2014-04-04 15:59:00 -07:00
}
2014-04-04 11:57:28 -07:00
}
}
});
it('should return PublicKeyRing addresses', function () {
var k = createW();
var w = k.w;
var a = w.getAddresses();
a.length.should.equal(0);
for(var isChange=0; isChange<2; isChange++)
for(var i=0; i<6; i++)
w.generateAddress(isChange);
var as = w.getAddresses();
as.length.should.equal(12);
2014-04-04 16:16:15 -07:00
for(var j in as) {
2014-04-10 21:09:42 -07:00
var a = as[j];
2014-04-04 11:57:28 -07:00
a.isValid().should.equal(true);
}
});
2014-04-04 16:16:15 -07:00
it('should count generation indexes', function () {
var k = createW();
var w = k.w;
for(var i=0; i<3; i++)
w.generateAddress(true);
for(var i=0; i<5; i++)
w.generateAddress(false);
w.changeAddressIndex.should.equal(3);
w.addressIndex.should.equal(5);
});
2014-04-07 07:46:45 -07:00
it('#merge index tests', function () {
var k = createW();
var w = k.w;
for(var i=0; i<2; i++)
w.generateAddress(true);
for(var i=0; i<3; i++)
w.generateAddress(false);
var w2 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'livenet',
2014-04-07 07:46:45 -07:00
id: w.id,
});
w2.merge(w.toObj()).should.equal(true);
w2.requiredCopayers.should.equal(3);
w2.totalCopayers.should.equal(5);
w2.changeAddressIndex.should.equal(2);
w2.addressIndex.should.equal(3);
//
w2.merge(w.toObj()).should.equal(false);
});
it('#merge check tests', function () {
var k = createW();
var w = k.w;
for(var i=0; i<2; i++)
w.generateAddress(true);
for(var i=0; i<3; i++)
w.generateAddress(false);
2014-04-07 20:54:38 -07:00
var w2 = new PublicKeyRing({
networkName: 'livenet',
});
(function() { w2.merge(w.toObj());}).should.throw();
(function() { w2.merge(w,true);}).should.throw();
w2.merge(w.toObj(),true).should.equal(true);
2014-04-07 07:46:45 -07:00
var w3 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'livenet',
2014-04-07 07:46:45 -07:00
id: w.id,
requiredCopayers: 2,
});
(function() { w3.merge(w.toObj());}).should.throw();
var w4 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'testnet',
2014-04-07 07:46:45 -07:00
id: w.id,
});
(function() { w4.merge(w.toObj());}).should.throw();
var w5 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'livenet',
2014-04-07 07:46:45 -07:00
id: w.id,
totalCopayers: 4,
});
(function() { w5.merge(w.toObj());}).should.throw();
var w6 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'livenet',
2014-04-07 07:46:45 -07:00
id: w.id,
});
(function() { w6.merge(w);}).should.throw();
w.networkName= 'livenet';
(function() { w6.merge(w);}).should.throw();
2014-04-07 20:54:38 -07:00
2014-04-07 07:46:45 -07:00
});
it('#merge pubkey tests', function () {
var w = new PublicKeyRing(config);
should.exist(w);
var copayers = [];
for(var i=0; i<2; i++) {
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(false);
2014-04-07 07:46:45 -07:00
w.addCopayer();
}
var w2 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'livenet',
2014-04-07 07:46:45 -07:00
id: w.id,
});
should.exist(w);
var copayers = [];
for(var i=0; i<3; i++) {
2014-04-09 10:30:12 -07:00
w2.isComplete().should.equal(false);
2014-04-07 07:46:45 -07:00
w2.addCopayer();
}
w2.merge(w.toObj()).should.equal(true);
2014-04-09 10:30:12 -07:00
w2.isComplete().should.equal(true);
2014-04-07 07:46:45 -07:00
w2.merge(w.toObj()).should.equal(false);
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(false);
2014-04-07 07:46:45 -07:00
w.merge(w2.toObj()).should.equal(true);
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(true);
2014-04-07 07:46:45 -07:00
w.merge(w2.toObj()).should.equal(false);
});
it('#merge pubkey tests (case 2)', function () {
var w = new PublicKeyRing(config);
should.exist(w);
for(var i=0; i<5; i++) {
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(false);
2014-04-07 07:46:45 -07:00
var w2 = new PublicKeyRing({
2014-04-07 20:54:38 -07:00
networkName: 'livenet',
2014-04-07 07:46:45 -07:00
id: w.id,
});
w2.addCopayer();
2014-04-07 09:26:29 -07:00
w.merge(w2.toObj()).should.equal(true);
2014-04-07 07:46:45 -07:00
}
2014-04-09 10:30:12 -07:00
w.isComplete().should.equal(true);
2014-04-07 07:46:45 -07:00
});
2014-04-09 13:28:35 -07:00
it('#getRedeemScriptMap check tests', function () {
var k = createW();
var w = k.w;
for(var i=0; i<2; i++)
w.generateAddress(true);
for(var i=0; i<3; i++)
w.generateAddress(false);
var m = w.getRedeemScriptMap();
Object.keys(m).length.should.equal(5);
Object.keys(m).forEach(function (k) {
should.exist(m[k]);
});
});
2014-04-04 11:57:28 -07:00
});