Skip to content

Commit 3e456e2

Browse files
Fix link_secret from/to value methods (#3)
1 parent 0be6d24 commit 3e456e2

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

src/data_types/link_secret.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ impl TryFrom<&str> for LinkSecret {
8383
mod link_secret_tests {
8484
use super::*;
8585

86+
#[test]
87+
fn should_serialize_and_deserialize_into_the_same_value() {
88+
let link_secret = LinkSecret::new().expect("Error creating link secret");
89+
let link_secret_srt: String = link_secret.try_into().expect("Error creating link secret");
90+
println!("{}", link_secret_srt);
91+
let link_secret2 =
92+
LinkSecret::try_from(link_secret_srt.as_str()).expect("Error creating link secret");
93+
let link_secret_srt2: String = link_secret2.try_into().expect("Error creating link secret");
94+
// println!("{}", link_secret_srt2);
95+
assert_eq!(link_secret_srt, link_secret_srt2);
96+
}
97+
8698
#[test]
8799
fn should_create_new_link_secret() {
88100
let link_secret = LinkSecret::new();

uniffi/src/anoncreds.udl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ enum RegistryType {
3636

3737
interface LinkSecret {
3838
constructor();
39-
[Throws=AnoncredsError, Name=new_from_json]
40-
constructor(string json_string);
39+
[Throws=AnoncredsError, Name=new_from_value]
40+
constructor(string value_string);
4141
string get_big_number();
4242
[Throws=AnoncredsError]
43-
string get_json();
43+
string get_value();
4444
};
4545

4646
interface Nonce {

uniffi/src/types/link_secret.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::types::error::AnoncredsError;
2-
use anoncreds_core::data_types::link_secret::{LinkSecret as AnoncredsLinkSecret};
2+
use anoncreds_core::data_types::link_secret::LinkSecret as AnoncredsLinkSecret;
33
use std::convert::TryFrom;
44
use std::convert::TryInto;
55
use std::sync::Arc;
@@ -14,54 +14,88 @@ impl LinkSecret {
1414
LinkSecret { secret: secret }
1515
}
1616

17-
pub fn new_from_json(json_string: String) -> Result<Self, AnoncredsError> {
18-
let core_def = AnoncredsLinkSecret::try_from(json_string.as_str()).map_err(|_| AnoncredsError::ConversionError)?;
19-
return Ok(LinkSecret { secret: core_def })
17+
pub fn new_from_value(value_string: String) -> Result<Self, AnoncredsError> {
18+
let core_def = AnoncredsLinkSecret::try_from(value_string.as_str())
19+
.map_err(|_| AnoncredsError::ConversionError)?;
20+
return Ok(LinkSecret { secret: core_def });
2021
}
2122

2223
pub fn get_big_number(&self) -> String {
2324
let clone = self.clone();
2425
clone.into()
2526
}
2627

27-
pub fn get_json(&self) -> Result<String, AnoncredsError> {
28+
pub fn get_value(&self) -> Result<String, AnoncredsError> {
2829
let clone = self.clone();
29-
return Ok(clone.into())
30+
return Ok(clone.into());
3031
}
3132
}
3233

3334
impl From<AnoncredsLinkSecret> for LinkSecret {
34-
3535
fn from(acr: AnoncredsLinkSecret) -> Self {
36-
return LinkSecret { secret: acr }
36+
return LinkSecret { secret: acr };
3737
}
3838
}
3939

4040
impl TryFrom<&str> for LinkSecret {
4141
type Error = AnoncredsError;
4242

4343
fn try_from(string: &str) -> Result<Self, Self::Error> {
44-
let acr = AnoncredsLinkSecret::try_from(string).map_err(|_| AnoncredsError::ConversionError)?;
45-
return Ok(LinkSecret { secret: acr })
44+
let acr =
45+
AnoncredsLinkSecret::try_from(string).map_err(|_| AnoncredsError::ConversionError)?;
46+
return Ok(LinkSecret { secret: acr });
4647
}
4748
}
4849

4950
impl TryFrom<&LinkSecret> for AnoncredsLinkSecret {
5051
type Error = AnoncredsError;
5152

5253
fn try_from(acr: &LinkSecret) -> Result<Self, Self::Error> {
53-
acr.secret.try_clone().map_err(|_| AnoncredsError::ConversionError)
54-
}
54+
acr.secret
55+
.try_clone()
56+
.map_err(|_| AnoncredsError::ConversionError)
57+
}
5558
}
5659

5760
impl Into<String> for LinkSecret {
5861
fn into(self) -> String {
59-
self.secret.0.to_hex().unwrap()
60-
}
62+
self.secret.0.to_dec().unwrap()
63+
}
6164
}
6265

6366
impl Clone for LinkSecret {
6467
fn clone(&self) -> Self {
65-
LinkSecret { secret: self.secret.try_clone().unwrap() }
68+
LinkSecret {
69+
secret: self.secret.try_clone().unwrap(),
70+
}
71+
}
72+
}
73+
74+
#[cfg(test)]
75+
mod warp_link_secret_tests {
76+
use super::*;
77+
78+
#[test]
79+
fn should_serialize_and_deserialize_AnoncredsLinkSecret_into_the_same_value() {
80+
let link_secret = AnoncredsLinkSecret::new().expect("Error creating link secret");
81+
let link_secret_srt: String = link_secret.try_into().expect("Error creating link secret");
82+
// println!("{}", link_secret_srt);
83+
let link_secret2 = AnoncredsLinkSecret::try_from(link_secret_srt.as_str())
84+
.expect("Error creating link secret");
85+
let link_secret_srt2: String = link_secret2.try_into().expect("Error creating link secret");
86+
// println!("{}", link_secret_srt2);
87+
assert_eq!(link_secret_srt, link_secret_srt2);
6688
}
67-
}
89+
90+
#[test]
91+
fn should_serialize_and_deserialize_LinkSecret_into_the_same_value() {
92+
let link_secret = LinkSecret::new();
93+
let link_secret_srt: String = link_secret.try_into().expect("Error creating link secret");
94+
println!("{}", link_secret_srt);
95+
let link_secret2 =
96+
LinkSecret::try_from(link_secret_srt.as_str()).expect("Error creating link secret");
97+
let link_secret_srt2: String = link_secret2.try_into().expect("Error creating link secret");
98+
println!("{}", link_secret_srt2);
99+
assert_eq!(link_secret_srt, link_secret_srt2);
100+
}
101+
}

0 commit comments

Comments
 (0)