From 9faa60d2aff48d66df837dd441bd50384053677e Mon Sep 17 00:00:00 2001 From: jonghwan Date: Tue, 23 Apr 2024 22:28:38 +0900 Subject: [PATCH] Problem: walletconnect 2.0 defiwallet not working on c++ bindings (fix #472) fix null "data" in eth_sendTransaction reformat fix data field --- CHANGELOG.md | 3 ++ extra-cpp-bindings/src/walletconnect2.rs | 5 ++- wallet-connect/examples/web3_v2.rs | 42 ++++++++++++++++++++---- wallet-connect/src/v2/client.rs | 3 ++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e0ee348..4920cd8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog ## [Unreleased] +## [v0.0.26-alpha] - 2024-4-24 +- fix null `data` field in wc 2.0 + ## [v0.0.25-alpha] - 2024-4-23 - support defi-wallet diff --git a/extra-cpp-bindings/src/walletconnect2.rs b/extra-cpp-bindings/src/walletconnect2.rs index ba09d31d..a19df8cd 100644 --- a/extra-cpp-bindings/src/walletconnect2.rs +++ b/extra-cpp-bindings/src/walletconnect2.rs @@ -304,13 +304,16 @@ impl Walletconnect2Client { // from address is necessary for wc.20 , metamask tx = tx.from(Address::from_str(&userinfo.from)?); } - if !userinfo.to.is_empty() { tx = tx.to(NameOrAddress::Address(Address::from_str(&userinfo.to)?)); } if !userinfo.data.is_empty() { tx = tx.data(userinfo.data.as_slice().to_vec()); + } else { + // for defiwallet + tx = tx.data(vec![]); } + if !userinfo.common.gas_limit.is_empty() { tx = tx.gas(U256::from_dec_str(&userinfo.common.gas_limit)?); } diff --git a/wallet-connect/examples/web3_v2.rs b/wallet-connect/examples/web3_v2.rs index 9eceeb2e..b5665e9c 100644 --- a/wallet-connect/examples/web3_v2.rs +++ b/wallet-connect/examples/web3_v2.rs @@ -131,7 +131,7 @@ pub async fn sign_eip155_transaction_blocking( tx = tx.nonce(U256::from_dec_str(&userinfo.common.nonce)?); } if userinfo.common.chainid != 0 { - // tx = tx.chain_id(userinfo.common.chainid); + tx = tx.chain_id(userinfo.common.chainid); } if !userinfo.value.is_empty() { tx = tx.value(U256::from_dec_str(&userinfo.value)?); @@ -162,14 +162,16 @@ pub async fn send_eip155_transaction_blocking( let signeraddress = Address::from_slice(&address); let mut tx = Eip1559TransactionRequest::new(); - + // need for defiwallet + tx = tx.from(signeraddress); if !userinfo.to.is_empty() { tx = tx.to(NameOrAddress::Address(Address::from_str(&userinfo.to)?)); } if !userinfo.data.is_empty() { tx = tx.data(userinfo.data.as_slice().to_vec()); + } else { + tx = tx.data(vec![]); } - tx = tx.data(vec![]); if !userinfo.common.gas_limit.is_empty() { tx = tx.gas(U256::from_dec_str(&userinfo.common.gas_limit)?); @@ -183,7 +185,7 @@ pub async fn send_eip155_transaction_blocking( tx = tx.nonce(U256::from_dec_str(&userinfo.common.nonce)?); } if userinfo.common.chainid != 0 { - // tx = tx.chain_id(userinfo.common.chainid); + tx = tx.chain_id(userinfo.common.chainid); } if !userinfo.value.is_empty() { tx = tx.value(U256::from_dec_str(&userinfo.value)?); @@ -198,7 +200,6 @@ pub async fn send_eip155_transaction_blocking( Ok(tx_bytes.0.to_vec()) } - async fn make_qrcode(uri: &str) -> Result<()> { // Generate the QR code for the data you want let code = QrCode::new(uri)?; @@ -266,6 +267,7 @@ async fn main() -> Result<(), Box> { let test_personal_signing = false; let test_sign_tx = false; let test_send_tx = true; + let test_send_typedtx = false; let test_event_listening = false; let uri = client.get_connection_string().await; @@ -319,6 +321,34 @@ async fn main() -> Result<(), Box> { println!("signature length {sig_hex_length} 0x{sig_hex}"); } + if test_send_tx { + let from_address = std::env::var("MY_FROM_ADDRESS").expect("MY_FROM_ADDRESS not set"); + let to_address = std::env::var("MY_TO_ADDRESS").expect("MY_TO_ADDRESS not set"); + + // convert fromaddress to 20 fixed byte array + let fromaddress = Address::from_str(&from_address)?; + + let txinfo = WalletConnectTxEip155 { + common: WalletConnectTxCommon { + chainid: std::env::var("MY_CHAIN_ID") + .expect("MY_CHAIN_ID not set") + .parse::()?, + gas_limit: "2100000".into(), + gas_price: "17646852851231".into(), + nonce: "".into(), + web3api_url: "".into(), + }, + to: to_address.into(), + data: vec![1], + value: "1000".into(), + }; + let txhash = + send_eip155_transaction_blocking(&mut client, &txinfo, fromaddress.into()).await?; + let txhash_hex = hex::encode(txhash.as_slice()); + let txhash_length = txhash.len(); + println!("txhash {txhash_hex} 0x{txhash_length}"); + } + if test_event_listening { println!("press anykey to exit"); loop { @@ -331,7 +361,7 @@ async fn main() -> Result<(), Box> { } } - if test_send_tx { + if test_send_typedtx { let from_address = std::env::var("MY_FROM_ADDRESS").expect("MY_FROM_ADDRESS not set"); let to_address = std::env::var("MY_TO_ADDRESS").expect("MY_TO_ADDRESS not set"); diff --git a/wallet-connect/src/v2/client.rs b/wallet-connect/src/v2/client.rs index 1493519d..86740df2 100644 --- a/wallet-connect/src/v2/client.rs +++ b/wallet-connect/src/v2/client.rs @@ -365,6 +365,9 @@ impl Middleware for WCMiddleware> { } if let Some(data) = tx.data() { tx_obj.insert("data", format!("0x{}", hex::encode(data))); + } else { + // need for defi wallet, otherwise user rejection error + tx_obj.insert("data", "0x".to_string()); } if let Some(gas) = tx.gas() { tx_obj.insert("gasLimit", append_hex(pad_zero(format!("{gas:x}"))));