Skip to content

Commit

Permalink
Add more methods to anoncreds uniffi (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro authored Aug 16, 2023
1 parent f1d21b4 commit 6455f59
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
6 changes: 6 additions & 0 deletions uniffi/src/anoncreds.udl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ interface LinkSecret {

interface Nonce {
constructor();
[Throws=AnoncredsError, Name=new_from_value]
constructor(string value_string);
[Throws=AnoncredsError]
string get_value();
};

dictionary CredentialDefinitionConfig {
Expand Down Expand Up @@ -96,6 +100,8 @@ interface CredentialOffer {
};

interface CredentialRequest {
[Throws=AnoncredsError]
constructor(string json_string);
string get_blinded_credential_secrets_json();
string get_blinded_credential_secrets_correctness_proof_json();
Nonce get_nonce();
Expand Down
18 changes: 18 additions & 0 deletions uniffi/src/types/cred_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub struct CredentialRequest {
}

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

pub fn get_blinded_credential_secrets_json(&self) -> String {
serde_json::to_string(&self.core.blinded_ms).unwrap()
}
Expand All @@ -35,6 +41,18 @@ pub struct CredentialRequestMetadata {
pub link_secret_name: String,
}

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

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

impl Into<AnoncredsCredentialRequestMetadata> for CredentialRequestMetadata {
fn into(self) -> AnoncredsCredentialRequestMetadata {
let link_secret_core: ursa::cl::CredentialSecretsBlindingFactors = serde_json::from_str(&self.link_secret_blinding_data).unwrap();
Expand Down
53 changes: 45 additions & 8 deletions uniffi/src/types/nonce.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
use crate::types::error::AnoncredsError;
use anoncreds_core::data_types::nonce::{Nonce as AnoncredsNounce};
use anoncreds_core::data_types::nonce::Nonce as AnoncredsNonce;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::sync::Arc;

pub struct Nonce {
pub anoncreds_nonce: AnoncredsNounce,
pub anoncreds_nonce: AnoncredsNonce,
}

impl Nonce {
pub fn new() -> Self {
let nonce = AnoncredsNounce::new().unwrap();
return Nonce { anoncreds_nonce: nonce }
let nonce = AnoncredsNonce::new().unwrap();
return Nonce {
anoncreds_nonce: nonce,
};
}

pub fn new_from_value(value_string: String) -> Result<Self, AnoncredsError> {
let nonce = AnoncredsNonce::try_from(value_string.as_str())
.map_err(|_| AnoncredsError::ConversionError)?;
return Ok(Nonce {
anoncreds_nonce: nonce,
});
}

pub fn get_value(&self) -> Result<String, AnoncredsError> {
let clone = self.clone();
return Ok(clone.into());
}
}

impl From<AnoncredsNonce> for Nonce {
fn from(acr: AnoncredsNonce) -> Self {
return Nonce {
anoncreds_nonce: acr,
};
}
}

impl TryFrom<&Nonce> for AnoncredsNonce {
type Error = AnoncredsError;

fn try_from(acr: &Nonce) -> Result<Self, Self::Error> {
acr.anoncreds_nonce
.try_clone()
.map_err(|_| AnoncredsError::ConversionError)
}
}

Expand All @@ -25,14 +58,18 @@ impl TryFrom<&str> for Nonce {
type Error = AnoncredsError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let nonce = AnoncredsNounce::try_from(value).map_err(|_| AnoncredsError::ConversionError)?;
return Ok(Nonce { anoncreds_nonce: nonce })
let nonce = AnoncredsNonce::try_from(value).map_err(|_| AnoncredsError::ConversionError)?;
return Ok(Nonce {
anoncreds_nonce: nonce,
});
}
}

impl Clone for Nonce {
fn clone(&self) -> Self {
let original = self.anoncreds_nonce.try_clone().unwrap();
return Nonce { anoncreds_nonce: original }
return Nonce {
anoncreds_nonce: original,
};
}
}
}

0 comments on commit 6455f59

Please sign in to comment.