Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions examples/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ enum Commands {
help = "parent VID of the sender, used to listen for a response"
)]
parent_vid: Option<String>,
#[arg(
long,
help = "Ask for confirmation before interacting with unknown end-points"
)]
ask: bool,
#[arg(long, help = "wait for a response")]
wait: bool,
},
Expand Down Expand Up @@ -253,6 +258,31 @@ async fn read_wallet(
}
}

async fn ensure_vid_verified(
vid_wallet: &mut AsyncSecureStore,
receiver_vid: &str,
wallet_name: &str,
ask: bool,
) -> Result<(), Error> {
if !vid_wallet.has_verified_vid(receiver_vid)? {
if !ask
|| prompt(format!(
"Do you want to verify receiver DID {}",
receiver_vid
))
{
vid_wallet.verify_vid(receiver_vid, None).await?;
info!("{receiver_vid} is verified and added to the wallet {wallet_name}");
} else {
tracing::error!("Message cannot be sent without verifying the receiver's DID.");
return Err(Error::UnverifiedVid(
"Message cannot be sent without verifying the receiver's DID.".to_string(),
));
}
}
Ok(())
}

fn prompt(message: String) -> bool {
use std::io::{self, BufRead, Write};
print!("{message}? [y/n]");
Expand Down Expand Up @@ -568,18 +598,8 @@ async fn run() -> Result<(), Error> {
} => {
let non_confidential_data = non_confidential_data.as_deref().map(|s| s.as_bytes());

if !vid_wallet.has_verified_vid(&receiver_vid)? {
if !ask || prompt(format!("Do you want to verify receiver DID {receiver_vid}")) {
vid_wallet.verify_vid(&receiver_vid, None).await?;
info!(
"{receiver_vid} is verified and added to the wallet {}",
&args.wallet
);
} else {
tracing::error!("Message cannot be sent without verifying the receiver's DID.");
return Ok(());
}
}
dbg!(ask);
ensure_vid_verified(&mut vid_wallet, &receiver_vid, &args.wallet, ask).await?;

let mut message = Vec::new();
tokio::io::stdin()
Expand Down Expand Up @@ -823,8 +843,11 @@ async fn run() -> Result<(), Error> {
receiver_vid,
nested,
parent_vid,
ask,
wait,
} => {
ensure_vid_verified(&mut vid_wallet, &receiver_vid, &args.wallet, ask).await?;

// Setup receive stream before sending the request
let listener_vid = parent_vid.unwrap_or(sender_vid.clone());
let mut messages = vid_wallet.receive(&listener_vid).await?;
Expand Down
57 changes: 56 additions & 1 deletion examples/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rand::{Rng, thread_rng};
use std::process::Command as StdCommand;
use std::thread;
use std::time::Duration;

fn random_string(n: usize) -> String {
thread_rng()
.sample_iter(&Alphanumeric)
Expand Down Expand Up @@ -163,7 +164,7 @@ fn test_send_command_unverified_receiver_ask_flag() {
.stderr(predicate::str::contains(
"Message cannot be sent without verifying the receiver's DID",
))
.success();
.failure();

// Send a message from Marlon to Marc with --ask flag, answer yes
thread::scope(|s| {
Expand Down Expand Up @@ -207,6 +208,60 @@ fn test_send_command_unverified_receiver_ask_flag() {
clean_wallet();
}

#[test]
#[serial_test::serial(clean_wallet)]
fn test_request_command_unverified_receiver_default() {
clean_wallet();

// create a new sender identity to send the request
let random_sender_name = create_wallet("alice", "web");

// create a new receiver identity
let random_receiver_name = create_wallet("bob", "web");

// print the sender's DID
let alice_did = print_did(&random_sender_name, "alice");

// print the receiver's DID
let bob_did = print_did(&random_receiver_name, "bob");

thread::scope(|s| {
s.spawn(|| {
// send a relationship request from alice to bob
let mut cmd: Command = Command::new(cargo_bin!("tsp"));
cmd.args([
"--wallet",
random_sender_name.as_str(),
"request",
"-s",
"alice",
"-r",
&bob_did,
])
.assert()
.stderr(predicate::str::contains("sent relationship request"))
.success();
});
// s.spawn(|| {
// // receive the relationship request
// let mut cmd: Command = Command::new(cargo_bin!("tsp"));
// cmd.args([
// "--wallet",
// random_receiver_name.as_str(),
// "receive",
// "--one",
// &bob_did,
// ])
// .assert()
// .stderr(predicate::str::contains("received relationship request"))
// .stdout(predicate::str::contains(&alice_did))
// .success();
// });
});

clean_wallet();
}

#[test]
#[cfg(feature = "create-webvh")]
#[serial_test::serial(clean_wallet)]
Expand Down
Loading