working on sending client seq request to sp

This commit is contained in:
mfahampshire
2023-07-07 11:23:56 +02:00
parent 6c0d63af9a
commit e7643eb982
4 changed files with 23 additions and 36 deletions
@@ -19,7 +19,8 @@ use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct SequenceRequestData<'a> {
validator: &'a str,
signer_address: AccountId
signer_address: AccountId,
request_type: String
}
struct SequenceResponseData {
@@ -53,7 +54,8 @@ pub async fn offline_sign(mnemonic: bip39::Mnemonic, to: AccountId, client: &mut
let message = SequenceRequestData{
validator,
signer_address
signer_address,
request_type: String::from("sequence_request")
};
// send req to client
client.send_str(sp_address, &serde_json::to_string(&message).unwrap()).await;
@@ -102,21 +104,9 @@ pub async fn offline_sign(mnemonic: bip39::Mnemonic, to: AccountId, client: &mut
*/
}
pub async fn send_tx(base58_tx: String, sp_address: Recipient, client: &MixnetClient) -> Option<Vec<mixnet::ReconstructedMessage>> {
// send message w sdk to broadcaster who will do:
/*
// decode the base58 tx to vec<u8>
// broadcast the tx
let res = rpc::Client::broadcast_tx_commit(&broadcaster, tx_bytes.into())
.await
.unwrap();
// send res back via SURBs
*/
// client.send_str(sp_address, &base58_tx).await; // send as base58 encoded and it can be decoded by the SP
// println!("\nWaiting for reply\n");
// let res = client.wait_for_messages().await;
// res
todo!()
pub async fn send_tx(base58_tx: String, sp_address: Recipient, client: &mut MixnetClient) -> Option<Vec<mixnet::ReconstructedMessage>> {
client.send_str(sp_address, &base58_tx).await;
println!("\nWaiting for reply\n");
let res = client.wait_for_messages().await;
res
}
@@ -49,35 +49,27 @@ struct SendTx {
#[tokio::main]
async fn main() {
// setup_logging();
setup_logging();
let cli = Cli::parse();
// TODO look @ arg env setup from NR main.rs
// TODO take from args
let sp_address = Recipient::try_from_base58_string("2f7rmjCSKt6YxNBVfKSzQmvbgr4wZRprP8wMgBCyD4Xi.3rv7jSLYZXjFjDiVfiS24UVmMHmH6uZnGbxuDxjMELPu@CfWcDJq8QBz6cVAPCYSaLbaJEhVTmHEmyYgQ6C5GdDW9").unwrap();
// let mut client = MixnetClient::connect_new().await.unwrap(); // this was always seeing an existing connection...
// Specify some config options
let sp_address = Recipient::try_from_base58_string("6RkSh3QZCBQaSsMjZFSdRgRYkgKkKErQTtZtCyffsaSu.5xLdj5scMH116KazYDBUPRs16oE3BZhEAko8rjynTCJ6@E3mvZTHQCdBvhfr178Swx9g4QG3kkRUun7YnToLMcMbM").unwrap();
let config_dir = PathBuf::from("/tmp/cosmos-broadcaster-mixnet-client");
let storage_paths = StoragePaths::new_from_dir(&config_dir).unwrap();
// Create the client with a storage backend, and enable it by giving it some paths. If keys
// exists at these paths, they will be loaded, otherwise they will be generated.
let client = MixnetClientBuilder::new_with_default_storage(storage_paths)
.await
.unwrap()
.build()
.await
.unwrap();
// Now we connect to the mixnet, using keys now stored in the paths provided.
let mut client = client.connect_to_mixnet().await.unwrap();
// Be able to get our client address
let our_address = client.nym_address();
println!("Our client nym address is: {our_address}");
match &cli.command {
Some(Commands::OfflineSignTx(OfflineSignTx { mnemonic, nyx_token_receipient} )) => {
println!("sending offline sign info");
let base58_tx_bytes = commands::commands::offline_sign(mnemonic.clone(), nyx_token_receipient.clone(), &mut client, sp_address.clone()).await;
println!("base58 encoded signed tx payload: \n\n{}\n\n", &base58_tx_bytes);
@@ -89,7 +81,7 @@ async fn main() {
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(base58_tx_bytes, sp_address, &client).await;
let tx_hash = commands::commands::send_tx(base58_tx_bytes, sp_address, &mut client).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")
@@ -98,7 +90,7 @@ async fn main() {
}
}
Some(Commands::SendTx(SendTx { base58_payload, sp_address} )) => {
let tx_hash = commands::commands::send_tx(base58_payload.clone(), sp_address.clone(), &client).await;
let tx_hash = commands::commands::send_tx(base58_payload.clone(), sp_address.clone(), &mut client).await;
println!("the response from the broadcaster: {:#?}", tx_hash);
}
None => {println!("no command specified - nothing to do")}
@@ -21,4 +21,5 @@ cosmrs = { git = "https://github.com/neacsu/cosmos-rust", branch = "neacsu/feegr
tokio = { version = "1.24.1", features = ["rt-multi-thread", "macros"] }
ts-rs = "6.1.2"
bs58 = "0.5.0"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
@@ -3,6 +3,7 @@ use nym_validator_client::nyxd::AccountId;
use nym_bin_common::logging::setup_logging;
use std::path::PathBuf;
mod commands;
use serde::{Deserialize, Serialize};
#[tokio::main]
async fn main() {
@@ -22,9 +23,12 @@ async fn main() {
println!("Waiting for message");
if let Some(received) = client.wait_for_messages().await {
// parse traffic based on... something ... and match {} to functions in commands
for r in received {
println!("Received: {}", String::from_utf8_lossy(&r.message));
let to_parse = String::from_utf8_lossy(&r.message);
println!("Received: {}", &to_parse);
let parsed = serde_json::from_str(&to_parse).unwrap();
/*
deserialise json
check fn struct match