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 11 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
28 changes: 1 addition & 27 deletions anoncreds/src/data_types/anoncreds/cred_offer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
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 super::{cred_def::CredentialDefinitionId, nonce::Nonce, schema::SchemaId};

#[derive(Debug, Deserialize, Serialize)]
pub struct CredentialOffer {
Expand All @@ -13,25 +9,3 @@ pub struct CredentialOffer {
#[serde(skip_serializing_if = "Option::is_none")]
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()?;
self.cred_def_id.validate()?;
Ok(())
}
}
18 changes: 1 addition & 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,22 +11,8 @@ 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()?;
Ok(())
}
Expand Down
13 changes: 5 additions & 8 deletions anoncreds/src/data_types/anoncreds/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ 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, schema::SchemaId};

#[derive(Debug, Deserialize, Serialize)]
pub struct Credential {
pub schema_id: SchemaId,
pub cred_def_id: CredentialDefinitionId,
pub rev_reg_id: Option<RevocationRegistryId>,
pub rev_reg_id: Option<String>,
pub values: CredentialValues,
pub signature: ursa::cl::CredentialSignature,
pub signature_correctness_proof: ursa::cl::SignatureCorrectnessProof,
Expand Down Expand Up @@ -49,8 +48,6 @@ impl Credential {

impl Validatable for Credential {
fn validate(&self) -> Result<(), ValidationError> {
self.schema_id.validate()?;
self.cred_def_id.validate()?;
self.values.validate()?;

if self.rev_reg_id.is_some() && (self.witness.is_none() || self.rev_reg.is_none()) {
Expand All @@ -70,8 +67,8 @@ pub struct CredentialInfo {
pub referent: String,
pub attrs: ShortCredentialValues,
pub schema_id: SchemaId,
pub cred_def_id: CredentialDefinitionId,
pub rev_reg_id: Option<RevocationRegistryId>,
pub cred_def_id: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why we do not use CredentialDefinitionId here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think that one was not changed, I will update this.

pub rev_reg_id: Option<String>,
pub cred_rev_id: Option<String>,
}

Expand Down
62 changes: 62 additions & 0 deletions anoncreds/src/data_types/anoncreds/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#[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(s: impl Into<String>) -> Self {
Self(s.into())
}

pub fn validated_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: better URI regex
let uri_regex = regex::Regex::new(r".*").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
}
}

impl From<String> for $i {
fn from(value: String) -> Self {
$i::new(value)
}
}

impl From<&str> for $i {
fn from(value: &str) -> Self {
$i::new(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