From 7e0e6dcbabd22dc055fca208adb414dcc2b13cb7 Mon Sep 17 00:00:00 2001 From: Bogdan Mircea Date: Thu, 2 Nov 2023 15:59:39 +0200 Subject: [PATCH] Added VcVerifier impl Signed-off-by: Bogdan Mircea --- aries_vcx_core/src/vc/credx.rs | 95 +++++++++++++++++++++++++++++++--- aries_vcx_core/src/vc/mod.rs | 59 ++++++++++----------- 2 files changed, 119 insertions(+), 35 deletions(-) diff --git a/aries_vcx_core/src/vc/credx.rs b/aries_vcx_core/src/vc/credx.rs index d992ec3dd5..c5a34ca479 100644 --- a/aries_vcx_core/src/vc/credx.rs +++ b/aries_vcx_core/src/vc/credx.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use async_trait::async_trait; use indy_credx::{ @@ -7,15 +7,17 @@ use indy_credx::{ types::{ AttributeNames, Credential, CredentialDefinition, CredentialDefinitionConfig, CredentialDefinitionId, CredentialDefinitionPrivate, CredentialKeyCorrectnessProof, - CredentialOffer, CredentialRequest, CredentialRevocationConfig, CredentialValues, DidValue, - IssuanceType, RegistryType, RevocationRegistry, RevocationRegistryDefinition, - RevocationRegistryDefinitionPrivate, RevocationRegistryDelta, RevocationRegistryId, Schema, - SchemaId, SignatureType, + CredentialOffer, CredentialRequest, CredentialRequestMetadata, CredentialRevocationConfig, + CredentialRevocationState, CredentialValues, DidValue, IssuanceType, LinkSecret, + Presentation, PresentationRequest, RegistryType, RevocationRegistry, + RevocationRegistryDefinition, RevocationRegistryDefinitionPrivate, RevocationRegistryDelta, + RevocationRegistryId, Schema, SchemaId, SignatureType, }, + verifier, }; use serde::{Deserialize, Serialize}; -use super::VcIssuer; +use super::{VcIssuer, VcProver, VcVerifier}; use crate::{ errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult}, wallet2::{Wallet, WalletRecord}, @@ -475,9 +477,90 @@ impl VcIssuer for IndyCredxIssuer { } } +pub struct IndyCredxVerifier; + +#[async_trait] +impl VcVerifier for IndyCredxVerifier { + type PresentationRequest = PresentationRequest; + type Presentation = Presentation; + + type SchemaId = SchemaId; + type Schema = Schema; + + type CredDefId = CredentialDefinitionId; + type CredDef = CredentialDefinition; + + type RevRegId = RevocationRegistryId; + type RevRegDef = RevocationRegistryDefinition; + type RevStates = HashMap; + + async fn verify_proof( + &self, + pres_request: &Self::PresentationRequest, + presentation: &Self::Presentation, + schemas: &HashMap, + credential_defs: &HashMap, + rev_reg_defs: Option<&HashMap>, + rev_regs: Option<&HashMap>, + ) -> VcxCoreResult { + let rev_regs = if let Some(map) = rev_regs { + let new_map = map + .iter() + .map(|(k, v)| (k.clone(), v.iter().map(|(k, v)| (*k, v)).collect())) + .collect(); + + Some(new_map) + } else { + None + }; + let output = verifier::verify_presentation( + presentation, + pres_request, + &hashmap_as_ref(schemas), + &hashmap_as_ref(credential_defs), + rev_reg_defs.map(hashmap_as_ref).as_ref(), + rev_regs.as_ref(), + )?; + + #[cfg(feature = "legacy_proof")] + let output = output + || verifier::verify_presentation_legacy( + presentation, + pres_request, + &hashmap_as_ref(schemas), + &hashmap_as_ref(credential_defs), + rev_reg_defs.map(hashmap_as_ref).as_ref(), + rev_regs.as_ref(), + )?; + + Ok(output) + } + + async fn generate_nonce(&self) -> VcxCoreResult { + verifier::generate_nonce() + .map_err(From::from) + .map(|v| v.to_string()) + } +} + #[derive(Debug, Deserialize, Serialize, Clone)] pub struct RevocationRegistryInfo { pub id: RevocationRegistryId, pub curr_id: u32, pub used_ids: HashSet, } + +// common transformation requirement in credx +fn hashmap_as_ref(map: &HashMap) -> HashMap +where + T: std::hash::Hash, + T: std::cmp::Eq, + T: std::clone::Clone, +{ + let mut new_map: HashMap = HashMap::new(); + for (k, v) in map.iter() { + new_map.insert(k.clone(), v); + } + + new_map +} diff --git a/aries_vcx_core/src/vc/mod.rs b/aries_vcx_core/src/vc/mod.rs index 14e7aa3932..cf16fa9787 100644 --- a/aries_vcx_core/src/vc/mod.rs +++ b/aries_vcx_core/src/vc/mod.rs @@ -158,37 +158,38 @@ pub trait VcIssuer { #[async_trait] pub trait VcProver { - type PresentationRequest; + type PresentationRequest: Send + Sync; - type SchemaId; - type Schema; + type SchemaId: Send + Sync; + type Schema: Send + Sync; - type CredDefId; - type CredDef; + type CredDefId: Send + Sync; + type CredDef: Send + Sync; - type CredId; - type Cred; + type CredId: Send + Sync; + type Cred: Send + Sync; type CredRevId: Send + Sync; type CredRevState: Send + Sync; type CredRevStateParts: Send + Sync; - type RevRegId; - type RevRegDef; - type RevStates; + type RevRegId: Send + Sync; + type RevRegDef: Send + Sync; + type RevRegDelta: Send + Sync; + type RevStates: Send + Sync; - type CredReq; - type CredReqMeta; - type CredOffer; + type CredReq: Send + Sync; + type CredReqMeta: Send + Sync; + type CredOffer: Send + Sync; - type LinkSecretId; + type LinkSecretId: Send + Sync; #[allow(clippy::too_many_arguments)] - async fn create_proof( + async fn create_proof( &self, - wallet: &impl Wallet, + wallet: &W, proof_req: Self::PresentationRequest, - requested_credentials_json: &str, - master_secret_id: &str, + requested_credentials_json: &str, // needs a type + link_secret_id: &Self::LinkSecretId, schemas_json: &HashMap, credential_defs_json: &HashMap, revoc_states_json: Option<&HashMap>, @@ -257,23 +258,23 @@ pub trait VcProver { #[async_trait] pub trait VcVerifier { - type PresentationRequest; - type Presentation; + type PresentationRequest: Send + Sync; + type Presentation: Send + Sync; - type SchemaId; - type Schema; + type SchemaId: Send + Sync; + type Schema: Send + Sync; - type CredDefId; - type CredDef; + type CredDefId: Send + Sync; + type CredDef: Send + Sync; - type RevRegId; - type RevRegDef; - type RevStates; + type RevRegId: Send + Sync; + type RevRegDef: Send + Sync; + type RevStates: Send + Sync; async fn verify_proof( &self, - proof_request: Self::PresentationRequest, - proof: Self::Presentation, + pres_request: &Self::PresentationRequest, + presentation: &Self::Presentation, schemas: &HashMap, credential_defs: &HashMap, rev_reg_defs: Option<&HashMap>,