Browse Source

use options in wallet contructor & add tests

[#26]
hk-custom-address
Wei Lu 11 years ago
parent
commit
36d3d6ebaa
  1. 21
      src/wallet.js
  2. 36
      test/wallet.js

21
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) {

36
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')
})
})
})
Loading…
Cancel
Save