Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"dotenv": "^17.0.0",
"ethers": "^6.13.4",
"fs-extra": "^11.3.0",
"genlayer-js": "^0.18.9",
"genlayer-js": "^0.18.10",
"inquirer": "^12.0.0",
"keytar": "^7.9.0",
"node-fetch": "^3.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/account/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface CreateAccountOptions {
name: string;
overwrite: boolean;
setActive?: boolean;
password?: string;
}

export class CreateAccountAction extends BaseAction {
Expand All @@ -14,7 +15,7 @@ export class CreateAccountAction extends BaseAction {
async execute(options: CreateAccountOptions): Promise<void> {
try {
this.startSpinner(`Creating account '${options.name}'...`);
await this.createKeypairByName(options.name, options.overwrite);
await this.createKeypairByName(options.name, options.overwrite, options.password);

if (options.setActive !== false) {
this.setActiveAccount(options.name);
Expand Down
7 changes: 5 additions & 2 deletions src/commands/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function initializeAccountCommands(program: Command) {
.command("create")
.description("Create a new account with encrypted keystore")
.requiredOption("--name <name>", "Name for the account")
.option("--password <password>", "Password for the keystore (skips interactive prompt)")
.option("--overwrite", "Overwrite existing account", false)
.option("--no-set-active", "Do not set as active account")
.action(async (options: CreateAccountOptions) => {
Expand Down Expand Up @@ -99,15 +100,17 @@ export function initializeAccountCommands(program: Command) {
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--account <name>", "Account to send from")
.action(async (to: string, amount: string, options: {rpc?: string; network?: string; account?: string}) => {
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.action(async (to: string, amount: string, options: {rpc?: string; network?: string; account?: string; password?: string}) => {
const sendAction = new SendAction();
await sendAction.execute({to, amount, rpc: options.rpc, network: options.network, account: options.account});
await sendAction.execute({to, amount, rpc: options.rpc, network: options.network, account: options.account, password: options.password});
});

accountCommand
.command("unlock")
.description("Unlock account by caching private key in OS keychain")
.option("--account <name>", "Account to unlock")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.action(async (options: UnlockAccountOptions) => {
const unlockAction = new UnlockAccountAction();
await unlockAction.execute(options);
Expand Down
12 changes: 9 additions & 3 deletions src/commands/account/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SendOptions {
rpc?: string;
network?: string;
account?: string;
password?: string;
}

export class SendAction extends BaseAction {
Expand Down Expand Up @@ -75,9 +76,14 @@ export class SendAction extends BaseAction {
if (cachedKey) {
privateKey = cachedKey;
} else {
this.stopSpinner();
const password = await this.promptPassword(`Enter password to unlock account '${accountName}':`);
this.startSpinner("Preparing transfer...");
let password: string;
if (options.password) {
password = options.password;
} else {
this.stopSpinner();
password = await this.promptPassword(`Enter password to unlock account '${accountName}':`);
this.startSpinner("Preparing transfer...");
}
const wallet = await ethers.Wallet.fromEncryptedJson(keystoreJson, password);
privateKey = wallet.privateKey;
}
Expand Down
11 changes: 8 additions & 3 deletions src/commands/account/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ethers} from "ethers";

export interface UnlockAccountOptions {
account?: string;
password?: string;
}

export class UnlockAccountAction extends BaseAction {
Expand Down Expand Up @@ -36,10 +37,14 @@ export class UnlockAccountAction extends BaseAction {
return;
}

this.stopSpinner();

try {
const password = await this.promptPassword(`Enter password to unlock '${accountName}':`);
let password: string;
if (options?.password) {
password = options.password;
} else {
this.stopSpinner();
password = await this.promptPassword(`Enter password to unlock '${accountName}':`);
}
const wallet = await ethers.Wallet.fromEncryptedJson(keystoreJson, password);

await this.keychainManager.storePrivateKey(accountName, wallet.privateKey);
Expand Down
18 changes: 15 additions & 3 deletions src/commands/staking/StakingAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export interface StakingConfig {
stakingAddress?: string;
network?: string;
account?: string;
password?: string;
}

export class StakingAction extends BaseAction {
private _stakingClient: GenLayerClient<GenLayerChain> | null = null;
private _passwordOverride: string | undefined;

constructor() {
super();
Expand All @@ -53,6 +55,9 @@ export class StakingAction extends BaseAction {
if (config.account) {
this.accountOverride = config.account;
}
if (config.password) {
this._passwordOverride = config.password;
}

const network = this.getNetwork(config);

Expand Down Expand Up @@ -140,9 +145,13 @@ export class StakingAction extends BaseAction {
}
}

// Stop spinner before prompting for password
this.stopSpinner();
const password = await this.promptPassword(`Enter password to unlock account '${accountName}':`);
let password: string;
if (this._passwordOverride) {
password = this._passwordOverride;
} else {
this.stopSpinner();
password = await this.promptPassword(`Enter password to unlock account '${accountName}':`);
}
this.startSpinner("Unlocking account...");

const wallet = await ethers.Wallet.fromEncryptedJson(keystoreJson, password);
Expand Down Expand Up @@ -180,6 +189,9 @@ export class StakingAction extends BaseAction {
if (config.account) {
this.accountOverride = config.account;
}
if (config.password) {
this._passwordOverride = config.password;
}

const network = this.getNetwork(config);
const rpcUrl = config.rpc || network.rpcUrls.default.http[0];
Expand Down
11 changes: 11 additions & 0 deletions src/commands/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function initializeStakingCommands(program: Command) {
.requiredOption("--amount <amount>", "Amount to stake (in wei or with 'eth'/'gen' suffix, e.g., '42000gen')")
.option("--operator <address>", "Operator address (defaults to signer)")
.option("--account <name>", "Account to use")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--staking-address <address>", "Staking contract address (overrides chain config)")
Expand All @@ -51,6 +52,7 @@ export function initializeStakingCommands(program: Command) {
.option("--validator <address>", "Validator wallet contract address (deprecated, use positional arg)")
.requiredOption("--amount <amount>", "Amount to deposit (in wei or with 'eth'/'gen' suffix)")
.option("--account <name>", "Account to use (must be validator owner)")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.action(async (validatorArg: string | undefined, options: ValidatorDepositOptions) => {
Expand All @@ -69,6 +71,7 @@ export function initializeStakingCommands(program: Command) {
.option("--validator <address>", "Validator wallet contract address (deprecated, use positional arg)")
.requiredOption("--shares <shares>", "Number of shares to withdraw")
.option("--account <name>", "Account to use (must be validator owner)")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.action(async (validatorArg: string | undefined, options: ValidatorExitOptions) => {
Expand All @@ -86,6 +89,7 @@ export function initializeStakingCommands(program: Command) {
.description("Claim validator withdrawals after unbonding period")
.option("--validator <address>", "Validator wallet contract address (deprecated, use positional arg)")
.option("--account <name>", "Account to use")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.action(async (validatorArg: string | undefined, options: ValidatorClaimOptions) => {
Expand All @@ -103,6 +107,7 @@ export function initializeStakingCommands(program: Command) {
.description("Prime a validator to prepare their stake record for the next epoch")
.option("--validator <address>", "Validator address to prime (deprecated, use positional arg)")
.option("--account <name>", "Account to use")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--staking-address <address>", "Staking contract address (overrides chain config)")
Expand All @@ -120,6 +125,7 @@ export function initializeStakingCommands(program: Command) {
.command("prime-all")
.description("Prime all validators that need priming")
.option("--account <name>", "Account to use (pays gas)")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--staking-address <address>", "Staking contract address (overrides chain config)")
Expand All @@ -134,6 +140,7 @@ export function initializeStakingCommands(program: Command) {
.option("--validator <address>", "Validator wallet address (deprecated, use positional arg)")
.option("--operator <address>", "New operator address (deprecated, use positional arg)")
.option("--account <name>", "Account to use (must be validator owner)")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.action(async (validatorArg: string | undefined, operatorArg: string | undefined, options: SetOperatorOptions) => {
Expand Down Expand Up @@ -161,6 +168,7 @@ export function initializeStakingCommands(program: Command) {
.option("--github <handle>", "GitHub handle")
.option("--extra-cid <cid>", "Extra data as IPFS CID or hex bytes (0x...)")
.option("--account <name>", "Account to use (must be validator operator)")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.action(async (validatorArg: string | undefined, options: SetIdentityOptions) => {
Expand All @@ -180,6 +188,7 @@ export function initializeStakingCommands(program: Command) {
.option("--validator <address>", "Validator address to delegate to (deprecated, use positional arg)")
.requiredOption("--amount <amount>", "Amount to stake (in wei or with 'eth'/'gen' suffix)")
.option("--account <name>", "Account to use")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--staking-address <address>", "Staking contract address (overrides chain config)")
Expand All @@ -199,6 +208,7 @@ export function initializeStakingCommands(program: Command) {
.option("--validator <address>", "Validator address to exit from (deprecated, use positional arg)")
.requiredOption("--shares <shares>", "Number of shares to withdraw")
.option("--account <name>", "Account to use")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--staking-address <address>", "Staking contract address (overrides chain config)")
Expand All @@ -218,6 +228,7 @@ export function initializeStakingCommands(program: Command) {
.option("--validator <address>", "Validator address (deprecated, use positional arg)")
.option("--delegator <address>", "Delegator address (defaults to signer)")
.option("--account <name>", "Account to use")
.option("--password <password>", "Password to unlock account (skips interactive prompt)")
.option("--network <network>", "Network to use (localnet, testnet-asimov)")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--staking-address <address>", "Staking contract address (overrides chain config)")
Expand Down
16 changes: 10 additions & 6 deletions src/lib/actions/BaseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class BaseAction extends ConfigFileManager {
return keystoreData.address as Address;
}

protected async createKeypairByName(accountName: string, overwrite: boolean): Promise<string> {
protected async createKeypairByName(accountName: string, overwrite: boolean, passwordInput?: string): Promise<string> {
const keystorePath = this.getKeystorePath(accountName);
this.stopSpinner();

Expand All @@ -181,11 +181,15 @@ export class BaseAction extends ConfigFileManager {

const wallet = ethers.Wallet.createRandom();

const password = await this.promptPassword("Enter a password to encrypt your keystore (minimum 8 characters):");
const confirmPassword = await this.promptPassword("Confirm password:");

if (password !== confirmPassword) {
this.failSpinner("Passwords do not match");
let password: string;
if (passwordInput) {
password = passwordInput;
} else {
password = await this.promptPassword("Enter a password to encrypt your keystore (minimum 8 characters):");
const confirmPassword = await this.promptPassword("Confirm password:");
if (password !== confirmPassword) {
this.failSpinner("Passwords do not match");
}
}

if (password.length < BaseAction.MIN_PASSWORD_LENGTH) {
Expand Down
2 changes: 1 addition & 1 deletion tests/actions/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("CreateAccountAction", () => {
await createAction.execute(options);

expect(createAction["startSpinner"]).toHaveBeenCalledWith("Creating account 'main'...");
expect(createAction["createKeypairByName"]).toHaveBeenCalledWith("main", false);
expect(createAction["createKeypairByName"]).toHaveBeenCalledWith("main", false, undefined);
expect(createAction["setActiveAccount"]).toHaveBeenCalledWith("main");
expect(createAction["succeedSpinner"]).toHaveBeenCalledWith(
`Account 'main' created at: ${mockKeystorePath}`,
Expand Down