From 1a8f243dc7623268465cf4075d2a433a029dceb9 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Mon, 6 Jan 2025 11:45:49 +0100 Subject: [PATCH] move u256_to_hex fn to common mod and minor changes --- mm2src/coins/eth/wallet_connect.rs | 3 +-- mm2src/coins/tendermint/wallet_connect.rs | 31 ++++++++++++----------- mm2src/common/common.rs | 5 ++++ mm2src/kdf_walletconnect/src/lib.rs | 11 ++++---- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/mm2src/coins/eth/wallet_connect.rs b/mm2src/coins/eth/wallet_connect.rs index 05945f4237..c9ff6e347f 100644 --- a/mm2src/coins/eth/wallet_connect.rs +++ b/mm2src/coins/eth/wallet_connect.rs @@ -4,6 +4,7 @@ use crate::Eip1559Ops; use crate::{BytesJson, MarketCoinOps, TransactionErr}; use common::log::info; +use common::u256_to_hex; use derive_more::Display; use enum_derives::EnumFromStringify; use ethcore_transaction::{Action, SignedTransaction}; @@ -56,8 +57,6 @@ pub struct WcEthTxParams<'a> { impl<'a> WcEthTxParams<'a> { fn prepare_wc_tx_format(&self) -> MmResult { - fn u256_to_hex(value: U256) -> String { format!("0x{:x}", value) } - let mut tx_json = json!({ "nonce": u256_to_hex(self.nonce), "from": format!("{:x}", self.my_address), diff --git a/mm2src/coins/tendermint/wallet_connect.rs b/mm2src/coins/tendermint/wallet_connect.rs index ec84128310..335c50ad86 100644 --- a/mm2src/coins/tendermint/wallet_connect.rs +++ b/mm2src/coins/tendermint/wallet_connect.rs @@ -192,21 +192,22 @@ where let value = Value::deserialize(deserializer)?; match value { - Value::Object(map) => { - let mut vec = Vec::new(); - for i in 0..map.len() { - if let Some(Value::Number(num)) = map.get(&i.to_string()) { - if let Some(byte) = num.as_u64() { - vec.push(byte as u8); - } else { - return Err(serde::de::Error::custom("Invalid byte value")); - } - } else { - return Err(serde::de::Error::custom("Invalid format")); - } - } - Ok(vec) - }, + Value::Object(map) => map + .iter() + .enumerate() + .map(|(_, (_, value))| { + value + .as_u64() + .ok_or_else(|| serde::de::Error::custom("Invalid byte value")) + .and_then(|n| { + if n <= 255 { + Ok(n as u8) + } else { + Err(serde::de::Error::custom("Invalid byte value")) + } + }) + }) + .collect(), Value::Array(arr) => arr .into_iter() .map(|v| { diff --git a/mm2src/common/common.rs b/mm2src/common/common.rs index 288882d0ae..7b05b21fc7 100644 --- a/mm2src/common/common.rs +++ b/mm2src/common/common.rs @@ -141,6 +141,7 @@ pub mod wio; #[cfg(target_arch = "wasm32")] pub mod wasm; +use primitive_types::U256; #[cfg(target_arch = "wasm32")] pub use wasm::*; use backtrace::SymbolName; @@ -1155,6 +1156,10 @@ pub fn http_uri_to_ws_address(uri: http::Uri) -> String { format!("{}{}{}{}", address_prefix, host_address, port, path) } +/// Converts a U256 value to a lowercase hexadecimal string with "0x" prefix +#[inline] +pub fn u256_to_hex(value: U256) -> String { format!("0x{:x}", value) } + /// If 0x prefix exists in an str strip it or return the str as-is #[macro_export] macro_rules! str_strip_0x { diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 586eb5c9af..a504669cc3 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -108,7 +108,11 @@ impl WalletConnectCtx { let (conn_live_sender, conn_live_receiver) = unbounded(); let (client, _) = Client::new_with_callback( Handler::new("Komodefi", inbound_message_tx, conn_live_sender), - |r, h| abortable_system.weak_spawner().spawn(client_event_loop(r, h)), + |receiver, handler| { + abortable_system + .weak_spawner() + .spawn(client_event_loop(receiver, handler)) + }, ); let message_id_generator = MessageIdGenerator::new(); @@ -330,10 +334,7 @@ impl WalletConnectCtxImpl { pairing_topics.push(pairing_topic); } - let all_topics = valid_topics - .into_iter() - .chain(pairing_topics.into_iter()) - .collect::>(); + let all_topics = valid_topics.into_iter().chain(pairing_topics).collect::>(); if !all_topics.is_empty() { self.client.batch_subscribe(all_topics).await?;