Skip to content

Commit

Permalink
Added VcVerifier impl
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Mircea <[email protected]>
  • Loading branch information
bobozaur committed Nov 2, 2023
1 parent 980493a commit 7e0e6dc
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 35 deletions.
95 changes: 89 additions & 6 deletions aries_vcx_core/src/vc/credx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};

use async_trait::async_trait;
use indy_credx::{
Expand All @@ -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},
Expand Down Expand Up @@ -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<u64, RevocationRegistry>;

async fn verify_proof(
&self,
pres_request: &Self::PresentationRequest,
presentation: &Self::Presentation,
schemas: &HashMap<Self::SchemaId, Self::Schema>,
credential_defs: &HashMap<Self::CredDefId, Self::CredDef>,
rev_reg_defs: Option<&HashMap<Self::RevRegId, Self::RevRegDef>>,
rev_regs: Option<&HashMap<Self::RevRegId, Self::RevStates>>,
) -> VcxCoreResult<bool> {
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<String> {
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<u32>,
}

// common transformation requirement in credx
fn hashmap_as_ref<T, U>(map: &HashMap<T, U>) -> HashMap<T, &U>
where
T: std::hash::Hash,
T: std::cmp::Eq,
T: std::clone::Clone,
{
let mut new_map: HashMap<T, &U> = HashMap::new();
for (k, v) in map.iter() {
new_map.insert(k.clone(), v);
}

new_map
}
59 changes: 30 additions & 29 deletions aries_vcx_core/src/vc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W>(
&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<Self::SchemaId, Self::Schema>,
credential_defs_json: &HashMap<Self::CredDefId, Self::CredDef>,
revoc_states_json: Option<&HashMap<Self::RevRegId, Self::RevStates>>,
Expand Down Expand Up @@ -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<Self::SchemaId, Self::Schema>,
credential_defs: &HashMap<Self::CredDefId, Self::CredDef>,
rev_reg_defs: Option<&HashMap<Self::RevRegId, Self::RevRegDef>>,
Expand Down

0 comments on commit 7e0e6dc

Please sign in to comment.