1use multibase::Base;
2use ssi_dids_core::{resolution::Error, ssi_json_ld::syntax::ContextEntry};
3use ssi_jwk::JWK;
4use static_iref::iri_ref;
5
6#[derive(Debug, Clone, Copy)]
7#[non_exhaustive]
8pub enum VerificationMethodType {
9 Multikey,
10 JsonWebKey2020,
11}
12
13impl VerificationMethodType {
14 pub fn from_name(name: &str) -> Option<Self> {
15 match name {
16 "Multikey" => Some(Self::Multikey),
17 "JsonWebKey2020" => Some(Self::JsonWebKey2020),
18 _ => None,
19 }
20 }
21
22 pub fn name(&self) -> &'static str {
23 match self {
24 Self::Multikey => "Multikey",
25 Self::JsonWebKey2020 => "JsonWebKey2020",
26 }
27 }
28
29 pub fn encode_public_key(&self, jwk: JWK) -> Result<PublicKey, Error> {
30 match self {
31 Self::Multikey => {
32 let multicodec = jwk.to_multicodec().map_err(Error::internal)?;
33 let encoded = multibase::encode(Base::Base58Btc, multicodec.as_bytes());
34 Ok(PublicKey::Multibase(encoded))
35 }
36 Self::JsonWebKey2020 => Ok(PublicKey::Jwk(Box::new(jwk))),
37 }
38 }
39
40 pub fn context_entry(&self) -> ContextEntry {
41 match self {
42 Self::Multikey => {
43 ContextEntry::IriRef(iri_ref!("https://w3id.org/security/multikey/v1").to_owned())
44 }
45 Self::JsonWebKey2020 => ContextEntry::IriRef(
46 iri_ref!("https://w3id.org/security/suites/jws-2020/v1").to_owned(),
47 ),
48 }
49 }
50}
51
52pub enum PublicKey {
53 Jwk(Box<JWK>),
54 Multibase(String),
55}
56
57impl PublicKey {
58 pub fn property(&self) -> &'static str {
59 match self {
60 Self::Jwk(_) => "publicKeyJwk",
61 Self::Multibase(_) => "publicKeyMultibase",
62 }
63 }
64
65 pub fn into_json(self) -> serde_json::Value {
66 match self {
67 Self::Jwk(jwk) => serde_json::to_value(jwk).unwrap(),
68 Self::Multibase(s) => serde_json::Value::String(s),
69 }
70 }
71}