Skip to content

No id creation #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions anoncreds/src/data_types/anoncreds/cred_def.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::data_types::identifiers::cred_def::CredentialDefinitionId;
use crate::data_types::identifiers::schema::SchemaId;
use crate::data_types::{ConversionError, Validatable, ValidationError};
use indy_utils::{Validatable, ValidationError};

use crate::{data_types::ConversionError, impl_anoncreds_object_identifier};

use super::schema::SchemaId;

pub const CL_SIGNATURE_TYPE: &str = "CL";

impl_anoncreds_object_identifier!(CredentialDefinitionId);

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum SignatureType {
CL,
Expand Down Expand Up @@ -38,26 +42,9 @@ pub enum CredentialDefinition {
CredentialDefinitionV1(CredentialDefinitionV1),
}

impl CredentialDefinition {
pub fn id(&self) -> &CredentialDefinitionId {
match self {
CredentialDefinition::CredentialDefinitionV1(c) => &c.id,
}
}
}

impl Validatable for CredentialDefinition {
fn validate(&self) -> Result<(), ValidationError> {
match self {
CredentialDefinition::CredentialDefinitionV1(cred_def) => cred_def.validate(),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CredentialDefinitionV1 {
pub id: CredentialDefinitionId,
pub schema_id: SchemaId,
#[serde(rename = "type")]
pub signature_type: SignatureType,
Expand All @@ -78,7 +65,6 @@ impl CredentialDefinitionV1 {

impl Validatable for CredentialDefinitionV1 {
fn validate(&self) -> Result<(), ValidationError> {
self.id.validate()?;
self.schema_id.validate()
}
}
Expand Down
22 changes: 3 additions & 19 deletions anoncreds/src/data_types/anoncreds/cred_offer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use super::nonce::Nonce;
use crate::data_types::identifiers::cred_def::CredentialDefinitionId;
use crate::data_types::identifiers::schema::SchemaId;
use crate::data_types::utils::Qualifiable;
use crate::data_types::{Validatable, ValidationError};
use indy_utils::{Validatable, ValidationError};

use super::{cred_def::CredentialDefinitionId, nonce::Nonce, schema::SchemaId};

#[derive(Debug, Deserialize, Serialize)]
pub struct CredentialOffer {
Expand All @@ -14,20 +12,6 @@ pub struct CredentialOffer {
pub method_name: Option<String>,
}

impl CredentialOffer {
#[allow(unused)]
pub fn to_unqualified(self) -> CredentialOffer {
let method_name = self.cred_def_id.get_method().map(str::to_owned);
CredentialOffer {
schema_id: self.schema_id.to_unqualified(),
cred_def_id: self.cred_def_id.to_unqualified(),
key_correctness_proof: self.key_correctness_proof,
nonce: self.nonce,
method_name,
}
}
}

impl Validatable for CredentialOffer {
fn validate(&self) -> Result<(), ValidationError> {
self.schema_id.validate()?;
Expand Down
19 changes: 2 additions & 17 deletions anoncreds/src/data_types/anoncreds/cred_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::nonce::Nonce;
use crate::data_types::identifiers::cred_def::CredentialDefinitionId;
use crate::data_types::utils::Qualifiable;
use super::{cred_def::CredentialDefinitionId, nonce::Nonce};
use crate::data_types::{Validatable, ValidationError};
use indy_utils::did::DidValue;

Expand All @@ -13,23 +11,10 @@ pub struct CredentialRequest {
pub nonce: Nonce,
}

impl CredentialRequest {
#[allow(unused)]
pub fn to_unqualified(self) -> CredentialRequest {
CredentialRequest {
prover_did: self.prover_did.to_unqualified(),
cred_def_id: self.cred_def_id.to_unqualified(),
blinded_ms: self.blinded_ms,
blinded_ms_correctness_proof: self.blinded_ms_correctness_proof,
nonce: self.nonce,
}
}
}

impl Validatable for CredentialRequest {
fn validate(&self) -> Result<(), ValidationError> {
self.cred_def_id.validate()?;
self.prover_did.validate()?;
self.cred_def_id.validate()?;
Ok(())
}
}
Expand Down
8 changes: 4 additions & 4 deletions anoncreds/src/data_types/anoncreds/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::collections::HashMap;

use zeroize::Zeroize;

use crate::data_types::identifiers::cred_def::CredentialDefinitionId;
use crate::data_types::identifiers::rev_reg::RevocationRegistryId;
use crate::data_types::identifiers::schema::SchemaId;
use crate::data_types::{Validatable, ValidationError};

use super::{cred_def::CredentialDefinitionId, rev_reg::RevocationRegistryId, schema::SchemaId};

#[derive(Debug, Deserialize, Serialize)]
pub struct Credential {
pub schema_id: SchemaId,
Expand Down Expand Up @@ -49,9 +48,10 @@ impl Credential {

impl Validatable for Credential {
fn validate(&self) -> Result<(), ValidationError> {
self.values.validate()?;
self.schema_id.validate()?;
self.cred_def_id.validate()?;
self.values.validate()?;
self.rev_reg_id.as_ref().map(|i| i.validate()).transpose()?;

if self.rev_reg_id.is_some() && (self.witness.is_none() || self.rev_reg.is_none()) {
return Err("Credential validation failed: `witness` and `rev_reg` must be passed for revocable Credential".into());
Expand Down
64 changes: 64 additions & 0 deletions anoncreds/src/data_types/anoncreds/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#[macro_export]
macro_rules! impl_anoncreds_object_identifier {
($i:ident) => {
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)]
pub struct $i(pub String);

impl $i {
pub fn new_unchecked(s: impl Into<String>) -> Self {
Self(s.into())
}

pub fn new(s: impl Into<String>) -> Result<Self, crate::data_types::ValidationError> {
let s = Self(s.into());
s.validate()?;
Ok(s)
}
}

impl crate::data_types::Validatable for $i {
fn validate(&self) -> Result<(), crate::data_types::ValidationError> {
// TODO: stricten the URI regex.
// Right now everything after the first colon is allowed, we might want to restrict
// this
let uri_regex = regex::Regex::new(r"^[a-zA-Z0-9\+\-\.]+:.+$").unwrap();
uri_regex
.captures(&self.0)
.ok_or_else(|| {
indy_utils::invalid!(
"type: {}, identifier: {} is invalid. It MUST be a URI.",
stringify!($i),
self.0
)
})
.map(|_| ())
}
}

impl Into<String> for $i {
fn into(self) -> String {
self.0
}
}

// TODO: replace these with TryInto
impl From<String> for $i {
fn from(value: String) -> Self {
$i::new_unchecked(value)
}
}

// TODO: replace these with TryInto
impl From<&str> for $i {
fn from(value: &str) -> Self {
$i::new_unchecked(value.to_owned())
}
}

impl std::fmt::Display for $i {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
3 changes: 3 additions & 0 deletions anoncreds/src/data_types/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ pub mod rev_reg_def;

/// V1 credential schemas
pub mod schema;

/// Macros for the data types
pub mod macros;
Loading