All files / src/command wallet.js

100% Statements 15/15
100% Branches 2/2
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75          3x                                                 7x                                     2x 2x 2x 2x 2x   1x   2x   1x   1x   1x 1x   1x   1x            
import BaseSubCommand from './baseSubCommand.js';
import { commonGlobalOptionValidatorDesc } from '../utils/constants.js';
import { getWallet } from '../utils/wallet.js';
import { logger } from '../utils/myLogger.js';
 
const walletCommandValidatorDesc = {
  ...commonGlobalOptionValidatorDesc,
  endpoint: {
    ...commonGlobalOptionValidatorDesc.endpoint,
    required: false
  },
  password: {
    ...commonGlobalOptionValidatorDesc.password,
    required: true
  },
  account: {
    ...commonGlobalOptionValidatorDesc.account,
    required: true
  }
};
/**
 * @typedef {import('commander').Command} Command
 * @typedef {import('../../types/rc/index.js').default} Registry
 */
class WalletCommand extends BaseSubCommand {
  /**
   * Constructs a new WalletCommand instance.
   * @param {Registry} rc - The Registry instance for configuration.
   */
  constructor(rc) {
    super(
      'wallet',
      [],
      'Show wallet details which include private key, address, public key and mnemonic',
      [],
      ['-a <account> -p <password>', ''],
      rc,
      walletCommandValidatorDesc
    );
  }
 
  /**
   * Runs the wallet command logic.
   * @param {Command} commander - The Commander instance for command handling.
   * @param {...any} args - Additional arguments for command execution.
   * @returns {Promise<void>} A promise that resolves when the command execution is complete.
   */
  async run(commander, ...args) {
    // @ts-ignore
    const { options } = await super.run(commander, ...args);
    const { datadir, account, password } = options;
    try {
      const wallet = getWallet(datadir, account, password);
      if (wallet.mnemonic) {
        // @ts-ignore
        logger.info(`Mnemonic            : ${wallet.mnemonic}`);
      }
      wallet.publicKey = wallet.keyPair.getPublic().encode('hex');
      // @ts-ignore
      logger.info(`Private Key         : ${wallet.privateKey}`);
      // @ts-ignore
      logger.info(`Public Key          : ${wallet.publicKey}`);
      // @ts-ignore
      logger.info(`Address             : ${wallet.address}`);
      this.oraInstance.succeed('Succeed!');
    } catch (e) {
      this.oraInstance.fail('Failed!');
      // @ts-ignore
      logger.error(e);
    }
  }
}
 
export default WalletCommand;