Skip to content

Commit

Permalink
get_credential returns Credential
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Feb 9, 2024
1 parent 637e928 commit 55c5cd8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fmt::Display, path::Path};

use anoncreds_types::data_types::messages::credential::Credential;
use aries_vcx_core::{
anoncreds::base_anoncreds::BaseAnonCreds, ledger::base_ledger::AnoncredsLedgerRead,
wallet::base_wallet::BaseWallet,
Expand Down Expand Up @@ -101,12 +102,15 @@ pub struct IssuerSM {
pub(crate) state: IssuerFullState,
}

fn build_credential_message(libindy_credential: String, thread_id: String) -> IssueCredentialV1 {
fn build_credential_message(
libindy_credential: Credential,
thread_id: String,
) -> IssueCredentialV1 {
let id = Uuid::new_v4().to_string();

let content = IssueCredentialV1Content::builder()
.credentials_attach(vec![make_attach_from_str!(
&libindy_credential,
&serde_json::to_string(&libindy_credential).unwrap(),
AttachmentId::Credential.as_ref().to_string()
)])
.build();
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub async fn create_and_write_credential(
.prover_store_credential(
wallet_holder,
&req_meta,
serde_json::from_str(&cred).unwrap(),
cred,
cred_def.get_cred_def_json().try_clone().unwrap(),
rev_reg_def_json
.as_deref()
Expand Down
6 changes: 2 additions & 4 deletions aries/aries_vcx_core/src/anoncreds/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl BaseAnonCreds for Anoncreds {
cred_values_json: &str,
rev_reg_id: Option<&RevocationRegistryDefinitionId>,
tails_dir: Option<&Path>,
) -> VcxCoreResult<(String, Option<String>)> {
) -> VcxCoreResult<(Credential, Option<String>)> {
let cred_offer: AnoncredsCredentialOffer = cred_offer_json.convert(())?;
let cred_request: AnoncredsCredentialRequest = cred_req_json.convert(())?;
let cred_values = serde_json::from_str(cred_values_json)?;
Expand Down Expand Up @@ -694,9 +694,7 @@ impl BaseAnonCreds for Anoncreds {
None
};

let str_cred = serde_json::to_string(&cred)?;

Ok((str_cred, cred_rev_id))
Ok((cred.convert(())?, cred_rev_id))
}

#[allow(clippy::too_many_arguments)]
Expand Down
26 changes: 26 additions & 0 deletions aries/aries_vcx_core/src/anoncreds/anoncreds/type_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,32 @@ impl Convert for OurCredential {
}
}

impl Convert for AnoncredsCredential {
type Args = ();
type Target = OurCredential;
type Error = Box<dyn std::error::Error>;

fn convert(self, _args: Self::Args) -> Result<Self::Target, Self::Error> {
Ok(OurCredential {
schema_id: OurSchemaId::new_unchecked(self.schema_id.to_string()),
cred_def_id: OurCredentialDefinitionId::new(self.cred_def_id.to_string())?,
rev_reg_id: self
.rev_reg_id
.as_ref()
.map(ToString::to_string)
.map(OurRevocationRegistryDefinitionId::new)
.transpose()?,
values: serde_json::from_value(serde_json::to_value(self.values)?)?,
signature: serde_json::from_value(serde_json::to_value(self.signature)?)?,
signature_correctness_proof: serde_json::from_value(serde_json::to_value(
self.signature_correctness_proof,
)?)?,
rev_reg: serde_json::from_value(serde_json::to_value(self.rev_reg)?)?,
witness: serde_json::from_value(serde_json::to_value(self.witness)?)?,
})
}
}

impl Convert for OurPresentationRequest {
type Args = ();
type Target = AnoncredsPresentationRequest;
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx_core/src/anoncreds/base_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub trait BaseAnonCreds: std::fmt::Debug + Send + Sync {
cred_values_json: &str,
rev_reg_id: Option<&RevocationRegistryDefinitionId>,
tails_dir: Option<&Path>,
) -> VcxCoreResult<(String, Option<String>)>;
) -> VcxCoreResult<(Credential, Option<String>)>;

#[allow(clippy::too_many_arguments)]
async fn prover_create_proof(
Expand Down
6 changes: 2 additions & 4 deletions aries/aries_vcx_core/src/anoncreds/credx_anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds {
cred_values_json: &str,
rev_reg_id: Option<&RevocationRegistryDefinitionId>,
tails_dir: Option<&Path>,
) -> VcxCoreResult<(String, Option<String>)> {
) -> VcxCoreResult<(Credential, Option<String>)> {
let rev_reg_id = rev_reg_id.map(ToString::to_string);
let cred_offer: CredxCredentialOffer = cred_offer_json.convert(())?;
let cred_request: CredxCredentialRequest = cred_req_json.convert(())?;
Expand Down Expand Up @@ -645,9 +645,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds {
None
};

let str_cred = serde_json::to_string(&cred)?;

Ok((str_cred, cred_rev_id))
Ok((cred.convert(())?, cred_rev_id))
}

/// * `requested_credentials_json`: either a credential or self-attested attribute for each
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,30 @@ impl Convert for OurCredential {
}
}

impl Convert for CredxCredential {
type Args = ();
type Target = OurCredential;
type Error = Box<dyn std::error::Error>;

fn convert(self, _args: Self::Args) -> Result<Self::Target, Self::Error> {
Ok(OurCredential {
schema_id: OurSchemaId::new_unchecked(self.schema_id.0),
cred_def_id: OurCredentialDefinitionId::new(self.cred_def_id.0)?,
rev_reg_id: self
.rev_reg_id
.map(|id| OurRevocationRegistryDefinitionId::new(id.0))
.transpose()?,
values: serde_json::from_value(serde_json::to_value(self.values)?)?,
signature: serde_json::from_value(serde_json::to_value(self.signature)?)?,
signature_correctness_proof: serde_json::from_value(serde_json::to_value(
self.signature_correctness_proof,
)?)?,
rev_reg: serde_json::from_value(serde_json::to_value(self.rev_reg)?)?,
witness: serde_json::from_value(serde_json::to_value(self.witness)?)?,
})
}
}

impl Convert for OurPresentationRequest {
type Args = ();
type Target = CredxPresentationRequest;
Expand Down
4 changes: 2 additions & 2 deletions aries/misc/test_utils/src/mockdata/mock_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ impl BaseAnonCreds for MockAnoncreds {
_cred_values_json: &str,
_rev_reg_id: Option<&RevocationRegistryDefinitionId>,
_tails_dir: Option<&Path>,
) -> VcxCoreResult<(String, Option<String>)> {
Ok((CREDENTIAL_JSON.to_owned(), None))
) -> VcxCoreResult<(Credential, Option<String>)> {
Ok((serde_json::from_str(CREDENTIAL_JSON)?, None))
}

async fn prover_create_proof(
Expand Down

0 comments on commit 55c5cd8

Please sign in to comment.