Skip to content

Commit b085597

Browse files
authored
chore: pick output format of account-id (#396)
1 parent 90596fa commit b085597

3 files changed

Lines changed: 56 additions & 22 deletions

File tree

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
use candid::Principal;
2-
use clap::Args;
2+
use clap::{Args, ValueEnum};
33
use ic_ledger_types::{AccountIdentifier, Subaccount};
44
use icp::context::Context;
55
use icrc_ledger_types::icrc1::account::Account;
66

77
use crate::commands::parsers::parse_subaccount;
88
use crate::options::IdentityOpt;
99

10-
/// Display the ICP ledger and ICRC-1 account identifiers for the current identity
10+
/// The account identifier format to display
11+
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
12+
pub(crate) enum OutputFormat {
13+
/// ICP ledger account identifier
14+
#[default]
15+
Ledger,
16+
/// ICRC-1 account identifier
17+
Icrc1,
18+
}
19+
20+
/// Display the ICP ledger or ICRC-1 account identifier for the current identity
1121
#[derive(Debug, Args)]
1222
pub(crate) struct AccountIdArgs {
1323
#[command(flatten)]
@@ -17,9 +27,13 @@ pub(crate) struct AccountIdArgs {
1727
#[arg(long = "of-principal", conflicts_with = "identity")]
1828
pub(crate) of_principal: Option<Principal>,
1929

20-
/// Specify a subaccount. If absent, the ICRC-1 account will be omitted as it is just the principal
30+
/// Specify a subaccount
2131
#[arg(long, value_parser = parse_subaccount)]
2232
pub(crate) of_subaccount: Option<[u8; 32]>,
33+
34+
/// Account identifier format to display
35+
#[arg(long, default_value = "ledger")]
36+
pub(crate) format: OutputFormat,
2337
}
2438

2539
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
3145
.map_err(|e| anyhow::anyhow!("failed to load identity principal: {e}"))?
3246
};
3347

34-
let account_id = AccountIdentifier::new(
35-
&principal,
36-
&args
37-
.of_subaccount
38-
.map(Subaccount)
39-
.unwrap_or(Subaccount([0; 32])),
40-
);
41-
42-
println!("ICP ledger: {account_id}");
43-
if args.of_subaccount.is_some() {
44-
let account = Account {
45-
owner: principal,
46-
subaccount: args.of_subaccount,
47-
};
48-
println!("ICRC-1: {account}");
48+
match args.format {
49+
OutputFormat::Ledger => {
50+
let account_id = AccountIdentifier::new(
51+
&principal,
52+
&args
53+
.of_subaccount
54+
.map(Subaccount)
55+
.unwrap_or(Subaccount([0; 32])),
56+
);
57+
println!("{account_id}");
58+
}
59+
OutputFormat::Icrc1 => {
60+
if let Some(subaccount) = args.of_subaccount {
61+
let account = Account {
62+
owner: principal,
63+
subaccount: Some(subaccount),
64+
};
65+
println!("{account}");
66+
} else {
67+
println!("{principal}");
68+
}
69+
}
4970
}
5071
Ok(())
5172
}

docs/guides/tokens-and-cycles.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,12 @@ The ICRC-1 account format works for both `icp token transfer` and `icp cycles tr
308308
```bash
309309
# Show account identifiers for a subaccount
310310
icp identity account-id --of-subaccount 1
311+
312+
# Show the icrc1 format
313+
icp identity account-id --of-subaccount 1 --format icrc1
311314
```
312315

313-
When a subaccount is specified, this prints both the ICP ledger account identifier and the ICRC-1 account format.
316+
You can choose to the output format as either `ledger` (the default) or `icrc1`.
314317

315318
## Fees and Safety
316319

docs/reference/cli.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ Manage your identities
825825

826826
###### **Subcommands:**
827827

828-
* `account-id` — Display the ICP ledger and ICRC-1 account identifiers for the current identity
828+
* `account-id` — Display the ICP ledger or ICRC-1 account identifier for the current identity
829829
* `default` — Display the currently selected identity
830830
* `delete` — Delete an identity
831831
* `export` — Print the PEM file for the identity
@@ -840,15 +840,25 @@ Manage your identities
840840

841841
## `icp identity account-id`
842842

843-
Display the ICP ledger and ICRC-1 account identifiers for the current identity
843+
Display the ICP ledger or ICRC-1 account identifier for the current identity
844844

845845
**Usage:** `icp identity account-id [OPTIONS]`
846846

847847
###### **Options:**
848848

849849
* `--identity <IDENTITY>` — The user identity to run this command as
850850
* `--of-principal <OF_PRINCIPAL>` — Convert this Principal instead of the current identity's Principal
851-
* `--of-subaccount <OF_SUBACCOUNT>` — Specify a subaccount. If absent, the ICRC-1 account will be omitted as it is just the principal
851+
* `--of-subaccount <OF_SUBACCOUNT>` — Specify a subaccount
852+
* `--format <FORMAT>` — Account identifier format to display
853+
854+
Default value: `ledger`
855+
856+
Possible values:
857+
- `ledger`:
858+
ICP ledger account identifier
859+
- `icrc1`:
860+
ICRC-1 account identifier
861+
852862

853863

854864

0 commit comments

Comments
 (0)