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