forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.rs
334 lines (299 loc) · 11.5 KB
/
payload.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//! The payload (non-signature) portion of a response from an [`Invocation`].
//!
//! [`Invocation`]: crate::invocation::Invocation
use super::responds::Responds;
use crate::{
ability::arguments,
capsule::Capsule,
crypto::Nonce,
did::{Did, Verifiable},
time::Timestamp,
};
use derive_builder::Builder;
use libipld_core::{cid::Cid, error::SerdeError, ipld::Ipld, serde as ipld_serde};
use serde::{
de::{self, MapAccess, Visitor},
ser::SerializeStruct,
Deserialize, Serialize, Serializer,
};
use std::{collections::BTreeMap, fmt, fmt::Debug};
#[cfg(feature = "test_utils")]
use proptest::prelude::*;
#[cfg(feature = "test_utils")]
use crate::ipld;
#[cfg(feature = "test_utils")]
use crate::ipld::cid;
impl<T: Responds, DID: Did> Verifiable<DID> for Payload<T, DID> {
fn verifier(&self) -> &DID {
&self.issuer
}
}
/// The payload (non-signature) portion of a response from an [`Invocation`].
///
/// [`Invocation`]: crate::invocation::Invocation
#[derive(Debug, Clone, PartialEq, Builder)]
pub struct Payload<T: Responds, DID: Did> {
/// The issuer of the [`Receipt`]. This [`Did`] *must* match the signature on
/// the outer layer of [`Receipt`].
///
/// [`Receipt`]: super::Receipt
pub issuer: DID,
/// The [`Cid`] of the [`Invocation`] that was run.
///
/// [`Invocation`]: crate::invocation::Invocation
pub ran: Cid,
/// The output of the [`Invocation`]. This is always of
/// the form `{"ok": ...}` or `{"err": ...}`.
///
/// [`Invocation`]: crate::invocation::Invocation
pub out: Result<T::Success, arguments::Named<Ipld>>,
/// Any further [`Invocation`]s that the `ran` [`Invocation`]
/// requested to be queued next.
///
/// [`Invocation`]: crate::invocation::Invocation
#[builder(default)]
pub next: Vec<Cid>,
/// An optional proof chain authorizing a different [`Did`] to
/// be the receipt `iss` than the audience (or subject) of the
/// [`Invocation`] that was run.
///
/// [`Invocation`]: crate::invocation::Invocation
#[builder(default)]
pub proofs: Vec<Cid>,
/// Extensible, free-form fields.
#[builder(default)]
pub metadata: BTreeMap<String, Ipld>,
/// A [cryptographic nonce] to ensure that the UCAN's [`Cid`] is unique.
///
/// [cryptographic nonce]: https://en.wikipedia.org/wiki/Cryptographic_nonce
/// [`Cid`]: libipld_core::cid::Cid
#[builder(default = "Nonce::generate_16()")]
pub nonce: Nonce,
/// An optional [Unix timestamp] (wall-clock time) at which the
/// receipt claims to have been issued at.
///
/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time
#[builder(default)]
pub issued_at: Option<Timestamp>,
}
impl<T: Responds, DID: Did> Capsule for Payload<T, DID> {
const TAG: &'static str = "ucan/r@1.0.0-rc.1";
}
impl<T: Responds, DID: Did> From<Payload<T, DID>> for arguments::Named<Ipld>
where
Ipld: From<T::Success>,
{
fn from(payload: Payload<T, DID>) -> Self {
let out_ipld = match payload.out {
Ok(ok) => BTreeMap::from_iter([("ok".to_string(), Ipld::from(ok))]).into(),
Err(err) => BTreeMap::from_iter([("err".to_string(), err.0.into())]).into(),
};
let mut args = arguments::Named::<Ipld>::from_iter([
("iss".to_string(), Ipld::String(payload.issuer.to_string())),
("ran".to_string(), payload.ran.into()),
("out".to_string(), out_ipld),
(
"next".to_string(),
Ipld::List(
payload
.next
.clone()
.into_iter()
.map(|x| Ipld::Link(x))
.collect(),
),
),
(
"prf".to_string(),
Ipld::List(payload.next.into_iter().map(|x| Ipld::Link(x)).collect()),
),
("meta".to_string(), payload.metadata.into()),
("nonce".to_string(), payload.nonce.into()),
]);
if let Some(issued_at) = payload.issued_at {
args.insert("iat".to_string(), issued_at.into());
}
args
}
}
impl<T: Responds, DID: Did + Clone> Serialize for Payload<T, DID>
where
T::Success: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let field_count = 7 + self.issued_at.is_some() as usize;
let mut state = serializer.serialize_struct("receipt::Payload", field_count)?;
state.serialize_field("iss", &self.issuer.to_string().as_str())?;
state.serialize_field("ran", &self.ran)?;
state.serialize_field("out", &self.out)?;
state.serialize_field("next", &self.next)?;
state.serialize_field("prf", &self.proofs)?;
state.serialize_field("meta", &self.metadata)?;
state.serialize_field("nonce", &self.nonce)?;
state.serialize_field("iat", &self.issued_at)?;
state.end()
}
}
impl<'de, T: Responds, DID: Did + Deserialize<'de>> Deserialize<'de> for Payload<T, DID>
where
T::Success: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct ReceiptPayloadVisitor<T, DID>(std::marker::PhantomData<(T, DID)>);
const FIELDS: &'static [&'static str] =
&["iss", "ran", "out", "next", "prf", "meta", "nonce", "iat"];
impl<'de, T: Responds, DID: Did + Deserialize<'de>> Visitor<'de> for ReceiptPayloadVisitor<T, DID>
where
T::Success: Deserialize<'de>,
{
type Value = Payload<T, DID>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("struct delegation::Payload")
}
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut issuer = None;
let mut ran = None;
let mut out = None;
let mut next = None;
let mut proofs = None;
let mut metadata = None;
let mut nonce = None;
let mut issued_at = None;
while let Some(key) = map.next_key()? {
match key {
"iss" => {
if issuer.is_some() {
return Err(de::Error::duplicate_field("iss"));
}
issuer = Some(map.next_value()?);
}
"ran" => {
if ran.is_some() {
return Err(de::Error::duplicate_field("ran"));
}
ran = Some(map.next_value()?);
}
"out" => {
if out.is_some() {
return Err(de::Error::duplicate_field("out"));
}
out = Some(map.next_value()?);
}
"next" => {
if next.is_some() {
return Err(de::Error::duplicate_field("next"));
}
next = Some(map.next_value()?);
}
"prf" => {
if proofs.is_some() {
return Err(de::Error::duplicate_field("prf"));
}
proofs = Some(map.next_value()?);
}
"meta" => {
if metadata.is_some() {
return Err(de::Error::duplicate_field("meta"));
}
metadata = Some(map.next_value()?);
}
"nonce" => {
if nonce.is_some() {
return Err(de::Error::duplicate_field("nonce"));
}
nonce = Some(map.next_value()?);
}
"iat" => {
if issued_at.is_some() {
return Err(de::Error::duplicate_field("iat"));
}
issued_at = map.next_value()?;
}
other => {
return Err(de::Error::unknown_field(other, FIELDS));
}
}
}
Ok(Payload {
issuer: issuer.ok_or(de::Error::missing_field("iss"))?,
ran: ran.ok_or(de::Error::missing_field("ran"))?,
out: out.ok_or(de::Error::missing_field("out"))?,
next: next.ok_or(de::Error::missing_field("next"))?,
proofs: proofs.ok_or(de::Error::missing_field("prf"))?,
metadata: metadata.ok_or(de::Error::missing_field("meta"))?,
nonce: nonce.ok_or(de::Error::missing_field("nonce"))?,
issued_at,
})
}
}
deserializer.deserialize_struct(
"ReceiptPayload",
FIELDS,
ReceiptPayloadVisitor(Default::default()),
)
}
}
impl<T: Responds, DID: Did> From<Payload<T, DID>> for Ipld {
fn from(payload: Payload<T, DID>) -> Self {
payload.into()
}
}
impl<T: Responds, DID: Did> TryFrom<Ipld> for Payload<T, DID>
where
Payload<T, DID>: for<'de> Deserialize<'de>,
{
type Error = SerdeError;
fn try_from(ipld: Ipld) -> Result<Self, Self::Error> {
ipld_serde::from_ipld(ipld)
}
}
#[cfg(feature = "test_utils")]
impl<T: Responds + Debug, DID: Arbitrary + Did> Arbitrary for Payload<T, DID>
where
T::Success: Arbitrary + 'static,
DID::Parameters: Clone,
DID::Strategy: 'static,
{
type Parameters = (<T::Success as Arbitrary>::Parameters, DID::Parameters);
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with((t_params, did_params): Self::Parameters) -> Self::Strategy {
(
DID::arbitrary_with(did_params),
cid::Newtype::arbitrary(),
prop_oneof![
T::Success::arbitrary_with(t_params).prop_map(Result::Ok),
arguments::Named::arbitrary().prop_map(Result::Err),
],
prop::collection::vec(cid::Newtype::arbitrary(), 0..25),
prop::collection::vec(cid::Newtype::arbitrary(), 0..25),
prop::collection::btree_map(".*", ipld::Newtype::arbitrary(), 0..50),
Nonce::arbitrary(),
prop::option::of(Timestamp::arbitrary()),
)
.prop_map(
|(issuer, ran, out, next, proofs, newtype_metadata, nonce, issued_at)| Payload {
issuer,
ran: ran.cid,
out,
next: next.into_iter().map(|nt| nt.cid).collect(),
proofs: proofs.into_iter().map(|nt| nt.cid).collect(),
metadata: newtype_metadata
.into_iter()
.map(|(k, v)| (k, v.0))
.collect(),
nonce,
issued_at,
},
)
.boxed()
}
}