Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions crates/icp-cli/src/commands/identity/account_id.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
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;

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)]
Expand All @@ -17,9 +27,13 @@ pub(crate) struct AccountIdArgs {
#[arg(long = "of-principal", conflicts_with = "identity")]
pub(crate) of_principal: Option<Principal>,

/// 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> {
Expand All @@ -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(())
}
5 changes: 4 additions & 1 deletion docs/guides/tokens-and-cycles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 13 additions & 3 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -840,15 +840,25 @@ 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]`

###### **Options:**

* `--identity <IDENTITY>` — The user identity to run this command as
* `--of-principal <OF_PRINCIPAL>` — Convert this Principal instead of the current identity's Principal
* `--of-subaccount <OF_SUBACCOUNT>` — Specify a subaccount. If absent, the ICRC-1 account will be omitted as it is just the principal
* `--of-subaccount <OF_SUBACCOUNT>` — Specify a subaccount
* `--format <FORMAT>` — Account identifier format to display

Default value: `ledger`

Possible values:
- `ledger`:
ICP ledger account identifier
- `icrc1`:
ICRC-1 account identifier




Expand Down
Loading