Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate TryFrom<Type> for all suites #597

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 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,33 @@ mod unspecified;
))]
#[allow(unused_imports)]
pub use unspecified::*;

#[allow(unused_macros)]
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))
}
}
}
)*
};
}

#[allow(unused_imports)]
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);
Loading