use options in wallet contructor & add tests

[#26]
This commit is contained in:
Wei Lu 2014-02-27 11:54:54 +08:00
parent baaddc8930
commit 36d3d6ebaa
2 changed files with 48 additions and 9 deletions

View file

@ -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 Normal file
View file

@ -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')
})
})
})