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
use crate::DIDKeyTypeInternal;
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};

pub static mut CONTENT_TYPE: ContentType = ContentType::JsonLd;

pub enum ContentType {
    JsonLd,
    Json,
}

pub trait DIDCore {
    fn to_verification_method(&self, controller: &str) -> Vec<VerificationMethod>;
    fn get_did_document(&self) -> Document;
    fn get_fingerprint(&self) -> String;
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Document {
    #[serde(rename = "@context")]
    pub context: String,
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assertion_method: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authentication: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capability_delegation: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capability_invocation: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_agreement: Option<Vec<String>>,
    pub verification_method: Vec<VerificationMethod>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct VerificationMethod {
    pub id: String,
    pub(crate) key_type: DIDKeyTypeInternal,
    pub controller: String,
    pub public_key: Option<Vec<u8>>,
    pub private_key: Option<Vec<u8>>,
}

impl Serialize for VerificationMethod {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(None)?;

        map.serialize_entry("id", &self.id)?;
        map.serialize_entry("controller", &self.controller)?;

        unsafe {
            match CONTENT_TYPE {
                ContentType::JsonLd => {
                    map.serialize_entry(
                        "type",
                        match &self.key_type {
                            DIDKeyTypeInternal::Ed25519 => "Ed25519VerificationKey2018",
                            DIDKeyTypeInternal::X25519 => "X25519KeyAgreementKey2019",
                            DIDKeyTypeInternal::Bls12381G1 => "Bls12381G1Key2020",
                            DIDKeyTypeInternal::Bls12381G2 => "Bls12381G2Key2020",
                            _ => todo!(),
                        },
                    )?;
                    match &self.public_key {
                        Some(key) => {
                            map.serialize_entry("publicKeyBase58", &bs58::encode(key.as_slice()).into_string())?
                        }
                        None => {}
                    }
                    match &self.private_key {
                        Some(key) => {
                            map.serialize_entry("privateKeyBase58", &bs58::encode(key.as_slice()).into_string())?
                        }
                        None => {}
                    }
                }
                ContentType::Json => {
                    map.serialize_entry("type", "JsonWebKey2020")?;

                    let config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
                    let jwk = JWK {
                        key_type: match self.key_type {
                            DIDKeyTypeInternal::Ed25519 | DIDKeyTypeInternal::X25519 => "OKP",
                            _ => "EC",
                        }
                        .to_string(),
                        curve: match &self.key_type {
                            DIDKeyTypeInternal::Ed25519 => "Ed25519",
                            DIDKeyTypeInternal::X25519 => "X25519",
                            DIDKeyTypeInternal::P256 => "P-256",
                            DIDKeyTypeInternal::Bls12381G1 => "BLS12381_G1",
                            DIDKeyTypeInternal::Bls12381G2 => "BLS12381_G2",
                            DIDKeyTypeInternal::Secp256k1 => "Secp256k1",
                        }
                        .to_string(),
                        x: self.public_key.as_ref().map(|key| base64::encode_config(key, config)),
                        d: self.private_key.as_ref().map(|key| base64::encode_config(key, config)),
                    };

                    match &jwk.d {
                        Some(_) => map.serialize_entry("privateKeyJwk", &jwk)?,
                        None => map.serialize_entry("publicKeyJwk", &jwk)?,
                    }
                }
            };
        }

        map.end()
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct JWK {
    #[serde(rename = "kty")]
    key_type: String,
    #[serde(rename = "crv")]
    curve: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    x: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    d: Option<String>,
}