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
  • Loading branch information
leejw51crypto committed Jun 5, 2023
1 parent 0f5e0bc commit dbe8c35
Show file tree
Hide file tree
Showing 8 changed files with 745 additions and 13 deletions.
102 changes: 101 additions & 1 deletion 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 @@ -364,7 +365,7 @@ void test_wallet_connect() {
* @description basic test for wallet connect 2.0
*/

void test_wallet_connect2() {
void test_wallet_connect2_old() {
std::string mycronosrpc = getEnv("CRONOSRPC").c_str();
bool test_personal = true;
bool test_basic = false;
Expand Down Expand Up @@ -430,6 +431,105 @@ void test_wallet_connect2() {
}
}

void test_wallet_connect2() {
std::string mycronosrpc = getEnv("CRONOSRPC").c_str();
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()
<< std::endl;

std::cout << "ping" << endl;
String pingresult = client->ping_blocking(60000);
std::cout << "ping result=" << pingresult.c_str() << std::endl;

// spawn thread
std::thread pollingthread([&]() {
try {
bool *exitthread = &exit_program;
while (!(*exitthread)) {
try {
String ret = client->poll_events_blocking(1000);
std::cout << "poll events result=" << ret.c_str()
<< std::endl;
} catch (const std::exception &e) {
}
}
} catch (const std::exception &e) {
std::cout << "wallet connect error=" << e.what() << std::endl;
}
});
String sessioninfo = client->save_client();
{
ofstream outfile(filename);
outfile.write(sessioninfo.c_str(), sessioninfo.size());
}

assert(result.eip155.accounts.size() > 0);

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

auto receipt = org::defi_wallet_core::broadcast_eth_signed_raw_tx(
rawtx, mycronosrpc, 3000);
std::cout << "transaction_hash="
<< bytes_to_hex_string(receipt.transaction_hash).c_str()
<< endl;
}



std::cout << "enter q to exit" << std::endl;
while (true) {
// read input, if q is pressed, quit
char c = getchar();
if (c == 'q') {
exit_program = true;
std::cout << "exit program" << endl;
break;
}
}
pollingthread.join();

} catch (const std::exception &e) {
std::cout << "wallet connect error=" << e.what() << std::endl;
}
}

// pay api examples
void test_crypto_pay() {
if (PAY_API_KEY == "")
Expand Down
18 changes: 10 additions & 8 deletions demo/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

int main(int argc, char *argv[]) {
try {
chainmain_process(); // chain-main
test_chainmain_nft(); // chainmain nft tests
test_login(); // decentralized login
cronos_process(); // cronos
test_cronos_testnet(); // cronos testnet
test_interval();
test_blackscout_cronoscan();
test_wallet_connect();
/* chainmain_process(); // chain-main
test_chainmain_nft(); // chainmain nft tests
test_login(); // decentralized login
cronos_process(); // cronos
test_cronos_testnet(); // cronos testnet
test_interval();
test_blackscout_cronoscan();
test_wallet_connect();I*/
std::cout << "hello world" << std::endl;
test_wallet_connect2();

} catch (const std::exception &e) {
// Use `Assertion failed`, the same as `assert` function
Expand Down
33 changes: 33 additions & 0 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
Loading

0 comments on commit dbe8c35

Please sign in to comment.