-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalice_bob.rs
181 lines (153 loc) · 4.77 KB
/
alice_bob.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Sends a message from Alice to Bob and then retrieves it.
use affinidi_messaging_didcomm::Message;
use affinidi_messaging_helpers::common::profiles::Profiles;
use affinidi_messaging_sdk::{config::Config, errors::ATMError, protocols::Protocols, ATM};
use clap::Parser;
use serde_json::json;
use std::{
env,
time::{Duration, SystemTime},
};
use tracing::debug;
use tracing_subscriber::filter;
use uuid::Uuid;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long)]
profile: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), ATMError> {
let args: Args = Args::parse();
let (profile_name, profile) = Profiles::smart_load(args.profile, env::var("AM_PROFILE").ok())
.map_err(|err| ATMError::ConfigError(err.to_string()))?;
println!("Using Profile: {}", profile_name);
// construct a subscriber that prints formatted traces to stdout
let subscriber = tracing_subscriber::fmt()
// Use a more compact, abbreviated log format
.with_env_filter(filter::EnvFilter::from_default_env())
.finish();
// use that subscriber to process traces emitted after this point
tracing::subscriber::set_global_default(subscriber).expect("Logging failed, exiting...");
let alice = if let Some(alice) = profile.friends.get("Alice") {
alice
} else {
return Err(ATMError::ConfigError(
format!("Alice not found in Profile: {}", profile_name).to_string(),
));
};
let bob = if let Some(bob) = profile.friends.get("Bob") {
bob
} else {
return Err(ATMError::ConfigError(
format!("Bob not found in Profile: {}", profile_name).to_string(),
));
};
let mut config = Config::builder();
if let Some(ssl_cert) = &profile.ssl_certificate {
config = config.with_ssl_certificates(&mut vec![ssl_cert.to_string()]);
println!("Using SSL Certificate: {}", ssl_cert);
}
// Create a new ATM Client
let atm = ATM::new(config.build()?).await?;
let protocols = Protocols::new();
debug!("Enabling Alice's Profile");
let alice = atm
.profile_add(&alice.into_profile(&atm).await?, true)
.await?;
debug!("Enabling Bob's Profile");
let bob = atm
.profile_add(&bob.into_profile(&atm).await?, true)
.await?;
let start = SystemTime::now();
// Ensure Profile has a valid mediator to forward through
let mediator_did = if let Some(mediator) = profile.default_mediator {
mediator.clone()
} else {
return Err(ATMError::ConfigError(
"Profile Mediator not found".to_string(),
));
};
// Create message from Alice to Bob
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
let msg = Message::build(
Uuid::new_v4().into(),
"Chatty Alice".into(),
json!("Hello Bob!"),
)
.to(bob.inner.did.clone())
.from(alice.inner.did.clone())
.created_time(now)
.expires_time(now + 10)
.finalize();
let msg_id = msg.id.clone();
println!(
"Plaintext Message from Alice to Bob msg_id({}):\n {:#?}",
msg_id, msg
);
println!();
let packed_msg = atm
.pack_encrypted(
&msg,
&bob.inner.did,
Some(&alice.inner.did),
Some(&alice.inner.did),
)
.await?;
println!(
"Packed encrypted+signed message from Alice to Bob:\n{:#?}",
packed_msg.0
);
println!();
// Wrap it in a forward
let (forward_id, forward_msg) = protocols
.routing
.forward_message(
&atm,
&alice,
&packed_msg.0,
&mediator_did,
&bob.inner.did,
None,
None,
)
.await?;
println!(
"Forwarded message from Alice to Mediator:\n{:#?}",
forward_msg
);
println!();
// Send the message
atm.send_message(&alice, &forward_msg, &forward_id, false, false)
.await?;
println!("Alice sent message to Bob");
// Bob gets his messages
println!();
println!("Bob receiving messages");
match protocols
.message_pickup
.live_stream_get(&atm, &bob, true, &msg_id, Duration::from_secs(5), true)
.await?
{
Some(msg) => {
println!();
println!(
"Decrypted Message from Alice to Bob msg_id({}):\n {:#?}\n",
msg_id, msg.0
);
}
None => {
println!("No messages found. Exiting...");
}
}
let end = SystemTime::now();
println!(
"Forwarding Example took {}ms in total",
end.duration_since(start).unwrap().as_millis(),
);
Ok(())
}