diff --git a/uniffi/src/anoncreds.udl b/uniffi/src/anoncreds.udl index 0d0d18a5..5ee0b6e6 100644 --- a/uniffi/src/anoncreds.udl +++ b/uniffi/src/anoncreds.udl @@ -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 { @@ -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(); diff --git a/uniffi/src/types/cred_req.rs b/uniffi/src/types/cred_req.rs index 632c162f..03010e72 100644 --- a/uniffi/src/types/cred_req.rs +++ b/uniffi/src/types/cred_req.rs @@ -12,6 +12,12 @@ pub struct CredentialRequest { } impl CredentialRequest { + pub fn new(jsonString: String) -> Result { + 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() } @@ -35,6 +41,18 @@ pub struct CredentialRequestMetadata { pub link_secret_name: String, } +// impl CredentialRequestMetadata { +// pub fn new(jsonString: String) -> Result { +// 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 { +// serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError) +// } +// } + impl Into for CredentialRequestMetadata { fn into(self) -> AnoncredsCredentialRequestMetadata { let link_secret_core: ursa::cl::CredentialSecretsBlindingFactors = serde_json::from_str(&self.link_secret_blinding_data).unwrap(); diff --git a/uniffi/src/types/nonce.rs b/uniffi/src/types/nonce.rs index e8f8e7d3..8477264b 100644 --- a/uniffi/src/types/nonce.rs +++ b/uniffi/src/types/nonce.rs @@ -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 { + 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 { + let clone = self.clone(); + return Ok(clone.into()); + } +} + +impl From 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 { + acr.anoncreds_nonce + .try_clone() + .map_err(|_| AnoncredsError::ConversionError) } } @@ -25,14 +58,18 @@ impl TryFrom<&str> for Nonce { type Error = AnoncredsError; fn try_from(value: &str) -> Result { - 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, + }; } -} \ No newline at end of file +}