radicle/identity/
did.rs

1use std::ops::Deref;
2use std::{fmt, str::FromStr};
3
4use serde::{Deserialize, Serialize};
5use thiserror::Error;
6
7use crate::crypto;
8
9#[derive(Error, Debug)]
10pub enum DidError {
11    #[error("invalid did: {0}")]
12    Did(String),
13    #[error("invalid public key: {0}")]
14    PublicKey(#[from] crypto::PublicKeyError),
15}
16
17#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
18#[serde(into = "String", try_from = "String")]
19pub struct Did(crypto::PublicKey);
20
21impl Did {
22    /// We use the format specified by the DID `key` method, which is described as:
23    ///
24    /// `did:key:MULTIBASE(base58-btc, MULTICODEC(public-key-type, raw-public-key-bytes))`
25    ///
26    pub fn encode(&self) -> String {
27        format!("did:key:{}", self.0.to_human())
28    }
29
30    pub fn decode(input: &str) -> Result<Self, DidError> {
31        let key = input
32            .strip_prefix("did:key:")
33            .ok_or_else(|| DidError::Did(input.to_owned()))?;
34
35        crypto::PublicKey::from_str(key)
36            .map(Did)
37            .map_err(DidError::from)
38    }
39
40    pub fn as_key(&self) -> &crypto::PublicKey {
41        self.deref()
42    }
43}
44
45impl From<&crypto::PublicKey> for Did {
46    fn from(key: &crypto::PublicKey) -> Self {
47        Self(*key)
48    }
49}
50
51impl From<crypto::PublicKey> for Did {
52    fn from(key: crypto::PublicKey) -> Self {
53        (&key).into()
54    }
55}
56
57impl From<Did> for crypto::PublicKey {
58    fn from(Did(key): Did) -> Self {
59        key
60    }
61}
62
63impl From<Did> for String {
64    fn from(other: Did) -> Self {
65        other.encode()
66    }
67}
68
69impl FromStr for Did {
70    type Err = DidError;
71
72    fn from_str(s: &str) -> Result<Self, Self::Err> {
73        Self::decode(s)
74    }
75}
76
77impl TryFrom<String> for Did {
78    type Error = DidError;
79
80    fn try_from(value: String) -> Result<Self, Self::Error> {
81        Self::decode(&value)
82    }
83}
84
85impl fmt::Display for Did {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "{}", self.encode())
88    }
89}
90
91impl fmt::Debug for Did {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(f, "Did({:?})", self.to_string())
94    }
95}
96
97impl Deref for Did {
98    type Target = crypto::PublicKey;
99
100    fn deref(&self) -> &Self::Target {
101        &self.0
102    }
103}
104
105#[cfg(test)]
106#[allow(clippy::unwrap_used)]
107mod test {
108    use super::*;
109
110    #[test]
111    fn test_did_encode_decode() {
112        let input = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
113        let Did(key) = Did::decode(input).unwrap();
114
115        assert_eq!(Did::from(key).encode(), input);
116    }
117
118    #[test]
119    fn test_did_vectors() {
120        Did::decode("did:key:z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp").unwrap();
121        Did::decode("did:key:z6MkjchhfUsD6mmvni8mCdXHw216Xrm9bQe2mBH1P5RDjVJG").unwrap();
122        Did::decode("did:key:z6MknGc3ocHs3zdPiJbnaaqDi58NGb4pk1Sp9WxWufuXSdxf").unwrap();
123    }
124}