diff --git a/src/wallet.js b/src/wallet.js index 0af210d..995a823 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -23,6 +23,7 @@ var Wallet = function (seed, options) { // Addresses this.addresses = []; + this.changeAddresses = []; // Transaction output data this.outputs = {}; @@ -38,21 +39,24 @@ var Wallet = function (seed, options) { } this.newMasterKey(seed, network) - // HD first-level child derivation method (i.e. public or private child key derivation) - // NB: if not specified, defaults to private child derivation - // Also see https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254 + // 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) - // Add a new address this.generateAddress = function() { - var key = this.externalAccount.derive(keys.length) - keys.push(key); // consider removing this and derive on-demand for simplified encrypted keychain + var key = this.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) + this.changeAddresses.push(key.getBitcoinAddress().toString()) + return this.changeAddresses[this.changeAddresses.length - 1] + } + // Processes a transaction object // If "verified" is true, then we trust the transaction as "final" this.processTx = function(tx, verified) { diff --git a/test/wallet.js b/test/wallet.js index 0f6747c..39a8500 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -52,12 +52,26 @@ describe('Wallet', function() { }) describe('generateAddress', function(){ - var wallet; - beforeEach(function() { wallet = new Wallet(seed, {network: 'testnet'}) }) + it('generate receiving addresses', function(){ + var wallet = new Wallet(seed, {network: 'testnet'}) + var expectedAddresses = [ + "n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa", + "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X" + ] - it('defaults to generating receiving addresses', function(){ - assert.equal(wallet.generateAddress(), "n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa") - assert.equal(wallet.generateAddress(), "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X") + assert.equal(wallet.generateAddress(), expectedAddresses[0]) + assert.equal(wallet.generateAddress(), expectedAddresses[1]) + assert.deepEqual(wallet.addresses, expectedAddresses) + }) + }) + + describe('generateChangeAddress', function(){ + it('generates change addresses', function(){ + var wallet = new Wallet(seed, {network: 'testnet'}) + var expectedAddresses = ["mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"] + + assert.equal(wallet.generateChangeAddress(), expectedAddresses[0]) + assert.deepEqual(wallet.changeAddresses, expectedAddresses) }) }) })