bitcore-wallet-service/lib/model/addressmanager.js

40 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-02-03 10:44:44 -08:00
var _ = require('lodash');
var HDPath = require('./hdpath');
function AddressManager(opts) {
this.receiveAddressIndex = 0;
this.changeAddressIndex = 0;
this.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : HDPath.SHARED_INDEX;
};
AddressManager.fromObj = function (obj) {
var x = new AddressManager();
x.receiveAddressIndex = obj.receiveAddressIndex;
x.changeAddressIndex = obj.changeAddressIndex;
x.copayerIndex = obj.copayerIndex;
return x;
};
AddressManager.prototype._incrementIndex = function (isChange) {
if (isChange) {
this.changeAddressIndex++;
} else {
this.receiveAddressIndex++;
}
};
AddressManager.prototype.getCurrentAddressPath = function (isChange) {
return HDPath.Branch(isChange ? this.changeAddressIndex : this.receiveAddressIndex, isChange, this.copayerIndex);
};
AddressManager.prototype.getNewAddressPath = function (isChange) {
2015-02-03 11:46:28 -08:00
var ret = this.getCurrentAddressPath(isChange);
2015-02-03 10:44:44 -08:00
this._incrementIndex(isChange);
2015-02-03 11:46:28 -08:00
return ret;
2015-02-03 10:44:44 -08:00
};
module.exports = AddressManager;