From 2d1aa215a311db62600f6dba54eb29b475ab04c3 Mon Sep 17 00:00:00 2001 From: Pablo Labarta Date: Wed, 17 Jan 2024 11:27:19 -0300 Subject: [PATCH] add: convert-address command & unknown address option --- cli/src/cli.ts | 2 ++ cli/src/commands/convertAddress.ts | 26 ++++++++++++++++++++++++++ cli/src/commands/options.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 cli/src/commands/convertAddress.ts diff --git a/cli/src/cli.ts b/cli/src/cli.ts index 1dee8b226..df4ce3eb3 100644 --- a/cli/src/cli.ts +++ b/cli/src/cli.ts @@ -16,6 +16,7 @@ import { makeWithdrawUnbondedCommand } from './commands/staking/withdraw'; import { makeWizardCommand } from './commands/staking/wizard'; import { makeEvmCommand } from './commands/evm'; import { urlOption } from './commands/options'; +import { makeConvertAddressCommand } from './commands/convertAddress'; const program = new Command(); @@ -24,6 +25,7 @@ program .addCommand(makeBalanceCommand()) .addCommand(makeBondCommand()) .addCommand(makeChillCommand()) + .addCommand(makeConvertAddressCommand()) .addCommand(makeDistributeRewardsCommand()) .addCommand(makeNewSeedCommand()) .addCommand(makeRotateKeysCommand()) diff --git a/cli/src/commands/convertAddress.ts b/cli/src/commands/convertAddress.ts new file mode 100644 index 000000000..ca9351c1c --- /dev/null +++ b/cli/src/commands/convertAddress.ts @@ -0,0 +1,26 @@ +import { Command, OptionValues } from 'commander'; +import { substrateAddressToEvmAddress, evmAddressToSubstrateAddress } from '../lib/evm/address'; +import { ValidatedAddress, unknownAddressOption } from './options'; + +export function makeConvertAddressCommand() { + const cmd = new Command('convert-address'); + cmd.description('Get the associated EVM/Substrate address for a Substrate or EVM account'); + cmd.addOption(unknownAddressOption.makeOptionMandatory()); + cmd.action(convertAddressAction); + return cmd; +} + +function convertAddressAction (options: OptionValues) +{ + const address = options.address as ValidatedAddress; + const type = address.type; + if (type === 'EVM') { + console.log(`AssociatedSubstrate address: ${evmAddressToSubstrateAddress(address.address)}`); + } else if (type === 'Substrate') { + console.log(`Associated EVM address: ${substrateAddressToEvmAddress(address.address)}`); + } else { + console.error('Invalid address type'); + process.exit(1); + } + process.exit(0); +} diff --git a/cli/src/commands/options.ts b/cli/src/commands/options.ts index 9695ec79c..1243f24f2 100644 --- a/cli/src/commands/options.ts +++ b/cli/src/commands/options.ts @@ -9,12 +9,20 @@ import { BN } from '..'; // Connection export const urlOption = new Option('-u, --url [url]', 'URL of the node to connect to').default('ws://127.0.0.1:9944'); + + // Addresses +export interface ValidatedAddress { + address: string; + type: 'Substrate' | 'EVM'; +} + export const evmAddressOption = new Option('--evm-address [address]', 'Specify EVM address').argParser(parseEVMAddress); export const substrateAddressOption = new Option( '--substrate-address [address]', 'Specify Substrate address', ).argParser(parseSubstrateAddress); +export const unknownAddressOption = new Option('--address [address]', 'Specify address').argParser(parseAddress); // Address parsing export function parseEVMAddress(value: string): string { if (isAddress(value)) { @@ -32,6 +40,25 @@ export function parseSubstrateAddress(value: string): string { return value; } +export function parseAddress(value: string): ValidatedAddress { + // Parsed has to be one of EVM or Substrate addresses + try { + return { + address: parseEVMAddress(value), + type: 'EVM' + } + } catch { + try { + return { + address: parseSubstrateAddress(value), + type: 'Substrate' + } + } catch { + throw new InvalidArgumentError('Not a valid Substrate or EVM address.'); + } + } +} + // Amounts export const amountOption = new Option('--amount [amount]', 'CTC amount').argParser(parseAmount); // Amount parsing