mnemonic refactor.

This commit is contained in:
Christopher Jeffrey 2016-04-29 05:56:30 -07:00
parent 8e9f0d1bf7
commit 9814dc32f7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 26 additions and 34 deletions

View File

@ -91,7 +91,7 @@ var BufferReader = require('./reader');
var unorm = require('../../vendor/unorm');
/**
* HD Seed
* HD Mnemonic
* @exports Mnemonic
* @constructor
* @param {Object} options
@ -136,14 +136,12 @@ function Mnemonic(options) {
* @returns {Buffer} pbkdf2 seed.
*/
Mnemonic.prototype.createSeed = function createSeed() {
Mnemonic.prototype.toSeed = function toSeed() {
if (this.seed)
return this.seed;
if (!this.entropy)
this.entropy = ec.random(this.bits / 8);
else
this.bits = this.entropy.length * 8;
if (!this.phrase)
this.phrase = this.createMnemonic();
@ -175,7 +173,7 @@ Mnemonic.prototype.createMnemonic = function createMnemonic() {
while (bits.length % 256 !== 0)
bits = '0' + bits;
bin += bits.slice(0, this.bits / 32);
bin += bits.slice(0, (this.entropy.length * 8) / 32);
assert(bin.length % 11 === 0);
@ -224,7 +222,7 @@ Mnemonic.getWordlist = function getWordlist(lang) {
Mnemonic.isMnemonic = function isMnemonic(obj) {
return obj
&& typeof obj.bits === 'number'
&& typeof obj.createSeed === 'function';
&& typeof obj.toSeed === 'function';
};
/**
@ -375,7 +373,7 @@ function HDPrivateKey(options) {
this.network = options.network || network.type;
this.xprivkey = options.xprivkey;
this.seed = options.seed;
this.mnemonic = options.mnemonic;
this.version = options.version;
this.depth = options.depth;
@ -681,10 +679,10 @@ HDPrivateKey.parseSeed = function parseSeed(seed, networkType) {
data = seed;
seed = null;
} else if (seed instanceof Mnemonic) {
data = seed.createSeed();
data = seed.toSeed();
} else {
seed = new Mnemonic(seed);
data = seed.createSeed();
data = seed.toSeed();
}
if (data.length < constants.hd.MIN_ENTROPY
@ -848,14 +846,14 @@ HDPrivateKey.prototype.toJSON = function toJSON(passphrase) {
if (this instanceof HDPrivateKey) {
json.encrypted = passphrase ? true : false;
if (this.seed) {
if (this.mnemonic) {
json.phrase = passphrase
? utils.encrypt(this.seed.phrase, passphrase)
: this.seed.phrase;
? utils.encrypt(this.mnemonic.phrase, passphrase)
: this.mnemonic.phrase;
json.passphrase = passphrase
? utils.encrypt(this.seed.passphrase, passphrase)
: this.seed.passphrase;
json.lang = this.seed.lang;
? utils.encrypt(this.mnemonic.passphrase, passphrase)
: this.mnemonic.passphrase;
json.lang = this.mnemonic.lang;
}
json.xprivkey = passphrase
? utils.encrypt(this.xprivkey, passphrase)
@ -884,7 +882,7 @@ HDPrivateKey.parseJSON = function parseJSON(json, passphrase) {
throw new Error('Cannot decrypt address');
if (json.phrase) {
data.seed = {
data.mnemonic = {
phrase: json.encrypted
? utils.decrypt(json.phrase, passphrase)
: json.phrase,
@ -927,12 +925,12 @@ HDPrivateKey.fromJSON = function fromJSON(json, passphrase) {
if (json.xprivkey) {
key = HDPrivateKey.fromBase58(json.xprivkey);
key.seed = json.seed ? new Mnemonic(json.seed) : null;
key.mnemonic = json.mnemonic ? new Mnemonic(json.mnemonic) : null;
return key;
}
if (json.seed)
return HDPrivateKey.fromSeed(json.seed);
if (json.mnemonic)
return HDPrivateKey.fromSeed(json.mnemonic);
if (json.xpubkey)
return HDPublicKey.fromBase58(json.xprivkey);
@ -1327,7 +1325,7 @@ HDPrivateKey.prototype.toSecret = function toSecret() {
return KeyPair.toSecret.call(this);
};
HD.seed = Mnemonic;
HD.mnemonic = Mnemonic;
HD.priv = HDPrivateKey;
HD.pub = HDPublicKey;
HD.privateKey = HDPrivateKey;

View File

@ -146,14 +146,6 @@ describe('HD', function() {
assert.equal(bcoin.hd.fromJSON(key.toJSON()).xprivkey, key.xprivkey);
});
it('should create an hd seed', function() {
var seed = new bcoin.hd.seed({
// I have the same combination on my luggage:
entropy: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
passphrase: 'foo'
});
});
function ub58(data) {
return utils.fromBase58(data).toString('hex');
}

View File

@ -12,32 +12,34 @@ describe('Mnemonic', function() {
var seed = new Buffer(data[2], 'hex');
var xpriv = data[3];
it('should create an english mnemonic (' + i + ')', function() {
var mnem = new bcoin.hd.seed({
var mnem = new bcoin.hd.mnemonic({
passphrase: 'TREZOR',
lang: 'english',
entropy: entropy
});
mnem.createSeed();
mnem.toSeed();
assert.equal(mnem.phrase, mnemonic);
assert.equal(mnem.createSeed().toString('hex'), seed.toString('hex'));
assert.equal(mnem.toSeed().toString('hex'), seed.toString('hex'));
var key = bcoin.hd.fromSeed(mnem);
assert.equal(key.xprivkey, xpriv);
});
});
// Disabled for now
return;
mnemonic2.forEach(function(data, i) {
var entropy = new Buffer(data.entropy, 'hex');
var mnemonic = data.mnemonic;
var seed = new Buffer(data.seed, 'hex');
var xpriv = data.bip32_xprv;
it('should create a japanese mnemonic (' + i + ')', function() {
var mnem = new bcoin.hd.seed({
var mnem = new bcoin.hd.mnemonic({
passphrase: 'メートルガバヴァぱばぐゞちぢ十人十色',
lang: 'japanese',
entropy: entropy
});
mnem.createSeed();
mnem.toSeed();
assert.equal(mnem.phrase, mnemonic);
assert.equal(mnem.createSeed().toString('hex'), seed.toString('hex'));
assert.equal(mnem.toSeed().toString('hex'), seed.toString('hex'));
var key = bcoin.hd.fromSeed(mnem);
assert.equal(key.xprivkey, xpriv);
});