store accounts in closure instead of on object
This commit is contained in:
parent
d4f3398d91
commit
b2f010428f
2 changed files with 22 additions and 16 deletions
|
@ -19,6 +19,9 @@ var Wallet = function (seed, options) {
|
|||
// Stored in a closure to make accidental serialization less likely
|
||||
var masterkey = null;
|
||||
var me = this;
|
||||
var accountZero = null;
|
||||
var internalAccount = null;
|
||||
var externalAccount = null;
|
||||
|
||||
// Addresses
|
||||
this.addresses = [];
|
||||
|
@ -39,18 +42,18 @@ var Wallet = function (seed, options) {
|
|||
|
||||
// HD first-level child derivation method should be private
|
||||
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
|
||||
this.accountZero = masterkey.derivePrivate(0)
|
||||
this.externalAccount = this.accountZero.derive(0)
|
||||
this.internalAccount = this.accountZero.derive(1)
|
||||
accountZero = masterkey.derivePrivate(0)
|
||||
externalAccount = accountZero.derive(0)
|
||||
internalAccount = accountZero.derive(1)
|
||||
|
||||
this.generateAddress = function() {
|
||||
var key = this.externalAccount.derive(this.addresses.length)
|
||||
var key = externalAccount.derive(this.addresses.length)
|
||||
this.addresses.push(key.getBitcoinAddress().toString())
|
||||
return this.addresses[this.addresses.length - 1]
|
||||
}
|
||||
|
||||
this.generateChangeAddress = function() {
|
||||
var key = this.internalAccount.derive(this.changeAddresses.length)
|
||||
var key = internalAccount.derive(this.changeAddresses.length)
|
||||
this.changeAddresses.push(key.getBitcoinAddress().toString())
|
||||
return this.changeAddresses[this.changeAddresses.length - 1]
|
||||
}
|
||||
|
@ -174,13 +177,16 @@ var Wallet = function (seed, options) {
|
|||
}
|
||||
|
||||
this.getMasterKey = function() { return masterkey }
|
||||
this.getAccountZero = function() { return accountZero }
|
||||
this.getInternalAccount = function() { return internalAccount }
|
||||
this.getExternalAccount = function() { return externalAccount }
|
||||
|
||||
this.getPrivateKey = function(index) {
|
||||
return this.externalAccount.derive(index).priv
|
||||
return externalAccount.derive(index).priv
|
||||
}
|
||||
|
||||
this.getInternalPrivateKey = function(index) {
|
||||
return this.internalAccount.derive(index).priv
|
||||
return internalAccount.derive(index).priv
|
||||
}
|
||||
|
||||
this.getPrivateKeyForAddress = function(address) {
|
||||
|
|
|
@ -22,19 +22,19 @@ describe('Wallet', function() {
|
|||
})
|
||||
|
||||
it("generates m/0' as the main account", function() {
|
||||
var mainAccount = wallet.accountZero
|
||||
var mainAccount = wallet.getAccountZero()
|
||||
assert.equal(mainAccount.index, 0 + HDNode.HIGHEST_BIT)
|
||||
assert.equal(mainAccount.depth, 1)
|
||||
})
|
||||
|
||||
it("generates m/0'/0 as the external account", function() {
|
||||
var account = wallet.externalAccount
|
||||
var account = wallet.getExternalAccount()
|
||||
assert.equal(account.index, 0)
|
||||
assert.equal(account.depth, 2)
|
||||
})
|
||||
|
||||
it("generates m/0'/1 as the internal account", function() {
|
||||
var account = wallet.internalAccount
|
||||
var account = wallet.getInternalAccount()
|
||||
assert.equal(account.index, 1)
|
||||
assert.equal(account.depth, 2)
|
||||
})
|
||||
|
@ -79,8 +79,8 @@ describe('Wallet', function() {
|
|||
it('returns the private key at the given index of external account', function(){
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
|
||||
assertPrivateKeyEqual(wallet.getPrivateKey(0), wallet.externalAccount.derive(0).priv)
|
||||
assertPrivateKeyEqual(wallet.getPrivateKey(1), wallet.externalAccount.derive(1).priv)
|
||||
assertPrivateKeyEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv)
|
||||
assertPrivateKeyEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).priv)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -88,8 +88,8 @@ describe('Wallet', function() {
|
|||
it('returns the private key at the given index of internal account', function(){
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
|
||||
assertPrivateKeyEqual(wallet.getInternalPrivateKey(0), wallet.internalAccount.derive(0).priv)
|
||||
assertPrivateKeyEqual(wallet.getInternalPrivateKey(1), wallet.internalAccount.derive(1).priv)
|
||||
assertPrivateKeyEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv)
|
||||
assertPrivateKeyEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -101,9 +101,9 @@ describe('Wallet', function() {
|
|||
wallet.generateAddress()
|
||||
|
||||
assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
||||
wallet.externalAccount.derive(1).priv)
|
||||
wallet.getExternalAccount().derive(1).priv)
|
||||
assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
||||
wallet.internalAccount.derive(0).priv)
|
||||
wallet.getInternalAccount().derive(0).priv)
|
||||
})
|
||||
|
||||
it('raises an error when address is not found', function(){
|
||||
|
|
Loading…
Reference in a new issue