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 23, 2024
1 parent 1eae146 commit 992a481
Show file tree
Hide file tree
Showing 24 changed files with 125 additions and 29 deletions.
28 changes: 28 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,31 @@ 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))
}
}
}
)*
};
}

pub(crate) use try_from_type;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::EcdsaSecp256k1RecoveryMethod2020;
use static_iref::iri;

use crate::try_from_type;

/// `EcdsaSecp256k1RecoverySignature2020`.
///
/// See: <https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/>
Expand Down Expand Up @@ -39,6 +41,8 @@ impl StandardCryptographicSuite for EcdsaSecp256k1RecoverySignature2020 {
}
}

try_from_type!(EcdsaSecp256k1RecoverySignature2020);

#[derive(Default)]
pub struct Secp256k1Recovery2020v2Context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use ssi_verification_methods::{
};
use static_iref::iri;

use crate::try_from_type;

/// Aleo Signature 2021
///
/// Linked data signature suite using Aleo.
Expand Down Expand Up @@ -86,6 +88,8 @@ impl StandardCryptographicSuite for AleoSignature2021 {
}
}

try_from_type!(AleoSignature2021);

pub struct AleoSignatureAlgorithm;

impl SignatureAndVerificationAlgorithm for AleoSignatureAlgorithm {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! EIP-712 Signature 2021 implementation.
use crate::eip712::{Eip712Hashing, Eip712Signature};
use crate::{
eip712::{Eip712Hashing, Eip712Signature},
try_from_type,
};
use rdf_types::{LexicalQuad, Quad};
use ssi_claims_core::{ProofValidationError, ProofValidity, SignatureError};
use ssi_data_integrity_core::{
Expand Down Expand Up @@ -97,6 +100,8 @@ impl StandardCryptographicSuite for Eip712Signature2021 {
}
}

try_from_type!(Eip712Signature2021);

pub struct Eip712Transformation;

impl TransformationAlgorithm<Eip712Signature2021> for Eip712Transformation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use ssi_verification_methods::{
};
use static_iref::iri;

use crate::try_from_type;

mod v0_1;
pub use v0_1::*;

Expand Down Expand Up @@ -59,6 +61,8 @@ impl StandardCryptographicSuite for EthereumPersonalSignature2021 {
}
}

try_from_type!(EthereumPersonalSignature2021);

#[derive(Default)]
pub struct EthereumPersonalSignature2021Context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use ssi_data_integrity_core::{
};
use static_iref::iri;

use crate::try_from_type;

#[derive(Debug, Default, Clone, Copy)]
pub struct EthereumPersonalSignature2021v0_1;

Expand Down Expand Up @@ -34,6 +36,8 @@ impl StandardCryptographicSuite for EthereumPersonalSignature2021v0_1 {
}
}

try_from_type!(EthereumPersonalSignature2021v0_1);

#[derive(Default)]
pub struct EthereumPersonalSignature2021v0_1Context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use ssi_verification_methods::{
};
use static_iref::iri;

use crate::try_from_type;

/// Solana Signature 2021
///
/// Linked data signature suite using Solana.
Expand Down Expand Up @@ -68,6 +70,8 @@ impl StandardCryptographicSuite for SolanaSignature2021 {
}
}

try_from_type!(SolanaSignature2021);

// pub fn wallet_sign(message: &[u8], key: &JWK) -> Result<Vec<u8>, MessageSignatureError> {
// let tx = LocalSolanaTransaction::with_message(message);
// let bytes = tx.to_bytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::Ed25519PublicKeyBLAKE2BDigestSize20Base58CheckEncoded2021;
use static_iref::iri;

use crate::try_from_type;

/// Proof type used with [did:tz](https://github.com/spruceid/did-tezos/) `tz1` addresses.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ed25519BLAKE2BDigestSize20Base58CheckEncodedSignature2021;
Expand Down Expand Up @@ -38,3 +40,5 @@ impl StandardCryptographicSuite for Ed25519BLAKE2BDigestSize20Base58CheckEncoded
TypeRef::Other(Self::NAME)
}
}

