Skip to content

Commit

Permalink
pipe thru schema to prover store credential
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
  • Loading branch information
gmulhearn-anonyome committed Dec 12, 2024
1 parent ecc7c81 commit 06f9afd
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 123 deletions.
39 changes: 26 additions & 13 deletions aries/aries_vcx/src/protocols/issuance/holder/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -276,13 +277,18 @@ 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,
anoncreds,
&credential,
&state_data.req_meta,
&state_data.cred_def_json,
&schema_json,
)
.await
{
Expand Down Expand Up @@ -549,6 +555,7 @@ async fn _store_credential(
credential: &IssueCredentialV1,
req_meta: &str,
cred_def_json: &str,
schema_json: &str,
) -> VcxResult<(String, Option<String>)> {
trace!(
"Holder::_store_credential >>> credential: {:?}, req_meta: {}, cred_def_json: {}",
Expand All @@ -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(),
)
Expand All @@ -585,14 +593,15 @@ 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,
anoncreds: &impl BaseAnonCreds,
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?;
Expand All @@ -619,18 +628,21 @@ 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,
anoncreds: &impl BaseAnonCreds,
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,
Expand All @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx/tests/test_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ async fn test_pool_revoke_credential() -> Result<(), Box<dyn Error>> {
&setup.anoncreds,
&setup.anoncreds,
&setup.institution_did,
&schema,
&cred_def,
Some(&rev_reg),
)
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx/tests/test_credential_retrieval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 2 additions & 0 deletions aries/aries_vcx/tests/test_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async fn test_pool_prover_get_credential() -> Result<(), Box<dyn Error>> {
&setup.anoncreds,
&setup.anoncreds,
&setup.institution_did,
&schema,
&cred_def,
Some(&rev_reg),
)
Expand Down Expand Up @@ -102,6 +103,7 @@ async fn test_pool_is_cred_revoked() -> Result<(), Box<dyn Error>> {
&setup.anoncreds,
&setup.anoncreds,
&setup.institution_did,
&schema,
&cred_def,
Some(&rev_reg),
)
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx/tests/test_proof_presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async fn test_agency_pool_generate_proof_with_predicates() -> Result<(), Box<dyn
&setup.anoncreds,
&setup.anoncreds,
&setup.institution_did,
&schema,
&cred_def,
Some(&rev_reg),
)
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx/tests/test_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ async fn create_and_store_nonrevocable_credential(
anoncreds_issuer,
anoncreds_holder,
issuer_did,
&schema,
&cred_def,
None,
)
Expand Down
2 changes: 2 additions & 0 deletions aries/aries_vcx/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub async fn create_and_write_credential(
anoncreds_issuer: &impl BaseAnonCreds,
anoncreds_holder: &impl BaseAnonCreds,
institution_did: &Did,
schema: &Schema,
cred_def: &CredentialDef,
rev_reg: Option<&RevocationRegistry>,
) -> String {
Expand Down Expand Up @@ -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()
Expand Down
119 changes: 17 additions & 102 deletions aries/aries_vcx_anoncreds/src/anoncreds/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RevocationRegistryDefinition>,
cred_req_metadata: CredentialRequestMetadata,
unprocessed_cred: Credential,
schema: Schema,
cred_def: CredentialDefinition,
rev_reg_def: Option<RevocationRegistryDefinition>,
) -> VcxAnoncredsResult<CredentialId> {
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<AnoncredsRevocationRegistryDefinition> =
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
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<Vec<&str>>();

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
}
9 changes: 5 additions & 4 deletions aries/aries_vcx_anoncreds/src/anoncreds/base_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RevocationRegistryDefinition>,
cred_req_metadata: CredentialRequestMetadata,
unprocessed_cred: Credential,
schema: Schema,
cred_def: CredentialDefinition,
rev_reg_def: Option<RevocationRegistryDefinition>,
) -> VcxAnoncredsResult<CredentialId>;

async fn prover_delete_credential(
Expand Down
9 changes: 5 additions & 4 deletions aries/misc/test_utils/src/mockdata/mock_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RevocationRegistryDefinition>,
_cred_req_metadata: CredentialRequestMetadata,
_cred: Credential,
_schema: Schema,
_cred_def: CredentialDefinition,
_rev_reg_def: Option<RevocationRegistryDefinition>,
) -> VcxAnoncredsResult<CredentialId> {
Ok("cred_id".to_string())
}
Expand Down

0 comments on commit 06f9afd

Please sign in to comment.