UNPKG

hardhat

Version:

Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

49 lines 2.16 kB
import { HardhatError } from "@nomicfoundation/hardhat-errors"; import { bytesToHexString } from "@nomicfoundation/hardhat-utils/bytes"; // ethereum-cryptography/bip39 is known to be slow to load, so we lazy load it let mnemonicToSeedSync; // ethereum-cryptography/hdkey is known to be slow to load, so we lazy load it let HDKey; const HD_PATH_REGEX = /^m(:?\/\d+'?)+\/?$/; export async function derivePrivateKeys(mnemonic, hdpath, initialIndex, count, passphrase) { if (!HD_PATH_REGEX.test(hdpath)) { throw new HardhatError(HardhatError.ERRORS.CORE.NETWORK.INVALID_HD_PATH, { path: hdpath, }); } if (!hdpath.endsWith("/")) { hdpath += "/"; } const privateKeys = []; for (let i = initialIndex; i < initialIndex + count; i++) { const privateKey = await deriveKeyFromMnemonicAndPath(mnemonic, hdpath + i.toString(), passphrase); if (privateKey === undefined) { throw new HardhatError(HardhatError.ERRORS.CORE.NETWORK.CANT_DERIVE_KEY, { mnemonic, path: hdpath, }); } privateKeys.push(privateKey); } return privateKeys; } async function deriveKeyFromMnemonicAndPath(mnemonic, hdPath, passphrase) { // NOTE: If mnemonic has space or newline at the beginning or end, it will be trimmed. // This is because mnemonic containing them may generate different private keys. const trimmedMnemonic = mnemonic.trim(); if (mnemonicToSeedSync === undefined) { const { mnemonicToSeedSync: importedMnemonicToSeedSync } = await import("ethereum-cryptography/bip39"); mnemonicToSeedSync = importedMnemonicToSeedSync; } if (HDKey === undefined) { const { HDKey: ImportedHDKey } = await import("ethereum-cryptography/hdkey"); HDKey = ImportedHDKey; } const seed = mnemonicToSeedSync(trimmedMnemonic, passphrase); const masterKey = HDKey.fromMasterSeed(seed); const derived = masterKey.derive(hdPath); return derived.privateKey === null ? undefined : bytesToHexString(derived.privateKey); } //# sourceMappingURL=derive-private-keys.js.map