Skip to content

Commit

Permalink
Correcting functions to exclude network from schema and cred_def ids
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Oct 2, 2024
1 parent 84013f6 commit f207de2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
3 changes: 2 additions & 1 deletion vdr/src/contracts/anoncreds/types/credential_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ pub mod test {
};
use serde_json::json;

pub const _CREDENTIAL_DEFINITION_ID: &str = "did:ethr:testnet:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/CLAIM_DEF/did:ethr:testnet:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/SCHEMA/F1DClaFEzi3t/1.0.0/default";
pub const CREDENTIAL_DEFINITION_ID: &str = "did:ethr:testnet:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/CLAIM_DEF/did:ethr:testnet:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5:F1DClaFEzi3t:1.0.0/default";
pub const CREDENTIAL_DEFINITION_ID_WITHOUT_NETWORK: &str = "did:ethr:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/CLAIM_DEF/did:ethr:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5:F1DClaFEzi3t:1.0.0/default";
pub const CREDENTIAL_DEFINITION_TAG: &str = "default";

pub fn credential_definition_value() -> serde_json::Value {
Expand Down
67 changes: 65 additions & 2 deletions vdr/src/contracts/anoncreds/types/credential_definition_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,74 @@ impl TryFrom<&CredentialDefinitionId> for ParsedCredentialDefinitionId {
}
let issuer_id = DID::from(parts[0]);
let parsed_issuer_id = ParsedDid::try_from(&issuer_id)?;
let parsed_schema_id = ParsedCredentialDefinitionSchemaId::try_from(parts[4])?;
Ok(ParsedCredentialDefinitionId {
issuer_id: DID::from(parts[0]),
schema_id: parts[3].to_string(),
tag: parts[4].to_string(),
schema_id: parsed_schema_id.as_short_id()?,
tag: parts[5].to_string(),
network: parsed_issuer_id.network,
})
}
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub(crate) struct ParsedCredentialDefinitionSchemaId {
pub(crate) issuer_id: DID,
pub(crate) name: String,
pub(crate) version: String,
pub(crate) network: Option<String>,
}

impl ParsedCredentialDefinitionSchemaId {
pub(crate) fn as_short_id(&self) -> VdrResult<String> {
Ok(
format!(
"{}:{}:{}",
self.issuer_id.without_network()?.as_ref(),
self.name,
self.version
)
)
}
}

impl TryFrom<&str> for ParsedCredentialDefinitionSchemaId {
type Error = VdrError;

fn try_from(schema_id: &str) -> Result<Self, Self::Error> {
let parts = schema_id.split(':').collect::<Vec<&str>>();
if parts.len() == 6 {
return Ok(ParsedCredentialDefinitionSchemaId {
issuer_id: DID::build(parts[1], None, parts[3]),
name: parts[4].to_string(),
version: parts[5].to_string(),
network: Some(parts[2].to_string()),
})
}
if parts.len() == 5 {
return Ok(ParsedCredentialDefinitionSchemaId {
issuer_id: DID::build(parts[1], None, parts[2]),
name: parts[3].to_string(),
version: parts[4].to_string(),
network: None,
})
}
return Err(VdrError::CommonInvalidData(
"Invalid credential definition id provided".to_string(),
));
}
}

#[cfg(test)]
pub mod test {
use crate::contracts::anoncreds::types::credential_definition::test::{CREDENTIAL_DEFINITION_ID, CREDENTIAL_DEFINITION_ID_WITHOUT_NETWORK};
use super::*;

#[test]
fn cred_def_id_id_without_network() {
assert_eq!(
CredentialDefinitionId::from(CREDENTIAL_DEFINITION_ID_WITHOUT_NETWORK),
CredentialDefinitionId::from(CREDENTIAL_DEFINITION_ID).without_network().unwrap()
)
}
}
2 changes: 2 additions & 0 deletions vdr/src/contracts/anoncreds/types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ pub mod test {

pub const SCHEMA_ID: &str =
"did:ethr:testnet:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/SCHEMA/F1DClaFEzi3t/1.0.0";
pub const SCHEMA_ID_WITHOUT_NETWORK: &str =
"did:ethr:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/SCHEMA/F1DClaFEzi3t/1.0.0";
pub const SCHEMA_NAME: &str = "F1DClaFEzi3t";
pub const SCHEMA_VERSION: &str = "1.0.0";
pub const SCHEMA_ATTRIBUTE_FIRST_NAME: &str = "First Name";
Expand Down
18 changes: 16 additions & 2 deletions vdr/src/contracts/anoncreds/types/schema_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,23 @@ impl TryFrom<&SchemaId> for ParsedSchemaId {
let parsed_issuer_id = ParsedDid::try_from(&issuer_id)?;
Ok(ParsedSchemaId {
issuer_id: DID::from(parts[0]),
name: parts[3].to_string(),
version: parts[4].to_string(),
name: parts[4].to_string(),
version: parts[5].to_string(),
network: parsed_issuer_id.network,
})
}
}

#[cfg(test)]
pub mod test {
use crate::contracts::anoncreds::types::schema::test::{SCHEMA_ID, SCHEMA_ID_WITHOUT_NETWORK};
use super::*;

#[test]
fn schema_id_without_network() {
assert_eq!(
SchemaId::from(SCHEMA_ID_WITHOUT_NETWORK),
SchemaId::from(SCHEMA_ID).without_network().unwrap()
)
}
}

0 comments on commit f207de2

Please sign in to comment.