hardhat-secure-accounts
Version:
Account management plugin for Hardhat
52 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_HD_PASSPHRASE = exports.DEFAULT_HD_PATH_PREFIX = exports.DEFAULT_HD_INITIAL_INDEX = exports.DEFAULT_HD_COUNT = void 0;
exports.getWallet = getWallet;
exports.getWallets = getWallets;
exports.removeIndexFromHDPath = removeIndexFromHDPath;
const ethers_1 = require("ethers");
const error_1 = require("../helpers/error");
const logger_1 = require("../helpers/logger");
const ask_1 = require("./ask");
exports.DEFAULT_HD_COUNT = 20;
exports.DEFAULT_HD_INITIAL_INDEX = 0;
exports.DEFAULT_HD_PATH_PREFIX = `m/44'/60'/0'/0/`;
exports.DEFAULT_HD_PASSPHRASE = '';
async function getWallet(accountsDir, name, _password) {
const account = await (0, ask_1.getAccountOrAsk)(name, accountsDir);
const password = await (0, ask_1.getPasswordOrAsk)(_password);
try {
const wallet = await ethers_1.Wallet.fromEncryptedJson(account.json, password);
(0, logger_1.logDebug)(`Account unlocked: ${wallet.address}`);
if (isHDNodeWallet(wallet)) {
return wallet;
}
else {
throw new error_1.SecureAccountPluginError('Wallet is not an HDNodeWallet');
}
}
catch (error) {
throw new error_1.SecureAccountPluginError(error);
}
}
async function getWallets(accountsDir, name, password) {
const wallet = await getWallet(accountsDir, name, password);
if (wallet.mnemonic === null) {
throw new error_1.SecureAccountPluginError('Wallet is not an HDNodeWallet');
}
const mnemonic = ethers_1.Mnemonic.fromPhrase(wallet.mnemonic.phrase);
(0, logger_1.logDebug)(`Deriving private keys from mnemonic`);
return Array.from(Array(exports.DEFAULT_HD_COUNT).keys()).map((i) => ethers_1.HDNodeWallet.fromMnemonic(mnemonic, `${exports.DEFAULT_HD_PATH_PREFIX}${i}`));
}
function removeIndexFromHDPath(hdpath) {
if (hdpath.endsWith('/')) {
return hdpath;
}
else {
return hdpath.substring(0, hdpath.lastIndexOf('/') + 1);
}
}
function isHDNodeWallet(wallet) {
return wallet.mnemonic !== undefined;
}
//# sourceMappingURL=wallet.js.map