Ivan Socolsky
10 years ago
8 changed files with 112 additions and 96 deletions
@ -0,0 +1,38 @@ |
|||||
|
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) { |
||||
|
this._incrementIndex(isChange); |
||||
|
return this.getCurrentAddressPath(isChange); |
||||
|
}; |
||||
|
|
||||
|
module.exports = AddressManager; |
@ -0,0 +1,36 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var _ = require('lodash'); |
||||
|
var chai = require('chai'); |
||||
|
var sinon = require('sinon'); |
||||
|
var should = chai.should(); |
||||
|
var AddressManager = require('../lib/model/addressmanager'); |
||||
|
|
||||
|
|
||||
|
describe('AddressManager', function() { |
||||
|
describe('#getCurrentAddressPath', function() { |
||||
|
it('should return a valid BIP32 path for given index', function() { |
||||
|
var am = new AddressManager({ |
||||
|
copayerIndex: 4 |
||||
|
}); |
||||
|
am.getCurrentAddressPath(false).should.equal('m/4/0/0'); |
||||
|
am.getCurrentAddressPath(true).should.equal('m/4/1/0'); |
||||
|
}); |
||||
|
}); |
||||
|
describe('#getCurrentAddressPath', function() { |
||||
|
it('should return a valid BIP32 path for defaut Index', function() { |
||||
|
var am = new AddressManager(); |
||||
|
am.getCurrentAddressPath(false).should.equal('m/2147483647/0/0'); |
||||
|
am.getCurrentAddressPath(true).should.equal('m/2147483647/1/0'); |
||||
|
}); |
||||
|
}); |
||||
|
describe('#getNewAddressPath', function() { |
||||
|
it('should return a new valid BIP32 path for given index', function() { |
||||
|
var am = new AddressManager({ |
||||
|
copayerIndex: 2 |
||||
|
}); |
||||
|
am.getNewAddressPath(false).should.equal('m/2/0/1'); |
||||
|
am.getNewAddressPath(true).should.equal('m/2/1/1'); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
@ -1,28 +0,0 @@ |
|||||
'use strict'; |
|
||||
|
|
||||
var _ = require('lodash'); |
|
||||
var chai = require('chai'); |
|
||||
var sinon = require('sinon'); |
|
||||
var should = chai.should(); |
|
||||
var Copayer = require('../lib/model/copayer'); |
|
||||
|
|
||||
|
|
||||
describe('Copayer', function() { |
|
||||
|
|
||||
describe('#getCurrentAddressPath', function() { |
|
||||
it('return a valid BIP32 path for defaut copayer Index', function() { |
|
||||
var c = new Copayer(); |
|
||||
c.getCurrentAddressPath(false).should.equal('m/0/0/0'); |
|
||||
c.getCurrentAddressPath(true).should.equal('m/0/1/0'); |
|
||||
}); |
|
||||
|
|
||||
it('return a valid BIP32 path for given index', function() { |
|
||||
var c = new Copayer({ |
|
||||
copayerIndex: 4 |
|
||||
}); |
|
||||
c.getCurrentAddressPath(false).should.equal('m/4/0/0'); |
|
||||
c.getCurrentAddressPath(true).should.equal('m/4/1/0'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
Loading…
Reference in new issue