mirror of https://github.com/BTCPrivate/copay.git
Create indexes for all copayers
This commit is contained in:
parent
e9f20b5de6
commit
b02cb17989
|
@ -6,9 +6,22 @@ var Structure = require('./Structure');
|
||||||
|
|
||||||
function AddressIndex(opts) {
|
function AddressIndex(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
this.cosigner = opts.cosigner || Structure.SHARED_INDEX;
|
this.cosigner = opts.cosigner
|
||||||
this.changeIndex = opts.changeIndex || 0;
|
this.changeIndex = opts.changeIndex || 0;
|
||||||
this.receiveIndex = opts.receiveIndex || 0;
|
this.receiveIndex = opts.receiveIndex || 0;
|
||||||
|
|
||||||
|
if (typeof this.cosigner === 'undefined') {
|
||||||
|
this.cosigner = Structure.SHARED_INDEX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AddressIndex.init = function(totalCopayers) {
|
||||||
|
preconditions.shouldBeNumber(totalCopayers);
|
||||||
|
var indexes = [new AddressIndex()];
|
||||||
|
for (var i = 0 ; i < totalCopayers ; i++) {
|
||||||
|
indexes.push(new AddressIndex({cosigner: i}));
|
||||||
|
}
|
||||||
|
return indexes;
|
||||||
}
|
}
|
||||||
|
|
||||||
AddressIndex.fromList = function(indexes) {
|
AddressIndex.fromList = function(indexes) {
|
||||||
|
|
|
@ -24,7 +24,8 @@ function PublicKeyRing(opts) {
|
||||||
|
|
||||||
this.copayersHK = opts.copayersHK || [];
|
this.copayersHK = opts.copayersHK || [];
|
||||||
|
|
||||||
this.indexes = opts.indexes ? AddressIndex.fromList(opts.indexes) : [new AddressIndex()];
|
this.indexes = opts.indexes ? AddressIndex.fromList(opts.indexes)
|
||||||
|
: AddressIndex.init(this.totalCopayers);
|
||||||
|
|
||||||
this.publicKeysCache = opts.publicKeysCache || {};
|
this.publicKeysCache = opts.publicKeysCache || {};
|
||||||
this.nicknameFor = opts.nicknameFor || {};
|
this.nicknameFor = opts.nicknameFor || {};
|
||||||
|
|
|
@ -12,6 +12,7 @@ try {
|
||||||
}
|
}
|
||||||
var PublicKeyRing = copay.PublicKeyRing;
|
var PublicKeyRing = copay.PublicKeyRing;
|
||||||
var AddressIndex = copay.AddressIndex;
|
var AddressIndex = copay.AddressIndex;
|
||||||
|
var Structure = copay.Structure;
|
||||||
|
|
||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
|
@ -34,6 +35,18 @@ describe('AddressIndex model', function() {
|
||||||
should.exist(i);
|
should.exist(i);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should init indexes', function() {
|
||||||
|
var is = AddressIndex.init(2);
|
||||||
|
should.exist(is);
|
||||||
|
is.length.should.equal(3);
|
||||||
|
|
||||||
|
var cosigners = is.map(function(i) { return i.cosigner; });
|
||||||
|
cosigners.indexOf(Structure.SHARED_INDEX).should.not.equal(-1);
|
||||||
|
cosigners.indexOf(0).should.not.equal(-1);
|
||||||
|
cosigners.indexOf(1).should.not.equal(-1);
|
||||||
|
cosigners.indexOf(2).should.equal(-1);
|
||||||
|
});
|
||||||
|
|
||||||
it('show be able to store and read', function() {
|
it('show be able to store and read', function() {
|
||||||
var i = createAI();
|
var i = createAI();
|
||||||
var changeN = 2;
|
var changeN = 2;
|
||||||
|
|
|
@ -383,6 +383,29 @@ describe('PublicKeyRing model', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('#getIndex should return the right one', function() {
|
||||||
|
var config = {
|
||||||
|
networkName: 'livenet',
|
||||||
|
};
|
||||||
|
var p = new PublicKeyRing(config);
|
||||||
|
var i = p.getIndex(Structure.SHARED_INDEX);
|
||||||
|
should.exist(i);
|
||||||
|
i.cosigner.should.equal(Structure.SHARED_INDEX);
|
||||||
|
var shared = p.getSharedIndex();
|
||||||
|
shared.should.equal(i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('#getIndex should throw error', function() {
|
||||||
|
var config = {
|
||||||
|
networkName: 'livenet',
|
||||||
|
};
|
||||||
|
var p = new PublicKeyRing(config);
|
||||||
|
|
||||||
|
(function badCosigner() {
|
||||||
|
return p.getIndex(54);
|
||||||
|
}).should.throw();
|
||||||
|
});
|
||||||
|
|
||||||
it('#getRedeemScriptMap check tests', function() {
|
it('#getRedeemScriptMap check tests', function() {
|
||||||
var k = createW();
|
var k = createW();
|
||||||
var w = k.w;
|
var w = k.w;
|
||||||
|
|
Loading…
Reference in New Issue