try_from_type!(Ed25519BLAKE2BDigestSize20Base58CheckEncodedSignature2021);
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::P256PublicKeyBLAKE2BDigestSize20Base58CheckEncoded2021;
use static_iref::iri;

use crate::try_from_type;

/// Proof type used with [did:tz](https://github.com/spruceid/did-tezos/) `tz3` addresses.
#[derive(Debug, Default, Clone, Copy)]
pub struct P256BLAKE2BDigestSize20Base58CheckEncodedSignature2021;
Expand Down Expand Up @@ -38,3 +40,5 @@ impl StandardCryptographicSuite for P256BLAKE2BDigestSize20Base58CheckEncodedSig
TypeRef::Other(Self::NAME)
}
}

try_from_type!(P256BLAKE2BDigestSize20Base58CheckEncodedSignature2021);
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ssi_tzkey::EncodeTezosSignedMessageError;
use ssi_verification_methods::{protocol::WithProtocol, MessageSigner, TezosMethod2021};
use static_iref::iri;

use crate::{TezosJcsVmV1Context, TezosWallet};
use crate::{try_from_type, TezosJcsVmV1Context, TezosWallet};

use super::decode_jwk_from_multibase;
pub use super::{Signature, TZJCSVM_CONTEXT};
Expand Down Expand Up @@ -85,6 +85,8 @@ impl StandardCryptographicSuite for TezosJcsSignature2021 {
}
}

try_from_type!(TezosJcsSignature2021);

#[derive(
Debug,
Clone,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::TezosWallet;
use crate::{try_from_type, TezosWallet};
use ssi_claims_core::{ProofValidationError, ProofValidity, SignatureError};
use ssi_crypto::algorithm::AnyBlake2b;
use ssi_data_integrity_core::{
Expand Down Expand Up @@ -69,6 +69,8 @@ impl StandardCryptographicSuite for TezosSignature2021 {
}
}

try_from_type!(TezosSignature2021);

pub struct EncodeTezosMessage;

impl HashingAlgorithm<TezosSignature2021> for EncodeTezosMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ 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};
use ssi_rdf::LexicalInterpretation;
use ssi_verification_methods::{Multikey, VerificationMethodResolver};

use crate::try_from_type;

pub(crate) mod transformation;
pub use transformation::{Bbs2023Transformation, Bbs2023TransformationOptions, Transformed};

Expand Down Expand Up @@ -90,16 +92,7 @@ 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)),
}
}
}
try_from_type!(Bbs2023);

#[derive(Debug, Default, Clone)]
pub struct Bbs2023SignatureOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::{multikey::DecodedMultikey, Multikey};
use static_iref::iri;

use crate::try_from_type;

/// The `ecdsa-rdfc-2019` cryptosuite.
///
/// See: <https://www.w3.org/TR/vc-di-ecdsa/#ecdsa-rdfc-2019>
Expand Down Expand Up @@ -47,6 +49,8 @@ impl StandardCryptographicSuite for EcdsaRdfc2019 {
}
}

try_from_type!(EcdsaRdfc2019);

pub struct EcdsaRdfc2019HashingAlgorithm;

impl HashingAlgorithm<EcdsaRdfc2019> for EcdsaRdfc2019HashingAlgorithm {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serde::Serialize;
use ssi_data_integrity_core::{
suite::{CryptographicSuiteSelect, SelectionError, SelectiveCryptographicSuite},
CryptosuiteStr, DataIntegrity, ProofRef, StandardCryptographicSuite, Type, TypeRef,
UnsupportedProofSuite,
CryptosuiteStr, DataIntegrity, ProofRef, StandardCryptographicSuite, TypeRef,
};

use crate::try_from_type;

mod configuration;
pub use configuration::*;

Expand Down Expand Up @@ -72,13 +73,4 @@ where
}
}

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)),
}
}
}
try_from_type!(EcdsaSd2023);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::EcdsaSecp256k1VerificationKey2019;
use static_iref::iri;

