diff --git a/lib/model/addressmanager.js b/lib/model/addressmanager.js index b6d739a..1e47cd2 100644 --- a/lib/model/addressmanager.js +++ b/lib/model/addressmanager.js @@ -3,7 +3,7 @@ var $ = require('preconditions').singleton(); var WalletUtils = require('bitcore-wallet-utils'); -var SHARED_INDEX = 0x80000000 - 1; +var BIP45_SHARED_INDEX = 0x80000000 - 1; function AddressManager() {}; @@ -18,7 +18,7 @@ AddressManager.create = function(opts) { x.receiveAddressIndex = 0; x.changeAddressIndex = 0; - x.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : SHARED_INDEX; + x.copayerIndex = _.isNumber(opts.copayerIndex) ? opts.copayerIndex : BIP45_SHARED_INDEX; return x; }; @@ -35,9 +35,8 @@ AddressManager.fromObj = function(obj) { return x; }; -AddressManager.prototype.supportsDerivation = function() { - // BIP44 does not support copayer specific indexes - return !(this.derivationStrategy == WalletUtils.DERIVATION_STRATEGIES.BIP44 && this.copayerIndex != SHARED_INDEX); +AddressManager.supportsCopayerBranches = function(derivationStrategy) { + return derivationStrategy == WalletUtils.DERIVATION_STRATEGIES.BIP45; }; AddressManager.prototype._incrementIndex = function(isChange) { diff --git a/lib/model/copayer.js b/lib/model/copayer.js index 3fffe72..d7cfd3b 100644 --- a/lib/model/copayer.js +++ b/lib/model/copayer.js @@ -36,10 +36,13 @@ Copayer.create = function(opts) { signature: opts.signature, }]; - x.addressManager = AddressManager.create({ - derivationStrategy: opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45, - copayerIndex: opts.copayerIndex, - }); + var derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; + if (AddressManager.supportsCopayerBranches(derivationStrategy)) { + x.addressManager = AddressManager.create({ + derivationStrategy: derivationStrategy, + copayerIndex: opts.copayerIndex, + }); + } x.customData = opts.customData; @@ -67,7 +70,9 @@ Copayer.fromObj = function(obj) { x.requestPubKeys = obj.requestPubKeys; } - x.addressManager = AddressManager.fromObj(obj.addressManager); + if (obj.addressManager) { + x.addressManager = AddressManager.fromObj(obj.addressManager); + } x.customData = obj.customData; return x; diff --git a/lib/server.js b/lib/server.js index 9f75a2d..3ffb5ba 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1842,7 +1842,7 @@ WalletService.prototype.scan = function(opts, cb) { }); if (opts.includeCopayerBranches) { _.each(wallet.copayers, function(copayer) { - if (copayer.addressManager.supportsDerivation()) { + if (copayer.addressManager) { derivators.push({ derive: _.bind(copayer.createAddress, copayer, wallet, isChange), rewind: _.bind(copayer.addressManager.rewindIndex, copayer.addressManager, isChange), diff --git a/test/integration/server.js b/test/integration/server.js index a48baac..0742d64 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1222,7 +1222,9 @@ describe('Wallet service', function() { describe('#getStatus', function() { var server, wallet; beforeEach(function(done) { - helpers.createAndJoinWallet(1, 1, function(s, w) { + helpers.createAndJoinWallet(1, 1, { + derivationStrategy: 'BIP45' + }, function(s, w) { server = s; wallet = w; done(); diff --git a/test/models/addressmanager.js b/test/models/addressmanager.js index 3e34733..975856e 100644 --- a/test/models/addressmanager.js +++ b/test/models/addressmanager.js @@ -27,6 +27,12 @@ describe('AddressManager', function() { am.getCurrentAddressPath(false).should.equal('m/4/0/2'); }); }); + describe('#supportsCopayerBranches', function() { + it('should return true for BIP45 & false for BIP44', function() { + AddressManager.supportsCopayerBranches('BIP45').should.be.true; + AddressManager.supportsCopayerBranches('BIP44').should.be.false; + }); + }); describe('BIP45', function() { describe('#getCurrentAddressPath', function() { it('should return a valid BIP32 path for given index', function() { @@ -34,7 +40,6 @@ describe('AddressManager', function() { derivationStrategy: 'BIP45', copayerIndex: 4, }); - am.supportsDerivation().should.be.true; am.getCurrentAddressPath(false).should.equal('m/4/0/0'); am.getCurrentAddressPath(true).should.equal('m/4/1/0'); }); @@ -43,7 +48,6 @@ describe('AddressManager', function() { var am = AddressManager.create({ derivationStrategy: 'BIP45', }); - am.supportsDerivation().should.be.true; am.getCurrentAddressPath(false).should.equal('m/2147483647/0/0'); am.getCurrentAddressPath(true).should.equal('m/2147483647/1/0'); }); @@ -94,7 +98,6 @@ describe('AddressManager', function() { describe('#getCurrentAddressPath', function() { it('should return first address path', function() { var am = AddressManager.create(); - am.supportsDerivation().should.be.true; am.getCurrentAddressPath(false).should.equal('m/0/0'); am.getCurrentAddressPath(true).should.equal('m/1/0'); }); @@ -102,7 +105,6 @@ describe('AddressManager', function() { var am = AddressManager.create({ copayerIndex: 4, }); - am.supportsDerivation().should.be.false; am.getCurrentAddressPath(false).should.equal('m/0/0'); am.getCurrentAddressPath(true).should.equal('m/1/0'); });