Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jul 25, 2023
1 parent be76184 commit 0433307
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
50 changes: 14 additions & 36 deletions bin/xcm-tools/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,9 @@ pub struct RemoteAccountCmd {
}

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

impl Into<[u8; 32]> for AccountWrapper {
fn into(self) -> [u8; 32] {
self.account
}
}

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
}
pub enum AccountWrapper {
SS58([u8; 32]),
H160([u8; 20]),
}

impl std::str::FromStr for AccountWrapper {
Expand All @@ -106,20 +85,19 @@ impl std::str::FromStr for AccountWrapper {
2 + pos,
))
} else {
let account = hex::decode(rest).expect("Ensured in previous check it's hex; QED");
if rest.len() == 40 {
let mut account = [0u8; 32];
account[0..20].copy_from_slice(&hex::decode(rest).unwrap());
Ok(AccountWrapper {
account,
is_32: false,
})
Ok(AccountWrapper::H160(
account
.try_into()
.expect("Ensured length in previous check; QED"),
))
} else if rest.len() == 64 {
let mut account = [0u8; 32];
account.copy_from_slice(&hex::decode(rest).unwrap());
Ok(AccountWrapper {
account,
is_32: true,
})
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())
}
Expand Down
35 changes: 19 additions & 16 deletions bin/xcm-tools/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,25 @@ pub fn run() -> Result<(), Error> {
.expect("infallible, short sequence");
}

if cmd.account_key.is_32_bytes() {
sender_multilocation
.append_with(X1(AccountId32 {
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.into(),
// network is not relevant for account derivation
network: None,
}))
.expect("infallible, short sequence");
match cmd.account_key {
AccountWrapper::SS58(id) => {
sender_multilocation
.append_with(X1(AccountId32 {
id,
// network is not relevant for account derivation
network: None,
}))
.expect("infallible, short sequence");
}
AccountWrapper::H160(key) => {
sender_multilocation
.append_with(X1(AccountKey20 {
key,
// network is not relevant for account derivation
network: None,
}))
.expect("infallible, short sequence");
}
}

let derived_acc =
Expand Down

0 comments on commit 0433307

Please sign in to comment.