Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jul 25, 2023
1 parent e8e6340 commit 8d4ca14
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
30 changes: 12 additions & 18 deletions bin/xcm-tools/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,27 @@ pub struct RemoteAccountCmd {
pub account_key: AccountWrapper,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct AccountWrapper {
account: [u8; 32],
is_32: bool,
}

impl AccountWrapper {
/// Get AccountId32 public key (SS58) or error if it is not 32 bytes long.
pub fn get_account_id_32(&self) -> Result<[u8; 32], &str> {
if self.is_32 {
Ok(self.account)
} else {
Err("Account is not 32 bytes long")
}
impl Into<[u8; 32]> for AccountWrapper {
fn into(self) -> [u8; 32] {
self.account
}
}

/// Get AccountKey20 public key (H160) or error if it is not 20 bytes long.
pub fn get_account_key_20(&self) -> Result<[u8; 20], &str> {
if !self.is_32 {
let mut account = [0u8; 20];
account.copy_from_slice(&self.account[0..20]);
Ok(account)
} else {
Err("Account is not 20 bytes long")
}
impl Into<[u8; 20]> for AccountWrapper {
fn into(self) -> [u8; 20] {
self.account[0..20]
.try_into()
.expect("Slice is of length 20; qed.")
}
}

impl AccountWrapper {
/// `true` if AccountId32, `false` if AccountKey20.
pub fn is_32_bytes(&self) -> bool {
self.is_32
Expand Down
4 changes: 2 additions & 2 deletions bin/xcm-tools/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ pub fn run() -> Result<(), Error> {
if cmd.account_key.is_32_bytes() {
sender_multilocation
.append_with(X1(AccountId32 {
id: cmd.account_key.get_account_id_32().unwrap(),
id: cmd.account_key.into(),
// network is not relevant for account derivation
network: None,
}))
.expect("infallible, short sequence");
} else {
sender_multilocation
.append_with(X1(AccountKey20 {
key: cmd.account_key.get_account_key_20().unwrap(),
key: cmd.account_key.into(),
// network is not relevant for account derivation
network: None,
}))
Expand Down

0 comments on commit 8d4ca14

Please sign in to comment.