use crate::try_from_type;

/// Ecdsa Secp256k1 Signature 2019.
///
/// See: <https://w3c-ccg.github.io/lds-ecdsa-secp256k1-2019/>
Expand Down Expand Up @@ -38,3 +40,5 @@ impl StandardCryptographicSuite for EcdsaSecp256k1Signature2019 {
TypeRef::Other(Self::NAME)
}
}

try_from_type!(EcdsaSecp256k1Signature2019);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::EcdsaSecp256r1VerificationKey2019;
use static_iref::iri;

use crate::try_from_type;

/// ECDSA Cryptosuite v2019 `EcdsaSecp256r1Signature2019`.
///
/// See: <https://www.w3.org/community/reports/credentials/CG-FINAL-di-ecdsa-2019-20220724/#ecdsasecp256r1signature2019>
Expand Down Expand Up @@ -38,3 +40,5 @@ impl StandardCryptographicSuite for EcdsaSecp256r1Signature2019 {
TypeRef::Other(Self::NAME)
}
}

try_from_type!(EcdsaSecp256r1Signature2019);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::Ed25519VerificationKey2018;
use static_iref::iri;

use crate::try_from_type;

/// Ed25519 Signature 2018.
///
/// See: <https://w3c-ccg.github.io/lds-ed25519-2018/>
Expand Down Expand Up @@ -37,3 +39,5 @@ impl StandardCryptographicSuite for Ed25519Signature2018 {
TypeRef::Other(Self::NAME)
}
}

try_from_type!(Ed25519Signature2018);
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::Ed25519VerificationKey2020;
use static_iref::{iri, iri_ref};

use crate::try_from_type;

lazy_static! {
static ref PROOF_CONTEXT: ssi_json_ld::syntax::ContextEntry = {
ssi_json_ld::syntax::ContextEntry::IriRef(
Expand Down Expand Up @@ -64,3 +66,5 @@ impl StandardCryptographicSuite for Ed25519Signature2020 {
TypeRef::Other(Self::NAME)
}
}

try_from_type!(Ed25519Signature2020);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::Multikey;
use static_iref::iri;

use crate::try_from_type;

/// The `eddsa-2022` cryptosuite, a draft version of the `eddsa-rdfc-2022`
/// cryptosuite.
///
Expand Down Expand Up @@ -41,3 +43,5 @@ impl StandardCryptographicSuite for EdDsa2022 {
TypeRef::DataIntegrityProof(CryptosuiteStr::new("eddsa-2022").unwrap())
}
}

try_from_type!(EdDsa2022);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ssi_data_integrity_core::{
use ssi_verification_methods::Multikey;
use static_iref::iri;

use crate::try_from_type;

/// The `eddsa-rdfc-2022` cryptosuite.
///
/// See: <https://w3c.github.io/vc-di-eddsa/#eddsa-rdfc-2022>
Expand Down Expand Up @@ -37,3 +39,5 @@ impl StandardCryptographicSuite for EdDsaRdfc2022 {
TypeRef::DataIntegrityProof(CryptosuiteStr::new("eddsa-rdfc-2022").unwrap())
}
}

try_from_type!(EdDsaRdfc2022);
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use static_iref::{iri, iri_ref};
pub mod v0_1;
pub use v0_1::EthereumEip712Signature2021v0_1;

use crate::eip712::{Eip712Hashing, Eip712Signature, Input, TypesOrURI};
use crate::{
eip712::{Eip712Hashing, Eip712Signature, Input, TypesOrURI},
try_from_type,
};

lazy_static! {
static ref PROOF_CONTEXT: ssi_json_ld::syntax::ContextEntry = {
Expand Down Expand Up @@ -113,6 +116,8 @@ impl StandardCryptographicSuite for EthereumEip712Signature2021 {
}
}

try_from_type!(EthereumEip712Signature2021);

#[derive(
Debug,
serde::Serialize,
Expand Down
Loading

0 comments on commit 992a481

Please sign in to comment.