copay/test/test.HDParams.js

109 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-06-04 09:44:32 -07:00
'use strict';
var chai = chai || require('chai');
var should = chai.should();
var bitcore = bitcore || require('bitcore');
var Address = bitcore.Address;
var buffertools = bitcore.buffertools;
try {
var copay = require('copay'); //browser
} catch (e) {
var copay = require('../copay'); //node
}
var PublicKeyRing = copay.PublicKeyRing;
2014-07-29 07:23:58 -07:00
var HDParams = copay.HDParams;
var HDPath = copay.HDPath;
2014-06-04 09:44:32 -07:00
2014-07-29 07:23:58 -07:00
describe('HDParams model', function() {
2014-06-04 09:44:32 -07:00
it('should create an instance (livenet)', function() {
2014-07-29 07:23:58 -07:00
var i = new HDParams();
2014-06-04 09:44:32 -07:00
should.exist(i);
});
2014-07-01 08:49:50 -07:00
it('should init indexes', function() {
2014-07-29 07:23:58 -07:00
var is = HDParams.init(2);
2014-07-01 08:49:50 -07:00
should.exist(is);
is.length.should.equal(3);
2014-07-29 07:23:58 -07:00
var cosigners = is.map(function(i) { return i.copayerIndex; });
cosigners.indexOf(HDPath.SHARED_INDEX).should.not.equal(-1);
2014-07-01 08:49:50 -07:00
cosigners.indexOf(0).should.not.equal(-1);
cosigners.indexOf(1).should.not.equal(-1);
cosigners.indexOf(2).should.equal(-1);
});
2014-07-04 05:45:02 -07:00
it('should serialize to object list and back', function() {
2014-07-29 07:23:58 -07:00
var is = HDParams.init(3);
2014-07-04 05:45:02 -07:00
should.exist(is);
is.length.should.equal(4);
2014-07-29 07:23:58 -07:00
var list = HDParams.serialize(is);
2014-07-04 05:45:02 -07:00
list.length.should.equal(4);
2014-07-29 07:23:58 -07:00
var is2 = HDParams.fromList(list);
2014-07-04 05:45:02 -07:00
is2.length.should.equal(4);
});
2014-06-27 11:06:39 -07:00
it('show be able to store and read', function() {
var i = new HDParams();
i.copayerIndex = 1;
2014-06-04 09:44:32 -07:00
var changeN = 2;
var addressN = 2;
for (var j = 0; j < changeN; j++) {
2014-06-04 09:44:32 -07:00
i.increment(true);
}
for (var j = 0; j < addressN; j++) {
2014-06-04 09:44:32 -07:00
i.increment(false);
}
var data = i.toObj();
should.exist(data);
2014-07-29 07:23:58 -07:00
var i2 = HDParams.fromObj(data);
i2.copayerIndex.should.equal(i.copayerIndex);
2014-06-04 09:44:32 -07:00
i2.getChangeIndex().should.equal(changeN);
i2.getReceiveIndex().should.equal(addressN);
2014-06-04 09:44:32 -07:00
});
it('should count generation indexes', function() {
var j = new HDParams();
j.copayerIndex = 1;
for (var i = 0; i < 3; i++)
2014-07-29 07:23:58 -07:00
j.increment(true);
for (var i = 0; i < 2; i++)
2014-07-29 07:23:58 -07:00
j.increment(false);
2014-06-04 09:44:32 -07:00
j.changeIndex.should.equal(3);
j.receiveIndex.should.equal(2);
2014-06-04 09:44:32 -07:00
});
it('#merge tests', function() {
var j = new HDParams();
j.copayerIndex = 1;
2014-06-04 09:44:32 -07:00
for (var i = 0; i < 15; i++)
2014-07-29 07:23:58 -07:00
j.increment(true);
for (var i = 0; i < 7; i++)
2014-07-29 07:23:58 -07:00
j.increment(false);
var j2 = new HDParams({
copayerIndex: j.copayerIndex,
2014-06-04 09:44:32 -07:00
});
j2.merge(j).should.equal(true);
j2.changeIndex.should.equal(15);
j2.receiveIndex.should.equal(7);
2014-06-04 09:44:32 -07:00
j2.merge(j).should.equal(false);
});
2014-07-29 07:23:58 -07:00
it('#merge should fail with different copayerIndex index', function() {
var j1 = new HDParams({ walletId: '1234', copayerIndex: 2 });
var j2 = new HDParams({ walletId: '1234', copayerIndex: 3 });
2014-06-27 11:06:39 -07:00
var merge = function() { j2.merge(j1); };
merge.should.throw(Error);
})
});