diff --git a/xpallets/bridge/bitcoin/src/tx/utils.rs b/xpallets/bridge/bitcoin/src/tx/utils.rs index 06abeab662507..d82a25ba38146 100644 --- a/xpallets/bridge/bitcoin/src/tx/utils.rs +++ b/xpallets/bridge/bitcoin/src/tx/utils.rs @@ -8,7 +8,7 @@ use xr_primitives::generic::b58; use xbridge_common::{traits::TrusteeSession, types::TrusteeSessionInfo, utils::two_thirds_unsafe}; #[cfg(feature = "std")] -use xsupport::u8array_to_hex; +use xsupport::as_string_hex; use xsupport::{error, warn}; // light-bitcoin @@ -39,7 +39,7 @@ pub fn parse_output_addr_with_networkid(script: &Script, network: Network) -> Op error!( "[parse_output_addr]|parse output script error|e:{:?}|script:{:?}", _e, - u8array_to_hex(&script) + as_string_hex(&script) ); _e }).ok().and_then(|script_addresses| { @@ -54,7 +54,7 @@ pub fn parse_output_addr_with_networkid(script: &Script, network: Network) -> Op return Some(addr); } // the type is `NonStandard`, `Multisig`, `NullData`, `WitnessScript`, `WitnessKey` - warn!("[parse_output_addr]|can't parse addr from output script|type:{:?}|addr:{:?}|script:{:}", script.script_type(), script_addresses, u8array_to_hex(&script)); + warn!("[parse_output_addr]|can't parse addr from output script|type:{:?}|addr:{:?}|script:{:}", script.script_type(), script_addresses, as_string_hex(&script)); None }) } diff --git a/xpallets/bridge/bitcoin/src/tx/validator.rs b/xpallets/bridge/bitcoin/src/tx/validator.rs index 5926ada3d44dc..aaf871f179c0c 100644 --- a/xpallets/bridge/bitcoin/src/tx/validator.rs +++ b/xpallets/bridge/bitcoin/src/tx/validator.rs @@ -18,7 +18,7 @@ use crate::Trait; // ChainX #[cfg(feature = "std")] -use xsupport::u8array_to_hex; +use xsupport::as_string_hex; use xsupport::{debug, error}; pub fn validate_transaction( @@ -134,7 +134,7 @@ pub fn parse_and_check_signed_tx_impl( } } if !verify { - error!("[parse_and_check_signed_tx]|Verify sign failed|tx:{:?}|input:{:?}|bytes_sedeem_script:{:?}", tx, i, u8array_to_hex(&bytes_redeem_script)); + error!("[parse_and_check_signed_tx]|Verify sign failed|tx:{:?}|input:{:?}|bytes_sedeem_script:{:?}", tx, i, as_string_hex(&bytes_redeem_script)); return Err("Verify sign failed"); } } diff --git a/xpallets/support/src/lib.rs b/xpallets/support/src/lib.rs index 8c91d56a950f6..4f84224bd4fd2 100644 --- a/xpallets/support/src/lib.rs +++ b/xpallets/support/src/lib.rs @@ -1,11 +1,14 @@ #![cfg_attr(not(feature = "std"), no_std)] pub mod base58; -pub mod rlog; -pub use rlog::RUNTIME_TARGET; +mod macros; +#[cfg(feature = "std")] +pub mod x_std; use frame_support::dispatch::{DispatchError, DispatchResult}; + pub use frame_support::fail; +pub use macros::*; /// Although xss is imperceptible on-chain, we merely want to make it look safer off-chain. #[inline] @@ -17,137 +20,3 @@ pub fn xss_check(input: &[u8]) -> DispatchResult { } Ok(()) } - -#[cfg(feature = "std")] -pub mod _std { - use std::fmt; - - pub struct Str<'a>(pub &'a String); - impl<'a> fmt::Debug for Str<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } - } - #[inline] - pub fn u8array_to_string(s: &[u8]) -> String { - String::from_utf8_lossy(s).into_owned() - } - - #[inline] - pub fn u8array_to_addr(s: &[u8]) -> String { - for i in s { - // 0x30 = '0' 0x39 = '9'; 0x41 = 'A' 0x7A = 'z' - if (0x30 <= *i && *i <= 0x39) || (0x41 <= *i && *i <= 0x7A) { - continue; - } else { - // 0x30 = '0' 0x7A = 'z' - return u8array_to_hex(s); // when any item is not a char, use hex to decode it - } - } - return u8array_to_string(s); - } - #[inline] - pub fn u8array_to_hex(s: &[u8]) -> String { - use rustc_hex::ToHex; - let s: String = s.to_hex(); - "0x".to_string() + &s - } - #[inline] - pub fn try_hex_or_str(src: &[u8]) -> String { - let check_is_str = |src: &[u8]| -> bool { - for c in src { - // 0x21 = !, 0x71 = ~ - if 0x21 <= *c && *c <= 0x7E { - continue; - } else { - return false; - } - } - return true; - }; - if check_is_str(src) { - u8array_to_string(src) - } else { - u8array_to_hex(src) - } - } -} - -#[cfg(feature = "std")] -#[macro_export] -macro_rules! str { - ( $x:expr ) => {{ - use $crate::_std::u8array_to_string; - $crate::_std::Str(&u8array_to_string(&$x)) - }}; -} - -#[cfg(not(feature = "std"))] -#[macro_export] -macro_rules! str { - ( $x:expr ) => {{ - &$x - }}; -} - -#[macro_export] -macro_rules! token { - ( $x:expr ) => { - $crate::str!($x) - }; -} - -#[cfg(feature = "std")] -#[macro_export] -macro_rules! try_addr { - ( $x:expr ) => {{ - use $crate::_std::u8array_to_addr; - $crate::_std::Str(&u8array_to_addr(&$x)) - }}; -} - -#[cfg(not(feature = "std"))] -#[macro_export] -macro_rules! try_addr { - ( $x:expr ) => {{ - &$x - }}; -} - -#[cfg(feature = "std")] -#[macro_export] -macro_rules! try_hex { - ( $x:expr ) => {{ - use $crate::_std::try_hex_or_str; - $crate::_std::Str(&try_hex_or_str(&$x)) - }}; -} - -#[cfg(not(feature = "std"))] -#[macro_export] -macro_rules! try_hex { - ( $x:expr ) => {{ - &$x - }}; -} - -#[cfg(feature = "std")] -#[macro_export] -macro_rules! ensure_with_errorlog { - ( $x:expr, $y:expr, $($arg:tt)*) => {{ - if !$x { - $crate::error!("{:?}|{}", $y, format!($($arg)*)); - $crate::fail!($y); - } - }} -} - -#[cfg(not(feature = "std"))] -#[macro_export] -macro_rules! ensure_with_errorlog { - ( $x:expr, $y:expr, $($arg:tt)*) => {{ - if !$x { - $crate::fail!($y); - } - }}; -} diff --git a/xpallets/support/src/macros.rs b/xpallets/support/src/macros.rs new file mode 100644 index 0000000000000..661d29ea13b81 --- /dev/null +++ b/xpallets/support/src/macros.rs @@ -0,0 +1,131 @@ +pub use log::*; + +#[cfg(feature = "std")] +#[macro_export] +macro_rules! str { + ( $x:expr ) => { + $crate::x_std::as_string(&$x) + }; +} + +#[cfg(not(feature = "std"))] +#[macro_export] +macro_rules! str { + ( $x:expr ) => { + &x + }; +} + +#[macro_export] +macro_rules! token { + ( $x:expr ) => { + $crate::str!($x) + }; +} + +#[cfg(feature = "std")] +#[macro_export] +macro_rules! try_addr { + ( $x:expr ) => {{ + $crate::x_std::as_addr(&$x) + }}; +} + +#[cfg(not(feature = "std"))] +#[macro_export] +macro_rules! try_addr { + ( $x:expr ) => {{ + &$x + }}; +} + +#[cfg(feature = "std")] +#[macro_export] +macro_rules! try_hex { + ( $x:expr ) => {{ + $crate::x_std::try_hex_or_str(&$x) + }}; +} + +#[cfg(not(feature = "std"))] +#[macro_export] +macro_rules! try_hex { + ( $x:expr ) => {{ + &$x + }}; +} + +#[cfg(feature = "std")] +#[macro_export] +macro_rules! ensure_with_errorlog { + ( $x:expr, $y:expr, $($arg:tt)*) => {{ + if !$x { + $crate::error!("{:?}|{}", $y, format!($($arg)*)); + $crate::fail!($y); + } + }} + } + +#[cfg(not(feature = "std"))] +#[macro_export] +macro_rules! ensure_with_errorlog { + ( $x:expr, $y:expr, $($arg:tt)*) => {{ + if !$x { + $crate::fail!($y); + } + }}; +} + +mod log { + pub const RUNTIME_TARGET: &str = "runtime"; + + #[macro_export] + macro_rules! error { + (target: $target:expr, $($arg:tt)+) => ( + frame_support::debug::error!(target: $target, $($arg)+); + ); + ($($arg:tt)+) => ( + $crate::error!(target: "runtime", $($arg)+); + ) + } + + #[macro_export] + macro_rules! warn { + (target: $target:expr, $($arg:tt)+) => ( + frame_support::debug::warn!(target: $target, $($arg)+); + ); + ($($arg:tt)+) => ( + $crate::warn!(target: "runtime", $($arg)+); + ) + } + + #[macro_export] + macro_rules! info { + (target: $target:expr, $($arg:tt)+) => ( + frame_support::debug::info!(target: $target, $($arg)+); + ); + ($($arg:tt)+) => ( + $crate::info!(target: "runtime", $($arg)+); + ) + } + + #[macro_export] + macro_rules! debug { + (target: $target:expr, $($arg:tt)+) => ( + frame_support::debug::debug!(target: $target, $($arg)+); + ); + ($($arg:tt)+) => ( + $crate::debug!(target: "runtime", $($arg)+); + ) + } + + #[macro_export] + macro_rules! trace { + (target: $target:expr, $($arg:tt)+) => ( + frame_support::debug::trace!(target: $target, $($arg)+); + ); + ($($arg:tt)+) => ( + $crate::trace!(target: "runtime", $($arg)+); + ) + } +} diff --git a/xpallets/support/src/rlog.rs b/xpallets/support/src/rlog.rs deleted file mode 100644 index 01c310103045a..0000000000000 --- a/xpallets/support/src/rlog.rs +++ /dev/null @@ -1,49 +0,0 @@ -pub use frame_support::debug; - -pub const RUNTIME_TARGET: &'static str = "runtime"; - -#[macro_export] -macro_rules! error { - (target: $target:expr, $($arg:tt)+) => ( - $crate::rlog::debug::error!(target: $target, $($arg)+); - ); - ($($arg:tt)+) => ( - $crate::rlog::debug::error!(target: "runtime", $($arg)+); - ) -} -#[macro_export] -macro_rules! warn { - (target: $target:expr, $($arg:tt)+) => ( - $crate::rlog::debug::warn!(target: $target, $($arg)+); - ); - ($($arg:tt)+) => ( - $crate::rlog::debug::warn!(target: "runtime", $($arg)+); - ) -} -#[macro_export] -macro_rules! info { - (target: $target:expr, $($arg:tt)+) => ( - $crate::rlog::debug::info!(target: $target, $($arg)+); - ); - ($($arg:tt)+) => ( - $crate::rlog::debug::info!(target: "runtime", $($arg)+); - ) -} -#[macro_export] -macro_rules! debug { - (target: $target:expr, $($arg:tt)+) => ( - $crate::rlog::debug::debug!(target: $target, $($arg)+); - ); - ($($arg:tt)+) => ( - $crate::rlog::debug::debug!(target: "runtime", $($arg)+); - ) -} -#[macro_export] -macro_rules! trace { - (target: $target:expr, $($arg:tt)+) => ( - $crate::rlog::debug::trace!(target: $target, $($arg)+); - ); - ($($arg:tt)+) => ( - $crate::rlog::debug::trace!(target: "runtime", $($arg)+); - ) -} diff --git a/xpallets/support/src/x_std.rs b/xpallets/support/src/x_std.rs new file mode 100644 index 0000000000000..58e9b8c267272 --- /dev/null +++ b/xpallets/support/src/x_std.rs @@ -0,0 +1,47 @@ +use rustc_hex::ToHex; + +/// Converts a slice of bytes to a string. +#[inline] +pub fn as_string(s: &[u8]) -> String { + String::from_utf8_lossy(s).into_owned() +} + +/// Converts a slice of bytes to a hex value, and then converts to a string with 0x prefix added. +#[inline] +pub fn as_string_hex(s: &[u8]) -> String { + format!("0x{}", s.to_hex::()) +} + +#[inline] +pub fn as_addr(s: &[u8]) -> String { + let should_as_string = s.iter().try_for_each(|i| { + if (b'0' <= *i && *i <= b'9') || (b'A' <= *i && *i <= b'z') { + Ok(()) + } else { + // 0x30 = '0' 0x7A = 'z' + Err(()) + } + }); + + if should_as_string.is_ok() { + as_string(s) + } else { + as_string_hex(s) + } +} + +#[inline] +pub fn try_hex_or_str(src: &[u8]) -> String { + let should_as_string = src.iter().try_for_each(|c| { + if b'!' <= *c && *c <= b'~' { + Ok(()) + } else { + Err(()) + } + }); + if should_as_string.is_ok() { + as_string(src) + } else { + as_string_hex(src) + } +}