diff --git a/crates/icp-cli/src/commands/identity/account_id.rs b/crates/icp-cli/src/commands/identity/account_id.rs index b7007fde..7e458a34 100644 --- a/crates/icp-cli/src/commands/identity/account_id.rs +++ b/crates/icp-cli/src/commands/identity/account_id.rs @@ -1,5 +1,5 @@ use candid::Principal; -use clap::Args; +use clap::{Args, ValueEnum}; use ic_ledger_types::{AccountIdentifier, Subaccount}; use icp::context::Context; use icrc_ledger_types::icrc1::account::Account; @@ -7,7 +7,17 @@ use icrc_ledger_types::icrc1::account::Account; use crate::commands::parsers::parse_subaccount; use crate::options::IdentityOpt; -/// Display the ICP ledger and ICRC-1 account identifiers for the current identity +/// The account identifier format to display +#[derive(Clone, Copy, Debug, Default, ValueEnum)] +pub(crate) enum OutputFormat { + /// ICP ledger account identifier + #[default] + Ledger, + /// ICRC-1 account identifier + Icrc1, +} + +/// Display the ICP ledger or ICRC-1 account identifier for the current identity #[derive(Debug, Args)] pub(crate) struct AccountIdArgs { #[command(flatten)] @@ -17,9 +27,13 @@ pub(crate) struct AccountIdArgs { #[arg(long = "of-principal", conflicts_with = "identity")] pub(crate) of_principal: Option, - /// Specify a subaccount. If absent, the ICRC-1 account will be omitted as it is just the principal + /// Specify a subaccount #[arg(long, value_parser = parse_subaccount)] pub(crate) of_subaccount: Option<[u8; 32]>, + + /// Account identifier format to display + #[arg(long, default_value = "ledger")] + pub(crate) format: OutputFormat, } pub(crate) async fn exec(ctx: &Context, args: &AccountIdArgs) -> Result<(), anyhow::Error> { @@ -31,21 +45,28 @@ pub(crate) async fn exec(ctx: &Context, args: &AccountIdArgs) -> Result<(), anyh .map_err(|e| anyhow::anyhow!("failed to load identity principal: {e}"))? }; - let account_id = AccountIdentifier::new( - &principal, - &args - .of_subaccount - .map(Subaccount) - .unwrap_or(Subaccount([0; 32])), - ); - - println!("ICP ledger: {account_id}"); - if args.of_subaccount.is_some() { - let account = Account { - owner: principal, - subaccount: args.of_subaccount, - }; - println!("ICRC-1: {account}"); + match args.format { + OutputFormat::Ledger => { + let account_id = AccountIdentifier::new( + &principal, + &args + .of_subaccount + .map(Subaccount) + .unwrap_or(Subaccount([0; 32])), + ); + println!("{account_id}"); + } + OutputFormat::Icrc1 => { + if let Some(subaccount) = args.of_subaccount { + let account = Account { + owner: principal, + subaccount: Some(subaccount), + }; + println!("{account}"); + } else { + println!("{principal}"); + } + } } Ok(()) } diff --git a/docs/guides/tokens-and-cycles.md b/docs/guides/tokens-and-cycles.md index c5fe100e..c63abb67 100644 --- a/docs/guides/tokens-and-cycles.md +++ b/docs/guides/tokens-and-cycles.md @@ -308,9 +308,12 @@ The ICRC-1 account format works for both `icp token transfer` and `icp cycles tr ```bash # Show account identifiers for a subaccount icp identity account-id --of-subaccount 1 + +# Show the icrc1 format +icp identity account-id --of-subaccount 1 --format icrc1 ``` -When a subaccount is specified, this prints both the ICP ledger account identifier and the ICRC-1 account format. +You can choose to the output format as either `ledger` (the default) or `icrc1`. ## Fees and Safety diff --git a/docs/reference/cli.md b/docs/reference/cli.md index b22106d9..31f341a3 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -825,7 +825,7 @@ Manage your identities ###### **Subcommands:** -* `account-id` — Display the ICP ledger and ICRC-1 account identifiers for the current identity +* `account-id` — Display the ICP ledger or ICRC-1 account identifier for the current identity * `default` — Display the currently selected identity * `delete` — Delete an identity * `export` — Print the PEM file for the identity @@ -840,7 +840,7 @@ Manage your identities ## `icp identity account-id` -Display the ICP ledger and ICRC-1 account identifiers for the current identity +Display the ICP ledger or ICRC-1 account identifier for the current identity **Usage:** `icp identity account-id [OPTIONS]` @@ -848,7 +848,17 @@ Display the ICP ledger and ICRC-1 account identifiers for the current identity * `--identity ` — The user identity to run this command as * `--of-principal ` — Convert this Principal instead of the current identity's Principal -* `--of-subaccount ` — Specify a subaccount. If absent, the ICRC-1 account will be omitted as it is just the principal +* `--of-subaccount ` — Specify a subaccount +* `--format ` — Account identifier format to display + + Default value: `ledger` + + Possible values: + - `ledger`: + ICP ledger account identifier + - `icrc1`: + ICRC-1 account identifier +