diff --git a/js/models/core/AddressIndex.js b/js/models/core/AddressIndex.js index bfc9b6464..484865b87 100644 --- a/js/models/core/AddressIndex.js +++ b/js/models/core/AddressIndex.js @@ -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) { diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index f9e608afb..627706128 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -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 || {}; diff --git a/test/test.AddressIndex.js b/test/test.AddressIndex.js index 767041529..3d87d9868 100644 --- a/test/test.AddressIndex.js +++ b/test/test.AddressIndex.js @@ -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; diff --git a/test/test.PublicKeyRing.js b/test/test.PublicKeyRing.js index c60aa8b01..0bb2523a5 100644 --- a/test/test.PublicKeyRing.js +++ b/test/test.PublicKeyRing.js @@ -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;