remove AddressManager from copayers on BIP44

This commit is contained in:
Ivan Socolsky 2015-08-31 17:38:39 -03:00
parent d1e318a5b7
commit e11da7cec5
5 changed files with 24 additions and 16 deletions

View File

@ -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) {

View File

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

View File

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

View File

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

View File

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