throws exception if specified network is unknown

also move network type tests to hdwallet
This commit is contained in:
Wei Lu 2014-02-28 17:26:16 +08:00
parent 02b3a2b564
commit 2e97828379
3 changed files with 37 additions and 20 deletions

View file

@ -15,6 +15,10 @@ var HDWallet = module.exports = function(seed, network) {
var I = Crypto.HMAC(Crypto.SHA512, seed, 'Bitcoin seed', { asBytes: true })
this.chaincode = I.slice(32)
this.network = network || 'mainnet'
if(!Network.hasOwnProperty(this.network)) {
throw new Error("Unknown network: " + this.network)
}
this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.getKeyVersion())
this.pub = this.priv.getPub()
this.index = 0

View file

@ -1,7 +1,10 @@
/* global describe, it */
var HDWallet = require('../src/hdwallet.js')
, assert = require('assert')
, convert = require('../src/convert.js')
var assert = require('assert')
var convert = require('../src/convert.js')
var Network = require('../src/network')
var mainnet = Network.mainnet.addressVersion
var testnet = Network.testnet.addressVersion
var b2h = convert.bytesToHex
@ -214,4 +217,32 @@ describe('HDWallet', function() {
assert.equal(hd.toBase58(true), 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j')
})
})
describe('network types', function() {
it('ensures that a mainnet Wallet has mainnet child keys (pub and priv)', function() {
var wallet = new HDWallet("foobar", "mainnet")
assert.equal(wallet.priv.version, mainnet)
var privChild = wallet.derivePrivate(0)
assert.equal(privChild.priv.version, mainnet)
var pubChild = wallet.derive(0)
assert.equal(pubChild.priv.version, mainnet)
})
it('ensures that a testnet Wallet has testnet child keys (pub and priv)', function() {
var wallet = new HDWallet("foobar", "testnet")
assert.equal(wallet.priv.version, testnet)
var privChild = wallet.derivePrivate(0)
assert.equal(privChild.priv.version, testnet)
var pubChild = wallet.derive(0)
assert.equal(pubChild.priv.version, testnet)
})
it('throws an excption when unknown network type is passed in', function() {
assert.throws(function() { new HDWallet("foobar", "doge") })
})
})
})

View file

@ -1,7 +1,4 @@
var Wallet = require('../src/wallet.js')
var Network = require('../src/network')
var mainnet = Network.mainnet.addressVersion
var testnet = Network.testnet.addressVersion
var assert = require('assert')
describe('Wallet', function() {
@ -37,19 +34,4 @@ describe('Wallet', function() {
})
})
describe('networkType', function() {
it('ensures that a mainnet Wallet has mainnet child keys (pub and priv)', function() {
var w = Wallet("foobar", {network: "mainnet"})
assert(w.getMasterKey().priv.version == mainnet)
w.generateAddress()
assert(w.getPrivateKey(0).priv.version == mainnet)
})
it('ensures that a testnet Wallet has testnet child keys (pub and priv)', function() {
var w = Wallet("foobar", {network: "testnet"})
assert(w.getMasterKey().priv.version == testnet)
w.generateAddress()
assert(w.getPrivateKey(0).priv.version == testnet)
})
})
})