diff --git a/lib/model/addressmanager.js b/lib/model/addressmanager.js index ca1740b..24f43c5 100644 --- a/lib/model/addressmanager.js +++ b/lib/model/addressmanager.js @@ -1,12 +1,9 @@ var _ = require('lodash'); var $ = require('preconditions').singleton(); -var STRATEGIES = { - BIP44: 'BIP44', - BIP45: 'BIP45', -}; +var WalletUtils = require('bitcore-wallet-utils'); -var SHARED_INDEX = 0x80000000 - 1; +var BIP45_SHARED_INDEX = 0x80000000 - 1; function AddressManager() {}; @@ -16,12 +13,12 @@ AddressManager.create = function(opts) { var x = new AddressManager(); x.version = 2; - x.derivationStrategy = opts.derivationStrategy || STRATEGIES.BIP44; - $.checkState(_.contains(_.values(STRATEGIES), x.derivationStrategy)); + x.derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP44; + $.checkState(_.contains(_.values(WalletUtils.DERIVATION_STRATEGIES), x.derivationStrategy)); x.receiveAddressIndex = 0; x.changeAddressIndex = 0; - x.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : SHARED_INDEX; + x.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : BIP45_SHARED_INDEX; return x; }; @@ -30,7 +27,7 @@ AddressManager.fromObj = function(obj) { var x = new AddressManager(); x.version = obj.version; - x.derivationStrategy = obj.derivationStrategy || STRATEGIES.BIP45; + x.derivationStrategy = obj.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; x.receiveAddressIndex = obj.receiveAddressIndex; x.changeAddressIndex = obj.changeAddressIndex; x.copayerIndex = obj.copayerIndex; @@ -40,7 +37,7 @@ AddressManager.fromObj = function(obj) { AddressManager.prototype.supportsDerivation = function() { // BIP44 does not support copayer specific indexes - return !(this.derivationStrategy == STRATEGIES.BIP44 && this.copayerIndex != SHARED_INDEX); + return !(this.derivationStrategy == WalletUtils.DERIVATION_STRATEGIES.BIP44 && this.copayerIndex != BIP45_SHARED_INDEX); }; AddressManager.prototype._incrementIndex = function(isChange) { @@ -62,7 +59,7 @@ AddressManager.prototype.rewindIndex = function(isChange, n) { AddressManager.prototype.getCurrentAddressPath = function(isChange) { return 'm/' + - (this.derivationStrategy == STRATEGIES.BIP45 ? this.copayerIndex + '/' : '') + + (this.derivationStrategy == WalletUtils.DERIVATION_STRATEGIES.BIP45 ? this.copayerIndex + '/' : '') + (isChange ? 1 : 0) + '/' + (isChange ? this.changeAddressIndex : this.receiveAddressIndex); }; diff --git a/lib/model/copayer.js b/lib/model/copayer.js index 73233f6..3fffe72 100644 --- a/lib/model/copayer.js +++ b/lib/model/copayer.js @@ -37,7 +37,7 @@ Copayer.create = function(opts) { }]; x.addressManager = AddressManager.create({ - derivationStrategy: opts.derivationStrategy || 'BIP45', + derivationStrategy: opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45, copayerIndex: opts.copayerIndex, }); diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index c574ae6..b8cd599 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -71,7 +71,7 @@ TxProposal.create = function(opts) { x.excludeUnconfirmedUtxos = opts.excludeUnconfirmedUtxos; x.proposalSignaturePubKey = opts.proposalSignaturePubKey; x.proposalSignaturePubKeySig = opts.proposalSignaturePubKeySig; - x.derivationStrategy = opts.derivationStrategy || 'BIP45'; + x.derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; if (_.isFunction(TxProposal._create[x.type])) { TxProposal._create[x.type](x, opts); @@ -118,7 +118,7 @@ TxProposal.fromObj = function(obj) { x.excludeUnconfirmedUtxos = obj.excludeUnconfirmedUtxos; x.proposalSignaturePubKey = obj.proposalSignaturePubKey; x.proposalSignaturePubKeySig = obj.proposalSignaturePubKeySig; - x.derivationStrategy = obj.derivationStrategy || 'BIP45'; + x.derivationStrategy = obj.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; return x; }; diff --git a/lib/model/wallet.js b/lib/model/wallet.js index 545185b..4639073 100644 --- a/lib/model/wallet.js +++ b/lib/model/wallet.js @@ -29,8 +29,9 @@ Wallet.create = function(opts) { x.copayers = []; x.pubKey = opts.pubKey; x.network = opts.network; + x.derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; x.addressManager = AddressManager.create({ - derivationStrategy: opts.derivationStrategy || 'BIP45', + derivationStrategy: x.derivationStrategy, }); x.scanStatus = null; @@ -53,6 +54,7 @@ Wallet.fromObj = function(obj) { }); x.pubKey = obj.pubKey; x.network = obj.network; + x.derivationStrategy = obj.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; x.addressManager = AddressManager.fromObj(obj.addressManager); x.scanStatus = obj.scanStatus; diff --git a/lib/server.js b/lib/server.js index 2e4bec1..9f75a2d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -220,8 +220,8 @@ WalletService.prototype.createWallet = function(opts, cb) { if (!_.contains(['livenet', 'testnet'], opts.network)) return cb(new ClientError('Invalid network')); - opts.derivationStrategy = opts.derivationStrategy || 'BIP45'; - if (!_.contains(['BIP44', 'BIP45'], opts.derivationStrategy)) + opts.derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; + if (!_.contains(_.values(WalletUtils.DERIVATION_STRATEGIES), opts.derivationStrategy)) return cb(new ClientError('Invalid address derivation strategy')); try { @@ -536,8 +536,8 @@ WalletService.prototype.joinWallet = function(opts, cb) { if (_.isEmpty(opts.name)) return cb(new ClientError('Invalid copayer name')); - opts.derivationStrategy = opts.derivationStrategy || 'BIP45'; - if (!_.contains(['BIP44', 'BIP45'], opts.derivationStrategy)) + opts.derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; + if (!_.contains(_.values(WalletUtils.DERIVATION_STRATEGIES), opts.derivationStrategy)) return cb(new ClientError('Invalid address derivation strategy')); self.walletId = opts.walletId;