Browse Source

store accounts in closure instead of on object

hk-custom-address
Wei Lu 11 years ago
parent
commit
b2f010428f
  1. 20
      src/wallet.js
  2. 18
      test/wallet.js

20
src/wallet.js

@ -19,6 +19,9 @@ var Wallet = function (seed, options) {
// Stored in a closure to make accidental serialization less likely // Stored in a closure to make accidental serialization less likely
var masterkey = null; var masterkey = null;
var me = this; var me = this;
var accountZero = null;
var internalAccount = null;
var externalAccount = null;
// Addresses // Addresses
this.addresses = []; this.addresses = [];
@ -39,18 +42,18 @@ var Wallet = function (seed, options) {
// HD first-level child derivation method should be private // HD first-level child derivation method should be private
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254 // See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
this.accountZero = masterkey.derivePrivate(0) accountZero = masterkey.derivePrivate(0)
this.externalAccount = this.accountZero.derive(0) externalAccount = accountZero.derive(0)
this.internalAccount = this.accountZero.derive(1) internalAccount = accountZero.derive(1)
this.generateAddress = function() { this.generateAddress = function() {
var key = this.externalAccount.derive(this.addresses.length) var key = externalAccount.derive(this.addresses.length)
this.addresses.push(key.getBitcoinAddress().toString()) this.addresses.push(key.getBitcoinAddress().toString())
return this.addresses[this.addresses.length - 1] return this.addresses[this.addresses.length - 1]
} }
this.generateChangeAddress = function() { this.generateChangeAddress = function() {
var key = this.internalAccount.derive(this.changeAddresses.length) var key = internalAccount.derive(this.changeAddresses.length)
this.changeAddresses.push(key.getBitcoinAddress().toString()) this.changeAddresses.push(key.getBitcoinAddress().toString())
return this.changeAddresses[this.changeAddresses.length - 1] return this.changeAddresses[this.changeAddresses.length - 1]
} }
@ -174,13 +177,16 @@ var Wallet = function (seed, options) {
} }
this.getMasterKey = function() { return masterkey } this.getMasterKey = function() { return masterkey }
this.getAccountZero = function() { return accountZero }
this.getInternalAccount = function() { return internalAccount }
this.getExternalAccount = function() { return externalAccount }
this.getPrivateKey = function(index) { this.getPrivateKey = function(index) {
return this.externalAccount.derive(index).priv return externalAccount.derive(index).priv
} }
this.getInternalPrivateKey = function(index) { this.getInternalPrivateKey = function(index) {
return this.internalAccount.derive(index).priv return internalAccount.derive(index).priv
} }
this.getPrivateKeyForAddress = function(address) { this.getPrivateKeyForAddress = function(address) {

18
test/wallet.js

@ -22,19 +22,19 @@ describe('Wallet', function() {
}) })
it("generates m/0' as the main account", 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.index, 0 + HDNode.HIGHEST_BIT)
assert.equal(mainAccount.depth, 1) assert.equal(mainAccount.depth, 1)
}) })
it("generates m/0'/0 as the external account", function() { 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.index, 0)
assert.equal(account.depth, 2) assert.equal(account.depth, 2)
}) })
it("generates m/0'/1 as the internal account", function() { 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.index, 1)
assert.equal(account.depth, 2) 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(){ it('returns the private key at the given index of external account', function(){
var wallet = new Wallet(seed, {network: 'testnet'}) var wallet = new Wallet(seed, {network: 'testnet'})
assertPrivateKeyEqual(wallet.getPrivateKey(0), wallet.externalAccount.derive(0).priv) assertPrivateKeyEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv)
assertPrivateKeyEqual(wallet.getPrivateKey(1), wallet.externalAccount.derive(1).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(){ it('returns the private key at the given index of internal account', function(){
var wallet = new Wallet(seed, {network: 'testnet'}) var wallet = new Wallet(seed, {network: 'testnet'})
assertPrivateKeyEqual(wallet.getInternalPrivateKey(0), wallet.internalAccount.derive(0).priv) assertPrivateKeyEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv)
assertPrivateKeyEqual(wallet.getInternalPrivateKey(1), wallet.internalAccount.derive(1).priv) assertPrivateKeyEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv)
}) })
}) })
@ -101,9 +101,9 @@ describe('Wallet', function() {
wallet.generateAddress() wallet.generateAddress()
assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"), assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
wallet.externalAccount.derive(1).priv) wallet.getExternalAccount().derive(1).priv)
assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"), assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
wallet.internalAccount.derive(0).priv) wallet.getInternalAccount().derive(0).priv)
}) })
it('raises an error when address is not found', function(){ it('raises an error when address is not found', function(){

Loading…
Cancel
Save