Skip to content
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

No id creation #25

Merged
merged 17 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 2 additions & 28 deletions anoncreds/src/data_types/anoncreds/cred_def.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::data_types::identifiers::cred_def::CredentialDefinitionId;
use crate::data_types::identifiers::schema::SchemaId;
use crate::data_types::{ConversionError, Validatable, ValidationError};
use crate::data_types::ConversionError;

pub const CL_SIGNATURE_TYPE: &str = "CL";

Expand Down Expand Up @@ -38,27 +36,10 @@ 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(),
}
}
}
berendsliedrecht marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CredentialDefinitionV1 {
pub id: CredentialDefinitionId,
pub schema_id: SchemaId,
pub schema_id: String,
berendsliedrecht marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename = "type")]
pub signature_type: SignatureType,
pub tag: String,
Expand All @@ -76,13 +57,6 @@ impl CredentialDefinitionV1 {
}
}

impl Validatable for CredentialDefinitionV1 {
fn validate(&self) -> Result<(), ValidationError> {
self.id.validate()?;
self.schema_id.validate()
berendsliedrecht marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct CredentialDefinitionPrivate {
pub value: ursa::cl::CredentialPrivateKey,
Expand Down
30 changes: 2 additions & 28 deletions anoncreds/src/data_types/anoncreds/cred_offer.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
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};

#[derive(Debug, Deserialize, Serialize)]
pub struct CredentialOffer {
pub schema_id: SchemaId,
pub cred_def_id: CredentialDefinitionId,
pub schema_id: String,
pub cred_def_id: String,
pub key_correctness_proof: ursa::cl::CredentialKeyCorrectnessProof,
pub nonce: Nonce,
#[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(())
berendsliedrecht marked this conversation as resolved.
Show resolved Hide resolved
}
}
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,34 +1,18 @@
use super::nonce::Nonce;
use crate::data_types::identifiers::cred_def::CredentialDefinitionId;
use crate::data_types::utils::Qualifiable;
use crate::data_types::{Validatable, ValidationError};
use indy_utils::did::DidValue;

#[derive(Debug, Deserialize, Serialize)]
pub struct CredentialRequest {
pub prover_did: DidValue,
pub cred_def_id: CredentialDefinitionId,
pub cred_def_id: String,
pub blinded_ms: ursa::cl::BlindedCredentialSecrets,
pub blinded_ms_correctness_proof: ursa::cl::BlindedCredentialSecretsCorrectnessProof,
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
17 changes: 6 additions & 11 deletions anoncreds/src/data_types/anoncreds/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ 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};

#[derive(Debug, Deserialize, Serialize)]
pub struct Credential {
pub schema_id: SchemaId,
pub cred_def_id: CredentialDefinitionId,
pub rev_reg_id: Option<RevocationRegistryId>,
pub schema_id: String,
pub cred_def_id: String,
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 +46,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 @@ -69,9 +64,9 @@ impl Validatable for Credential {
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 schema_id: String,
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
189 changes: 1 addition & 188 deletions anoncreds/src/data_types/anoncreds/pres_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ use serde_json::Value;

use super::credential::Credential;
use super::nonce::Nonce;
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::utils::{qualifiable, Qualifiable};
use crate::data_types::utils::qualifiable;
use crate::data_types::{Validatable, ValidationError};
use indy_utils::did::DidValue;
use indy_utils::invalid;
use indy_utils::query::Query;

Expand Down Expand Up @@ -237,89 +233,6 @@ impl Validatable for PresentationRequest {
}
}

impl PresentationRequest {
#[allow(unused)]
pub fn to_unqualified(self) -> PresentationRequest {
let convert = |request: &mut PresentationRequestPayload| {
for (_, requested_attribute) in request.requested_attributes.iter_mut() {
requested_attribute.restrictions = requested_attribute
.restrictions
.as_mut()
.map(|ref mut restrictions| _convert_query_to_unqualified(&restrictions));
}
for (_, requested_predicate) in request.requested_predicates.iter_mut() {
requested_predicate.restrictions = requested_predicate
.restrictions
.as_mut()
.map(|ref mut restrictions| _convert_query_to_unqualified(&restrictions));
}
};

match self {
PresentationRequest::PresentationRequestV2(mut request) => {
convert(&mut request);
PresentationRequest::PresentationRequestV2(request)
}
PresentationRequest::PresentationRequestV1(mut request) => {
convert(&mut request);
PresentationRequest::PresentationRequestV1(request)
}
}
}
}

fn _convert_query_to_unqualified(query: &Query) -> Query {
match query {
Query::Eq(tag_name, ref tag_value) => Query::Eq(
tag_name.to_string(),
_convert_value_to_unqualified(tag_name, tag_value),
),
Query::Neq(ref tag_name, ref tag_value) => Query::Neq(
tag_name.to_string(),
_convert_value_to_unqualified(tag_name, tag_value),
),
Query::In(ref tag_name, ref tag_values) => Query::In(
tag_name.to_string(),
tag_values
.iter()
.map(|tag_value| _convert_value_to_unqualified(tag_name, tag_value))
.collect::<Vec<String>>(),
),
Query::And(ref queries) => Query::And(
queries
.iter()
.map(|query| _convert_query_to_unqualified(query))
.collect::<Vec<Query>>(),
),
Query::Or(ref queries) => Query::Or(
queries
.iter()
.map(|query| _convert_query_to_unqualified(query))
.collect::<Vec<Query>>(),
),
Query::Not(ref query) => _convert_query_to_unqualified(query),
query => query.clone(),
}
}

fn _convert_value_to_unqualified(tag_name: &str, tag_value: &str) -> String {
match tag_name {
"issuer_did" | "schema_issuer_did" => DidValue(tag_value.to_string()).to_unqualified().0,
"schema_id" => SchemaId(tag_value.to_string()).to_unqualified().0,
"cred_def_id" => {
CredentialDefinitionId(tag_value.to_string())
.to_unqualified()
.0
}
"rev_reg_id" => {
RevocationRegistryId(tag_value.to_string())
.to_unqualified()
.0
}
_ => tag_value.to_string(),
}
}

fn _process_operator(
restriction_op: &Query,
version: &PresentationRequestVersion,
Expand Down Expand Up @@ -415,104 +328,4 @@ mod tests {
serde_json::from_str::<PresentationRequest>(&req_json).unwrap_err();
}
}

mod to_unqualified {
use super::*;

const DID_QUALIFIED: &str = "did:sov:NcYxiDXkpYi6ov5FcYDi1e";
const DID_UNQUALIFIED: &str = "NcYxiDXkpYi6ov5FcYDi1e";
const SCHEMA_ID_QUALIFIED: &str = "schema:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0";
const SCHEMA_ID_UNQUALIFIED: &str = "NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0";
const CRED_DEF_ID_QUALIFIED: &str = "creddef:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:3:CL:schema:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0:tag";
const CRED_DEF_ID_UNQUALIFIED: &str =
"NcYxiDXkpYi6ov5FcYDi1e:3:CL:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0:tag";
const REV_REG_ID_QUALIFIED: &str = "revreg:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:4:creddef:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:3:CL:schema:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0:tag:CL_ACCUM:TAG_1";
const REV_REG_ID_UNQUALIFIED: &str = "NcYxiDXkpYi6ov5FcYDi1e:4:NcYxiDXkpYi6ov5FcYDi1e:3:CL:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0:tag:CL_ACCUM:TAG_1";

#[test]
fn presentation_request_to_unqualified() {
let mut requested_attributes: HashMap<String, AttributeInfo> = HashMap::new();
requested_attributes.insert(
"attr1_referent".to_string(),
AttributeInfo {
name: Some("name".to_string()),
names: None,
restrictions: Some(Query::And(vec![
Query::Eq("issuer_did".to_string(), DID_QUALIFIED.to_string()),
Query::Eq("schema_id".to_string(), SCHEMA_ID_QUALIFIED.to_string()),
Query::Eq("cred_def_id".to_string(), CRED_DEF_ID_QUALIFIED.to_string()),
])),
non_revoked: None,
},
);

let mut requested_predicates: HashMap<String, PredicateInfo> = HashMap::new();
requested_predicates.insert(
"predicate1_referent".to_string(),
PredicateInfo {
name: "age".to_string(),
p_type: PredicateTypes::GE,
p_value: 0,
restrictions: Some(Query::And(vec![
Query::Eq("schema_issuer_did".to_string(), DID_QUALIFIED.to_string()),
Query::Eq("rev_reg_id".to_string(), REV_REG_ID_QUALIFIED.to_string()),
])),
non_revoked: None,
},
);

let request = PresentationRequest::PresentationRequestV2(PresentationRequestPayload {
nonce: Nonce::from_dec("112233445566").unwrap(), //Nonce::new().unwrap(),
name: "presentation_request_to_unqualified".to_string(),
version: "1.0".to_string(),
requested_attributes,
requested_predicates,
non_revoked: None,
});

let mut expected_requested_attributes: HashMap<String, AttributeInfo> = HashMap::new();
expected_requested_attributes.insert(
"attr1_referent".to_string(),
AttributeInfo {
name: Some("name".to_string()),
names: None,
restrictions: Some(Query::And(vec![
Query::Eq("issuer_did".to_string(), DID_UNQUALIFIED.to_string()),
Query::Eq("schema_id".to_string(), SCHEMA_ID_UNQUALIFIED.to_string()),
Query::Eq(
"cred_def_id".to_string(),
CRED_DEF_ID_UNQUALIFIED.to_string(),
),
])),
non_revoked: None,
},
);

let mut expected_requested_predicates: HashMap<String, PredicateInfo> = HashMap::new();
expected_requested_predicates.insert(
"predicate1_referent".to_string(),
PredicateInfo {
name: "age".to_string(),
p_type: PredicateTypes::GE,
p_value: 0,
restrictions: Some(Query::And(vec![
Query::Eq("schema_issuer_did".to_string(), DID_UNQUALIFIED.to_string()),
Query::Eq("rev_reg_id".to_string(), REV_REG_ID_UNQUALIFIED.to_string()),
])),
non_revoked: None,
},
);

let request = request.to_unqualified();
assert_eq!(
expected_requested_attributes,
request.value().requested_attributes
);
assert_eq!(
expected_requested_predicates,
request.value().requested_predicates
);
assert_eq!(PresentationRequestVersion::V2, request.version());
}
}
}
9 changes: 3 additions & 6 deletions anoncreds/src/data_types/anoncreds/presentation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::collections::HashMap;

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;

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -64,9 +61,9 @@ pub struct AttributeValue {

#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct Identifier {
pub schema_id: SchemaId,
pub cred_def_id: CredentialDefinitionId,
pub rev_reg_id: Option<RevocationRegistryId>,
pub schema_id: String,
pub cred_def_id: String,
pub rev_reg_id: Option<String>,
pub timestamp: Option<u64>,
}

Expand Down
Loading