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
335
336
337
338
339
340
341
342
343
344
345
346
347
//! DID Methods.
//!
//! This library provides Decentralized Identifiers (DIDs), a type of
//! identifier defined by the W3C that enables verifiable, self-sovereign
//! digital identities.
//! Unlike traditional identifiers, such as email addresses or usernames,
//! DIDs are not tied to a central authority. Instead, they are generated and
//! managed on decentralized networks like blockchains, providing greater
//! privacy, security, and control to the individual or entity that owns them.
//!
//! Each DID is an URI using the `did` scheme. This library uses the [`DID`] and
//! [`DIDBuf`] (similar to [`str`] and [`String`]) to parse and store DIDs.
//!
//! ```
//! use ssi_dids::{DID, DIDBuf};
//!
//! // Create a `&DID` from a `&str`.
//! let did = DID::new("did:web:w3c-ccg.github.io:user:alice").unwrap();
//!
//! // Create a `DIDBuf` from a `String`.
//! let owned_did = DIDBuf::from_string("did:web:w3c-ccg.github.io:user:alice".to_owned()).unwrap();
//! ```
//!
//! Just like regular URLs, it is possible to provide the DID with a fragment
//! part. The result is a DID URL, which can be parsed and stored using
//! [`DIDURL`] and [`DIDURLBuf`].
//!
//! ```
//! use ssi_dids::{DIDURL, DIDURLBuf};
//!
//! // Create a `&DIDURL` from a `&str`.
//! let did_url = DIDURL::new("did:web:w3c-ccg.github.io:user:alice#key").unwrap();
//!
//! // Create a `DIDBuf` from a `String`.
//! let owned_did_url = DIDURLBuf::from_string("did:web:w3c-ccg.github.io:user:bob#key".to_owned()).unwrap();
//! ```
//!
//! Note that a DID URL, with a fragment, is not a valid DID.
//!
//! # DID document resolution
//!
//! DID resolution is the process of retrieving the DID document associated with
//! a specific DID.
//! A DID document is a JSON-LD formatted file that contains crucial information
//! needed to interact with the DID, such as verification methods containing the
//! user's public keys, and service endpoints. Here is an example DID document:
//! ```json
//! {
//! "@context": [
//! "https://www.w3.org/ns/did/v1",
//! "https://w3id.org/security/suites/jws-2020/v1"
//! ],
//! "id": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9",
//! "verificationMethod": [
//! {
//! "id": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0",
//! "type": "JsonWebKey2020",
//! "controller": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9",
//! "publicKeyJwk": {
//! "crv": "P-256",
//! "kty": "EC",
//! "x": "acbIQiuMs3i8_uszEjJ2tpTtRM4EU3yz91PH6CdH2V0",
//! "y": "_KcyLj9vWMptnmKtm46GqDz8wf74I5LKgrl2GzH3nSE"
//! }
//! }
//! ],
//! "assertionMethod": ["did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0"],
//! "authentication": ["did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0"],
//! "capabilityInvocation": ["did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0"],
//! "capabilityDelegation": ["did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0"],
//! "keyAgreement": ["did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0"]
//! }
//! ```
//!
//! DID documents are represented using the [`Document`] type and can be
//! resolved from a DID using any implementation of the [`DIDResolver`] trait.
//! The [`AnyDidMethod`] type is provided as a default implementation for
//! [`DIDResolver`] that supports various *DID methods* (see below).
//!
//! ```
//! # #[async_std::main] async fn main() {
//! use ssi_dids::{DID, AnyDidMethod, DIDResolver};
//!
//! // Create a DID.
//! let did = DID::new("did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9").unwrap();
//!
//! // Setup the DID resolver.
//! let resolver = AnyDidMethod::default();
//!
//! // Resolve the DID document (equal to the example document above).
//! let document = resolver.resolve(did).await.unwrap().document;
//!
//! // Extract the first verification method.
//! let vm = document.verification_method.first().unwrap();
//! # }
//! ```
//!
//! Instead of resolving a DID document and extracting verification methods
//! manually, you can use the `dereference` method to resolve a DID URL:
//! ```
//! # #[async_std::main] async fn main() {
//! use ssi_dids::{DIDURL, AnyDidMethod, DIDResolver};
//!
//! // Create a DID URL with the fragment `#0` referencing a verification method.
//! let did_url = DIDURL::new("did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9#0").unwrap();
//!
//! // Setup the DID resolver.
//! let resolver = AnyDidMethod::default();
//!
//! // Dereference the verification method.
//! let vm = resolver
//! .dereference(did_url)
//! .await
//! .unwrap()
//! .content
//! .into_verification_method()
//! .unwrap();
//! # }
//! ```
//!
//! # DID methods
//!
//! A key component of the DID system is the concept of DID methods. A DID
//! method defines how a specific type of DID is created, resolved, updated,
//! and deactivated on a particular decentralized network or ledger.
//! Each DID method corresponds to a unique identifier format and a set of
//! operations that can be performed on the associated DIDs.
//! The general syntax of DIDs depends on the method used:
//!
//! ```text
//! did:method:method-specific-id
//! ```
//!
//! There exists various DID methods, each defined by their own specification.
//! In this library, methods are defining by implementing the [`DIDMethod`]
//! trait. Implementations are provided for the following methods:
//! - [`did:key`](https://w3c-ccg.github.io/did-method-key/): for static
//! cryptographic keys, implemented by [`DIDKey`].
//! - [`did:jwk`](https://github.com/quartzjer/did-jwk/blob/main/spec.md):
//! for [Json Web Keys (JWK)](https://datatracker.ietf.org/doc/html/rfc7517)
//! implemented by [`DIDJWK`].
//! - [`did:web`](https://w3c-ccg.github.io/did-method-web/): for web-hosted DID
//! documents, implemented by [`DIDWeb`].
//! - [`did:pkh`](https://github.com/w3c-ccg/did-pkh/blob/main/did-pkh-method-draft.md):
//! implemented by [`DIDPKH`].
//! - [`did:ethr`](https://github.com/decentralized-identity/ethr-did-resolver/):
//! implemented by [`DIDEthr`].
//! - [`did:ion`](https://identity.foundation/sidetree/spec/v1.0.0/#did-uri-composition):
//! implemented by [`DIDION`].
//! - [`did:tz`](https://github.com/spruceid/did-tezos/): implemented by
//! [`DIDTz`].
//!
//! The [`AnyDidMethod`] regroups all those methods into one [`DIDResolver`]
//! implementation.
//!
//! DID method types can also be used to generate fresh DID URLs:
//! ```
//! use ssi_jwk::JWK;
//! use ssi_dids::DIDJWK;
//!
//! /// Generate a new JWK.
//! let jwk = JWK::generate_p256();
//!
//! /// Generate a DID URL out of our JWK URL.
//! let did_url = DIDJWK::generate_url(&jwk);
//! ```
// Re-export core definitions.
pub use *;
// Re-export DID methods implementations.
pub use did_ethr as ethr;
pub use did_ion as ion;
pub use did_jwk as jwk;
pub use did_method_key as key;
pub use did_pkh as pkh;
pub use did_tz as tz;
pub use did_web as web;
pub use DIDEthr;
pub use DIDION;
pub use DIDJWK;
pub use DIDKey;
pub use DIDPKH;
pub use DIDTz;
pub use DIDWeb;
/// DID generation error.
///
/// Error raised by the [`AnyDidMethod::generate`] method.
/// DID resolver for any known DID method.
///
/// This type regroups all the [`DIDMethod`] implementations provided by `ssi`
/// into a single [`DIDResolver`] trait implementation.
///
/// # Supported methods
///
/// Here is the list of DID methods currently supported by this resolver:
/// - [`did:key`](https://w3c-ccg.github.io/did-method-key/): for static
/// cryptographic keys, implemented by [`DIDKey`].
/// - [`did:jwk`](https://github.com/quartzjer/did-jwk/blob/main/spec.md):
/// for [Json Web Keys (JWK)](https://datatracker.ietf.org/doc/html/rfc7517)
/// implemented by [`DIDJWK`].
/// - [`did:web`](https://w3c-ccg.github.io/did-method-web/): for web-hosted DID
/// documents, implemented by [`DIDWeb`].
/// - [`did:pkh`](https://github.com/w3c-ccg/did-pkh/blob/main/did-pkh-method-draft.md):
/// implemented by [`DIDPKH`].
/// - [`did:ethr`](https://github.com/decentralized-identity/ethr-did-resolver/):
/// implemented by [`DIDEthr`].
/// - [`did:ion`](https://identity.foundation/sidetree/spec/v1.0.0/#did-uri-composition):
/// implemented by [`DIDION`].
/// - [`did:tz`](https://github.com/spruceid/did-tezos/): implemented by
/// [`DIDTz`].