From 71f11ffefe74bbe44b42fbbe36d9f890967867d3 Mon Sep 17 00:00:00 2001 From: mfahampshire Date: Wed, 5 Jul 2023 14:51:53 +0200 Subject: [PATCH] trying to debug 'client in use' error --- .../rust-cosmos-broadcaster-client/Cargo.toml | 3 ++ .../src/commands/commands.rs | 43 ++++++++++++++-- .../src/main.rs | 51 ++++++++++++------- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/demos/rust-cosmos-broadcaster-client/Cargo.toml b/demos/rust-cosmos-broadcaster-client/Cargo.toml index 1e87d83cad..21ae89a1b6 100644 --- a/demos/rust-cosmos-broadcaster-client/Cargo.toml +++ b/demos/rust-cosmos-broadcaster-client/Cargo.toml @@ -8,6 +8,8 @@ edition = "2021" [dependencies] clap = { version = "4.0", features = ["derive"] } +nym-bin-common = { path = "../../common/bin-common"} +nym-sdk = { path = "../../sdk/rust/nym-sdk" } nym-cli-commands = { path = "../../common/commands" } nym-validator-client = { path = "../../common/client-libs/validator-client", features = ["nyxd-client"] } nym-network-defaults = { path = "../../common/network-defaults" } @@ -16,4 +18,5 @@ bip39 = { workspace = true, features = ["rand"] } cosmrs = { git = "https://github.com/neacsu/cosmos-rust", branch = "neacsu/feegrant_support", features = ["rpc", "bip32", "cosmwasm"] } tokio = { version = "1.24.1", features = ["rt-multi-thread", "macros"] } ts-rs = "6.1.2" +bs58 = "0.5.0" diff --git a/demos/rust-cosmos-broadcaster-client/src/commands/commands.rs b/demos/rust-cosmos-broadcaster-client/src/commands/commands.rs index af33555eae..477087dd47 100644 --- a/demos/rust-cosmos-broadcaster-client/src/commands/commands.rs +++ b/demos/rust-cosmos-broadcaster-client/src/commands/commands.rs @@ -8,8 +8,10 @@ use cosmrs::rpc::{self, HttpClient}; use cosmrs::tx::Msg; use cosmrs::{tx, AccountId, Coin, Denom}; use bip39; +use bs58; +use nym_sdk::mixnet; -pub async fn offline_sign(mnemonic: bip39::Mnemonic, to: AccountId) -> Vec { +pub async fn offline_sign(mnemonic: bip39::Mnemonic, to: AccountId) -> String { // TODO take coin amount from function args, + load network vars from config file. let prefix = "n"; @@ -58,11 +60,44 @@ pub async fn offline_sign(mnemonic: bip39::Mnemonic, to: AccountId) -> Vec { .sign_direct(&signer_address, vec![send_msg], fee, memo, signer_data) .unwrap(); - // TODO return this from function let tx_bytes = tx_raw.to_bytes().unwrap(); - - tx_bytes + let base58_tx_bytes = bs58::encode(tx_bytes).into_string(); + base58_tx_bytes } +pub async fn send_tx(base58_tx: String) -> Option> /*String*/ { + // 1. decode base58 -> vec + println!("this is where we decode the base58 string"); + // 2. get nym client address + let client = mixnet::MixnetClientBuilder::new_ephemeral() + .build() + .await + .unwrap(); + + let mut client = client.connect_to_mixnet().await.unwrap(); + + let our_address = client.nym_address(); + println!("Our client nym address is: {our_address}"); + + // client.send_str(*our_address, "hello there").await; + + // 3. send message w sdk to broadcaster who will do: + /* + // broadcast the tx + let res = rpc::Client::broadcast_tx_commit(&broadcaster, tx_bytes.into()) + .await + .unwrap(); + */ + // client.send_str(sp_address, signedtx); + // println!("Waiting for message"); + let res = client.wait_for_messages().await; + + // disconnect client + // return the res to return to main thread + client.disconnect().await; + // String::from("your tx hash") + res + +} diff --git a/demos/rust-cosmos-broadcaster-client/src/main.rs b/demos/rust-cosmos-broadcaster-client/src/main.rs index c3d7e07a47..4391efb9dd 100644 --- a/demos/rust-cosmos-broadcaster-client/src/main.rs +++ b/demos/rust-cosmos-broadcaster-client/src/main.rs @@ -1,11 +1,13 @@ use clap::{CommandFactory, Parser, Subcommand, Args}; use nym_validator_client::nyxd::AccountId; // use nym_cli_commands::context::{get_network_details, ClientArgs}; +// use nym_crypto::asymmetric::identity; mod commands; +use nym_bin_common::logging::setup_logging; #[derive(Debug, Parser)] #[clap(name = "nym cosmos tx signer ")] -#[clap(about = "binary with which users can perform offline signing and transmission of signed tx to broadcaster via the mixnet ")] +#[clap(about = "demo binary with which users can perform offline signing and transmission of signed tx to broadcaster via the mixnet ")] struct Cli { // TODO make this import from file & remove from cli args // #[clap(long, global = true)] @@ -14,11 +16,12 @@ struct Cli { // )] // mnemonic: Option, - #[clap(short, long, global = true)] - #[clap( - help = "Overrides configuration as a file of environment variables." - )] - config_env_file: Option, + // TODO add for diff network + // #[clap(short, long, global = true)] + // #[clap( + // help = "Overrides configuration as a file of environment variables." + // )] + // config_env_file: Option, #[clap(subcommand)] command: Option, @@ -42,29 +45,41 @@ struct OfflineSignTx { #[derive(Debug, Args)] struct SendTx { - /// the address of the nym service to send yr signed tx - sp_address: String // TODO replace with mixnet address type + /// the base58 encoded signed payload created in OfflineSign() + base58_payload: String } #[tokio::main] async fn main() { - - let tx_bytes: Vec; - + setup_logging(); let cli = Cli::parse(); + let sp_address = "4roCqqdh1mG76gYT2das1wNBER3e5AzxC5dsA4zoWoLh.2iRzCRhzVMod7Ar5MnGt3X3zJGR7c4NxvK8cXCnxMYe3@2xU4CBE6QiiYt6EyBXSALwxkNvM7gqJfjHXaMkjiFmYW"; match &cli.command { Some(Commands::OfflineSignTx(OfflineSignTx { mnemonic, to } )) => { - tx_bytes = commands::commands::offline_sign(mnemonic.clone(), to.clone()).await; - println!("{:?}", &tx_bytes.iter().collect::>()); - println!("signed"); - // TODO write to disk for use in next function? + let base58_tx_bytes = commands::commands::offline_sign(mnemonic.clone(), to.clone()).await; + + println!("signed tx payload: \n\n{}\n\n", &base58_tx_bytes); + println!("do you wish to send the signed tx? y/n"); + + let mut input = String::new(); + let stdin = std::io::stdin(); + let n = stdin.read_line(&mut input).unwrap(); + + if input.chars().next().unwrap() == 'y' { // TODO add proper parsing for getting y/n + println!("\nsending tx thru the mixnet to broadcaster service"); + let tx_hash = commands::commands::send_tx(sp_address.to_string()).await; + println!("the response from the broadcaster: {:#?}", tx_hash); + } else if input.chars().next().unwrap() == 'n' { + println!("\nok, you can send the signed tx at a later date by passing the base58 string above as the argument for send-tx") + } else { //TODO make a loop & return to the question if input is not y/n + println!("\nunrecognised user input"); + } } - Some(Commands::SendTx(sp_address)) => { + Some(Commands::SendTx(SendTx { base58_payload } )) => { todo!(); } None => {println!("no command specified - nothing to do")} } - - println!(" ~(0.o)~ ") + println!(" end ~(0.o)~ ") } \ No newline at end of file