Skip to main content

xdid_core/
lib.rs

1//! Core types for DID methods to implement.
2
3use std::{
4    future::Future,
5    pin::Pin,
6};
7
8use did::Did;
9use thiserror::Error;
10
11pub mod did;
12pub mod did_url;
13pub mod document;
14mod uri;
15
16/// Boxed future for [`Method::resolve`], borrowing for `'a` so a method need
17/// not clone itself or the DID to produce one. `Send` is kept on wasm, where
18/// the web method bridges its browser transport over a channel.
19pub type MethodFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
20
21pub trait Method: Send + Sync {
22    fn method_name(&self) -> &'static str;
23    fn resolve<'a>(
24        &'a self,
25        did: &'a Did,
26    ) -> MethodFuture<'a, Result<document::Document, ResolutionError>>;
27}
28
29#[derive(Error, Debug)]
30#[non_exhaustive]
31pub enum ResolutionError {
32    #[error("invalid DID")]
33    InvalidDid,
34    /// The resolved document's `id` was not the DID being resolved, so the
35    /// document speaks for an identifier its host does not control.
36    #[error("document id does not match the resolved DID")]
37    DocumentMismatch,
38    /// The target was rejected before any request was sent.
39    #[error("resolution target is not permitted")]
40    TargetNotAllowed,
41    #[error("document exceeds the configured size limit")]
42    DocumentTooLarge,
43    /// The host answered, and publishes no document for this DID. A caller
44    /// should not retry, and should not treat the DID as merely unreachable.
45    #[error("no document is published for this DID")]
46    NotFound,
47    /// The host could not be reached, or answered in a way that says nothing
48    /// about the DID. Worth retrying.
49    ///
50    /// Detail never includes the resolved URL, which would make resolution
51    /// errors an oracle for probing internal hosts.
52    #[error("could not reach the DID's host: {0}")]
53    Transport(String),
54    /// The host answered with something that is not a DID document.
55    #[error("response is not a DID document: {0}")]
56    Malformed(String),
57    #[error("unsupported method")]
58    UnsupportedMethod,
59}