Browse Source

HD wallet versions: Bitcoin -> prod, BitcoinTest -> testnet

hk-custom-address
Wei Lu 11 years ago
parent
commit
293c864f4f
  1. 11
      src/hdwallet.js
  2. 2
      src/wallet.js
  3. 12
      test/wallet.js

11
src/hdwallet.js

@ -13,7 +13,7 @@ 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 || 'Bitcoin'
this.network = network || 'prod'
this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.getKeyVersion())
this.pub = this.priv.getPub()
this.index = 0
@ -24,8 +24,8 @@ HDWallet.HIGHEST_BIT = 0x80000000
HDWallet.LENGTH = 78
HDWallet.VERSIONS = {
Bitcoin: [0x0488B21E, 0x0488ADE4],
BitcoinTest: [0x043587CF, 0x04358394]
prod: [0x0488B21E, 0x0488ADE4],
testnet: [0x043587CF, 0x04358394]
}
function arrayEqual(a, b) {
@ -124,8 +124,7 @@ HDWallet.prototype.getFingerprint = function() {
}
HDWallet.prototype.getBitcoinAddress = function() {
var test = this.network.match(/Test$/)
return new Address(util.sha256ripe160(this.pub.toBytes()), test ? 111 : 0)
return new Address(util.sha256ripe160(this.pub.toBytes()), this.getKeyVersion())
}
HDWallet.prototype.toBytes = function(priv) {
@ -234,7 +233,7 @@ HDWallet.prototype.derivePrivate = function(index) {
}
HDWallet.prototype.getKeyVersion = function() {
return this.network == 'Bitcoin' ? Address.address_types.prod : Address.address_types.testnet
return this.network == 'prod' ? Address.address_types.prod : Address.address_types.testnet
}
HDWallet.prototype.toString = HDWallet.prototype.toBase58

2
src/wallet.js

@ -18,7 +18,7 @@ var Wallet = function (seed, options) {
if (!(this instanceof Wallet)) { return new Wallet(seed, options); }
var options = options || {}
var network = options.network || 'Bitcoin'
var network = options.network || 'prod'
// HD first-level child derivation method (i.e. public or private child key derivation)
// NB: if not specified, defaults to private child derivation

12
test/wallet.js

@ -11,8 +11,8 @@ describe('Wallet', function() {
wallet = new Wallet(seed)
})
it('defaults to Bitcoin network', function() {
assert.equal(wallet.getMasterKey().network, 'Bitcoin')
it('defaults to Bitcoin mainnet', function() {
assert.equal(wallet.getMasterKey().network, 'prod')
})
it('defaults to private derivationMethod', function() {
@ -23,11 +23,11 @@ describe('Wallet', function() {
describe('constructor options', function() {
var wallet;
beforeEach(function() {
wallet = new Wallet(seed, {network: 'Test', derivationMethod: 'public'})
wallet = new Wallet(seed, {network: 'testnet', derivationMethod: 'public'})
})
it('uses the network if specified', function() {
assert.equal(wallet.getMasterKey().network, 'Test')
assert.equal(wallet.getMasterKey().network, 'testnet')
})
it('uses the derivationMethod if specified', function() {
@ -37,14 +37,14 @@ 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: "Bitcoin"})
var w = Wallet("foobar", {network: "prod"})
assert(w.getMasterKey().priv.version == Address.address_types['prod'])
w.generateAddress()
assert(w.getPrivateKey(0).priv.version == Address.address_types['prod'])
})
it('ensures that a testnet Wallet has testnet child keys (pub and priv)', function() {
var w = Wallet("foobar", {network: "BitcoinTest"})
var w = Wallet("foobar", {network: "testnet"})
assert(w.getMasterKey().priv.version == Address.address_types['testnet'])
w.generateAddress()
assert(w.getPrivateKey(0).priv.version == Address.address_types['testnet'])

Loading…
Cancel
Save