Skip to content

Commit

Permalink
Generate TryFrom<Type> for all suites
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan770 committed Aug 14, 2024
1 parent 1eae146 commit 3ed1b38
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 26 deletions.
101 changes: 101 additions & 0 deletions crates/claims/crates/data-integrity/suites/src/suites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,104 @@ mod unspecified;
))]
#[allow(unused_imports)]
pub use unspecified::*;

macro_rules! try_from_type {
{
$(
$(#[cfg($($t:tt)*)])?
$suite:ident
),*
} => {
$(
$(#[cfg($($t)*)])?
impl TryFrom<ssi_data_integrity_core::Type> for $suite {
type Error = ssi_data_integrity_core::UnsupportedProofSuite;

fn try_from(value: ssi_data_integrity_core::Type) -> Result<Self, Self::Error> {
let suite = $suite;

if value == <$suite as ssi_data_integrity_core::StandardCryptographicSuite>::type_(&suite) {
Ok($suite)
} else {
Err(ssi_data_integrity_core::UnsupportedProofSuite::Compact(value))
}
}
}
)*
};
}

try_from_type! {
#[cfg(all(feature = "rsa", feature = "w3c"))]
RsaSignature2018,

#[cfg(all(feature = "ed25519", feature = "w3c"))]
Ed25519Signature2018,

#[cfg(all(feature = "ed25519", feature = "w3c"))]
Ed25519Signature2020,

#[cfg(all(feature = "ed25519", feature = "w3c"))]
EdDsa2022,

#[cfg(all(feature = "ed25519", feature = "w3c"))]
EdDsaRdfc2022,

#[cfg(all(feature = "secp256k1", feature = "w3c"))]
EcdsaSecp256k1Signature2019,

#[cfg(all(
feature = "secp256r1",
feature = "secp384r1",
feature = "w3c"
))]
EcdsaRdfc2019,

#[cfg(all(feature = "secp256r1", feature = "w3c"))]
EcdsaSd2023,

#[cfg(all(feature = "eip712", feature = "w3c"))]
EthereumEip712Signature2021,

#[cfg(all(feature = "eip712", feature = "w3c"))]
EthereumEip712Signature2021v0_1,

#[cfg(all(feature = "secp256r1", feature = "w3c"))]
EcdsaSecp256r1Signature2019,

#[cfg(feature = "w3c")]
JsonWebSignature2020,

#[cfg(all(feature = "bbs", feature = "w3c"))]
Bbs2023,

#[cfg(all(feature = "secp256k1", feature = "dif"))]
EcdsaSecp256k1RecoverySignature2020,

#[cfg(feature = "aleo")]
AleoSignature2021,

#[cfg(all(feature = "ethereum", feature = "eip712"))]
Eip712Signature2021,

#[cfg(all(feature = "ethereum", feature = "secp256k1"))]
EthereumPersonalSignature2021,

#[cfg(all(feature = "ethereum", feature = "secp256k1"))]
EthereumPersonalSignature2021v0_1,

#[cfg(feature = "solana")]
SolanaSignature2021,

#[cfg(all(feature = "ed25519", feature = "tezos"))]
Ed25519BLAKE2BDigestSize20Base58CheckEncodedSignature2021,

#[cfg(all(feature = "secp256r1", feature = "tezos"))]
P256BLAKE2BDigestSize20Base58CheckEncodedSignature2021,

#[cfg(feature = "tezos")]
TezosJcsSignature2021,

#[cfg(feature = "tezos")]
TezosSignature2021
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use ssi_data_integrity_core::{
ConfigurationAlgorithm, ConfigurationError, CryptographicSuiteSelect, InputProofOptions,
SelectionError, SelectiveCryptographicSuite,
},
CryptosuiteStr, DataIntegrity, ProofConfiguration, ProofRef, StandardCryptographicSuite, Type,
TypeRef, UnsupportedProofSuite,
CryptosuiteStr, DataIntegrity, ProofConfiguration, ProofRef, StandardCryptographicSuite,
TypeRef,
};
use ssi_di_sd_primitives::{HmacSha256Key, JsonPointerBuf};
use ssi_json_ld::{Expandable, ExpandedDocument, JsonLdLoaderProvider, JsonLdNodeObject};
Expand Down Expand Up @@ -90,17 +90,6 @@ impl StandardCryptographicSuite for Bbs2023 {
}
}

impl TryFrom<Type> for Bbs2023 {
type Error = UnsupportedProofSuite;

fn try_from(value: Type) -> Result<Self, Self::Error> {
match value {
Type::DataIntegrityProof(c) if c == "bbs-2023" => Ok(Self),
ty => Err(UnsupportedProofSuite::Compact(ty)),
}
}
}

#[derive(Debug, Default, Clone)]
pub struct Bbs2023SignatureOptions {
pub mandatory_pointers: Vec<JsonPointerBuf>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use serde::Serialize;
use ssi_data_integrity_core::{
suite::{CryptographicSuiteSelect, SelectionError, SelectiveCryptographicSuite},
CryptosuiteStr, DataIntegrity, ProofRef, StandardCryptographicSuite, Type, TypeRef,
UnsupportedProofSuite,
CryptosuiteStr, DataIntegrity, ProofRef, StandardCryptographicSuite, TypeRef,
};

mod configuration;
Expand Down Expand Up @@ -71,14 +70,3 @@ where
.map_err(SelectionError::proof_derivation)
}
}

impl TryFrom<Type> for EcdsaSd2023 {
type Error = UnsupportedProofSuite;

fn try_from(value: Type) -> Result<Self, Self::Error> {
match value {
Type::DataIntegrityProof(c) if c == "ecdsa-sd-2023" => Ok(Self),
ty => Err(UnsupportedProofSuite::Compact(ty)),
}
}
}

0 comments on commit 3ed1b38

Please sign in to comment.