comments.

This commit is contained in:
Christopher Jeffrey 2016-04-30 02:09:15 -07:00
parent 96139ad31c
commit 839f915fb7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -2,7 +2,7 @@
* @module hd
*
* @description
* HD seeds and keys (BIP32, BIP39) for bcoin.
* HD mnemonics and keys (BIP32, BIP39) for bcoin.
* Code adapted from bitcore-lib:
* - {@link https://github.com/bitpay/bitcore-lib/blob/master/lib/hdprivatekey.js}
* - {@link https://github.com/bitpay/bitcore-lib/blob/master/lib/hdpublickey.js}
@ -100,7 +100,7 @@ var unorm = require('../../vendor/unorm');
* @param {Buffer?} options.entropy - Entropy bytes. Will
* be generated with `options.bits` bits of entropy
* if not present.
* @param {String?} options.mnemonic - Mnemonic string (will
* @param {String?} options.phrase - Mnemonic phrase (will
* be generated if not present).
* @param {String?} options.passphrase - Optional salt for
* key stretching (empty string if not present).
@ -119,7 +119,7 @@ function Mnemonic(options) {
this.phrase = options.phrase;
this.passphrase = options.passphrase || '';
this.language = options.language || 'english';
this.seed = null;
this.seed = options.seed;
assert(this.bits >= 128);
assert(this.bits % 32 === 0);
@ -134,9 +134,6 @@ Mnemonic.prototype.toSeed = function toSeed() {
if (this.seed)
return this.seed;
if (!this.entropy)
this.entropy = ec.random(this.bits / 8);
if (!this.phrase)
this.phrase = this.createMnemonic();
@ -155,9 +152,13 @@ Mnemonic.prototype.toSeed = function toSeed() {
Mnemonic.prototype.createMnemonic = function createMnemonic() {
var mnemonic = [];
var bits = this.entropy.length * 8;
var wordlist = Mnemonic.getWordlist(this.language);
var i, j, word, entropy, oct, bit;
var i, j, bits, entropy, word, oct, bit;
if (!this.entropy)
this.entropy = ec.random(this.bits / 8);
bits = this.entropy.length * 8;
// Append the hash to the entropy to
// make things easy when grabbing
@ -274,7 +275,8 @@ HD.generate = function generate(options, networkType) {
/**
* Generate an {@link HDPrivateKey} from a seed.
* @param {Object|Mnemonic} options - HD seed or HD seed options.
* @param {Object|Mnemonic|Buffer} options - seed,
* mnemonic, mnemonic options.
* @param {String?} networkType
* @returns {HDPrivateKey}
*/
@ -285,8 +287,8 @@ HD.fromSeed = function fromSeed(options, networkType) {
/**
* Generate an hdkey from any number of options.
* @param {Object|Mnemonic} options - HD seed, HD seed
* options, buffer seed, or base58 key.
* @param {Object|Mnemonic|Buffer} options - mnemonic, mnemonic
* options, seed, or base58 key.
* @param {String?} networkType
* @returns {HDPrivateKey|HDPublicKey}
*/
@ -349,7 +351,7 @@ HD.isHD = function isHD(obj) {
* @constructor
* @param {Object|Base58String} options
* @param {Base58String?} options.xkey - Serialized base58 key.
* @param {(Mnemonic|Object)?} options.seed - HD seed or HD seed options.
* @param {(Mnemonic|Object)?} options.mnemonic - mnemonic or mnemonic options.
* @param {Number?} options.version
* @param {Number?} options.depth
* @param {Buffer?} options.parentFingerPrint
@ -359,7 +361,7 @@ HD.isHD = function isHD(obj) {
* @property {String} network
* @property {Base58String} xprivkey
* @property {Base58String} xpubkey
* @property {Mnemonic?} seed
* @property {Mnemonic?} mnemonic
* @property {Number} version
* @property {Number} depth
* @property {Buffer} parentFingerPrint
@ -391,6 +393,7 @@ function HDPrivateKey(options) {
this.fingerPrint = null;
this.hdPrivateKey = this;
this._hdPublicKey = null;
if (!this.xprivkey)
this.xprivkey = HDPrivateKey.render(options);
@ -667,8 +670,8 @@ HDPrivateKey.prototype.derivePath = function derivePath(path) {
/**
* Create an hd private key from a seed.
* @param {Buffer|Mnemonic|Object} options - A buffer,
* HD seed, or HD seed options.
* @param {Buffer|Mnemonic|Object} options - A seed,
* mnemonic, or mnemonic options.
* @param {String?} networkType
* @returns {Object} A "naked" key (a
* plain javascript object which is suitable
@ -707,13 +710,14 @@ HDPrivateKey.parseSeed = function parseSeed(seed, networkType) {
childIndex: 0,
chainCode: hash.slice(32, 64),
privateKey: hash.slice(0, 32),
seed: seed
mnemonic: seed
};
};
/**
* Instantiate a transaction from an HD seed.
* @param {Buffer|Mnemonic|Object} seed - A buffer, HD seed, or HD seed options.
* Instantiate an hd private key from a seed.
* @param {Buffer|Mnemonic|Object} seed - A
* seed, mnemonic, or mnemonic options.
* @param {String?} networkType
* @returns {HDPrivateKey}
*/
@ -734,17 +738,22 @@ HDPrivateKey.fromSeed = function fromSeed(seed, networkType) {
*/
HDPrivateKey._generate = function _generate(options, networkType) {
var privateKey, entropy;
if (!options)
opitons = {};
if (Buffer.isBuffer(options))
options = { privateKey: options };
if (!options.privateKey)
options.privateKey = ec.generatePrivateKey();
privateKey = options.privateKey;
entropy = options.entropy;
if (!options.entropy)
options.entropy = ec.random(32);
if (!privateKey)
privateKey = ec.generatePrivateKey();
if (!entropy)
entropy = ec.random(32);
return {
version: networkType