1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct BbsCredential {
7 pub issuer: String,
9 pub subject: Option<String>,
11 pub types: Vec<String>,
13 pub claims: Vec<BbsClaim>,
15 pub signature: Vec<u8>,
17 pub issuer_public_key: Vec<u8>,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct BbsClaim {
24 pub namespace: String,
26 pub name: String,
28 pub value: Value,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct BbsDerivedProof {
35 pub issuer: String,
37 pub subject: Option<String>,
39 pub types: Vec<String>,
41 pub proof: Vec<u8>,
43 pub disclosed_claims: Vec<(usize, BbsClaim)>,
45 pub total_claims: usize,
47 pub issuer_public_key: Vec<u8>,
49 pub predicate_results: Vec<(String, bool)>,
51 pub nonce: Option<Vec<u8>>,
53}
54
55impl BbsCredential {
56 pub fn encode_messages(&self) -> Vec<Vec<u8>> {
58 self.claims
59 .iter()
60 .map(|claim| {
61 let canonical = serde_json::json!({
62 "ns": claim.namespace,
63 "name": claim.name,
64 "value": claim.value,
65 });
66 serde_json::to_vec(&canonical).unwrap_or_default()
67 })
68 .collect()
69 }
70
71 pub fn to_bytes(&self) -> baseid_core::Result<Vec<u8>> {
73 serde_json::to_vec(self).map_err(|e| baseid_core::error::SerializationError::Json(e).into())
74 }
75
76 pub fn from_bytes(bytes: &[u8]) -> baseid_core::Result<Self> {
78 serde_json::from_slice(bytes)
79 .map_err(|e| baseid_core::error::SerializationError::Json(e).into())
80 }
81}
82
83impl BbsDerivedProof {
84 pub fn to_bytes(&self) -> baseid_core::Result<Vec<u8>> {
86 serde_json::to_vec(self).map_err(|e| baseid_core::error::SerializationError::Json(e).into())
87 }
88
89 pub fn from_bytes(bytes: &[u8]) -> baseid_core::Result<Self> {
91 serde_json::from_slice(bytes)
92 .map_err(|e| baseid_core::error::SerializationError::Json(e).into())
93 }
94}