Skip to content

Commit 261ee8d

Browse files
feat: add more json serialisation methods
1 parent 83516be commit 261ee8d

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

uniffi/src/anoncreds.udl

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

3737
interface LinkSecret {
3838
constructor();
39+
[Throws=AnoncredsError, Name=new_from_json]
40+
constructor(string json_string);
3941
string get_big_number();
42+
[Throws=AnoncredsError]
43+
string get_json();
4044
};
4145

4246
interface Nonce {
@@ -75,6 +79,8 @@ interface CredentialDefinition {
7579
SignatureType get_signature_type();
7680
string get_tag();
7781
IssuerId get_issuer_id();
82+
[Throws=AnoncredsError]
83+
string get_json();
7884
};
7985

8086
interface CredentialOffer {
@@ -85,18 +91,22 @@ interface CredentialOffer {
8591
string get_key_correctness_proof();
8692
Nonce get_nonce();
8793
string? get_method_name();
94+
[Throws=AnoncredsError]
95+
string get_json();
8896
};
8997

9098
interface CredentialRequest {
9199
string get_blinded_credential_secrets_json();
92100
string get_blinded_credential_secrets_correctness_proof_json();
93101
Nonce get_nonce();
102+
[Throws=AnoncredsError]
94103
string get_json();
95104
};
96105

97106
interface CredentialKeyCorrectnessProof {
98107
[Throws=AnoncredsError]
99108
constructor(string json_string);
109+
[Throws=AnoncredsError]
100110
string get_json();
101111
};
102112

uniffi/src/types/cred_def.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ impl CredentialDefinition {
8484
pub fn get_issuer_id(&self) -> IssuerId {
8585
self.core.issuer_id.clone()
8686
}
87+
88+
pub fn get_json(&self) -> Result<String, AnoncredsError> {
89+
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
90+
}
8791
}
8892

8993
impl TryInto<AnoncredsCredentialDefinition> for CredentialDefinition {
@@ -191,8 +195,8 @@ impl CredentialKeyCorrectnessProof {
191195
return Ok(CredentialKeyCorrectnessProof { core: core_def })
192196
}
193197

194-
pub fn get_json(&self) -> String {
195-
serde_json::to_string(&self.core.value).unwrap()
198+
pub fn get_json(&self) -> Result<String, AnoncredsError> {
199+
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
196200
}
197201
}
198202

uniffi/src/types/cred_offer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ impl CredentialOffer {
4242
pub fn get_method_name(&self) -> Option<String> {
4343
self.core.method_name.clone()
4444
}
45+
46+
pub fn get_json(&self) -> Result<String, AnoncredsError> {
47+
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
48+
}
4549
}
4650

4751
impl Clone for CredentialOffer {

uniffi/src/types/cred_req.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl CredentialRequest {
2424
return Arc::new(Nonce { anoncreds_nonce: self.core.nonce.try_clone().unwrap() })
2525
}
2626

27-
pub fn get_json(&self) -> String {
28-
serde_json::to_string(&self.core).unwrap()
27+
pub fn get_json(&self) -> Result<String, AnoncredsError> {
28+
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
2929
}
3030
}
3131

uniffi/src/types/link_secret.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ 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 })
20+
}
21+
1722
pub fn get_big_number(&self) -> String {
1823
let clone = self.clone();
1924
clone.into()
2025
}
26+
27+
pub fn get_json(&self) -> Result<String, AnoncredsError> {
28+
let clone = self.clone();
29+
return Ok(clone.into())
30+
}
2131
}
2232

2333
impl From<AnoncredsLinkSecret> for LinkSecret {

0 commit comments

Comments
 (0)