Skip to content

Commit

Permalink
Problem: walletconnect 2.0 defiwallet not working on c++ bindings (fix
Browse files Browse the repository at this point in the history
…#472) (#473)

fix null "data" in eth_sendTransaction

reformat

fix data field
  • Loading branch information
leejw51crypto authored Apr 24, 2024
1 parent fe74aa9 commit 3b41318
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 4 additions & 1 deletion extra-cpp-bindings/src/walletconnect2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
}
Expand Down
42 changes: 36 additions & 6 deletions wallet-connect/examples/web3_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
Expand Down Expand Up @@ -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)?);
Expand All @@ -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)?);
Expand All @@ -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)?;
Expand Down Expand Up @@ -266,6 +267,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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;
Expand Down Expand Up @@ -319,6 +321,34 @@ async fn main() -> Result<(), Box<dyn Error>> {
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::<u64>()?,
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 {
Expand All @@ -331,7 +361,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

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");

Expand Down
3 changes: 3 additions & 0 deletions wallet-connect/src/v2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ impl Middleware for WCMiddleware<Provider<Client>> {
}
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}"))));
Expand Down

0 comments on commit 3b41318

Please sign in to comment.