Wallet address derivation follows bip32/bitcoinj keychain structure

Also wallet accepts bytes in constructor

[#60]
This commit is contained in:
Wei Lu 2014-03-11 23:42:01 +08:00
parent 44012b47b5
commit 1212099bfc
2 changed files with 54 additions and 31 deletions

View file

@ -6,7 +6,7 @@ var BigInteger = require('./jsbn/jsbn');
var Transaction = require('./transaction').Transaction; var Transaction = require('./transaction').Transaction;
var TransactionIn = require('./transaction').TransactionIn; var TransactionIn = require('./transaction').TransactionIn;
var TransactionOut = require('./transaction').TransactionOut; var TransactionOut = require('./transaction').TransactionOut;
var HDWallet = require('./hdwallet.js') var HDNode = require('./hdwallet.js')
var SecureRandom = require('./jsbn/rng'); var SecureRandom = require('./jsbn/rng');
var rng = new SecureRandom(); var rng = new SecureRandom();
@ -16,13 +16,6 @@ var Wallet = function (seed, options) {
var options = options || {} var options = options || {}
var network = options.network || 'mainnet' var network = options.network || 'mainnet'
// 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 // Stored in a closure to make accidental serialization less likely
var keys = []; var keys = [];
var masterkey = null; var masterkey = null;
@ -37,22 +30,26 @@ var Wallet = function (seed, options) {
// Make a new master key // Make a new master key
this.newMasterKey = function(seed, network) { this.newMasterKey = function(seed, network) {
if (!seed) { if (!seed) {
var seedBytes = new Array(32); var seed= new Array(32);
rng.nextBytes(seedBytes); rng.nextBytes(seedBytes);
seed = convert.bytesToString(seedBytes)
} }
masterkey = new HDWallet(seed, network); masterkey = new HDNode(seed, network);
keys = [] keys = []
} }
this.newMasterKey(seed, network) this.newMasterKey(seed, network)
// 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.accountZero = masterkey.derivePrivate(0)
this.externalAccount = this.accountZero.derive(0)
this.internalAccount = this.accountZero.derive(1)
// Add a new address // Add a new address
this.generateAddress = function() { this.generateAddress = function() {
if(this.derivationMethod == 'private') var key = this.externalAccount.derive(keys.length)
keys.push(masterkey.derivePrivate(keys.length)); keys.push(key); // consider removing this and derive on-demand for simplified encrypted keychain
else this.addresses.push(key.getBitcoinAddress().toString())
keys.push(masterkey.derive(keys.length));
this.addresses.push(keys[keys.length-1].getBitcoinAddress().toString())
return this.addresses[this.addresses.length - 1] return this.addresses[this.addresses.length - 1]
} }

View file

@ -1,10 +1,17 @@
var Wallet = require('../src/wallet.js') var Wallet = require('../src/wallet.js')
var HDNode = require('../src/hdwallet.js')
var convert = require('../src/convert.js')
var assert = require('assert') var assert = require('assert')
var SHA256 = require('crypto-js/sha256')
var Crypto = require('crypto-js')
describe('Wallet', function() { describe('Wallet', function() {
var seed = 'crazy horse battery staple' var seed;
beforeEach(function(){
seed = convert.wordArrayToBytes(SHA256("don't use a string seed like this in real life"))
})
describe('default constructor', function() { describe('constructor', function() {
var wallet; var wallet;
beforeEach(function() { beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
@ -14,24 +21,43 @@ describe('Wallet', function() {
assert.equal(wallet.getMasterKey().network, 'mainnet') assert.equal(wallet.getMasterKey().network, 'mainnet')
}) })
it('defaults to private derivationMethod', function() { it("generates m/0' as the main account", function() {
assert.equal(wallet.derivationMethod, 'private') var mainAccount = wallet.accountZero
assert.equal(mainAccount.index, 0 + HDNode.HIGHEST_BIT)
assert.equal(mainAccount.depth, 1)
}) })
it("generates m/0'/0 as the external account", function() {
var account = wallet.externalAccount
assert.equal(account.index, 0)
assert.equal(account.depth, 2)
})
it("generates m/0'/1 as the internal account", function() {
var account = wallet.internalAccount
assert.equal(account.index, 1)
assert.equal(account.depth, 2)
}) })
describe('constructor options', function() { describe('constructor options', function() {
var wallet; var wallet;
beforeEach(function() { beforeEach(function() {
wallet = new Wallet(seed, {network: 'testnet', derivationMethod: 'public'}) wallet = new Wallet(seed, {network: 'testnet'})
}) })
it('uses the network if specified', function() { it('uses the network if specified', function() {
assert.equal(wallet.getMasterKey().network, 'testnet') assert.equal(wallet.getMasterKey().network, 'testnet')
}) })
it('uses the derivationMethod if specified', function() {
assert.equal(wallet.derivationMethod, 'public')
}) })
}) })
describe('generateAddress', function(){
var wallet;
beforeEach(function() { wallet = new Wallet(seed, {network: 'testnet'}) })
it('defaults to generating receiving addresses', function(){
assert.equal(wallet.generateAddress(), "n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa")
assert.equal(wallet.generateAddress(), "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X")
})
})
}) })