From a43dc6066ec8761c9d38a9cca956b8e7321daece Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Wed, 6 Nov 2024 13:32:09 +0100 Subject: [PATCH] handle broadcast option for WalletConnect and some minor fixes --- Cargo.lock | 3 +- mm2src/coins/eth/eth_tests.rs | 2 ++ mm2src/coins/eth/eth_withdraw.rs | 16 ++++++---- mm2src/coins/eth/wallet_connect.rs | 3 +- mm2src/coins/lp_coins.rs | 5 ++-- mm2src/coins/qrc20/qrc20_tests.rs | 2 ++ mm2src/coins/tendermint/wallet_connect.rs | 29 ++++++++++--------- mm2src/coins/utxo/utxo_tests.rs | 13 +++++++++ mm2src/mm2_main/Cargo.toml | 1 + .../tests/docker_tests/qrc20_tests.rs | 4 +-- mm2src/mm2_net/Cargo.toml | 2 +- 11 files changed, 53 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de1f0b3704..099ee681b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4270,6 +4270,7 @@ dependencies = [ "primitives", "prost", "prost-build", + "rand 0.6.5", "rand 0.7.3", "rcgen", "regex", @@ -4361,7 +4362,7 @@ dependencies = [ "cfg-if 1.0.0", "common", "derive_more", - "ethkey 0.3.0 (git+https://github.com/KomodoPlatform/mm2-parity-ethereum.git?branch=fix-pubkey-recover-from-sig)", + "ethkey 0.3.0 (git+https://github.com/KomodoPlatform/mm2-parity-ethereum.git?rev=mm2-v2.1.1)", "futures 0.3.28", "futures-util", "gstuff", diff --git a/mm2src/coins/eth/eth_tests.rs b/mm2src/coins/eth/eth_tests.rs index c7f1e51d13..6c7c7d4f8c 100644 --- a/mm2src/coins/eth/eth_tests.rs +++ b/mm2src/coins/eth/eth_tests.rs @@ -226,6 +226,7 @@ fn test_withdraw_impl_manual_fee() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; block_on_f01(coin.get_balance()).unwrap(); @@ -275,6 +276,7 @@ fn test_withdraw_impl_fee_details() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; block_on_f01(coin.get_balance()).unwrap(); diff --git a/mm2src/coins/eth/eth_withdraw.rs b/mm2src/coins/eth/eth_withdraw.rs index c725bac29b..246aadf38f 100644 --- a/mm2src/coins/eth/eth_withdraw.rs +++ b/mm2src/coins/eth/eth_withdraw.rs @@ -321,11 +321,17 @@ where gas_price, }; - let (tx, bytes) = self - .coin() - .wc_sign_tx(&wc, params) - .await - .mm_err(|err| WithdrawError::SigningError(err.to_string()))?; + let (tx, bytes) = if req.broadcast { + self.coin() + .wc_send_tx(&wc, params) + .await + .mm_err(|err| WithdrawError::SigningError(err.to_string()))? + } else { + self.coin() + .wc_sign_tx(&wc, params) + .await + .mm_err(|err| WithdrawError::SigningError(err.to_string()))? + }; (tx.tx_hash(), bytes) }, diff --git a/mm2src/coins/eth/wallet_connect.rs b/mm2src/coins/eth/wallet_connect.rs index 8fc18fd78a..f04cf9c08f 100644 --- a/mm2src/coins/eth/wallet_connect.rs +++ b/mm2src/coins/eth/wallet_connect.rs @@ -1,3 +1,4 @@ +/// https://docs.reown.com/advanced/multichain/rpc-reference/ethereum-rpc use crate::common::Future01CompatExt; use crate::Eip1559Ops; use crate::{BytesJson, TransactionErr}; @@ -223,7 +224,7 @@ fn extract_pubkey_from_signature( Ok((uncompressed, recovered_address)) } -pub fn recover(signature: &Signature, message: &Message) -> Result { +pub(crate) fn recover(signature: &Signature, message: &Message) -> Result { let recovery_id = { let recovery_id = (signature[64] as i32) .checked_sub(27) diff --git a/mm2src/coins/lp_coins.rs b/mm2src/coins/lp_coins.rs index bc532fa5aa..da599999b2 100644 --- a/mm2src/coins/lp_coins.rs +++ b/mm2src/coins/lp_coins.rs @@ -2122,8 +2122,7 @@ pub struct WithdrawRequest { memo: Option, /// Tendermint specific field used for manually providing the IBC channel IDs. ibc_source_channel: Option, - /// Currently, this flag is used by ETH/ERC20 coins activated with MetaMask **only**. - #[cfg(target_arch = "wasm32")] + /// Currently, this flag is used by ETH/ERC20 coins activated with MetaMask/WalletConnect **only**. #[serde(default)] broadcast: bool, } @@ -2177,7 +2176,6 @@ impl WithdrawRequest { fee: None, memo: None, ibc_source_channel: None, - #[cfg(target_arch = "wasm32")] broadcast: false, } } @@ -5600,6 +5598,7 @@ pub mod for_tests { fee, memo: None, ibc_source_channel: None, + broadcast: false, }; let init = init_withdraw(ctx.clone(), withdraw_req).await.unwrap(); let timeout = wait_until_ms(150000); diff --git a/mm2src/coins/qrc20/qrc20_tests.rs b/mm2src/coins/qrc20/qrc20_tests.rs index 930f078c53..1da173cfa1 100644 --- a/mm2src/coins/qrc20/qrc20_tests.rs +++ b/mm2src/coins/qrc20/qrc20_tests.rs @@ -95,6 +95,7 @@ fn test_withdraw_to_p2sh_address_should_fail() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let err = block_on_f01(coin.withdraw(req)).unwrap_err().into_inner(); let expect = WithdrawError::InvalidAddress("QRC20 can be sent to P2PKH addresses only".to_owned()); @@ -142,6 +143,7 @@ fn test_withdraw_impl_fee_details() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); diff --git a/mm2src/coins/tendermint/wallet_connect.rs b/mm2src/coins/tendermint/wallet_connect.rs index 432881ee53..9544defaca 100644 --- a/mm2src/coins/tendermint/wallet_connect.rs +++ b/mm2src/coins/tendermint/wallet_connect.rs @@ -1,3 +1,4 @@ +/// https://docs.reown.com/advanced/multichain/rpc-reference/cosmos-rpc use base64::engine::general_purpose; use base64::Engine; use cosmrs::proto::cosmos::tx::v1beta1::TxRaw; @@ -13,33 +14,33 @@ use std::str::FromStr; use super::{CosmosTransaction, TendermintCoin}; #[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct CosmosTxSignedData { - pub signature: CosmosTxSignature, - pub signed: CosmosSignData, +pub(crate) struct CosmosTxSignedData { + pub(crate) signature: CosmosTxSignature, + pub(crate) signed: CosmosSignData, } #[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct CosmosTxSignature { - pub pub_key: CosmosTxPublicKey, - pub signature: String, +pub(crate) struct CosmosTxSignature { + pub(crate) pub_key: CosmosTxPublicKey, + pub(crate) signature: String, } #[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct CosmosTxPublicKey { +pub(crate) struct CosmosTxPublicKey { #[serde(rename = "type")] - pub key_type: String, - pub value: String, + pub(crate) key_type: String, + pub(crate) value: String, } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] -pub struct CosmosSignData { - pub chain_id: String, - pub account_number: String, +pub(crate) struct CosmosSignData { + pub(crate) chain_id: String, + pub(crate) account_number: String, #[serde(deserialize_with = "deserialize_vec_field")] - pub auth_info_bytes: Vec, + pub(crate) auth_info_bytes: Vec, #[serde(deserialize_with = "deserialize_vec_field")] - pub body_bytes: Vec, + pub(crate) body_bytes: Vec, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] diff --git a/mm2src/coins/utxo/utxo_tests.rs b/mm2src/coins/utxo/utxo_tests.rs index 8148cd4ce9..29a3a1875f 100644 --- a/mm2src/coins/utxo/utxo_tests.rs +++ b/mm2src/coins/utxo/utxo_tests.rs @@ -612,6 +612,7 @@ fn test_withdraw_impl_set_fixed_fee() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; let expected = Some( UtxoFeeDetails { @@ -661,6 +662,7 @@ fn test_withdraw_impl_sat_per_kb_fee() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; // The resulting transaction size might be 244 or 245 bytes depending on signature size // MM2 always expects the worst case during fee calculation @@ -713,6 +715,7 @@ fn test_withdraw_impl_sat_per_kb_fee_amount_equal_to_max() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); // The resulting transaction size might be 210 or 211 bytes depending on signature size @@ -767,6 +770,7 @@ fn test_withdraw_impl_sat_per_kb_fee_amount_equal_to_max_dust_included_to_fee() }), memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); // The resulting transaction size might be 210 or 211 bytes depending on signature size @@ -821,6 +825,7 @@ fn test_withdraw_impl_sat_per_kb_fee_amount_over_max() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; block_on_f01(coin.withdraw(withdraw_req)).unwrap_err(); } @@ -862,6 +867,7 @@ fn test_withdraw_impl_sat_per_kb_fee_max() { }), memo: None, ibc_source_channel: None, + broadcast: false, }; // The resulting transaction size might be 210 or 211 bytes depending on signature size // MM2 always expects the worst case during fee calculation @@ -929,6 +935,7 @@ fn test_withdraw_kmd_rewards_impl( fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let expected_fee = TxFeeDetails::Utxo(UtxoFeeDetails { coin: Some("KMD".into()), @@ -1011,6 +1018,7 @@ fn test_withdraw_rick_rewards_none() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let expected_fee = TxFeeDetails::Utxo(UtxoFeeDetails { coin: Some(TEST_COIN_NAME.into()), @@ -3211,6 +3219,7 @@ fn test_withdraw_to_p2pk_fails() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; assert!(matches!( @@ -3269,6 +3278,7 @@ fn test_withdraw_to_p2pkh() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); let transaction: UtxoTx = deserialize(tx_details.tx.tx_hex().unwrap().as_slice()).unwrap(); @@ -3329,6 +3339,7 @@ fn test_withdraw_to_p2sh() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); let transaction: UtxoTx = deserialize(tx_details.tx.tx_hex().unwrap().as_slice()).unwrap(); @@ -3389,6 +3400,7 @@ fn test_withdraw_to_p2wpkh() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); let transaction: UtxoTx = deserialize(tx_details.tx.tx_hex().unwrap().as_slice()).unwrap(); @@ -3444,6 +3456,7 @@ fn test_withdraw_p2pk_balance() { fee: None, memo: None, ibc_source_channel: None, + broadcast: false, }; let tx_details = block_on_f01(coin.withdraw(withdraw_req)).unwrap(); let transaction: UtxoTx = deserialize(tx_details.tx.tx_hex().unwrap().as_slice()).unwrap(); diff --git a/mm2src/mm2_main/Cargo.toml b/mm2src/mm2_main/Cargo.toml index d4af0213bd..5cfb8f0515 100644 --- a/mm2src/mm2_main/Cargo.toml +++ b/mm2src/mm2_main/Cargo.toml @@ -85,6 +85,7 @@ parking_lot = { version = "0.12.0", features = ["nightly"] } primitives = { path = "../mm2_bitcoin/primitives" } prost = "0.12" rand = { version = "0.7", features = ["std", "small_rng"] } +rand6 = { version = "0.6", package = "rand" } rmp-serde = "0.14.3" rpc = { path = "../mm2_bitcoin/rpc" } rpc_task = { path = "../rpc_task" } diff --git a/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs b/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs index 4ed3772f03..352b383941 100644 --- a/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs +++ b/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs @@ -20,7 +20,7 @@ use mm2_main::lp_swap::{dex_fee_amount, max_taker_vol_from_available}; use mm2_number::BigDecimal; use mm2_rpc::data::legacy::{CoinInitResponse, OrderbookResponse}; use mm2_test_helpers::structs::{trade_preimage_error, RpcErrorResponse, RpcSuccessResponse, TransactionDetails}; -use rand::Rng; +use rand6::Rng; use serde_json::{self as json, Value as Json}; use std::convert::TryFrom; use std::process::Command; @@ -1104,7 +1104,7 @@ fn test_max_taker_vol_dynamic_trade_fee() { // generate QTUM coin with the dynamic fee and fill the wallet by 2 Qtums let (_ctx, coin, priv_key) = generate_qtum_coin_with_random_privkey("QTUM", 2.into(), Some(0)); let my_address = coin.my_address().expect("!my_address"); - let mut rng = rand::thread_rng(); + let mut rng = rand6::thread_rng(); let mut qtum_balance = BigDecimal::from(2); let mut qtum_balance_steps = "2".to_owned(); for _ in 0..4 { diff --git a/mm2src/mm2_net/Cargo.toml b/mm2src/mm2_net/Cargo.toml index 380ed760eb..1f2b61551b 100644 --- a/mm2src/mm2_net/Cargo.toml +++ b/mm2src/mm2_net/Cargo.toml @@ -15,7 +15,7 @@ bytes = "1.1" cfg-if = "1.0" common = { path = "../common" } derive_more = "0.99" -ethkey = { git = "https://github.com/KomodoPlatform/mm2-parity-ethereum.git", branch = "fix-pubkey-recover-from-sig" } +ethkey = { git = "https://github.com/KomodoPlatform/mm2-parity-ethereum.git", rev = "mm2-v2.1.1" } futures = { version = "0.3", package = "futures", features = ["compat", "async-await", "thread-pool"] } gstuff = "0.7" http = "0.2"