Create indexes for all copayers

This commit is contained in:
Yemel Jardi 2014-07-01 12:49:50 -03:00
parent e9f20b5de6
commit b02cb17989
4 changed files with 52 additions and 2 deletions

View File

@ -6,9 +6,22 @@ var Structure = require('./Structure');
function AddressIndex(opts) {
opts = opts || {};
this.cosigner = opts.cosigner || Structure.SHARED_INDEX;
this.cosigner = opts.cosigner
this.changeIndex = opts.changeIndex || 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) {

View File

@ -24,7 +24,8 @@ function PublicKeyRing(opts) {
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.nicknameFor = opts.nicknameFor || {};

View File

@ -12,6 +12,7 @@ try {
}
var PublicKeyRing = copay.PublicKeyRing;
var AddressIndex = copay.AddressIndex;
var Structure = copay.Structure;
var config = {
@ -34,6 +35,18 @@ describe('AddressIndex model', function() {
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() {
var i = createAI();
var changeN = 2;

View File

@ -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() {
var k = createW();
var w = k.w;