{"size": 778, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "bloq/mnemonic-to-key-pair", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "bloq/mnemonic-to-key-pair", "max_issues_repo_issues_event_min_datetime": "2021-05-11T23:34:20.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T23:34:21.000Z", "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "bloq/mnemonic-to-key-pair", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 28.8148148148, "max_line_length": 78, "alphanum_fraction": 0.7223650386}
'use strict'

require('./typedefs')
/* global KeyPair */

const bip39 = require('bip39')
const hdkey = require('hdkey')

/**
 * Obtian a key pair from a BIP39 mnemonic phrase and following the supplied
 * derivation path. Optinally supply a password.
 *
 * @param {string} mnemonic The 12-words mnemonic phrase.
 * @param {string} derivationPath The derivation path to use. I.e. "m/44'...".
 * @param {string} [password] The mnemonic password.
 * @returns {KeyPair} The key pair.
 */
function mnemonicToKeyPair(mnemonic, derivationPath, password) {
  const seed = bip39.mnemonicToSeedSync(mnemonic, password)
  const { privateKey, publicKey } = hdkey
    .fromMasterSeed(seed)
    .derive(derivationPath)
  return { privateKey, publicKey }
}

module.exports = mnemonicToKeyPair
