This commit is contained in:
mfahampshire
2023-07-18 17:06:05 +02:00
parent 123c1983c8
commit 2dbd47a85b
4 changed files with 54 additions and 57 deletions
-3
View File
@@ -1,8 +1,5 @@
TODO FOR DEMO
- flags for cli
- single parse_incoming fn in lib and call from everywhere
- pass mnemonic as file (otherwise pass as env var)
- create signer in main.rs and pass to commands (client/src)
### Nym mixnet cosmos tx broadcaster demo
A demo showing how to:
+33 -36
View File
@@ -1,8 +1,6 @@
use nym_sphinx_anonymous_replies::{self, requests::AnonymousSenderTag};
use rust_cosmos_broadcaster::{
create_client,
listen_and_parse_request,
create_client, listen_and_parse_request,
service::{broadcast, create_broadcaster, get_sequence},
BroadcastResponse, RequestTypes, SequenceRequestResponse,
};
@@ -17,38 +15,37 @@ async fn main() -> anyhow::Result<()> {
let broadcaster = create_broadcaster().await;
loop {
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: {}",
request.signer_address
);
let sequence: SequenceRequestResponse =
get_sequence(broadcaster.clone(), request.signer_address).await.unwrap();
client
.send_str_reply(
return_recipient,
&serde_json::to_string(&sequence).unwrap(),
)
.await;
}
RequestTypes::Broadcast(request) => {
println!(
"\nincoming broadcast request: {}\n",
request.base58_tx_bytes
);
let tx_hash: BroadcastResponse =
broadcast(request.base58_tx_bytes, broadcaster.clone()).await.unwrap();
println!("return recipient surb bucket: {}", &return_recipient);
client
.send_str_reply(
return_recipient,
&serde_json::to_string(&tx_hash).unwrap(),
)
.await;
}
}
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: {}",
request.signer_address
);
let sequence: SequenceRequestResponse =
get_sequence(broadcaster.clone(), request.signer_address)
.await
.unwrap();
client
.send_str_reply(return_recipient, &serde_json::to_string(&sequence).unwrap())
.await;
}
RequestTypes::Broadcast(request) => {
println!(
"\nincoming broadcast request: {}\n",
request.base58_tx_bytes
);
let tx_hash: BroadcastResponse =
broadcast(request.base58_tx_bytes, broadcaster.clone())
.await
.unwrap();
println!("return recipient surb bucket: {}", &return_recipient);
client
.send_str_reply(return_recipient, &serde_json::to_string(&tx_hash).unwrap())
.await;
}
}
}
}
+8 -9
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};
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,7 +35,7 @@ pub async fn offline_sign(
.send_str(sp_address, &serde_json::to_string(&message).unwrap())
.await;
let sp_response = crate::listen_and_parse_response(client).await;
let sp_response = crate::listen_and_parse_response(client).await;
// match JSON -> ResponseType
let res = match sp_response {
@@ -86,9 +86,7 @@ pub async fn offline_sign(
let base58_tx_bytes = bs58::encode(tx_bytes).into_string();
base58_tx_bytes
}
_ => {
String::from("unexpected response")
}
_ => String::from("unexpected response"),
};
Ok(res)
@@ -112,7 +110,7 @@ pub async fn send_tx(
println!("Waiting for reply");
let sp_response = crate::listen_and_parse_response(client).await;
let sp_response = crate::listen_and_parse_response(client).await;
let res = match sp_response {
crate::ResponseTypes::Broadcast(response) => {
@@ -122,9 +120,10 @@ pub async fn send_tx(
};
(broadcast_response.tx_hash, broadcast_response.success)
}
_ => {
(String::from("Got strange incoming response, couldn't match"), false)
}
_ => (
String::from("Got strange incoming response, couldn't match"),
false,
),
};
Ok(res)
+13 -9
View File
@@ -1,5 +1,7 @@
use cosmrs::{tendermint, AccountId};
use nym_sdk::mixnet::{MixnetClient, MixnetClientBuilder, StoragePaths, ReconstructedMessage, AnonymousSenderTag};
use nym_sdk::mixnet::{
AnonymousSenderTag, MixnetClient, MixnetClientBuilder, ReconstructedMessage, StoragePaths,
};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
pub mod client;
@@ -76,15 +78,17 @@ pub async fn listen_and_parse_response(client: &mut MixnetClient) -> ResponseTyp
// 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();
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
// 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) {
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
@@ -99,12 +103,12 @@ pub async fn listen_and_parse_request(client: &mut MixnetClient) -> (RequestType
// 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();
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();
// get the sender_tag for anon reply
let return_recipient = message[0].sender_tag.unwrap();
(client_request, return_recipient)
}
(client_request, return_recipient)
}