ssi_data_integrity/any/
protocol.rs1use std::borrow::Cow;
2
3use ssi_claims_core::MessageSignatureError;
4use ssi_verification_methods::{protocol, SignatureProtocol};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7pub enum AnyProtocol {
8 None,
9 Base58Btc,
10 Base58BtcMultibase,
11 EthereumWallet,
12 #[cfg(feature = "tezos")]
13 TezosWallet,
14}
15
16impl SignatureProtocol<ssi_crypto::Algorithm> for AnyProtocol {
17 fn prepare_message<'b>(&self, bytes: &'b [u8]) -> Cow<'b, [u8]> {
18 match self {
19 Self::None => SignatureProtocol::<ssi_jwk::Algorithm>::prepare_message(&(), bytes),
20 Self::Base58Btc => SignatureProtocol::<ssi_jwk::Algorithm>::prepare_message(
21 &protocol::Base58Btc,
22 bytes,
23 ),
24 Self::Base58BtcMultibase => SignatureProtocol::<ssi_jwk::Algorithm>::prepare_message(
25 &protocol::Base58BtcMultibase,
26 bytes,
27 ),
28 Self::EthereumWallet => SignatureProtocol::<ssi_jwk::Algorithm>::prepare_message(
29 &protocol::EthereumWallet,
30 bytes,
31 ),
32 #[cfg(feature = "tezos")]
33 Self::TezosWallet => {
34 SignatureProtocol::<ssi_crypto::algorithm::AnyBlake2b>::prepare_message(
35 &ssi_data_integrity_suites::tezos::TezosWallet,
36 bytes,
37 )
38 }
39 }
40 }
41
42 fn encode_signature(
43 &self,
44 algorithm: ssi_crypto::Algorithm,
45 signature: Vec<u8>,
46 ) -> Result<Vec<u8>, MessageSignatureError> {
47 match self {
48 Self::None => SignatureProtocol::<ssi_crypto::Algorithm>::encode_signature(
49 &(),
50 algorithm,
51 signature,
52 ),
53 Self::Base58Btc => SignatureProtocol::<ssi_crypto::Algorithm>::encode_signature(
54 &protocol::Base58Btc,
55 algorithm,
56 signature,
57 ),
58 Self::Base58BtcMultibase => {
59 SignatureProtocol::<ssi_crypto::Algorithm>::encode_signature(
60 &protocol::Base58BtcMultibase,
61 algorithm,
62 signature,
63 )
64 }
65 Self::EthereumWallet => SignatureProtocol::<ssi_crypto::Algorithm>::encode_signature(
66 &protocol::EthereumWallet,
67 algorithm,
68 signature,
69 ),
70 #[cfg(feature = "tezos")]
71 Self::TezosWallet => {
72 let algorithm: ssi_crypto::algorithm::AnyBlake2b = algorithm.try_into()?;
73 ssi_data_integrity_suites::tezos::TezosWallet.encode_signature(algorithm, signature)
74 }
75 }
76 }
77
78 fn decode_signature<'s>(
79 &self,
80 encoded_signature: &'s [u8],
81 ) -> Result<Cow<'s, [u8]>, protocol::InvalidProtocolSignature> {
82 match self {
83 Self::None => {
84 SignatureProtocol::<ssi_jwk::Algorithm>::decode_signature(&(), encoded_signature)
85 }
86 Self::Base58Btc => SignatureProtocol::<ssi_jwk::Algorithm>::decode_signature(
87 &protocol::Base58Btc,
88 encoded_signature,
89 ),
90 Self::Base58BtcMultibase => SignatureProtocol::<ssi_jwk::Algorithm>::decode_signature(
91 &protocol::Base58BtcMultibase,
92 encoded_signature,
93 ),
94 Self::EthereumWallet => SignatureProtocol::<ssi_jwk::Algorithm>::decode_signature(
95 &protocol::EthereumWallet,
96 encoded_signature,
97 ),
98 #[cfg(feature = "tezos")]
99 Self::TezosWallet => {
100 SignatureProtocol::<ssi_crypto::algorithm::AnyBlake2b>::decode_signature(
101 &ssi_data_integrity_suites::tezos::TezosWallet,
102 encoded_signature,
103 )
104 }
105 }
106 }
107}
108
109impl From<()> for AnyProtocol {
110 fn from(_value: ()) -> Self {
111 Self::None
112 }
113}
114
115impl From<protocol::Base58Btc> for AnyProtocol {
116 fn from(_value: protocol::Base58Btc) -> Self {
117 Self::Base58Btc
118 }
119}
120
121impl From<protocol::Base58BtcMultibase> for AnyProtocol {
122 fn from(_value: protocol::Base58BtcMultibase) -> Self {
123 Self::Base58BtcMultibase
124 }
125}
126
127impl From<protocol::EthereumWallet> for AnyProtocol {
128 fn from(_value: protocol::EthereumWallet) -> Self {
129 Self::EthereumWallet
130 }
131}
132
133#[cfg(feature = "tezos")]
134impl From<ssi_data_integrity_suites::tezos::TezosWallet> for AnyProtocol {
135 fn from(_value: ssi_data_integrity_suites::tezos::TezosWallet) -> Self {
136 Self::TezosWallet
137 }
138}