Skip to content

Commit

Permalink
fix review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Jan 17, 2025
1 parent bb73b1f commit ed7b4d2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 59 deletions.
32 changes: 6 additions & 26 deletions mm2src/coins/eth/wallet_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ impl WalletConnectOps for EthCoin {
let tx_json = params.prepare_wc_tx_format()?;
let session_topic = self.session_topic()?;
let tx_hex: String = wc
.send_session_request_and_wait(
session_topic,
&chain_id,
WcRequestMethods::EthSignTransaction,
tx_json,
Ok,
)
.send_session_request_and_wait(session_topic, &chain_id, WcRequestMethods::EthSignTransaction, tx_json)
.await?;
// First 4 bytes from WalletConnect represents Protoc info
hex::decode(&tx_hex[4..])?
Expand All @@ -139,14 +133,8 @@ impl WalletConnectOps for EthCoin {
let chain_id = self.wc_chain_id(wc).await?;
let tx_json = params.prepare_wc_tx_format()?;
let session_topic = self.session_topic()?;
wc.send_session_request_and_wait(
session_topic,
&chain_id,
WcRequestMethods::EthSendTransaction,
tx_json,
Ok,
)
.await?
wc.send_session_request_and_wait(session_topic, &chain_id, WcRequestMethods::EthSendTransaction, tx_json)
.await?
};

let tx_hash = tx_hash.strip_prefix("0x").unwrap_or(&tx_hash);
Expand Down Expand Up @@ -196,19 +184,11 @@ pub async fn eth_request_wc_personal_sign(
json!(&[&message_hex, &account_str])
};
let data = wc
.send_session_request_and_wait(
session_topic,
&chain_id,
WcRequestMethods::PersonalSign,
params,
|data: String| {
extract_pubkey_from_signature(&data, message, &account_str)
.mm_err(|err| WalletConnectError::SessionError(err.to_string()))
},
)
.send_session_request_and_wait::<String>(session_topic, &chain_id, WcRequestMethods::PersonalSign, params)
.await?;

Ok(data)
Ok(extract_pubkey_from_signature(&data, message, &account_str)
.mm_err(|err| WalletConnectError::SessionError(err.to_string()))?)
}

fn extract_pubkey_from_signature(
Expand Down
38 changes: 14 additions & 24 deletions mm2src/coins/tendermint/wallet_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,18 @@ impl WalletConnectOps for TendermintCoin {
} else {
WcRequestMethods::CosmosSignDirect
};
let data: CosmosTxSignedData = wc
.send_session_request_and_wait(session_topic, &chain_id, method, params)
.await?;
let signature = general_purpose::STANDARD
.decode(data.signature.signature)
.map_to_mm(|err| WalletConnectError::PayloadError(err.to_string()))?;

wc.send_session_request_and_wait(session_topic, &chain_id, method, params, |data: CosmosTxSignedData| {
let signature = general_purpose::STANDARD
.decode(data.signature.signature)
.map_to_mm(|err| WalletConnectError::PayloadError(err.to_string()))?;

Ok(TxRaw {
body_bytes: data.signed.body_bytes,
auth_info_bytes: data.signed.auth_info_bytes,
signatures: vec![signature],
})
Ok(TxRaw {
body_bytes: data.signed.body_bytes,
auth_info_bytes: data.signed.auth_info_bytes,
signatures: vec![signature],
})
.await
}

async fn wc_send_tx<'a>(
Expand Down Expand Up @@ -169,20 +168,11 @@ pub async fn cosmos_get_accounts_impl(
}

let params = serde_json::to_value(&account).unwrap();
wc.send_session_request_and_wait(
session_topic,
&chain_id,
WcRequestMethods::CosmosGetAccounts,
params,
|accounts: Vec<CosmosAccount>| {
if accounts.is_empty() {
return MmError::err(WalletConnectError::EmptyAccount(chain_id.to_string()));
};
let accounts: Vec<CosmosAccount> = wc
.send_session_request_and_wait(session_topic, &chain_id, WcRequestMethods::CosmosGetAccounts, params)
.await?;

Ok(accounts[0].clone())
},
)
.await
Ok(accounts[0].clone())
}

fn deserialize_vec_field<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
Expand Down
9 changes: 5 additions & 4 deletions mm2src/kdf_walletconnect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ futures = { version = "0.3", package = "futures", features = [
"async-await",
] }
hkdf = "0.12.4"
mm2_core = { path = "../mm2_core" }
mm2_db = { path = "../mm2_db" }
mm2_core = { path = "../mm2_core" }
mm2_err_handle = { path = "../mm2_err_handle" }
mm2_test_helpers = { path = "../mm2_test_helpers" }
parking_lot = { version = "0.12.0", features = ["nightly"] }
pairing_api = { git = "https://github.com/komodoplatform/walletconnectrust", tag = "k-0.1.2" }
rand = "0.8"
relay_client = { git = "https://github.com/komodoplatform/walletconnectrust", tag = "k-0.1.2" }
relay_rpc = { git = "https://github.com/komodoplatform/walletconnectrust", tag = "k-0.1.2" }
thiserror = "1.0.40"
tokio = { version = "1.20" }
wc_common = { git = "https://github.com/komodoplatform/walletconnectrust", tag = "k-0.1.2" }
secp256k1 = { version = "0.20" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order", "raw_value"] }
sha2 = "0.10.7"
thiserror = "1.0.40"
tokio = { version = "1.20" }
x25519-dalek = { version = "2.0", features = ["static_secrets"] }
wc_common = { git = "https://github.com/komodoplatform/walletconnectrust", tag = "k-0.1.2" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = { version = "0.3.27" }
Expand Down
8 changes: 3 additions & 5 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,17 +582,15 @@ impl WalletConnectCtxImpl {

/// Waits for and handles a WalletConnect session response with arbitrary data.
/// https://specs.walletconnect.com/2.0/specs/clients/sign/session-events#session_request
pub async fn send_session_request_and_wait<T, R, F>(
pub async fn send_session_request_and_wait<R>(
&self,
session_topic: &str,
chain_id: &WcChainId,
method: WcRequestMethods,
params: serde_json::Value,
callback: F,
) -> MmResult<R, WalletConnectError>
where
T: DeserializeOwned,
F: Fn(T) -> MmResult<R, WalletConnectError>,
R: DeserializeOwned,
{
let session_topic = session_topic.into();
self.session_manager.validate_session_exists(&session_topic)?;
Expand All @@ -615,7 +613,7 @@ impl WalletConnectCtxImpl {
.map_to_mm(|_| WalletConnectError::TimeoutError)?
.map_to_mm(|err| WalletConnectError::InternalError(err.to_string()))??;
match response.data {
ResponseParamsSuccess::Arbitrary(data) => callback(serde_json::from_value::<T>(data)?),
ResponseParamsSuccess::Arbitrary(data) => Ok(serde_json::from_value::<R>(data)?),
_ => MmError::err(WalletConnectError::PayloadError("Unexpected response type".to_string())),
}
}
Expand Down

0 comments on commit ed7b4d2

Please sign in to comment.