Skip to content

Commit

Permalink
Problem: not exposing c++ api for wc 2.0 sign , send tx cronos-labs#453
Browse files Browse the repository at this point in the history
working

print qr

port wc 1.0 apis

reformat

tidy up

testing send tx

add makefile

add qrcode

move use

send_tx code added

add signing

reformat

fix signing

reformat

add zero padding to hex

tidy up

tidy up

refactoring

not use chain-id

remove Makefile

restore main

fix chainid compare

restore lock

fix compile
  • Loading branch information
leejw51crypto committed Jun 6, 2023
1 parent 0f5e0bc commit ce33555
Show file tree
Hide file tree
Showing 9 changed files with 698 additions and 25 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ demo/cronos.h
demo/sdk

install

wallet-connect/qrcode.png

wallet-connect/session.bin

defi-wallet-core-rs

demo/third_party/easywsclient

demo/third_party/json

wallet-connect/yarn.lock

wallet-connect/node_modules/

.DS_Store

sessioninfo2.json
33 changes: 29 additions & 4 deletions demo/extra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "sdk/include/rust/cxx.h"
#include "third_party/easywsclient/easywsclient.hpp"
#include "third_party/json/single_include/nlohmann/json.hpp"
#include <cassert>
#include <atomic>
#include <cassert>
#include <chrono>
Expand Down Expand Up @@ -363,16 +364,17 @@ void test_wallet_connect() {
* @summary test wallet connect 2.0
* @description basic test for wallet connect 2.0
*/

void test_wallet_connect2() {
std::string mycronosrpc = getEnv("CRONOSRPC").c_str();
bool test_personal = true;
bool test_basic = false;
bool test_personal = false;
bool test_basic = true;
bool test_nft = false;
std::string filename = "sessioninfo2.json";
bool exit_program = false;
try {
Box<Walletconnect2Client> client = make_new_client2(filename);
String uri = client->print_uri();
std::cout << "uri= " << uri.c_str() << std::endl;
WalletConnect2EnsureSessionResult result =
client->ensure_session_blocking(60000);
std::cout << "session result=" << result.eip155.accounts.size()
Expand Down Expand Up @@ -405,14 +407,37 @@ void test_wallet_connect2() {
}

assert(result.eip155.accounts.size() > 0);
bool test_personal = true;

if (test_personal) {
Vec<uint8_t> sig1 = client->sign_personal_blocking(
"hello", result.eip155.accounts.at(0).address.address);
std::cout << "signature length=" << sig1.size() << endl;
}

if (test_basic) {
std::string fromaddress = getenv("MYFROMADDRESS");
std::cout << "mycronosrpc=" << mycronosrpc << endl;
std::cout << "fromaddress=" << fromaddress << endl;
std::string toaddress = getenv("MYTOADDRESS");
std::cout << "toaddress=" << toaddress << endl;
std::string mynonce = org::defi_wallet_core::get_eth_nonce(
fromaddress.c_str(), mycronosrpc)
.c_str();
std::cout << "nonce=" << mynonce << endl;
WalletConnectTxEip155 info;
info.to = toaddress;
info.common.gas_limit = "21000"; // gas limit
info.common.gas_price = "10000"; // gas price
info.value = "100000000000000"; // 0.0001 eth
info.data = Vec<uint8_t>();
info.common.nonce = mynonce;
info.common.chainid = 1;

assert(result.eip155.accounts.size() > 0);
Vec<uint8_t> rawtx = client->sign_eip155_transaction_blocking(
info, result.eip155.accounts[0].address.address);
}

std::cout << "enter q to exit" << std::endl;
while (true) {
// read input, if q is pressed, quit
Expand Down
1 change: 0 additions & 1 deletion demo/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ int main(int argc, char *argv[]) {
test_interval();
test_blackscout_cronoscan();
test_wallet_connect();

} catch (const std::exception &e) {
// Use `Assertion failed`, the same as `assert` function
std::cout << "Assertion failed: " << e.what() << std::endl;
Expand Down
49 changes: 40 additions & 9 deletions extra-cpp-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ mod ffi {
pub fn save_client(self: &mut Walletconnect2Client) -> Result<String>;
/// print qrcode in termal, for debugging
pub fn print_uri(self: &mut WalletconnectClient) -> Result<String>;
pub fn print_uri(self: &mut Walletconnect2Client) -> Result<String>;
/// sign message
pub fn sign_personal_blocking(
self: &mut WalletconnectClient,
Expand All @@ -354,6 +355,11 @@ mod ffi {
info: &WalletConnectTxEip155,
address: [u8; 20],
) -> Result<Vec<u8>>;
pub fn sign_eip155_transaction_blocking(
self: &mut Walletconnect2Client,
info: &WalletConnectTxEip155,
address: [u8; 20],
) -> Result<Vec<u8>>;

/// send cronos(eth) eip155 transaction
/// Supported Wallets: Trust Wallet, MetaMask and Crypto.com Mobile Defi Wallet
Expand All @@ -362,6 +368,11 @@ mod ffi {
info: &WalletConnectTxEip155,
address: [u8; 20],
) -> Result<Vec<u8>>;
pub fn send_eip155_transaction_blocking(
self: &mut Walletconnect2Client,
info: &WalletConnectTxEip155,
address: [u8; 20],
) -> Result<Vec<u8>>;

/// eip1559_transaction_request: json string of Eip1559TransactionRequest
/// return signed transaction bytes
Expand All @@ -370,6 +381,11 @@ mod ffi {
eip1559_transaction_request: String,
address: [u8; 20],
) -> Result<Vec<u8>>;
pub fn sign_transaction(
self: &mut Walletconnect2Client,
eip1559_transaction_request: String,
address: [u8; 20],
) -> Result<Vec<u8>>;

/// eip1559_transaction_request: json string of Eip1559TransactionRequest
/// return transaction hash bytes
Expand All @@ -378,6 +394,11 @@ mod ffi {
eip1559_transaction_request: String,
address: [u8; 20],
) -> Result<Vec<u8>>;
pub fn send_transaction(
self: &mut Walletconnect2Client,
eip1559_transaction_request: String,
address: [u8; 20],
) -> Result<Vec<u8>>;

/// sign a contract transaction
/// contract_action is a json string of `ContractAction` type, for example:
Expand All @@ -398,6 +419,12 @@ mod ffi {
common: &WalletConnectTxCommon,
address: [u8; 20],
) -> Result<Vec<u8>>;
pub fn sign_contract_transaction(
self: &mut Walletconnect2Client,
contract_action: String,
common: &WalletConnectTxCommon,
address: [u8; 20],
) -> Result<Vec<u8>>;

// send a contract transaction
/// contract_action is a json string of `ContractAction` type
Expand All @@ -418,6 +445,12 @@ mod ffi {
common: &WalletConnectTxCommon,
address: [u8; 20],
) -> Result<Vec<u8>>;
pub fn send_contract_transaction(
self: &mut Walletconnect2Client,
contract_action: String,
common: &WalletConnectTxCommon,
address: [u8; 20],
) -> Result<Vec<u8>>;

/// returns the transactions of a given address.
/// The API key can be obtained from https://cronoscan.com
Expand Down Expand Up @@ -1017,14 +1050,12 @@ pub fn walletconnect2_client_new(
return Err(anyhow!("project_id is empty"));
}
// print all arguments
println!("relay_server_string: {:?}", relay_server_string);
println!("project_id: {:?}", project_id);
println!("required_namespaces_json: {:?}", required_namespaces_json);
println!("client_meta_json: {:?}", client_meta_json);
println!("relay_server_string: {relay_server_string:?}");
println!("project_id: {project_id:?}");
println!("required_namespaces_json: {required_namespaces_json:?}");
println!("client_meta_json: {client_meta_json:?}");

let mut opts = defi_wallet_connect::v2::ClientOptions::default();
// print opts
println!("opts1: {:?}", opts);

if !relay_server_string.is_empty() {
let relay_server = url::Url::parse(&relay_server_string)?;
Expand All @@ -1047,11 +1078,11 @@ pub fn walletconnect2_client_new(
opts.client_meta = client_meta;
}

println!("opts: {:?}", opts);
println!("opts: {opts:?}");
let required_namespaces = serde_json::to_string(&opts.required_namespaces)?;
println!("required_namespaces_json: {}", required_namespaces);
println!("required_namespaces_json: {required_namespaces}",);
let client_meta = serde_json::to_string(&opts.client_meta)?;
println!("client_meta_json: {}", client_meta);
println!("client_meta_json: {client_meta}");

let (tx, rx) = tokio::sync::mpsc::unbounded_channel();

Expand Down
4 changes: 2 additions & 2 deletions extra-cpp-bindings/src/walletconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl WalletconnectClient {
if !userinfo.common.nonce.is_empty() {
tx = tx.nonce(U256::from_dec_str(&userinfo.common.nonce)?);
}
if !userinfo.common.chainid == 0 {
if userinfo.common.chainid != 0 {
tx = tx.chain_id(userinfo.common.chainid);
}
if !userinfo.value.is_empty() {
Expand Down Expand Up @@ -437,7 +437,7 @@ impl WalletconnectClient {
if !userinfo.common.nonce.is_empty() {
tx = tx.nonce(U256::from_dec_str(&userinfo.common.nonce)?);
}
if !userinfo.common.chainid == 0 {
if userinfo.common.chainid != 0 {
tx = tx.chain_id(userinfo.common.chainid);
}
if !userinfo.value.is_empty() {
Expand Down
Loading

0 comments on commit ce33555

Please sign in to comment.