tidyup
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
TODO FOR DEMO
|
||||
- clippy
|
||||
- nicer logging - show sequence response etc
|
||||
- pass tx hash back to client nicely & remove from service
|
||||
|
||||
### Nym mixnet cosmos tx broadcaster demo
|
||||
|
||||
A demo showing how to:
|
||||
@@ -12,30 +17,18 @@ Built using:
|
||||
|
||||
#### Useage
|
||||
```
|
||||
# compile
|
||||
cargo build --release
|
||||
|
||||
example 1: sign & send @ same time
|
||||
# start service
|
||||
# start service
|
||||
../../target/release/service
|
||||
|
||||
# sign tx - when prompted enter 'y'
|
||||
../../target/release/client offline-sign-tx "<MNEMONIC>" "<RECIPIENT_NYX_ADDRESS>
|
||||
|
||||
example 2: sign first, send later
|
||||
# start service
|
||||
# sign tx - when prompted enter 'n' and copy encoded tx bytes from terminal
|
||||
# send tx using encoded bytes as arg
|
||||
```
|
||||
|
||||
|
||||
//
|
||||
|
||||
- bin
|
||||
- client : `main.rs` from `rust...client/`
|
||||
- service : `main.rs` from `rust...server/`
|
||||
|
||||
- src
|
||||
- lib : `reqres` definitions + define CONSTs in there
|
||||
- client : `commands.rs` from `rust...client/`
|
||||
- service : `commands.rs` from `rust...server/`
|
||||
|
||||
- added CONSTS to
|
||||
- client src DONE
|
||||
- client bin -- make sp an optional cli arg otherwise set as DEFAULT
|
||||
- service src
|
||||
- service bin
|
||||
@@ -1,11 +1,9 @@
|
||||
use std::path::PathBuf;
|
||||
use clap::{ Parser, Subcommand, Args};
|
||||
use nym_sdk::mixnet::{Recipient, MixnetClientBuilder, StoragePaths};
|
||||
use nym_sdk::mixnet::{Recipient};
|
||||
use nym_validator_client::nyxd::AccountId;
|
||||
use nym_bin_common::logging::setup_logging;
|
||||
use rust_cosmos_broadcaster::{client::{offline_sign, send_tx}, create_client};
|
||||
|
||||
// move this somewhere else as well??
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(name = "nym cosmos tx signer ")]
|
||||
#[clap(about = "demo binary with which users can perform offline signing and transmission of signed tx to broadcaster via the mixnet ")]
|
||||
@@ -42,7 +40,7 @@ async fn main() {
|
||||
let cli = Cli::parse();
|
||||
let mut client = create_client("/tmp/cosmos-broadcaster-mixnet-client-2".into()).await;
|
||||
let our_address = client.nym_address();
|
||||
println!("\nSetup test ---- our client nym address is: {our_address}");
|
||||
println!("\nour client nym address is: {our_address}");
|
||||
|
||||
// TODO take from args as Option otherwise set as default this logic moves to src/client and remove this from here
|
||||
let sp_address = Recipient::try_from_base58_string("HfbesQm2pRYCN4BAdYXhkqXBbV1Pp929mtKsESVeWXh8.8AgoUPUQbXNBCPaqAaWd3vnxhc9484qwfgrrQwBngQk2@Ck8zpXTSXMtS9YZ7k7a5BiaoLZfffWuqGWLndujh4Lw4").unwrap();
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
use nym_sdk::mixnet::{StoragePaths, MixnetClientBuilder, ReconstructedMessage, MixnetClient};
|
||||
use nym_sdk::mixnet::ReconstructedMessage;
|
||||
use nym_bin_common::logging::setup_logging;
|
||||
use std::path::PathBuf;
|
||||
use nym_sphinx_anonymous_replies::{self, requests::AnonymousSenderTag};
|
||||
// use rust_cosmos_broadcaster::service::{get_sequence, broadcast};
|
||||
use rust_cosmos_broadcaster::{RequestTypes, SequenceRequestResponse, BroadcastResponse, service::{get_sequence, broadcast}, create_client};
|
||||
use rust_cosmos_broadcaster::{RequestTypes, SequenceRequestResponse, BroadcastResponse, service::{get_sequence, broadcast, create_broadcaster}, create_client};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
// setup_logging();
|
||||
let mut client = create_client("/tmp/cosmos-broadcaster-mixnet-server-2".into()).await;
|
||||
let our_address = client.nym_address();
|
||||
println!("\nSetup test ---- our client nym address is: {our_address}");
|
||||
// TODO create broadcaster in src/service fn, call here & save as var to pass to other fns
|
||||
println!("\nour client nym address is: {our_address}");
|
||||
// the httpclient we will use to broadcast our signed tx to the Nyx blockchain
|
||||
let broadcaster = create_broadcaster().await;
|
||||
|
||||
/*
|
||||
TODO
|
||||
@@ -20,22 +18,16 @@ async fn main() {
|
||||
*/
|
||||
loop {
|
||||
println!("\nWaiting for message");
|
||||
// TODO rewrite this to parse any empty SURB data and then parse the actual incoming message
|
||||
// let received = client.wait_for_messages().await;
|
||||
|
||||
// handle incoming message - we presume its a reply from the SP
|
||||
// check incoming is empty - SURB requests also send data ( empty vec ) along
|
||||
let mut received: Vec<ReconstructedMessage> = Vec::new();
|
||||
|
||||
// get the actual message - discard the empty vec sent along with the SURB request
|
||||
while let Some(new_message) = client.wait_for_messages().await {
|
||||
if new_message.is_empty() {
|
||||
continue;
|
||||
}
|
||||
println!("got a response:");
|
||||
received = new_message;
|
||||
println!("{:#?}", &received);
|
||||
break
|
||||
if new_message.is_empty() {
|
||||
continue;
|
||||
}
|
||||
received = new_message;
|
||||
println!("recieved: {:#?}", &received);
|
||||
break
|
||||
}
|
||||
|
||||
for r in received.iter() {
|
||||
@@ -50,19 +42,13 @@ async fn main() {
|
||||
println!("incoming request: {:#?}", &request);
|
||||
match request {
|
||||
RequestTypes::Sequence(request) => {
|
||||
println!("\nincoming sequence request details:\nvalidator: {},\nsigner address: {}\n", request.validator, request.signer_address);
|
||||
let sequence: SequenceRequestResponse = get_sequence(request.validator, request.signer_address).await;
|
||||
// print!("debug print -------- {:#?}", sequence);
|
||||
// println!("debug print SENDER TAG --------- {:#?}", r.sender_tag);
|
||||
println!("\nincoming sequence request details:\nsigner address: {}\n", request.signer_address);
|
||||
let sequence: SequenceRequestResponse = get_sequence(broadcaster.clone(), request.signer_address).await;
|
||||
if Some(r.sender_tag).is_some() {
|
||||
// println!("debug print ---- sending reply ");
|
||||
let return_recipient: AnonymousSenderTag = r.sender_tag.unwrap();
|
||||
println!("return recipient surb bucket: {}", &return_recipient);
|
||||
// todo actually return sequence serialised as json
|
||||
client.send_str_reply(return_recipient, &serde_json::to_string(&sequence).unwrap()).await;
|
||||
// println!("sent reply - sleeping");
|
||||
// tokio::time::sleep(Duration::from_secs(25)).await;
|
||||
// println!("stopped sleep");
|
||||
} else {
|
||||
// TODO replace with actual error type to return
|
||||
println!("no surbs cannot reply an0n")
|
||||
@@ -70,12 +56,10 @@ async fn main() {
|
||||
},
|
||||
RequestTypes::Broadcast(request) => {
|
||||
println!("\nincoming sequence request details: {}\n", request.base58_tx_bytes);
|
||||
let tx_hash: BroadcastResponse = broadcast(request.base58_tx_bytes).await;
|
||||
let tx_hash: BroadcastResponse = broadcast(request.base58_tx_bytes, broadcaster.clone()).await;
|
||||
if Some(r.sender_tag).is_some() {
|
||||
// println!("debug print ---- sending reply ");
|
||||
let return_recipient: AnonymousSenderTag = r.sender_tag.unwrap();
|
||||
println!("return recipient surb bucket: {}", &return_recipient);
|
||||
// todo actually return sequence serialised as json
|
||||
client.send_str_reply(return_recipient, &serde_json::to_string(&tx_hash).unwrap()).await;
|
||||
} else {
|
||||
// TODO replace with actual error type to return
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use cosmrs::{AccountId, tendermint};
|
||||
use std::path::PathBuf;
|
||||
use nym_sdk::mixnet::{StoragePaths, MixnetClientBuilder, ReconstructedMessage, MixnetClient};
|
||||
use nym_sdk::mixnet::{StoragePaths, MixnetClientBuilder, MixnetClient};
|
||||
pub mod client;
|
||||
pub mod service;
|
||||
|
||||
@@ -55,6 +55,6 @@ pub async fn create_client(config_path: PathBuf) -> MixnetClient {
|
||||
.build()
|
||||
.await
|
||||
.unwrap();
|
||||
let mut client = client.connect_to_mixnet().await.unwrap();
|
||||
let client = client.connect_to_mixnet().await.unwrap();
|
||||
client
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
use nym_validator_client::nyxd::CosmWasmClient;
|
||||
use cosmrs::rpc::{HttpClient, Client};
|
||||
use cosmrs::{AccountId, tendermint};
|
||||
use bs58;
|
||||
use bs58;
|
||||
use crate::DEFAULT_VALIDATOR_RPC;
|
||||
|
||||
pub async fn get_sequence(validator: String, signer_address: AccountId) -> crate::SequenceRequestResponse {
|
||||
/*
|
||||
TODO create broadcaster in different fn and build on setup - pass to both fns as arg
|
||||
*/
|
||||
let broadcaster = HttpClient::new(validator.as_str()).unwrap();
|
||||
pub async fn create_broadcaster() -> HttpClient {
|
||||
let broadcaster: HttpClient = HttpClient::new(DEFAULT_VALIDATOR_RPC).unwrap();
|
||||
broadcaster
|
||||
}
|
||||
|
||||
pub async fn get_sequence(broadcaster: HttpClient, signer_address: AccountId) -> crate::SequenceRequestResponse {
|
||||
// get signer information
|
||||
let sequence = broadcaster.get_sequence(&signer_address).await.unwrap();
|
||||
let chain_id: tendermint::chain::Id = broadcaster.get_chain_id().await.unwrap();
|
||||
@@ -15,21 +17,17 @@ pub async fn get_sequence(validator: String, signer_address: AccountId) -> crate
|
||||
res
|
||||
}
|
||||
|
||||
pub async fn broadcast(base58_tx_bytes: String) -> crate::BroadcastResponse {
|
||||
pub async fn broadcast(base58_tx_bytes: String, broadcaster: HttpClient) -> crate::BroadcastResponse {
|
||||
// decode the base58 tx to vec<u8>
|
||||
let tx_bytes = bs58::decode(base58_tx_bytes).into_vec().unwrap();
|
||||
println!("decoded tx bytes: {:#?}", tx_bytes);
|
||||
|
||||
/*
|
||||
TODO create broadcaster in different fn and build on setup - pass to both fns as arg
|
||||
*/
|
||||
let broadcaster = HttpClient::new("https://qwerty-validator.qa.nymte.ch").unwrap();
|
||||
|
||||
let to_address: AccountId = "n1p8ayfmdash352gh6yy8zlxk24dm6yzc9mdq0p6".parse().unwrap();
|
||||
|
||||
// this is our sender address hardcoded for ease of the demo logging
|
||||
let from_address: AccountId = "n1p8ayfmdash352gh6yy8zlxk24dm6yzc9mdq0p6".parse().unwrap();
|
||||
|
||||
// compare balances from before and after the tx
|
||||
let before = broadcaster
|
||||
.get_balance(&to_address, "unym".to_string())
|
||||
.get_balance(&from_address, "unym".to_string())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -40,7 +38,7 @@ pub async fn broadcast(base58_tx_bytes: String) -> crate::BroadcastResponse {
|
||||
.unwrap();
|
||||
|
||||
let after = broadcaster
|
||||
.get_balance(&to_address, "unym".to_string())
|
||||
.get_balance(&from_address, "unym".to_string())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user