Browse Source

private a method to generate change addresses

hk-custom-address
Wei Lu 11 years ago
parent
commit
ab9e782d14
  1. 16
      src/wallet.js
  2. 24
      test/wallet.js

16
src/wallet.js

@ -23,6 +23,7 @@ var Wallet = function (seed, options) {
// Addresses // Addresses
this.addresses = []; this.addresses = [];
this.changeAddresses = [];
// Transaction output data // Transaction output data
this.outputs = {}; this.outputs = {};
@ -38,21 +39,24 @@ var Wallet = function (seed, options) {
} }
this.newMasterKey(seed, network) this.newMasterKey(seed, network)
// HD first-level child derivation method (i.e. public or private child key derivation) // HD first-level child derivation method should be private
// NB: if not specified, defaults to private child derivation // See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
// Also see https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
this.accountZero = masterkey.derivePrivate(0) this.accountZero = masterkey.derivePrivate(0)
this.externalAccount = this.accountZero.derive(0) this.externalAccount = this.accountZero.derive(0)
this.internalAccount = this.accountZero.derive(1) this.internalAccount = this.accountZero.derive(1)
// Add a new address
this.generateAddress = function() { this.generateAddress = function() {
var key = this.externalAccount.derive(keys.length) var key = this.externalAccount.derive(this.addresses.length)
keys.push(key); // consider removing this and derive on-demand for simplified encrypted keychain
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() {
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 // Processes a transaction object
// If "verified" is true, then we trust the transaction as "final" // If "verified" is true, then we trust the transaction as "final"
this.processTx = function(tx, verified) { this.processTx = function(tx, verified) {

24
test/wallet.js

@ -52,12 +52,26 @@ describe('Wallet', function() {
}) })
describe('generateAddress', function(){ describe('generateAddress', function(){
var wallet; it('generate receiving addresses', function(){
beforeEach(function() { wallet = new Wallet(seed, {network: 'testnet'}) }) var wallet = new Wallet(seed, {network: 'testnet'})
var expectedAddresses = [
"n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa",
"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"]
it('defaults to generating receiving addresses', function(){ assert.equal(wallet.generateChangeAddress(), expectedAddresses[0])
assert.equal(wallet.generateAddress(), "n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa") assert.deepEqual(wallet.changeAddresses, expectedAddresses)
assert.equal(wallet.generateAddress(), "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X")
}) })
}) })
}) })

Loading…
Cancel
Save