Skip to content
Open
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
3 changes: 1 addition & 2 deletions src/commands/config/getSetReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export class ConfigActions extends BaseAction {
return;
}

delete config[key];
this.writeConfig(key, undefined);
this.removeConfig(key);
this.succeedSpinner(`Configuration successfully reset`);
}
}
2 changes: 1 addition & 1 deletion src/commands/network/setNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class NetworkActions extends BaseAction {
async setNetwork(networkName?: string): Promise<void> {
const entries = this.getNetworkEntries();

if (networkName || networkName === "") {
if (networkName) {
const selectedNetwork = entries.find(n =>
n.alias === networkName || (n.type === "built-in" && n.name === networkName),
);
Expand Down
20 changes: 17 additions & 3 deletions src/lib/actions/BaseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {createClient, createAccount} from "genlayer-js";
import {localnet, studionet, testnetAsimov, testnetBradbury} from "genlayer-js/chains";
import type {GenLayerClient, GenLayerChain, Hash, Address, Account} from "genlayer-js/types";
import {
import { ethers } from "ethers";
import { writeFileSync, existsSync, readFileSync } from "fs";
applyCustomNetworkProfile,
CUSTOM_NETWORKS_CONFIG_KEY,
normalizeCustomNetworks,
Expand Down Expand Up @@ -58,8 +60,6 @@ export function resolveNetwork(stored: string | undefined, customNetworks?: Cust
throw new Error(`Unknown network: ${stored}`);
}
}
import { ethers } from "ethers";
import { writeFileSync, existsSync, readFileSync } from "fs";

export class BaseAction extends ConfigFileManager {
private static readonly DEFAULT_ACCOUNT_NAME = "default";
Expand Down Expand Up @@ -90,9 +90,19 @@ export class BaseAction extends ConfigFileManager {
const wallet = await ethers.Wallet.fromEncryptedJson(keystoreJson, password);

return wallet.privateKey;
} catch (error) {
} catch (error: any) {
// Re-throw non-password errors (corrupted keystore, malformed JSON, etc.)
const isPasswordError =
error?.message?.toLowerCase().includes("password") ||
error?.message?.toLowerCase().includes("decrypt") ||
error?.code === "INVALID_ARGUMENT";
if (!isPasswordError) {
throw new Error(`Failed to decrypt keystore: ${error?.message ?? error}`);
}

if (attempt >= BaseAction.MAX_PASSWORD_ATTEMPTS) {
this.failSpinner(`Maximum password attempts exceeded (${BaseAction.MAX_PASSWORD_ATTEMPTS}/${BaseAction.MAX_PASSWORD_ATTEMPTS}).`);
return "";
}
return await this.decryptKeystore(keystoreJson, attempt + 1);
}
Expand Down Expand Up @@ -149,6 +159,10 @@ export class BaseAction extends ConfigFileManager {
if (!existsSync(keystorePath)) {
await this.confirmPrompt(`Account '${accountName}' not found. Would you like to create it?`);
decryptedPrivateKey = await this.createKeypairByName(accountName, false);
if (!existsSync(keystorePath)) {
this.failSpinner(`Failed to create keystore file for account '${accountName}'. Check disk space and permissions.`);
return;
}
}

keystoreJson = readFileSync(keystorePath, "utf-8");
Expand Down
Loading