Skip to content

Commit

Permalink
handle broadcast option for WalletConnect and some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Nov 6, 2024
1 parent 1e78257 commit a43dc60
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
16 changes: 11 additions & 5 deletions mm2src/coins/eth/eth_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/eth/wallet_connect.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -223,7 +224,7 @@ fn extract_pubkey_from_signature(
Ok((uncompressed, recovered_address))
}

pub fn recover(signature: &Signature, message: &Message) -> Result<H520, ethkey::Error> {
pub(crate) fn recover(signature: &Signature, message: &Message) -> Result<H520, ethkey::Error> {
let recovery_id = {
let recovery_id = (signature[64] as i32)
.checked_sub(27)
Expand Down
5 changes: 2 additions & 3 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2122,8 +2122,7 @@ pub struct WithdrawRequest {
memo: Option<String>,
/// Tendermint specific field used for manually providing the IBC channel IDs.
ibc_source_channel: Option<String>,
/// 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,
}
Expand Down Expand Up @@ -2177,7 +2176,6 @@ impl WithdrawRequest {
fee: None,
memo: None,
ibc_source_channel: None,
#[cfg(target_arch = "wasm32")]
broadcast: false,
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/qrc20/qrc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();

Expand Down
29 changes: 15 additions & 14 deletions mm2src/coins/tendermint/wallet_connect.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<u8>,
pub(crate) auth_info_bytes: Vec<u8>,
#[serde(deserialize_with = "deserialize_vec_field")]
pub body_bytes: Vec<u8>,
pub(crate) body_bytes: Vec<u8>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down
13 changes: 13 additions & 0 deletions mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ fn test_withdraw_impl_set_fixed_fee() {
}),
memo: None,
ibc_source_channel: None,
broadcast: false,
};
let expected = Some(
UtxoFeeDetails {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -3211,6 +3219,7 @@ fn test_withdraw_to_p2pk_fails() {
fee: None,
memo: None,
ibc_source_channel: None,
broadcast: false,
};

assert!(matches!(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a43dc60

Please sign in to comment.