11use candid:: Principal ;
2- use clap:: Args ;
2+ use clap:: { Args , ValueEnum } ;
33use ic_ledger_types:: { AccountIdentifier , Subaccount } ;
44use icp:: context:: Context ;
55use icrc_ledger_types:: icrc1:: account:: Account ;
66
77use crate :: commands:: parsers:: parse_subaccount;
88use 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 ) ]
1222pub ( 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
2539pub ( 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}
0 commit comments