diff --git a/src/wallet.js b/src/wallet.js index e738f9b..95f2e91 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -14,8 +14,18 @@ var HDWallet = require('./hdwallet.js') var SecureRandom = require('./jsbn/rng'); var rng = new SecureRandom(); -var Wallet = function (seed, network, derivationMethod) { - if (!(this instanceof Wallet)) { return new Wallet(seed, network, derivationMethod); } +var Wallet = function (seed, options) { + if (!(this instanceof Wallet)) { return new Wallet(seed, options); } + + var options = options || {} + var network = options.network || 'Bitcoin' + + // 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 + this.derivationMethod = options.derivationMethod || 'private' + assert(this.derivationMethod == 'public' || this.derivationMethod == 'private', + "derivationMethod must be either 'public' or 'private'"); // Stored in a closure to make accidental serialization less likely var keys = []; @@ -28,13 +38,6 @@ var Wallet = function (seed, network, derivationMethod) { // Transaction output data this.outputs = {}; - // 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 - this.derivationMethod = derivationMethod || 'private'; - assert(this.derivationMethod == 'public' || this.derivationMethod == 'private', - "derivationMethod must be either 'public' or 'private'"); - // Make a new master key this.newMasterKey = function(seed, network) { if (!seed) { diff --git a/test/wallet.js b/test/wallet.js new file mode 100644 index 0000000..0f39d02 --- /dev/null +++ b/test/wallet.js @@ -0,0 +1,36 @@ +var Wallet = require('../src/wallet.js') +var assert = require('assert') + +describe('Wallet', function() { + var seed = 'crazy horse battery staple' + + describe('default constructor', function() { + var wallet; + beforeEach(function() { + wallet = new Wallet(seed) + }) + + it('defaults to Bitcoin network', function() { + assert.equal(wallet.getMasterKey().network, 'Bitcoin') + }) + + it('defaults to private derivationMethod', function() { + assert.equal(wallet.derivationMethod, 'private') + }) + }) + + describe('constructor options', function() { + var wallet; + beforeEach(function() { + wallet = new Wallet(seed, {network: 'Test', derivationMethod: 'public'}) + }) + + it('uses the network if specified', function() { + assert.equal(wallet.getMasterKey().network, 'Test') + }) + + it('uses the derivationMethod if specified', function() { + assert.equal(wallet.derivationMethod, 'public') + }) + }) +})