@elephant-xyz/cli
Version: 
CLI tool for Elephant Network
53 lines • 2.32 kB
JavaScript
import { readFileSync } from 'fs';
import { Wallet, isKeystoreJson, decryptKeystoreJson, decryptKeystoreJsonSync, } from 'ethers';
import { logger } from '../utils/logger.js';
export class EncryptedWalletService {
    static readAndValidateKeystoreFile(keystoreJsonPath) {
        logger.technical(`Loading encrypted wallet from: ${keystoreJsonPath}`);
        let jsonContent;
        try {
            jsonContent = readFileSync(keystoreJsonPath, 'utf-8');
        }
        catch (error) {
            const errorMsg = `Failed to read keystore JSON file: ${error instanceof Error ? error.message : String(error)}`;
            logger.error(errorMsg);
            throw new Error(errorMsg);
        }
        if (!isKeystoreJson(jsonContent)) {
            const errorMsg = 'Invalid keystore JSON format';
            logger.error(errorMsg);
            throw new Error(errorMsg);
        }
        return jsonContent;
    }
    static async loadWalletFromEncryptedJson(options) {
        const jsonContent = this.readAndValidateKeystoreFile(options.keystoreJsonPath);
        try {
            const account = await decryptKeystoreJson(jsonContent, options.password);
            logger.success('Successfully decrypted wallet from keystore JSON');
            return new Wallet(account.privateKey);
        }
        catch (error) {
            const errorMsg = `Failed to decrypt keystore JSON: ${error instanceof Error ? error.message : String(error)}`;
            logger.error(errorMsg);
            throw new Error(errorMsg);
        }
    }
    static loadWalletFromEncryptedJsonSync(options) {
        const jsonContent = this.readAndValidateKeystoreFile(options.keystoreJsonPath);
        try {
            const account = decryptKeystoreJsonSync(jsonContent, options.password);
            logger.success('Successfully decrypted wallet from keystore JSON');
            return new Wallet(account.privateKey);
        }
        catch (error) {
            const errorMsg = `Failed to decrypt keystore JSON: ${error instanceof Error ? error.message : String(error)}`;
            logger.error(errorMsg);
            throw new Error(errorMsg);
        }
    }
    static validateKeystoreJson(jsonContent) {
        return isKeystoreJson(jsonContent);
    }
}
//# sourceMappingURL=encrypted-wallet.service.js.map