Skip to content

Commit

Permalink
Added validations for cred_def
Browse files Browse the repository at this point in the history
Signed-off-by: DenisRybas <[email protected]>
  • Loading branch information
DenisRybas committed Feb 7, 2024
1 parent c430485 commit 088456e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
8 changes: 5 additions & 3 deletions vdr/src/contracts/cl/credential_definition_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn build_create_credential_definition_transaction(
id: &CredentialDefinitionId,
credential_definition: &CredentialDefinition,
) -> VdrResult<Transaction> {
// TODO: validate credential definition
credential_definition.validate()?;
let identity = Address::try_from(&credential_definition.issuer_id)?;
TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
Expand Down Expand Up @@ -70,6 +70,7 @@ pub async fn build_create_credential_definition_endorsing_data(
id: &CredentialDefinitionId,
credential_definition: &CredentialDefinition,
) -> VdrResult<TransactionEndorsingData> {
credential_definition.validate()?;
let identity = Address::try_from(&credential_definition.issuer_id)?;
TransactionEndorsingDataBuilder::new()
.set_contract(CONTRACT_NAME)
Expand Down Expand Up @@ -104,7 +105,7 @@ pub async fn build_create_credential_definition_signed_transaction(
credential_definition: &CredentialDefinition,
signature: &SignatureData,
) -> VdrResult<Transaction> {
// TODO: validate credential definition
credential_definition.validate()?;
let identity = Address::try_from(&credential_definition.issuer_id)?;
TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
Expand Down Expand Up @@ -207,7 +208,6 @@ pub fn parse_credential_definition_created_event(
client: &LedgerClient,
log: &EventLog,
) -> VdrResult<CredentialDefinitionCreatedEvent> {
// TODO: validate schema
EventParser::new()
.set_contract(CONTRACT_NAME)
.set_event(EVENT_CREDENTIAL_DEFINITION_CREATED)
Expand Down Expand Up @@ -248,6 +248,8 @@ pub async fn resolve_credential_definition(
}

let cred_def = parse_credential_definition_created_event(client, &events[0])?.cred_def;
cred_def.matches_id(id)?;

Ok(cred_def)
}

Expand Down
2 changes: 2 additions & 0 deletions vdr/src/contracts/cl/schema_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub async fn build_create_schema_endorsing_data(
id: &SchemaId,
schema: &Schema,
) -> VdrResult<TransactionEndorsingData> {
schema.validate()?;
let identity = Address::try_from(&schema.issuer_id)?;
TransactionEndorsingDataBuilder::new()
.set_contract(CONTRACT_NAME)
Expand Down Expand Up @@ -101,6 +102,7 @@ pub async fn build_create_schema_signed_transaction(
schema: &Schema,
signature: &SignatureData,
) -> VdrResult<Transaction> {
schema.validate()?;
let identity = Address::try_from(&schema.issuer_id)?;
TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
Expand Down
41 changes: 40 additions & 1 deletion vdr/src/contracts/cl/types/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{error::VdrError, types::ContractParam, Address};
use crate::{error::VdrError, types::ContractParam, Address, CredentialDefinitionId, VdrResult};

use crate::{
contracts::{cl::types::schema_id::SchemaId, did::types::did::DID},
Expand All @@ -24,6 +24,45 @@ pub enum CredentialDefinitionTypes {
CL,
}

impl CredentialDefinition {
pub fn id(&self) -> CredentialDefinitionId {
CredentialDefinitionId::build(&self.issuer_id, &self.schema_id.to_string(), &self.tag)
}

pub fn matches_id(&self, expected_id: &CredentialDefinitionId) -> VdrResult<()> {
let actual_id = self.id();

if expected_id.to_string() != actual_id.to_string() {
return Err(VdrError::InvalidCredDef(format!(
"Id built from cred_def: {} != provided id: {}",
actual_id.to_string(),
expected_id.to_string()
)));
}

Ok(())
}

pub fn validate(&self) -> VdrResult<()> {
if self.cred_def_type != CredentialDefinitionTypes::CL {
Err(VdrError::InvalidCredDef(format!(
"Unsupported type: {}",
self.cred_def_type.as_ref()
)))
}

if self.tag.is_empty() {
Err(VdrError::InvalidCredDef("Tag is not provided".to_string()))
}

if self.value.is_empty() {
Err(VdrError::InvalidCredDef("Value is not provided".to_string()))
}

Ok(())
}
}

impl AsRef<str> for CredentialDefinitionTypes {
fn as_ref(&self) -> &str {
match self {
Expand Down
10 changes: 5 additions & 5 deletions vdr/src/contracts/cl/types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ impl Schema {
SchemaId::build(&self.issuer_id, &self.name, &self.version)
}

pub fn matches_id(&self, expected_schema_id: &SchemaId) -> VdrResult<()> {
let actual_schema_id = self.id();
pub fn matches_id(&self, expected_id: &SchemaId) -> VdrResult<()> {
let actual_id = self.id();

if expected_schema_id.to_string() != actual_schema_id.to_string() {
if expected_id.to_string() != actual_id.to_string() {
return Err(VdrError::InvalidSchema(format!(
"Id built from schema: {} != provided id: {}",
actual_schema_id.to_string(),
expected_schema_id.to_string()
actual_id.to_string(),
expected_id.to_string()
)));
}

Expand Down
3 changes: 3 additions & 0 deletions vdr/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub enum VdrError {

#[error("Invalid schema: {}", _0)]
InvalidSchema(String),

#[error("Invalid credential definition: {}", _0)]
InvalidCredDef(String),
}

pub type VdrResult<T> = Result<T, VdrError>;
Expand Down

0 comments on commit 088456e

Please sign in to comment.