credibil_did/
lib.rs

1//! # DID Resolver
2//!
3//! This crate provides common utilities for the Credibil project and is not
4//! intended to be used directly.
5//!
6//! The crate provides a DID Resolver trait and a set of default implementations
7//! for resolving DIDs.
8//!
9//! See [DID resolution](https://www.w3.org/TR/did-core/#did-resolution) fpr more.
10
11// TODO: add support for the following:
12//   key type: EcdsaSecp256k1VerificationKey2019 | JsonWebKey2020 |
13// Ed25519VerificationKey2020 |             Ed25519VerificationKey2018 |
14// X25519KeyAgreementKey2019   crv: Ed25519 | secp256k1 | P-256 | P-384 | p-521
15
16mod core;
17mod document;
18mod error;
19mod jwk;
20mod key;
21mod resolution;
22mod web;
23
24use std::future::Future;
25
26pub use credibil_infosec::{Curve, KeyType, PublicKeyJwk};
27pub use document::{CreateOptions, Document};
28pub use error::Error;
29pub use key::DidKey;
30pub use resolution::{
31    dereference, resolve, ContentType, Dereferenced, Metadata, Options, Resolved, Resource,
32};
33pub use web::DidWeb;
34
35const ED25519_CODEC: [u8; 2] = [0xed, 0x01];
36const X25519_CODEC: [u8; 2] = [0xec, 0x01];
37
38/// Returns DID-specific errors.
39pub type Result<T> = std::result::Result<T, Error>;
40
41/// [`DidResolver`] is used to proxy the resolution of a DID document. Resolution
42/// can either be local as in the case of `did:key`, or remote as in the case of
43/// `did:web` or `did:dht`.
44///
45/// Implementers need only return the DID document specified by the url. This
46/// may be by directly dereferencing the URL, looking up a local cache, or
47/// fetching from a remote DID resolver.
48///
49/// For example, a DID resolver for `did:web` would fetch the DID document from
50/// the specified URL. A DID resolver for `did:dht`should forward the request to
51/// a remote DID resolver for the DHT network.
52pub trait DidResolver: Send + Clone {
53    /// Resolve the DID URL to a DID Document.
54    ///
55    /// # Errors
56    ///
57    /// Returns an error if the DID URL cannot be resolved.
58    fn resolve(&self, url: &str) -> impl Future<Output = anyhow::Result<Document>> + Send;
59}
60
61/// [`DidOperator`] is used by implementers to provide material required for DID
62/// document operations — creation, update, etc.
63pub trait DidOperator: Send + Sync {
64    /// Provides verification material to be used for the specified
65    /// verification method.
66    fn verification(&self, purpose: KeyPurpose) -> Option<PublicKeyJwk>;
67}
68
69/// The purpose the requested key material will be used for.
70pub enum KeyPurpose {
71    /// The document's `verification_method` field.
72    VerificationMethod,
73
74    /// The document's `authentication` field.
75    Authentication,
76
77    /// The document's `assertion_method` field.
78    AssertionMethod,
79
80    /// The document's `key_agreement` field.
81    KeyAgreement,
82
83    /// The document's `capability_invocation` field.
84    CapabilityInvocation,
85
86    /// The document's `capability_delegation` field.
87    CapabilityDelegation,
88}