forked from iotaledger/streams-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant_and_revoke_access.rs
285 lines (246 loc) · 9.76 KB
/
grant_and_revoke_access.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use iota_streams::{
app::transport::tangle::client::Client,
app_channels::api::tangle::{
Address, Author, Bytes, ChannelType, PublicKey, Subscriber, UnwrappedMessage,
},
core::{println, Result},
};
use crate::examples::{verify_messages, ALPH9};
use rand::Rng;
use core::str::FromStr;
pub async fn example(node_url: &str) -> Result<()> {
// Generate a unique seed for the author
let seed: &str = &(0..81)
.map(|_| {
ALPH9
.chars()
.nth(rand::thread_rng().gen_range(0, 27))
.unwrap()
})
.collect::<String>();
// Create the Transport Client
let client = Client::new_from_url(node_url);
// Generate an Author
let mut author = Author::new(seed, ChannelType::MultiBranch, client.clone());
// Create the channel with an announcement message. Make sure to save the resulting link somewhere,
let announcement_link = author.send_announce().await?;
// This link acts as a root for the channel itself
let ann_link_string = announcement_link.to_string();
println!(
"Announcement Link: {}\nTangle Index: {:#}\n",
ann_link_string, announcement_link.to_msg_index()
);
// ------------------------------------------------------------------
// In their own separate instances generate the subscriber(s) that will be attaching to the channel
// This subscriber will subscribe traditionally
let mut subscriber_a = Subscriber::new("SubscriberA", client.clone());
// This subscriber will be added later in the channel
let mut subscriber_b = Subscriber::new("SubscriberB", client);
// Generate an Address object from the provided announcement link string from the Author
let ann_address = Address::from_str(&ann_link_string)?;
// Receive the announcement message to start listening to the channel
subscriber_a.receive_announcement(&ann_address).await?;
subscriber_b.receive_announcement(&ann_address).await?;
// Subs A and B send subscription messages linked to announcement message
let subscribe_msg_a = subscriber_a.send_subscribe(&ann_address).await?;
let subscribe_msg_b = subscriber_b.send_subscribe(&ann_address).await?;
let sub_a_pk = subscriber_a.get_public_key().as_bytes();
let sub_b_pk = subscriber_b.get_public_key().as_bytes();
// These are the subscription links that should be provided to the Author to complete
// subscription for users A and B
let sub_msg_a_str = subscribe_msg_a.to_string();
let sub_msg_b_str = subscribe_msg_b.to_string();
println!(
"Subscription msgs:\n\tSubscriber A: {}\n\tTangle Index: {:#}\n\tSubscriber B: {}\n\tTangle Index: {:#}\n",
sub_msg_a_str, subscribe_msg_a.to_msg_index(), sub_msg_b_str, subscribe_msg_b.to_msg_index(),
);
// ----------------------------------------------------------------------
// Get Address object from subscription message link provided by Subscriber A
let sub_a_address = Address::from_str(&sub_msg_a_str)?;
// Get Address object from subscription message link provided by Subscriber B
let sub_b_address = Address::from_str(&sub_msg_b_str)?;
// Author processes subscribers A and B
author.receive_subscribe(&sub_a_address).await?;
author.receive_subscribe(&sub_b_address).await?;
// Expectant users are now ready to be included in Keyload messages
// Author sends keyload with the public key of Sub A (linked to announcement message) to generate
// a new branch. This will return a tuple containing the message links. The first is the message
// link itself, the second is an optional sequencing message.
let (_keyload_a_link, seq_a_link) = author.send_keyload(
&announcement_link,
&vec![PublicKey::from_bytes(sub_a_pk)?.into()],
).await?;
println!(
"\nSent Keyload for Sub A: {}, sequence tangle index: {:#}\n",
_keyload_a_link,
seq_a_link.as_ref().unwrap().to_msg_index()
);
// Author will now send signed encrypted messages to Sub A in a chain attached to Keyload A
let msg_inputs_a = vec![
"These",
"Messages",
"Will",
"Be",
"Masked",
"And",
"Only",
"Readable",
"By",
"Subscriber",
"A",
];
let mut prev_msg_link = _keyload_a_link;
let mut seq_msg_link = seq_a_link.unwrap();
for input in &msg_inputs_a {
let (msg_link, seq_link) = author.send_signed_packet(
&prev_msg_link,
&Bytes::default(),
&Bytes(input.as_bytes().to_vec()),
).await?;
let seq_link = seq_link.unwrap();
println!("Sent msg for Sub A: {}, tangle index: {:#}", msg_link, msg_link.to_msg_index());
prev_msg_link = msg_link;
seq_msg_link = seq_link;
}
// Author will send the second Keyload with the Public Key of Subscriber B attached to the
// sequence message of the previous message.
//
// ** In order to allow users to access the message without having permission for the previous
// messages, the keyload can be attached to the sequence message link, since the sequence message
// link is stored in state regardless of user access to the referenced message.
let (_keyload_b_link, seq_b_link) = author.send_keyload(
&seq_msg_link,
&vec![PublicKey::from_bytes(sub_b_pk)?.into()]
).await?;
println!(
"\nSent Keyload granting Sub B Forward Access, while revoking Sub A: {}, sequence tangle index: {:#}\n",
_keyload_b_link,
seq_b_link.as_ref().unwrap().to_msg_index()
);
// Author will now send signed encrypted messages to Sub B in a chain attached to Keyload B
let msg_inputs_b = vec![
"These",
"Messages",
"Will",
"Be",
"Masked",
"And",
"Only",
"Readable",
"By",
"Subscriber",
"B",
];
prev_msg_link = _keyload_b_link;
seq_msg_link = seq_b_link.unwrap();
for input in &msg_inputs_b {
let (msg_link, seq_link) = author.send_signed_packet(
&prev_msg_link,
&Bytes::default(),
&Bytes(input.as_bytes().to_vec()),
).await?;
let seq_link = seq_link.unwrap();
println!("Sent msg for Sub B: {}, tangle index: {:#}", msg_link, msg_link.to_msg_index());
prev_msg_link = msg_link;
seq_msg_link = seq_link;
}
// Author will send the third Keyload with the Public Key of Subscriber A again attached to the
// sequence message of the previous message.
//
// ** In order to allow users to access the message without having permission for the previous
// messages, the keyload can be attached to the sequence message link, since the sequence message
// link is stored in state regardless of user access to the referenced message.
let (_keyload_c_link, seq_c_link) = author.send_keyload(
&seq_msg_link,
&vec![PublicKey::from_bytes(sub_a_pk)?.into()]
).await?;
println!(
"\nSent Keyload granting Sub A Forward Access again, while revoking Sub B: {}, sequence tangle index: {:#}\n",
_keyload_c_link,
seq_c_link.as_ref().unwrap().to_msg_index()
);
// Author will send signed encrypted messages to Sub A again in a chain attached to Keyload C
let msg_inputs_c = vec![
"These",
"Messages",
"Will",
"Be",
"Masked",
"And",
"Only",
"Readable",
"By",
"Subscriber",
"A",
"Again",
];
prev_msg_link = _keyload_c_link;
for input in &msg_inputs_c {
let (msg_link, seq_link) = author.send_signed_packet(
&prev_msg_link,
&Bytes::default(),
&Bytes(input.as_bytes().to_vec()),
).await?;
let seq_link = seq_link.unwrap();
println!("Sent msg for Sub A again: {}, tangle index: {:#}", msg_link, msg_link.to_msg_index());
prev_msg_link = msg_link;
}
// -----------------------------------------------------------------------------
// Subscribers can now fetch these messages
let mut retrieved = subscriber_a.fetch_all_next_msgs().await;
let (retrieveda, retrievedb, retrieveda2) = split_retrieved(
&mut retrieved,
msg_inputs_a.len(),
msg_inputs_b.len(),
msg_inputs_c.len(),
);
println!("\nVerifying message retrieval: SubscriberA");
verify_messages(&msg_inputs_a, retrieveda)?;
verify_messages(&[], retrievedb)?;
verify_messages(&msg_inputs_c, retrieveda2)?;
retrieved = subscriber_b.fetch_all_next_msgs().await;
let (retrieveda, retrievedb, retrieveda2) = split_retrieved(
&mut retrieved,
msg_inputs_a.len(),
msg_inputs_b.len(),
msg_inputs_c.len(),
);
println!("\nVerifying message retrieval: SubscriberB");
verify_messages(&[], retrieveda)?;
verify_messages(&msg_inputs_b, retrievedb)?;
verify_messages(&[], retrieveda2)?;
Ok(())
}
fn split_retrieved(
retrieved: &mut Vec<UnwrappedMessage>,
len1: usize,
len2: usize,
len3: usize,
) -> (
Vec<UnwrappedMessage>,
Vec<UnwrappedMessage>,
Vec<UnwrappedMessage>,
) {
let mut retrieved_msgs_a = Vec::new();
let mut retrieved_msgs_b = Vec::new();
let mut retrieved_msgs_a2 = Vec::new();
// Keyload A
retrieved.remove(0);
for _ in 0..len1 {
// Messages for sub A
retrieved_msgs_a.push(retrieved.remove(0));
}
// Keyload B
retrieved.remove(0);
for _ in 0..len2 {
// Messages for sub B
retrieved_msgs_b.push(retrieved.remove(0));
}
// Keyload C
retrieved.remove(0);
for _ in 0..len3 {
// Messages for sub A again
retrieved_msgs_a2.push(retrieved.remove(0));
}
(retrieved_msgs_a, retrieved_msgs_b, retrieved_msgs_a2)
}