diff --git a/bin/xcm-tools/src/cli.rs b/bin/xcm-tools/src/cli.rs index 979c81e603..1beb7313aa 100644 --- a/bin/xcm-tools/src/cli.rs +++ b/bin/xcm-tools/src/cli.rs @@ -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 diff --git a/bin/xcm-tools/src/command.rs b/bin/xcm-tools/src/command.rs index feff5593bd..c283fa404b 100644 --- a/bin/xcm-tools/src/command.rs +++ b/bin/xcm-tools/src/command.rs @@ -78,7 +78,7 @@ 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, })) @@ -86,7 +86,7 @@ pub fn run() -> Result<(), Error> { } 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, }))