removed redundant doubled code

This commit is contained in:
mfahampshire
2023-07-18 17:04:26 +02:00
parent 3d299f80fd
commit 123c1983c8
3 changed files with 62 additions and 83 deletions
+11 -41
View File
@@ -1,8 +1,8 @@
use nym_sdk::mixnet::ReconstructedMessage;
use nym_sphinx_anonymous_replies::{self, requests::AnonymousSenderTag};
use rust_cosmos_broadcaster::{
create_client,
create_client,
listen_and_parse_request,
service::{broadcast, create_broadcaster, get_sequence},
BroadcastResponse, RequestTypes, SequenceRequestResponse,
};
@@ -17,25 +17,9 @@ async fn main() -> anyhow::Result<()> {
let broadcaster = create_broadcaster().await;
loop {
// 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;
}
received = new_message;
break;
}
for r in received.iter() {
// convert incoming vec<u8> -> String
let s = String::from_utf8(r.message.clone());
if s.is_ok() {
let p = s.unwrap();
// parse JSON string -> request type & match
let request: RequestTypes = serde_json::from_str(&p).unwrap();
match request {
let request: (RequestTypes, AnonymousSenderTag) = listen_and_parse_request(&mut client).await;
let return_recipient: AnonymousSenderTag = request.1;
match request.0 {
RequestTypes::Sequence(request) => {
println!(
"\nincoming sequence request details:\nsigner address: {}",
@@ -43,19 +27,12 @@ async fn main() -> anyhow::Result<()> {
);
let sequence: SequenceRequestResponse =
get_sequence(broadcaster.clone(), request.signer_address).await.unwrap();
if Some(r.sender_tag).is_some() {
let return_recipient: AnonymousSenderTag = r.sender_tag.unwrap();
println!("replying to sender with sequence response from Nyx chain");
println!("return recipient surb bucket: {}", &return_recipient);
client
.send_str_reply(
return_recipient,
&serde_json::to_string(&sequence).unwrap(),
)
.await;
} else {
println!("no surbs cannot reply an0n")
}
client
.send_str_reply(
return_recipient,
&serde_json::to_string(&sequence).unwrap(),
)
.await;
}
RequestTypes::Broadcast(request) => {
println!(
@@ -64,8 +41,6 @@ async fn main() -> anyhow::Result<()> {
);
let tx_hash: BroadcastResponse =
broadcast(request.base58_tx_bytes, broadcaster.clone()).await.unwrap();
if Some(r.sender_tag).is_some() {
let return_recipient: AnonymousSenderTag = r.sender_tag.unwrap();
println!("return recipient surb bucket: {}", &return_recipient);
client
.send_str_reply(
@@ -73,12 +48,7 @@ async fn main() -> anyhow::Result<()> {
&serde_json::to_string(&tx_hash).unwrap(),
)
.await;
} else {
println!("no surbs cannot reply an0n")
}
}
}
}
}
}
}
+3 -38
View File
@@ -4,7 +4,7 @@ use bs58;
use cosmrs::bank::MsgSend;
use cosmrs::tx::Msg;
use cosmrs::{tx, AccountId, Coin, Denom};
use nym_sdk::mixnet::{MixnetClient, ReconstructedMessage};
use nym_sdk::mixnet::{MixnetClient};
use nym_sphinx_addressing::clients::Recipient;
use nym_validator_client::nyxd::cosmwasm_client::types;
use nym_validator_client::signing::direct_wallet::DirectSecp256k1HdWallet;
@@ -35,24 +35,7 @@ pub async fn offline_sign(
.send_str(sp_address, &serde_json::to_string(&message).unwrap())
.await;
// handle incoming message - we presume its a reply from the SP
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
while let Some(new_message) = client.wait_for_messages().await {
if new_message.is_empty() {
continue;
}
message = new_message;
break;
}
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone()).unwrap();
}
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed).unwrap();
let sp_response = crate::listen_and_parse_response(client).await;
// match JSON -> ResponseType
let res = match sp_response {
@@ -129,25 +112,7 @@ pub async fn send_tx(
println!("Waiting for reply");
// handle incoming message - we presume its a reply from the SP
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
while let Some(new_message) = client.wait_for_messages().await {
if new_message.is_empty() {
continue;
}
message = new_message;
break;
}
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone()).unwrap();
}
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed).unwrap();
let sp_response = crate::listen_and_parse_response(client).await;
let res = match sp_response {
crate::ResponseTypes::Broadcast(response) => {
+48 -4
View File
@@ -1,5 +1,5 @@
use cosmrs::{tendermint, AccountId};
use nym_sdk::mixnet::{MixnetClient, MixnetClientBuilder, StoragePaths, ReconstructedMessage};
use nym_sdk::mixnet::{MixnetClient, MixnetClientBuilder, StoragePaths, ReconstructedMessage, AnonymousSenderTag};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
pub mod client;
@@ -59,8 +59,52 @@ pub async fn create_client(config_path: PathBuf) -> MixnetClient {
client.connect_to_mixnet().await.unwrap()
}
// parse incoming: ignore empty SURB data packets + parse incoming message to struct or error
// parse returned response from service: ignore empty SURB data packets + parse incoming message to struct or error
// we know we are expecting JSON here but an irl helper would parse conditionally on bytes / string incoming
pub fn _parse_incoming(_incoming: Option<Vec<ReconstructedMessage>>) -> ResponseTypes {
todo!()
pub async fn listen_and_parse_response(client: &mut MixnetClient) -> ResponseTypes {
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
while let Some(new_message) = client.wait_for_messages().await {
if new_message.is_empty() {
continue;
}
message = new_message;
break;
}
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone()).unwrap();
}
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed).unwrap();
sp_response
}
// parse incoming request: parse incoming message to struct + get sender_tag for SURB reply
// we know we are expecting JSON here but an irl helper would parse conditionally on bytes / string incoming
pub async fn listen_and_parse_request(client: &mut MixnetClient) -> (RequestTypes, AnonymousSenderTag) {
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
while let Some(new_message) = client.wait_for_messages().await {
if new_message.is_empty() {
continue;
}
message = new_message;
break;
}
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone()).unwrap();
}
let client_request: crate::RequestTypes = serde_json::from_str(&parsed).unwrap();
// get the sender_tag for anon reply
let return_recipient = message[0].sender_tag.unwrap();
(client_request, return_recipient)
}