From 3ed1b384d8869bbf7bded42efb54746e96fe7c8d Mon Sep 17 00:00:00 2001 From: ivan770 Date: Wed, 14 Aug 2024 12:15:37 -0400 Subject: [PATCH] Generate TryFrom for all suites --- .../data-integrity/suites/src/suites.rs | 101 ++++++++++++++++++ .../suites/src/suites/w3c/bbs_2023/mod.rs | 15 +-- .../src/suites/w3c/ecdsa_sd_2023/mod.rs | 14 +-- 3 files changed, 104 insertions(+), 26 deletions(-) diff --git a/crates/claims/crates/data-integrity/suites/src/suites.rs b/crates/claims/crates/data-integrity/suites/src/suites.rs index ceec168fc..b13fd4793 100644 --- a/crates/claims/crates/data-integrity/suites/src/suites.rs +++ b/crates/claims/crates/data-integrity/suites/src/suites.rs @@ -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 for $suite { + type Error = ssi_data_integrity_core::UnsupportedProofSuite; + + fn try_from(value: ssi_data_integrity_core::Type) -> Result { + 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 +} diff --git a/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/mod.rs b/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/mod.rs index 3de5d8b1c..84ca92372 100644 --- a/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/mod.rs +++ b/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/mod.rs @@ -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}; @@ -90,17 +90,6 @@ impl StandardCryptographicSuite for Bbs2023 { } } -impl TryFrom for Bbs2023 { - type Error = UnsupportedProofSuite; - - fn try_from(value: Type) -> Result { - 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, diff --git a/crates/claims/crates/data-integrity/suites/src/suites/w3c/ecdsa_sd_2023/mod.rs b/crates/claims/crates/data-integrity/suites/src/suites/w3c/ecdsa_sd_2023/mod.rs index d6296b6af..3fe1b0fb8 100644 --- a/crates/claims/crates/data-integrity/suites/src/suites/w3c/ecdsa_sd_2023/mod.rs +++ b/crates/claims/crates/data-integrity/suites/src/suites/w3c/ecdsa_sd_2023/mod.rs @@ -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; @@ -71,14 +70,3 @@ where .map_err(SelectionError::proof_derivation) } } - -impl TryFrom for EcdsaSd2023 { - type Error = UnsupportedProofSuite; - - fn try_from(value: Type) -> Result { - match value { - Type::DataIntegrityProof(c) if c == "ecdsa-sd-2023" => Ok(Self), - ty => Err(UnsupportedProofSuite::Compact(ty)), - } - } -}