bcoin-org/bcoin

Bit of a type mismatch between HD.fromSeed and HDPrivateKey.fromSeed

haxwell opened this issue · 2 comments

This bit of code.. in lib/hd/hd.js

/**
 * Generate an {@link HDPrivateKey} from a seed.
 * @param {Object|Mnemonic|Buffer} options - seed,
 * mnemonic, mnemonic options.
 * @returns {HDPrivateKey}
 */

HD.fromSeed = function fromSeed(options) {
  return HDPrivateKey.fromSeed(options);
};

eventually calls lib/hd/private.js

  /**
   * Inject properties from seed.
   * @private
   * @param {Buffer} seed
   */

  fromSeed(seed) {
    assert(Buffer.isBuffer(seed));
    
    ...
    }

The problem is that the first method (HD) has a parameter named options, and its comment indicates it can come in various formats. But the code it is actually calling (HDPrivateKey) says, no this must be a Buffer.

We should update that original code, recognizing that a Buffer is needed there.

Another case is here in hd.js

 * Instantiate an hd private key from a mnemonic.
 * @param {Mnemonic|Object} mnemonic
 * @returns {HDPrivateKey}
 */

HD.fromMnemonic = function fromMnemonic(options) {
  return HDPrivateKey.fromMnemonic(options);
};

The method that it eventually calls is in lib/hd/private.js:

  /**
   * Inject properties from a mnemonic.
   * @private
   * @param {Mnemonic} mnemonic
   * @param {String?} passphrase
   */

  fromMnemonic(mnemonic, passphrase) {
    assert(mnemonic instanceof Mnemonic);
    return this.fromSeed(mnemonic.toSeed(passphrase));
  }

It requires a Mnemonic, but the method above it allows for non-Mnemonic objects.

I have created a PR, adopting the necessary changes.