use constants form BWU

This commit is contained in:
Ivan Socolsky 2015-08-31 17:13:46 -03:00
parent 2048a20ef4
commit 094e19bc31
5 changed files with 18 additions and 19 deletions

View File

@ -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);
};

View File

@ -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,
});

View File

@ -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;
};

View File

@ -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;

View File

@ -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;