forked from sicpa-dlab/didcomm-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_did.rs
135 lines (111 loc) · 3.98 KB
/
rotate_did.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
#[allow(unused_imports, dead_code)]
#[path = "../src/test_vectors/mod.rs"]
mod test_vectors;
// TODO: look for better solution
// Allows test vectors usage inside and outside crate
pub(crate) use didcomm;
use didcomm::{
did::resolvers::ExampleDIDResolver, protocols::routing::try_parse_forward,
secrets::resolvers::ExampleSecretsResolver, FromPrior, Message, PackEncryptedOptions,
UnpackOptions,
};
use serde_json::json;
use test_vectors::{
ALICE_DID, ALICE_DID_DOC, BOB_DID, BOB_DID_DOC, BOB_SECRETS, CHARLIE_DID, CHARLIE_DID_DOC,
CHARLIE_ROTATED_TO_ALICE_SECRETS, MEDIATOR1_DID_DOC, MEDIATOR1_SECRETS,
};
#[tokio::main(flavor = "current_thread")]
async fn main() {
let did_resolver = ExampleDIDResolver::new(vec![
ALICE_DID_DOC.clone(),
BOB_DID_DOC.clone(),
CHARLIE_DID_DOC.clone(),
MEDIATOR1_DID_DOC.clone(),
]);
let secrets_resolver = ExampleSecretsResolver::new(CHARLIE_ROTATED_TO_ALICE_SECRETS.clone());
// --- Building from_prior header
let from_prior = FromPrior::build(CHARLIE_DID.into(), ALICE_DID.into())
.aud("123".into())
.exp(1234)
.nbf(12345)
.iat(123456)
.jti("dfg".into())
.finalize();
println!("Original from_prior is\n{:?}\n", from_prior);
let (from_prior, issuer_kid) = from_prior
.pack(None, &did_resolver, &secrets_resolver)
.await
.expect("Unable pack from_prior");
println!("Packed from_prior is\n{}\n", from_prior);
println!("from_prior issuer kid is\n{}\n", issuer_kid);
// --- Building message from ALICE (ex-CHARLIE) to BOB ---
let msg = Message::build(
"1234567890".to_owned(),
"http://example.com/protocols/lets_do_lunch/1.0/proposal".to_owned(),
json!({"messagespecificattribute": "and its value"}),
)
.from(ALICE_DID.to_owned())
.to(BOB_DID.to_owned())
.created_time(1516269022)
.expires_time(1516385931)
.from_prior(from_prior)
.finalize();
println!("Original message is\n{:?}\n", msg);
// --- Packing encrypted and authenticated message ---
let (msg, metadata) = msg
.pack_encrypted(
BOB_DID,
Some(ALICE_DID),
None,
&did_resolver,
&secrets_resolver,
&PackEncryptedOptions::default(),
)
.await
.expect("Unable pack_encrypted");
println!("Encryption metadata is\n{:?}\n", metadata);
// --- Sending message by Alice ---
println!("Alice is sending message \n{}\n", msg);
// --- Unpacking message by Mediator1 ---
let did_resolver = ExampleDIDResolver::new(vec![
ALICE_DID_DOC.clone(),
BOB_DID_DOC.clone(),
CHARLIE_DID_DOC.clone(),
MEDIATOR1_DID_DOC.clone(),
]);
let secrets_resolver = ExampleSecretsResolver::new(MEDIATOR1_SECRETS.clone());
let (msg, metadata) = Message::unpack(
&msg,
&did_resolver,
&secrets_resolver,
&UnpackOptions::default(),
)
.await
.expect("Unable unpack");
println!("Mediator1 received message is \n{:?}\n", msg);
println!(
"Mediator1 received message unpack metadata is \n{:?}\n",
metadata
);
// --- Forwarding message by Mediator1 ---
let msg = serde_json::to_string(&try_parse_forward(&msg).unwrap().forwarded_msg).unwrap();
println!("Mediator1 is forwarding message \n{}\n", msg);
// --- Unpacking message by Bob ---
let did_resolver = ExampleDIDResolver::new(vec![
ALICE_DID_DOC.clone(),
BOB_DID_DOC.clone(),
CHARLIE_DID_DOC.clone(),
MEDIATOR1_DID_DOC.clone(),
]);
let secrets_resolver = ExampleSecretsResolver::new(BOB_SECRETS.clone());
let (msg, metadata) = Message::unpack(
&msg,
&did_resolver,
&secrets_resolver,
&UnpackOptions::default(),
)
.await
.expect("Unable unpack");
println!("Bob received message is \n{:?}\n", msg);
println!("Bob received message unpack metadata is \n{:?}\n", metadata);
}