hardhat-secure-accounts
Version:
Account management plugin for Hardhat
65 lines • 3.76 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ACCOUNTS_TASKS = exports.TASK_ACCOUNTS_LIST = exports.TASK_ACCOUNTS_REMOVE = exports.TASK_ACCOUNTS_ADD = exports.TASK_ACCOUNTS = void 0;
const fs_1 = __importDefault(require("fs"));
const config_1 = require("hardhat/config");
const account_1 = require("./lib/account");
const ask_1 = require("./helpers/ask");
const logger_1 = require("./helpers/logger");
const error_1 = require("./helpers/error");
exports.TASK_ACCOUNTS = 'accounts';
exports.TASK_ACCOUNTS_ADD = 'accounts:add';
exports.TASK_ACCOUNTS_REMOVE = 'accounts:remove';
exports.TASK_ACCOUNTS_LIST = 'accounts:list';
exports.ACCOUNTS_TASKS = [exports.TASK_ACCOUNTS_ADD, exports.TASK_ACCOUNTS_REMOVE, exports.TASK_ACCOUNTS_LIST];
(0, config_1.task)('tt')
.setAction(async (_, hre) => {
console.log(await hre.network.provider.send('eth_accounts'));
});
(0, config_1.task)(exports.TASK_ACCOUNTS, 'Manage local accounts')
.addOptionalPositionalParam('action', 'Action to perform: list, add, remove')
.addOptionalParam('name', 'Name of the account')
.addOptionalParam('password', 'Password to encrypt the account')
.setAction(async (taskArgs, hre) => {
const action = taskArgs.action ? `accounts:${taskArgs.action}` : exports.TASK_ACCOUNTS_LIST;
if (!exports.ACCOUNTS_TASKS.includes(action)) {
throw new error_1.SecureAccountsPluginError('Action not supported. Run `npx hardhat accounts --help` for more info');
}
(0, logger_1.logDebug)(`Running action ${action}`);
await hre.run(action, taskArgs);
});
(0, config_1.subtask)(exports.TASK_ACCOUNTS_LIST, 'List local accounts').setAction(async (_, hre) => {
const accounts = (0, account_1.getSecureAccounts)(hre.config.paths.secureAccounts);
console.log('Managed accounts:');
for (const account of accounts) {
console.log(`> ${account.name} - 0x${JSON.parse(account.json).address}`);
}
});
(0, config_1.subtask)(exports.TASK_ACCOUNTS_ADD, 'Add a new account via mnemonic.')
.addOptionalParam('name', 'Name of the account')
.addOptionalParam('mnemonic', 'Mnemonic to derive the account from')
.addOptionalParam('password', 'Password used to encrypt the account')
.setAction(async (taskArgs, hre) => {
const name = await (0, ask_1.getStringOrAsk)('name', 'Enter account name', taskArgs.name);
(0, logger_1.logDebug)(`Account name: ${name}`);
if ((0, account_1.accountExists)(hre.config.paths.secureAccounts, name)) {
throw new error_1.SecureAccountsPluginError(`Account with name ${name} already exists!`);
}
const mnemonic = await (0, ask_1.getStringOrAsk)('mnemonic', 'Enter mnemonic (leave empty to create a random mnemonic)', taskArgs.mnemonic);
const password = await (0, ask_1.getPasswordOrAsk)(taskArgs.password);
const account = await (0, account_1.encryptAccount)(hre.config.paths.secureAccounts, name, password, mnemonic);
console.log(`Saved account ${name} to ${account.filename}`);
});
(0, config_1.subtask)(exports.TASK_ACCOUNTS_REMOVE, 'Remove an existing account').setAction(async (taskArgs, hre) => {
const account = await (0, ask_1.getAccountOrAsk)(taskArgs.name, hre.config.paths.secureAccounts);
(0, logger_1.logDebug)(`Attempting to delete ${account.name}...`);
const deleteAccount = await (0, ask_1.askForConfirmation)(`Are you sure you want to delete ${account.name}?`);
if (deleteAccount) {
fs_1.default.unlinkSync(account.filename);
console.log(`Deleted account ${account.name} from ${account.filename}`);
}
});
//# sourceMappingURL=tasks.js.map