clippy + fmt + minor logging change

This commit is contained in:
mfahampshire
2023-07-21 11:50:19 +02:00
parent b5da9392ae
commit ed8e54c18a
5 changed files with 23 additions and 28 deletions
+5 -6
View File
@@ -9,7 +9,7 @@ use rust_cosmos_broadcaster::{
#[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 "
about = "demo binary with which users can perform offline signing and transmission of signed token tx to broadcaster via the mixnet "
)]
struct Cli {
#[clap(subcommand)]
@@ -26,7 +26,7 @@ enum Commands {
#[derive(Debug, Clone, Args)]
struct OfflineSignTx {
/// mnemonic of signing + sending account (you!)
/// mnemonic of signing + sending account (you!)
mnemonic: bip39::Mnemonic,
/// recipient nyx chain address for token transfer
nyx_token_receipient: AccountId,
@@ -40,12 +40,10 @@ struct SendTx {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let mut client = create_client("/tmp/cosmos-broadcaster-mixnet-client-5".into()).await;
let our_address = client.nym_address();
println!("\nclient's nym address: {our_address}");
let sp_address = Recipient::try_from_base58_string("2f499xz7AfEmsdjd9zaxEVMZ4ed5pod2AqomZ74PSdTW.6heKJmwFZMw14Yz7CKF56iyKDaBBssmNWZJHErGg5jgm@HWdr8jgcr32cVGbjisjmwnVF4xrUBRGvbw86F9e3rFzS").unwrap();
match cli.command {
@@ -66,7 +64,7 @@ async fn main() -> anyhow::Result<()> {
"Encoded response (signed tx data) as base58 for tx broadcast: \n\n{:?}\n",
&base58_tx_bytes.as_ref()
);
println!("do you wish to send the tx? y/n");
println!("do you also wish to send the tx? y/n");
let mut input = String::new();
let stdin = std::io::stdin();
@@ -74,7 +72,8 @@ async fn main() -> anyhow::Result<()> {
if input.starts_with('y') {
println!("\nsending pre-signed tx through the mixnet to broadcaster service");
let (tx_hash, success) = send_tx(base58_tx_bytes.unwrap(), sp_address, &mut client).await?;
let (tx_hash, success) =
send_tx(base58_tx_bytes.unwrap(), sp_address, &mut client).await?;
println!(
"tx hash returned from the broadcaster: {}\ntx was successful: {}",
tx_hash, success
+3 -5
View File
@@ -17,7 +17,7 @@ async fn main() -> anyhow::Result<()> {
loop {
// listen out for incoming requests from mixnet, parse and match them
let request: (RequestTypes, AnonymousSenderTag) =
listen_and_parse_request(&mut client).await?;
listen_and_parse_request(&mut client).await?;
// grab sender_tag from parsed request for anonymous replies
let return_recipient: AnonymousSenderTag = request.1;
match request.0 {
@@ -28,8 +28,7 @@ async fn main() -> anyhow::Result<()> {
);
// query chain for sequence information on behalf of request sender
let sequence: SequenceRequestResponse =
get_sequence(broadcaster.clone(), request.signer_address)
.await?;
get_sequence(broadcaster.clone(), request.signer_address).await?;
println!("sequence information returned from chain: account number: {}, sequence:{}, chain id: {} \nsending response to requesting client via mixnet", sequence.account_number, sequence.sequence, sequence.chain_id);
// send serialised sequence response back to request sender via mixnet
client
@@ -43,8 +42,7 @@ async fn main() -> anyhow::Result<()> {
);
// broadcast the signed tx on behalf of request sender
let tx_hash: BroadcastResponse =
broadcast(request.base58_tx_bytes, broadcaster.clone())
.await?;
broadcast(request.base58_tx_bytes, broadcaster.clone()).await?;
println!("return recipient surb bucket: {}", &return_recipient);
// send response to tx (transaction hash) back to request sender via mixnet
client
+4 -4
View File
@@ -30,12 +30,12 @@ pub async fn offline_sign(
signer_address: signer_address.clone(), // our (sender) address, derived from mnemonic
};
// send req to service via the mixnet
// send req to service via the mixnet
client
.send_str(sp_address, &serde_json::to_string(&message).unwrap())
.await;
// listen for response from service
// listen for response from service
let sp_response = crate::listen_and_parse_response(client).await?;
// match JSON -> ResponseType
@@ -81,7 +81,7 @@ pub async fn offline_sign(
.unwrap();
let tx_bytes = tx_raw.to_bytes().unwrap();
// encode tx bytes as base58 for ease of logging + copying for user
// encode tx bytes as base58 for ease of logging + copying for user
let base58_tx_bytes = bs58::encode(tx_bytes).into_string();
base58_tx_bytes
}
@@ -108,7 +108,7 @@ pub async fn send_tx(
.await;
println!("Waiting for reply");
// again, listen for response and parse accordingly
// again, listen for response and parse accordingly
let sp_response = crate::listen_and_parse_response(client).await?;
let res = match sp_response {
+5 -7
View File
@@ -63,9 +63,7 @@ pub async fn create_client(config_path: PathBuf) -> MixnetClient {
// 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 async fn listen_and_parse_response(
client: &mut MixnetClient
) -> anyhow::Result<ResponseTypes> {
pub async fn listen_and_parse_response(client: &mut MixnetClient) -> anyhow::Result<ResponseTypes> {
let mut message: Vec<ReconstructedMessage> = Vec::new();
// get the actual message - discard the empty vec sent along with the SURB topup request
@@ -80,9 +78,9 @@ pub async fn listen_and_parse_response(
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone())?;
parsed = String::from_utf8(r.message.clone())?;
}
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed)?;
let sp_response: crate::ResponseTypes = serde_json::from_str(&parsed)?;
Ok(sp_response)
}
@@ -105,9 +103,9 @@ pub async fn listen_and_parse_request(
// parse vec<u8> -> JSON String
let mut parsed = String::new();
if let Some(r) = message.iter().next() {
parsed = String::from_utf8(r.message.clone())?;
parsed = String::from_utf8(r.message.clone())?;
}
let client_request: crate::RequestTypes = serde_json::from_str(&parsed)?;
let client_request: crate::RequestTypes = serde_json::from_str(&parsed)?;
// get the sender_tag for anon reply
let return_recipient = message[0].sender_tag.unwrap();
+6 -6
View File
@@ -2,9 +2,9 @@ use crate::DEFAULT_VALIDATOR_RPC;
use bs58;
use cosmrs::rpc::{Client, HttpClient};
use cosmrs::{tendermint, AccountId};
use nym_validator_client::nyxd::{CosmWasmClient,error::NyxdError};
use nym_validator_client::nyxd::{error::NyxdError, CosmWasmClient};
pub async fn create_broadcaster() -> anyhow::Result<HttpClient >{
pub async fn create_broadcaster() -> anyhow::Result<HttpClient> {
let broadcaster: HttpClient = HttpClient::new(DEFAULT_VALIDATOR_RPC)?;
Ok(broadcaster)
}
@@ -12,10 +12,10 @@ pub async fn create_broadcaster() -> anyhow::Result<HttpClient >{
pub async fn get_sequence(
broadcaster: HttpClient,
signer_address: AccountId,
) -> Result<crate::SequenceRequestResponse, NyxdError> {
) -> Result<crate::SequenceRequestResponse, NyxdError> {
// get signer information
let sequence = broadcaster.get_sequence(&signer_address).await?;
let chain_id: tendermint::chain::Id = broadcaster.get_chain_id().await?;
let sequence = broadcaster.get_sequence(&signer_address).await?;
let chain_id: tendermint::chain::Id = broadcaster.get_chain_id().await?;
Ok(crate::SequenceRequestResponse {
account_number: sequence.account_number,
sequence: sequence.sequence,
@@ -60,7 +60,7 @@ pub async fn broadcast(
println!("balance after transaction: {after}");
println!("returning tx hash to sender");
let success: bool = broadcast_res.deliver_tx.code.is_ok();
let success: bool = broadcast_res.deliver_tx.code.is_ok();
Ok(crate::BroadcastResponse {
tx_hash: serde_json::to_string(&broadcast_res.hash).unwrap(),