diff --git a/aries/aries_vcx/src/protocols/issuance/holder/state_machine.rs b/aries/aries_vcx/src/protocols/issuance/holder/state_machine.rs index d0b9270320..3f07a9d727 100644 --- a/aries/aries_vcx/src/protocols/issuance/holder/state_machine.rs +++ b/aries/aries_vcx/src/protocols/issuance/holder/state_machine.rs @@ -224,11 +224,12 @@ impl HolderSM { ) .await { - Ok((msg_credential_request, req_meta, cred_def_json)) => { + Ok((msg_credential_request, req_meta, cred_def_json, schema_id)) => { HolderFullState::RequestSet(RequestSetState { msg_credential_request, req_meta, cred_def_json, + schema_id, }) } Err(err) => { @@ -276,6 +277,10 @@ impl HolderSM { trace!("HolderSM::receive_credential >>"); let state = match self.state { HolderFullState::RequestSet(state_data) => { + let schema = ledger + .get_schema(&state_data.schema_id.clone().try_into()?, None) + .await?; + let schema_json = serde_json::to_string(&schema)?; match _store_credential( wallet, ledger, @@ -283,6 +288,7 @@ impl HolderSM { &credential, &state_data.req_meta, &state_data.cred_def_json, + &schema_json, ) .await { @@ -549,6 +555,7 @@ async fn _store_credential( credential: &IssueCredentialV1, req_meta: &str, cred_def_json: &str, + schema_json: &str, ) -> VcxResult<(String, Option)> { trace!( "Holder::_store_credential >>> credential: {:?}, req_meta: {}, cred_def_json: {}", @@ -572,6 +579,7 @@ async fn _store_credential( wallet, serde_json::from_str(req_meta)?, serde_json::from_str(&credential_json)?, + serde_json::from_str(schema_json)?, serde_json::from_str(cred_def_json)?, rev_reg_def_json.clone(), ) @@ -585,6 +593,7 @@ async fn _store_credential( )) } +/// On success, returns: credential request, request metadata, cred_def_id, cred def, schema_id pub async fn create_anoncreds_credential_request( wallet: &impl BaseWallet, ledger: &impl AnoncredsLedgerRead, @@ -592,7 +601,7 @@ pub async fn create_anoncreds_credential_request( cred_def_id: &str, prover_did: &Did, cred_offer: &str, -) -> VcxResult<(String, String, String, String)> { +) -> VcxResult<(String, String, String, String, String)> { let cred_def_json = ledger .get_cred_def(&cred_def_id.to_string().try_into()?, None) .await?; @@ -619,10 +628,13 @@ pub async fn create_anoncreds_credential_request( serde_json::to_string(&s2).unwrap(), cred_def_id.to_string(), serde_json::to_string(&cred_def_json).unwrap(), + cred_def_json.schema_id.to_string(), ) }) } +/// On success, returns: message with cred request, request metadata, cred def (for caching), +/// schema_id async fn build_credential_request_msg( wallet: &impl BaseWallet, ledger: &impl AnoncredsLedgerRead, @@ -630,7 +642,7 @@ async fn build_credential_request_msg( thread_id: String, my_pw_did: Did, offer: &OfferCredentialV1, -) -> VcxResult<(RequestCredentialV1, String, String)> { +) -> VcxResult<(RequestCredentialV1, String, String, String)> { trace!( "Holder::_make_credential_request >>> my_pw_did: {:?}, offer: {:?}", my_pw_did, @@ -641,16 +653,17 @@ async fn build_credential_request_msg( trace!("Parsed cred offer attachment: {}", cred_offer); let cred_def_id = parse_cred_def_id_from_cred_offer(&cred_offer)?; - let (req, req_meta, _cred_def_id, cred_def_json) = create_anoncreds_credential_request( - wallet, - ledger, - anoncreds, - &cred_def_id, - &my_pw_did, - &cred_offer, - ) - .await?; + let (req, req_meta, _cred_def_id, cred_def_json, schema_id) = + create_anoncreds_credential_request( + wallet, + ledger, + anoncreds, + &cred_def_id, + &my_pw_did, + &cred_offer, + ) + .await?; trace!("Created cred def json: {}", cred_def_json); let credential_request_msg = _build_credential_request_msg(req, &thread_id); - Ok((credential_request_msg, req_meta, cred_def_json)) + Ok((credential_request_msg, req_meta, cred_def_json, schema_id)) } diff --git a/aries/aries_vcx/src/protocols/issuance/holder/states/request_set.rs b/aries/aries_vcx/src/protocols/issuance/holder/states/request_set.rs index e348378872..586a75b2c6 100644 --- a/aries/aries_vcx/src/protocols/issuance/holder/states/request_set.rs +++ b/aries/aries_vcx/src/protocols/issuance/holder/states/request_set.rs @@ -11,6 +11,7 @@ use crate::{ pub struct RequestSetState { pub req_meta: String, pub cred_def_json: String, + pub schema_id: String, pub msg_credential_request: RequestCredentialV1, } diff --git a/aries/aries_vcx/tests/test_anoncreds.rs b/aries/aries_vcx/tests/test_anoncreds.rs index a1ec7631a0..6155deb463 100644 --- a/aries/aries_vcx/tests/test_anoncreds.rs +++ b/aries/aries_vcx/tests/test_anoncreds.rs @@ -110,6 +110,7 @@ async fn test_pool_revoke_credential() -> Result<(), Box> { &setup.anoncreds, &setup.anoncreds, &setup.institution_did, + &schema, &cred_def, Some(&rev_reg), ) diff --git a/aries/aries_vcx/tests/test_credential_retrieval.rs b/aries/aries_vcx/tests/test_credential_retrieval.rs index e43af06c3c..12cfbe5d15 100644 --- a/aries/aries_vcx/tests/test_credential_retrieval.rs +++ b/aries/aries_vcx/tests/test_credential_retrieval.rs @@ -147,6 +147,7 @@ async fn test_agency_pool_case_for_proof_req_doesnt_matter_for_retrieve_creds( &setup.anoncreds, &setup.anoncreds, &setup.institution_did, + &schema, &cred_def, None, ) diff --git a/aries/aries_vcx/tests/test_credentials.rs b/aries/aries_vcx/tests/test_credentials.rs index 91f6df1f77..cd5ef078b9 100644 --- a/aries/aries_vcx/tests/test_credentials.rs +++ b/aries/aries_vcx/tests/test_credentials.rs @@ -48,6 +48,7 @@ async fn test_pool_prover_get_credential() -> Result<(), Box> { &setup.anoncreds, &setup.anoncreds, &setup.institution_did, + &schema, &cred_def, Some(&rev_reg), ) @@ -102,6 +103,7 @@ async fn test_pool_is_cred_revoked() -> Result<(), Box> { &setup.anoncreds, &setup.anoncreds, &setup.institution_did, + &schema, &cred_def, Some(&rev_reg), ) diff --git a/aries/aries_vcx/tests/test_proof_presentation.rs b/aries/aries_vcx/tests/test_proof_presentation.rs index 3053450abd..2609da0175 100644 --- a/aries/aries_vcx/tests/test_proof_presentation.rs +++ b/aries/aries_vcx/tests/test_proof_presentation.rs @@ -71,6 +71,7 @@ async fn test_agency_pool_generate_proof_with_predicates() -> Result<(), Box, ) -> String { @@ -159,6 +160,7 @@ pub async fn create_and_write_credential( wallet_holder, req_meta, cred, + schema.schema_json.clone(), cred_def.get_cred_def_json().try_clone().unwrap(), rev_reg_def_json .as_deref() diff --git a/aries/aries_vcx_anoncreds/src/anoncreds/anoncreds/mod.rs b/aries/aries_vcx_anoncreds/src/anoncreds/anoncreds/mod.rs index 41ad7fb9cf..5b83239d47 100644 --- a/aries/aries_vcx_anoncreds/src/anoncreds/anoncreds/mod.rs +++ b/aries/aries_vcx_anoncreds/src/anoncreds/anoncreds/mod.rs @@ -966,26 +966,21 @@ impl BaseAnonCreds for Anoncreds { async fn prover_store_credential( &self, wallet: &impl BaseWallet, - cred_req_metadata_json: CredentialRequestMetadata, - cred_json: Credential, - cred_def_json: CredentialDefinition, - rev_reg_def_json: Option, + cred_req_metadata: CredentialRequestMetadata, + unprocessed_cred: Credential, + schema: Schema, + cred_def: CredentialDefinition, + rev_reg_def: Option, ) -> VcxAnoncredsResult { - let mut credential: AnoncredsCredential = cred_json.convert(())?; - - let cred_def_id = credential.cred_def_id.to_string(); - let (_cred_def_method, issuer_did, _signature_type, _schema_num, _tag) = - cred_def_parts(&cred_def_id).ok_or(VcxAnoncredsError::InvalidSchema( - "Could not process credential.cred_def_id as parts.".into(), - ))?; + let mut credential: AnoncredsCredential = unprocessed_cred.convert(())?; let cred_request_metadata: AnoncredsCredentialRequestMetadata = - cred_req_metadata_json.convert(())?; + cred_req_metadata.convert(())?; let link_secret_id = &cred_request_metadata.link_secret_name; let link_secret = self.get_link_secret(wallet, link_secret_id).await?; - let cred_def: AnoncredsCredentialDefinition = cred_def_json.convert(())?; + let cred_def: AnoncredsCredentialDefinition = cred_def.convert(())?; let rev_reg_def: Option = - if let Some(rev_reg_def_json) = rev_reg_def_json { + if let Some(rev_reg_def_json) = rev_reg_def { Some(rev_reg_def_json.convert(())?) } else { None @@ -1000,19 +995,20 @@ impl BaseAnonCreds for Anoncreds { )?; let schema_id = &credential.schema_id; + let cred_def_id = &credential.cred_def_id; + let issuer_did = &cred_def.issuer_id; - let (_schema_method, schema_issuer_did, schema_name, schema_version) = - schema_parts(schema_id.0.as_str()).ok_or(VcxAnoncredsError::InvalidSchema(format!( - "Could not process credential.schema_id {schema_id} as parts." - )))?; + let schema_issuer_did = schema.issuer_id; + let schema_name = schema.name; + let schema_version = schema.version; let mut tags = RecordTags::new(vec![ RecordTag::new("schema_id", &schema_id.0), - RecordTag::new("schema_issuer_did", schema_issuer_did.did()), + RecordTag::new("schema_issuer_did", &schema_issuer_did.0), RecordTag::new("schema_name", &schema_name), RecordTag::new("schema_version", &schema_version), - RecordTag::new("issuer_did", issuer_did.did()), - RecordTag::new("cred_def_id", &cred_def_id), + RecordTag::new("issuer_did", &issuer_did.0), + RecordTag::new("cred_def_id", &cred_def_id.0), ]); if let Some(rev_reg_id) = &credential.rev_reg_id { @@ -1403,84 +1399,3 @@ pub fn schema_parts(id: &str) -> Option<(Option<&str>, Did, String, String)> { None } - -pub fn cred_def_parts(id: &str) -> Option<(Option<&str>, Did, String, SchemaId, String)> { - let parts = id.split_terminator(':').collect::>(); - - if parts.len() == 4 { - // Th7MpTaRZVRYnPiabds81Y:3:CL:1 - let did = parts[0].to_string(); - let Ok(did) = Did::parse(did) else { - return None; - }; - let signature_type = parts[2].to_string(); - let schema_id = parts[3].to_string(); - let tag = String::new(); - return Some((None, did, signature_type, SchemaId(schema_id), tag)); - } - - if parts.len() == 5 { - // Th7MpTaRZVRYnPiabds81Y:3:CL:1:tag - let did = parts[0].to_string(); - let Ok(did) = Did::parse(did) else { - return None; - }; - let signature_type = parts[2].to_string(); - let schema_id = parts[3].to_string(); - let tag = parts[4].to_string(); - return Some((None, did, signature_type, SchemaId(schema_id), tag)); - } - - if parts.len() == 7 { - // NcYxiDXkpYi6ov5FcYDi1e:3:CL:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0 - let did = parts[0].to_string(); - let Ok(did) = Did::parse(did) else { - return None; - }; - let signature_type = parts[2].to_string(); - let schema_id = parts[3..7].join(":"); - let tag = String::new(); - return Some((None, did, signature_type, SchemaId(schema_id), tag)); - } - - if parts.len() == 8 { - // NcYxiDXkpYi6ov5FcYDi1e:3:CL:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0:tag - let did = parts[0].to_string(); - let Ok(did) = Did::parse(did) else { - return None; - }; - let signature_type = parts[2].to_string(); - let schema_id = parts[3..7].join(":"); - let tag = parts[7].to_string(); - return Some((None, did, signature_type, SchemaId(schema_id), tag)); - } - - if parts.len() == 9 { - // creddef:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:3:CL:3:tag - let method = parts[1]; - let did = parts[2..5].join(":"); - let Ok(did) = Did::parse(did) else { - return None; - }; - let signature_type = parts[6].to_string(); - let schema_id = parts[7].to_string(); - let tag = parts[8].to_string(); - return Some((Some(method), did, signature_type, SchemaId(schema_id), tag)); - } - - if parts.len() == 16 { - // creddef:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:3:CL:schema:sov:did:sov: - // NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0:tag - let method = parts[1]; - let did = parts[2..5].join(":"); - let Ok(did) = Did::parse(did) else { - return None; - }; - let signature_type = parts[6].to_string(); - let schema_id = parts[7..15].join(":"); - let tag = parts[15].to_string(); - return Some((Some(method), did, signature_type, SchemaId(schema_id), tag)); - } - - None -} diff --git a/aries/aries_vcx_anoncreds/src/anoncreds/base_anoncreds.rs b/aries/aries_vcx_anoncreds/src/anoncreds/base_anoncreds.rs index 49163b6c73..0dac88a6ef 100644 --- a/aries/aries_vcx_anoncreds/src/anoncreds/base_anoncreds.rs +++ b/aries/aries_vcx_anoncreds/src/anoncreds/base_anoncreds.rs @@ -146,10 +146,11 @@ pub trait BaseAnonCreds: std::fmt::Debug + Send + Sync { async fn prover_store_credential( &self, wallet: &impl BaseWallet, - cred_req_metadata_json: CredentialRequestMetadata, - cred_json: Credential, - cred_def_json: CredentialDefinition, - rev_reg_def_json: Option, + cred_req_metadata: CredentialRequestMetadata, + unprocessed_cred: Credential, + schema: Schema, + cred_def: CredentialDefinition, + rev_reg_def: Option, ) -> VcxAnoncredsResult; async fn prover_delete_credential( diff --git a/aries/misc/test_utils/src/mockdata/mock_anoncreds.rs b/aries/misc/test_utils/src/mockdata/mock_anoncreds.rs index 696dce466f..e09d19f949 100644 --- a/aries/misc/test_utils/src/mockdata/mock_anoncreds.rs +++ b/aries/misc/test_utils/src/mockdata/mock_anoncreds.rs @@ -188,10 +188,11 @@ impl BaseAnonCreds for MockAnoncreds { async fn prover_store_credential( &self, _wallet: &impl BaseWallet, - _cred_req_metadata_json: CredentialRequestMetadata, - _cred_json: Credential, - _cred_def_json: CredentialDefinition, - _rev_reg_def_json: Option, + _cred_req_metadata: CredentialRequestMetadata, + _cred: Credential, + _schema: Schema, + _cred_def: CredentialDefinition, + _rev_reg_def: Option, ) -> VcxAnoncredsResult { Ok("cred_id".to_string()) }