Skip to content

Commit

Permalink
add: convert-address command & unknown address option
Browse files Browse the repository at this point in the history
  • Loading branch information
pLabarta committed Jan 17, 2024
1 parent 4d48d2e commit 2d1aa21
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -24,6 +25,7 @@ program
.addCommand(makeBalanceCommand())
.addCommand(makeBondCommand())
.addCommand(makeChillCommand())
.addCommand(makeConvertAddressCommand())
.addCommand(makeDistributeRewardsCommand())
.addCommand(makeNewSeedCommand())
.addCommand(makeRotateKeysCommand())
Expand Down
26 changes: 26 additions & 0 deletions cli/src/commands/convertAddress.ts
Original file line number Diff line number Diff line change
@@ -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);
}
27 changes: 27 additions & 0 deletions cli/src/commands/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down

0 comments on commit 2d1aa21

Please sign in to comment.