Skip to content

Commit

Permalink
precompile: Add support for hex source address
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Sep 11, 2023
1 parent 31aa21e commit ec53725
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hex = { version = "0.4.3", default-features = false }
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"], default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use sp_std::vec::Vec;
pub use crate::weights::WeightInfo;

pub const MAX_SOURCE_CHAIN_BYTES: u32 = 128;
pub const MAX_SOURCE_ADDRESS_BYTES: u32 = 32;
// Ensure we allow enough to support a hex encoded address with the `0x` prefix.
pub const MAX_SOURCE_ADDRESS_BYTES: u32 = 42;
pub const MAX_TOKEN_SYMBOL_BYTES: u32 = 32;
pub const MAX_PAYLOAD_BYTES: u32 = 1024;
pub const PREFIX_CONTRACT_CALL_APPROVED: [u8; 32] = keccak256!("contract-call-approved");
Expand Down Expand Up @@ -273,8 +274,13 @@ where
exit_status: ExitError::Other("converter for source not found".into()),
})?;

let source_address_bytes =
get_source_address_bytes(source_address).ok_or(PrecompileFailure::Error {
exit_status: ExitError::Other("invalid source address".into()),
})?;

let domain_address = domain_converter
.try_convert(source_address.as_bytes())
.try_convert(source_address_bytes.as_slice())
.ok_or(PrecompileFailure::Error {
exit_status: ExitError::Other("account bytes mismatch for domain".into()),
})?;
Expand Down Expand Up @@ -320,3 +326,55 @@ where
Ok(())
}
}

const EXPECTED_SOURCE_ADDRESS_SIZE: usize = 20;
const HEX_PREFIX: &str = "0x";

pub(crate) fn get_source_address_bytes(
source_address: String<MAX_SOURCE_ADDRESS_BYTES>,
) -> Option<Vec<u8>> {
if source_address.as_bytes().len() == EXPECTED_SOURCE_ADDRESS_SIZE {
return Some(source_address.as_bytes().to_vec());
}

let str = source_address.as_str().ok()?;

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

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

#[cfg(test)]
mod tests {
use sp_core::H160;

use super::*;

#[test]
fn get_source_address_bytes_works() {
let hash = H160::from_low_u64_be(1);

let str = String::<MAX_SOURCE_ADDRESS_BYTES>::from(hash.as_fixed_bytes().to_vec());

get_source_address_bytes(str).expect("address bytes from H160 works");

let str = String::<MAX_SOURCE_ADDRESS_BYTES>::from(
"d47ed02acbbb66ee8a3fe0275bd98add0aa607c3".to_string(),
);

get_source_address_bytes(str).expect("address bytes from un-prefixed hex works");

let str = String::<MAX_SOURCE_ADDRESS_BYTES>::from(
"0xd47ed02acbbb66ee8a3fe0275bd98add0aa607c3".to_string(),
);

get_source_address_bytes(str).expect("address bytes from prefixed hex works");
}
}

0 comments on commit ec53725

Please sign in to comment.