1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This file is part of Astar.

// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

/// Astar XCM tools.
#[derive(Debug, clap::Parser)]
#[clap(subcommand_required = true)]
pub struct Cli {
    /// Possible subcommand with parameters.
    #[clap(subcommand)]
    pub subcommand: Option<Subcommand>,
}

/// Possible subcommands of the main binary.
#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
    /// Prints relay-chain SS58 account Id
    RelayChainAccount,
    /// Prints parachains sovereign SS58 account Id.
    SovereignAccount(SovereignAccountCmd),
    /// Prints AssetId for desired parachain asset.
    AssetId(AssetIdCmd),
    /// Prints derived remote SS58 account for the derived multilocation.
    RemoteAccount(RemoteAccountCmd),
}

/// Helper that prints AccountId of parachain.
#[derive(Debug, clap::Parser)]
pub struct SovereignAccountCmd {
    /// Print address for sibling parachain [child by default].
    #[clap(short)]
    pub sibling: bool,

    /// Target ParaId.
    pub parachain_id: u32,
}

/// Helper that prints AssetId for sibling parachain asset.
#[derive(Debug, clap::Parser)]
pub struct AssetIdCmd {
    /// External AssetId [relay by default].
    #[clap(default_value = "340282366920938463463374607431768211455")]
    pub asset_id: u128,
}

/// Helper that prints the derived AccountId32 value for the multilocation.
#[derive(Debug, clap::Parser)]
pub struct RemoteAccountCmd {
    /// Parachain id in case sender is from a sibling parachain.
    #[clap(short, long, default_value = None)]
    pub parachain_id: Option<u32>,
    /// Public key (SS58 or H160) in hex format. Must be either 32 or 20 bytes long.
    #[clap(short, long)]
    pub account_key: AccountWrapper,
}

#[derive(Debug, Clone, Copy)]
pub enum AccountWrapper {
    SS58([u8; 32]),
    H160([u8; 20]),
}

impl std::str::FromStr for AccountWrapper {
    type Err = String;

    fn from_str(account_pub_key: &str) -> Result<Self, Self::Err> {
        if let Some(rest) = account_pub_key.strip_prefix("0x") {
            if let Some(pos) = rest.chars().position(|c| !c.is_ascii_hexdigit()) {
                Err(format!(
					"Expected account public key in hex format, found illegal hex character at position: {}",
					2 + pos,
				))
            } else {
                let account = hex::decode(rest).expect("Ensured in previous check it's hex; QED");
                if rest.len() == 40 {
                    Ok(AccountWrapper::H160(
                        account
                            .try_into()
                            .expect("Ensured length in previous check; QED"),
                    ))
                } else if rest.len() == 64 {
                    Ok(AccountWrapper::SS58(
                        account
                            .try_into()
                            .expect("Ensured length in previous check; QED"),
                    ))
                } else {
                    Err("Account key should be 20 or 32 bytes long".into())
                }
            }
        } else {
            Err("Account key should start with '0x'".into())
        }
    }
}