Skip to content

Commit

Permalink
it's compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Oct 3, 2023
1 parent a621b5a commit 102a64c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 21 deletions.
11 changes: 9 additions & 2 deletions uniffi/src/anoncreds.udl
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,13 @@ interface Prover {
);

[Throws=AnoncredsError]
void create_presentation(
PresentationRequest presentation_request
Presentation create_presentation(
PresentationRequest presentation_request,
sequence<Credential> credentials,
record<string, string>? self_attested,
LinkSecret link_secret,
record<SchemaId, Schema> schemas,
record<CredentialDefinitionId, CredentialDefinition> credential_definitions
);

LinkSecret create_link_secret();
Expand All @@ -317,6 +322,8 @@ interface Prover {
interface Presentation {
[Throws=AnoncredsError]
constructor(string json_string);
[Throws=AnoncredsError]
string get_json();
};

interface PresentationRequest {
Expand Down
61 changes: 42 additions & 19 deletions uniffi/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ use crate::types::cred_def::CredentialDefinition;
use crate::types::cred_offer::CredentialOffer;
use crate::types::cred_req::{CredentialRequest, CredentialRequestMetadata};
use crate::types::link_secret::LinkSecret;
use crate::PresentationRequest;
use crate::RevocationRegistryDefinition;
use crate::{Credential, Presentation};
use crate::{PresentationRequest, RevocationRegistryDefinition};
use anoncreds_core::data_types::cred_def::{
CredentialDefinition as AnoncredsCredentialDefinition, CredentialDefinitionId,
};
use anoncreds_core::data_types::schema::{Schema, SchemaId};
use anoncreds_core::prover;
use anoncreds_core::types::PresentCredentials as AnoncredsPresentCredentials;
use std::collections::HashMap;
use std::convert::TryInto;
use std::sync::Arc;

Expand Down Expand Up @@ -80,27 +86,44 @@ impl Prover {
.map_err(|err| AnoncredsError::ProcessCredential(format!("Error: {}", err)))
}

//TODO
pub fn create_presentation(
&self,
presentation_request: Arc<PresentationRequest>,
credentials: Arc<AnoncredsPresentCredentials>,
// self_attested: Option<HashMap<String, String>>,
// link_secret: &LinkSecret,
// schemas: &HashMap<&SchemaId, &Schema>,
// cred_defs: &HashMap<&CredentialDefinitionId, &CredentialDefinition>,
) -> Result<(), AnoncredsError> {
// ) -> Result<Presentation, AnoncredsError> {
// let ret = prover::create_presentation(
// pres_req: &PresentationRequest,
// credentials: PresentCredentials,
// self_attested: Option<HashMap<String, String>>,
// link_secret: &LinkSecret,
// schemas: &HashMap<&SchemaId, &Schema>,
// cred_defs: &HashMap<&CredentialDefinitionId, &CredentialDefinition>,
// ).map_err(|err| AnoncredsError::CreatePresentationError(format!("Error: {}", err)))
credentials: Vec<Arc<Credential>>,
self_attested: Option<HashMap<String, String>>,
link_secret: Arc<LinkSecret>,
schemas: HashMap<SchemaId, Schema>,
credential_definitions: HashMap<CredentialDefinitionId, Arc<CredentialDefinition>>,
) -> Result<Arc<Presentation>, AnoncredsError> {
let pres_req = &presentation_request.core;

// return ret;
return Ok(());
let mut present_credentials = AnoncredsPresentCredentials::default();
let timestamp = None; // TODO
let rev_state = None; //TODO

credentials.iter().for_each(|c| {
let cred = &c.core;
present_credentials.add_credential(cred, timestamp, rev_state);
});

let schemas_anoncreds = schemas.iter().map(|(k, v)| (k, v)).collect();
let cred_defs = credential_definitions
.iter()
.map(|(k, v)| {
let tmp = &v.core;
(k, tmp)
})
.collect();

return prover::create_presentation(
pres_req,
present_credentials,
self_attested,
&link_secret.secret,
&schemas_anoncreds,
&cred_defs,
)
.map_err(|err| AnoncredsError::CreatePresentationError(format!("Error: {}", err)))
.map(|e| Arc::new(Presentation { core: e }));
}
}
32 changes: 32 additions & 0 deletions uniffi/src/types/presentation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use anoncreds_core::types::Presentation as AnoncredsPresentation;
use anoncreds_core::types::PresentationRequest as AnoncredsPresentationRequest;

use crate::AnoncredsError;

pub struct Presentation {
pub core: AnoncredsPresentation,
}

impl Presentation {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsPresentation =
serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
return Ok(Presentation { core: core_def });
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
}
}

pub struct PresentationRequest {
pub core: AnoncredsPresentationRequest,
}

impl PresentationRequest {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsPresentationRequest =
serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
return Ok(PresentationRequest { core: core_def });
}
}
58 changes: 58 additions & 0 deletions uniffi/src/verifier/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::error::AnoncredsError;
use crate::presentation::{Presentation, PresentationRequest};
use crate::CredentialDefinition;
use anoncreds_core::data_types::cred_def::CredentialDefinitionId;
use anoncreds_core::data_types::schema::{Schema, SchemaId};
use anoncreds_core::verifier;
use std::collections::HashMap;
use std::sync::Arc;

//https://mozilla.github.io/uniffi-rs/udl/builtin_types.html

pub struct Verifier {}

impl Verifier {
/// Create a new instance of [Verifier]
pub fn new() -> Self {
Verifier {}
}

/// Verify an incoming proof presentation
pub fn verify_presentation(
&self,
presentation: Arc<Presentation>,
presentation_request: Arc<PresentationRequest>,
schemas: HashMap<SchemaId, Schema>,
credential_definitions: HashMap<CredentialDefinitionId, Arc<CredentialDefinition>>,
// rev_reg_defs: Option<
// &HashMap<&RevocationRegistryDefinitionId, &RevocationRegistryDefinition>,
// >,
// rev_status_lists: Option<Vec<&RevocationStatusList>>,
// nonrevoke_interval_override: Option<
// &HashMap<&RevocationRegistryDefinitionId, HashMap<u64, u64>>,
// >,
) -> Result<bool, AnoncredsError> {
let schemas_anoncreds = schemas.iter().map(|(k, v)| (k, v)).collect();
let cred_defs = credential_definitions
.iter()
.map(|(k, v)| {
let tmp = &v.core;
(k, tmp)
})
.collect();

let ret = verifier::verify_presentation(
&presentation.core, //&(*presentation_core).core
&presentation_request.core,
&schemas_anoncreds,
&cred_defs,
None, //TODO
None, //TODO
None, //TODO
)
.map_err(|err| AnoncredsError::ProcessCredentialError(format!("Error: {}", err)))?;

return Ok(ret);
// return Ok(true);
}
}

0 comments on commit 102a64c

Please sign in to comment.