Skip to content

Commit

Permalink
move axelar utility to axelar
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 18, 2024
1 parent 90c5a1d commit cd7c3d0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 61 deletions.
58 changes: 0 additions & 58 deletions libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,42 +70,6 @@ pub fn set_block_number_timestamp<T>(
pallet_timestamp::Pallet::<T>::set_timestamp(timestamp);
}

pub fn decode_var_source<const EXPECTED_SOURCE_ADDRESS_SIZE: usize>(
source_address: &[u8],
) -> Option<[u8; EXPECTED_SOURCE_ADDRESS_SIZE]> {
const HEX_PREFIX: &str = "0x";

let mut address = [0u8; EXPECTED_SOURCE_ADDRESS_SIZE];

if source_address.len() == EXPECTED_SOURCE_ADDRESS_SIZE {
address.copy_from_slice(source_address);
return Some(address);
}

let try_bytes = match sp_std::str::from_utf8(source_address) {
Ok(res) => res.as_bytes(),
Err(_) => source_address,
};

// Attempt to hex decode source address.
let bytes = match hex::decode(try_bytes) {
Ok(res) => Some(res),
Err(_) => {
// Strip 0x prefix.
let res = try_bytes.strip_prefix(HEX_PREFIX.as_bytes())?;

hex::decode(res).ok()
}
}?;

if bytes.len() == EXPECTED_SOURCE_ADDRESS_SIZE {
address.copy_from_slice(bytes.as_slice());
Some(address)
} else {
None
}
}

pub mod math {
use sp_arithmetic::{
traits::{BaseArithmetic, EnsureFixedPointNumber},
Expand Down Expand Up @@ -233,28 +197,6 @@ pub mod math {
mod tests {
use super::*;

mod get_source_address_bytes {
const EXPECTED: usize = 20;
use super::*;

#[test]
fn get_source_address_bytes_works() {
let hash = [1u8; 20];

decode_var_source::<EXPECTED>(&hash).expect("address bytes from H160 works");

let str = String::from("d47ed02acbbb66ee8a3fe0275bd98add0aa607c3");

decode_var_source::<EXPECTED>(str.as_bytes())
.expect("address bytes from un-prefixed hex works");

let str = String::from("0xd47ed02acbbb66ee8a3fe0275bd98add0aa607c3");

decode_var_source::<EXPECTED>(str.as_bytes())
.expect("address bytes from prefixed hex works");
}
}

mod vec_to_fixed_array {
use super::*;

Expand Down
66 changes: 63 additions & 3 deletions pallets/axelar-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ pub mod pallet {

match config.domain {
DomainConfig::Evm(EvmConfig { chain_id, .. }) => {
let source_address_bytes =
cfg_utils::decode_var_source::<EVM_ADDRESS_LEN>(source_address)
.ok_or(Error::<T>::InvalidSourceAddress)?;
let source_address_bytes = decode_var_source::<EVM_ADDRESS_LEN>(source_address)
.ok_or(Error::<T>::InvalidSourceAddress)?;

T::Receiver::receive(
AxelarId::Evm(chain_id).into(),
Expand Down Expand Up @@ -426,3 +425,64 @@ pub fn wrap_into_axelar_msg(

Ok(encoded_axelar_contract)
}

/// Decodes the source address which can be:
/// - a 20 bytes array
/// - an hexadecimal character secuence (40 characters)
/// - an hexadecimal character secuence (40 characters) with 0x prefix
pub fn decode_var_source<const EXPECTED_SOURCE_ADDRESS_SIZE: usize>(
source_address: &[u8],
) -> Option<[u8; EXPECTED_SOURCE_ADDRESS_SIZE]> {
const HEX_PREFIX: &str = "0x";

let mut address = [0u8; EXPECTED_SOURCE_ADDRESS_SIZE];

if source_address.len() == EXPECTED_SOURCE_ADDRESS_SIZE {
address.copy_from_slice(source_address);
return Some(address);
}

let try_bytes = match sp_std::str::from_utf8(source_address) {
Ok(res) => res.as_bytes(),
Err(_) => source_address,
};

// Attempt to hex decode source address.
let bytes = match hex::decode(try_bytes) {
Ok(res) => Some(res),
Err(_) => {
// Strip 0x prefix.
let res = try_bytes.strip_prefix(HEX_PREFIX.as_bytes())?;

hex::decode(res).ok()
}
}?;

if bytes.len() == EXPECTED_SOURCE_ADDRESS_SIZE {
address.copy_from_slice(bytes.as_slice());
Some(address)
} else {
None
}
}

#[cfg(test)]
mod test_decode_var_source {
const EXPECTED: usize = 20;
use super::*;

#[test]
fn success() {
assert!(decode_var_source::<EXPECTED>(&[1; 20]).is_some());

assert!(decode_var_source::<EXPECTED>(
"d47ed02acbbb66ee8a3fe0275bd98add0aa607c3".as_bytes()
)
.is_some());

assert!(decode_var_source::<EXPECTED>(
"0xd47ed02acbbb66ee8a3fe0275bd98add0aa607c3".as_bytes()
)
.is_some());
}
}

0 comments on commit cd7c3d0

Please sign in to comment.