forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invocation.rs
254 lines (227 loc) · 6.55 KB
/
invocation.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
//! An [`Invocation`] is a request to use an [`Ability`][crate::ability].
//!
//! ## Data
//!
//! - [`Invocation`] is the top-level, signed data struture.
//! - [`Payload`] is the fields unique to an invocation.
//! - [`Preset`] is an [`Invocation`] preloaded with this library's [preset abilities](crate::ability::preset::Ready).
//! - [`promise`]s are a mechanism to chain invocations together.
//!
//! ## Stateful Helpers
//!
//! - [`Agent`] is a high-level interface for sessions that will involve more than one invoctaion.
//! - [`store`] is an interface for caching [`Invocation`]s.
pub mod agent;
pub mod payload;
pub mod promise;
pub mod store;
pub use agent::Agent;
pub use payload::*;
use crate::ability::arguments::Named;
use crate::ability::command::ToCommand;
use crate::ability::parse::ParseAbility;
use crate::{
crypto::{signature::Envelope, varsig},
did::{self, Did},
time::{Expired, Timestamp},
};
use libipld_core::{
cid::Cid,
codec::{Codec, Encode},
ipld::Ipld,
};
use serde::{Deserialize, Serialize};
use web_time::SystemTime;
/// The complete, signed [`invocation::Payload`][Payload].
///
/// Invocations are the actual "doing" in the UCAN lifecycle.
/// Unlike [`Delegation`][crate::Delegation]s, which live for some period of time and
/// can be used multiple times, [`Invocation`]s are unique and single-use.
///
/// # Expiration
///
/// `Invocations` include an optional expiration field which behaves like a timeout:
/// "if this isn't run by a the expiration time, I'm going to assume that it didn't happen."
/// This is a best practice in message-passing distributed systems because the network is
/// [unreliable](https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing).
#[derive(Debug, Clone, PartialEq)]
pub struct Invocation<
A,
DID: did::Did = did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec = varsig::encoding::Preset,
> {
pub varsig_header: V,
pub payload: Payload<A, DID>,
pub signature: DID::Signature,
_marker: std::marker::PhantomData<C>,
}
impl<
A: Clone + ToCommand + ParseAbility,
DID: Clone + did::Did,
V: Clone + varsig::Header<C>,
C: Codec,
> Encode<C> for Invocation<A, DID, V, C>
where
Ipld: Encode<C>,
Named<Ipld>: From<A> + From<Payload<A, DID>>,
Payload<A, DID>: TryFrom<Named<Ipld>>,
// <A as TryFrom<Named<Ipld>>>::Error: std::fmt::Debug,
{
fn encode<W: std::io::Write>(&self, c: C, w: &mut W) -> Result<(), libipld_core::error::Error> {
self.to_ipld_envelope().encode(c, w)
}
}
impl<A: Clone + ToCommand + ParseAbility, DID: Did + Clone, V: varsig::Header<C>, C: Codec>
Invocation<A, DID, V, C>
where
Ipld: Encode<C>,
{
pub fn new(varsig_header: V, signature: DID::Signature, payload: Payload<A, DID>) -> Self {
Invocation {
varsig_header,
payload,
signature,
_marker: std::marker::PhantomData,
}
}
pub fn audience(&self) -> &Option<DID> {
&self.payload.audience
}
pub fn normalized_audience(&self) -> &DID {
if let Some(audience) = &self.payload.audience {
audience
} else {
&self.payload.subject
}
}
pub fn issuer(&self) -> &DID {
&self.payload.issuer
}
pub fn subject(&self) -> &DID {
&self.payload.subject
}
pub fn ability(&self) -> &A {
&self.payload.ability
}
pub fn map_ability<F, Z: Clone>(self, f: F) -> Invocation<Z, DID, V, C>
where
F: FnOnce(A) -> Z,
Z: ParseAbility + ToCommand,
{
Invocation::new(
self.varsig_header,
self.signature,
self.payload.map_ability(f),
)
}
pub fn proofs(&self) -> &Vec<Cid> {
&self.payload.proofs
}
pub fn issued_at(&self) -> &Option<Timestamp> {
&self.payload.issued_at
}
pub fn expiration(&self) -> &Option<Timestamp> {
&self.payload.expiration
}
pub fn check_time(&self, now: SystemTime) -> Result<(), Expired> {
self.payload.check_time(now)
}
}
impl<A, DID: Did, V: varsig::Header<C>, C: Codec> did::Verifiable<DID>
for Invocation<A, DID, V, C>
{
fn verifier(&self) -> &DID {
&self.payload.verifier()
}
}
impl<
A: Clone + ToCommand + ParseAbility,
DID: Did + Clone,
V: varsig::Header<C> + Clone,
C: Codec,
> From<Invocation<A, DID, V, C>> for Ipld
where
Named<Ipld>: From<A>,
Payload<A, DID>: TryFrom<Named<Ipld>>,
{
fn from(invocation: Invocation<A, DID, V, C>) -> Self {
invocation.to_ipld_envelope()
}
}
impl<
A: Clone + ToCommand + ParseAbility,
DID: Did + Clone,
V: varsig::Header<C> + Clone,
C: Codec,
> Envelope for Invocation<A, DID, V, C>
where
Named<Ipld>: From<A>,
Payload<A, DID>: TryFrom<Named<Ipld>>,
{
type DID = DID;
type Payload = Payload<A, DID>;
type VarsigHeader = V;
type Encoder = C;
fn construct(
varsig_header: V,
signature: DID::Signature,
payload: Payload<A, DID>,
) -> Invocation<A, DID, V, C> {
Invocation {
varsig_header,
payload,
signature,
_marker: std::marker::PhantomData,
}
}
fn varsig_header(&self) -> &V {
&self.varsig_header
}
fn payload(&self) -> &Payload<A, DID> {
&self.payload
}
fn signature(&self) -> &DID::Signature {
&self.signature
}
fn verifier(&self) -> &DID {
&self.payload.issuer
}
}
impl<
A: Clone + ToCommand + ParseAbility,
DID: Did + Clone,
V: varsig::Header<C> + Clone,
C: Codec,
> Serialize for Invocation<A, DID, V, C>
where
Named<Ipld>: From<A>,
Payload<A, DID>: TryFrom<Named<Ipld>>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_ipld_envelope().serialize(serializer)
}
}
impl<
'de,
A: Clone + ToCommand + ParseAbility,
DID: Did + Clone,
V: varsig::Header<C> + Clone,
C: Codec,
> Deserialize<'de> for Invocation<A, DID, V, C>
where
Named<Ipld>: From<A>,
Payload<A, DID>: TryFrom<Named<Ipld>>,
<Payload<A, DID> as TryFrom<Named<Ipld>>>::Error: std::fmt::Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let ipld = Ipld::deserialize(deserializer)?;
Self::try_from_ipld_envelope(ipld).map_err(serde::de::Error::custom)
}
}