Skip to main content

did_bio_core/
error.rs

1//! Crate error type and W3C DID Resolution error codes.
2
3use core::fmt;
4
5/// Errors produced by parsing, encoding, and account deserialization.
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Error {
9    /// The DID does not conform to the `did:bio` ABNF (spec Section 4.1) or its
10    /// `idstring` does not decode to a 32 byte key (spec Section 6.2 step 3).
11    InvalidDid(&'static str),
12    /// The DID URL is not a `did:bio` DID with an optional valid fragment.
13    InvalidDidUrl(&'static str),
14    /// A `publicKeyMultibase` value is not a well formed Multikey.
15    InvalidMultikey(&'static str),
16    /// Key material length does not match the verification method type.
17    InvalidKeyLength {
18        /// Length required by the key type.
19        expected: usize,
20        /// Length actually supplied.
21        actual: usize,
22    },
23    /// Registry account bytes failed the discriminator check or Borsh
24    /// deserialization (spec Section 6.2 step 7).
25    InvalidAccountData(&'static str),
26    /// The operation is not defined for this verification method type, such
27    /// as signature verification with an X25519 key agreement key.
28    UnsupportedKeyType(&'static str),
29    /// A signature did not verify against the given key and message.
30    SignatureVerification,
31    /// The DID document holds no verification method with this id or
32    /// fragment.
33    VerificationMethodNotFound,
34    /// The verification method is not listed under the verification
35    /// relationship the check requires.
36    RelationshipNotGranted,
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Error::InvalidDid(reason) => write!(f, "invalid did:bio DID: {reason}"),
43            Error::InvalidDidUrl(reason) => write!(f, "invalid did:bio DID URL: {reason}"),
44            Error::InvalidMultikey(reason) => write!(f, "invalid Multikey: {reason}"),
45            Error::InvalidKeyLength { expected, actual } => {
46                write!(
47                    f,
48                    "invalid key length: expected {expected} bytes, got {actual}"
49                )
50            }
51            Error::InvalidAccountData(reason) => {
52                write!(f, "invalid registry account data: {reason}")
53            }
54            Error::UnsupportedKeyType(reason) => {
55                write!(f, "unsupported key type for this operation: {reason}")
56            }
57            Error::SignatureVerification => write!(f, "signature verification failed"),
58            Error::VerificationMethodNotFound => {
59                write!(f, "verification method not found in the DID document")
60            }
61            Error::RelationshipNotGranted => write!(
62                f,
63                "verification method does not hold the required verification relationship"
64            ),
65        }
66    }
67}
68
69impl std::error::Error for Error {}
70
71/// Registered DID Resolution error codes used by this method.
72pub mod resolution_error {
73    /// The input DID violates the method syntax.
74    pub const INVALID_DID: &str = "invalidDid";
75    /// The input DID URL violates DID URL syntax.
76    pub const INVALID_DID_URL: &str = "invalidDidUrl";
77    /// An unexpected internal condition, such as undecodable registry state.
78    pub const INTERNAL_ERROR: &str = "internalError";
79    /// An owned subject with no registry account, so there is nothing to
80    /// resolve.
81    pub const NOT_FOUND: &str = "notFound";
82}
83
84impl Error {
85    /// The DID Resolution `didResolutionMetadata.error` code corresponding to
86    /// this error when it occurs during resolution.
87    pub fn resolution_error_code(&self) -> &'static str {
88        match self {
89            Error::InvalidDid(_) => resolution_error::INVALID_DID,
90            Error::InvalidDidUrl(_) => resolution_error::INVALID_DID_URL,
91            _ => resolution_error::INTERNAL_ERROR,
92        }
93    }
94}