1use proof_core::{
9 model::{Proof, Proposition},
10 ErrorCode, Limits, ProofError,
11};
12use proof_crypto::build::{CreatedAttestation, CreatedEvent, CreatedEvidence, CreatedRelationship};
13use proof_crypto::id::proof_id;
14use proof_format::{decode_and_check_canonical, encode_canonical, proof_to_cbor};
15use std::collections::HashSet;
16
17#[derive(Debug, Clone)]
19pub struct BuiltProof {
20 pub proof: Proof,
21 pub canonical: Vec<u8>,
22 pub id: String,
23}
24
25#[derive(Debug, Default)]
26pub struct ProofBuilder {
27 proposition: Option<Proposition>,
28 created_at: u64,
29 events: Vec<CreatedEvent>,
30 attestations: Vec<CreatedAttestation>,
31 evidence: Vec<CreatedEvidence>,
32 relationships: Vec<CreatedRelationship>,
33}
34
35impl ProofBuilder {
36 pub fn new(proposition: Proposition, created_at: u64) -> Self {
37 Self {
38 proposition: Some(proposition),
39 created_at,
40 ..Default::default()
41 }
42 }
43
44 pub fn add_event(&mut self, e: CreatedEvent) {
45 self.events.push(e);
46 }
47
48 pub fn add_attestation(&mut self, a: CreatedAttestation) {
49 self.attestations.push(a);
50 }
51
52 pub fn add_evidence(&mut self, e: CreatedEvidence) {
53 self.evidence.push(e);
54 }
55
56 pub fn add_relationship(&mut self, r: CreatedRelationship) {
57 self.relationships.push(r);
58 }
59
60 fn check_duplicates(ids: &[String], what: &str) -> Result<(), ProofError> {
61 let mut seen = HashSet::new();
62 for id in ids {
63 if !seen.insert(id) {
64 return Err(ErrorCode::SchemaViolation.err(format!("duplicate {what} member {id}")));
65 }
66 }
67 Ok(())
68 }
69
70 pub fn build(mut self, limits: &Limits) -> Result<BuiltProof, ProofError> {
71 let proposition = self
72 .proposition
73 .take()
74 .ok_or_else(|| ErrorCode::SchemaViolation.err("proof needs a proposition"))?;
75 if proposition.v != 1 {
76 return Err(ErrorCode::UnsupportedVersion.err("only proposition v=1 supported"));
77 }
78 self.events.sort_by(|a, b| a.id.cmp(&b.id));
80 self.attestations.sort_by(|a, b| a.id.cmp(&b.id));
81 self.evidence.sort_by(|a, b| a.id.cmp(&b.id));
82 self.relationships.sort_by(|a, b| a.id.cmp(&b.id));
83
84 let event_ids: Vec<String> = self.events.iter().map(|e| e.id.clone()).collect();
85 let att_ids: Vec<String> = self.attestations.iter().map(|a| a.id.clone()).collect();
86 let evd_ids: Vec<String> = self.evidence.iter().map(|e| e.id.clone()).collect();
87 let rel_ids: Vec<String> = self.relationships.iter().map(|r| r.id.clone()).collect();
88 Self::check_duplicates(&event_ids, "event")?;
89 Self::check_duplicates(&att_ids, "attestation")?;
90 Self::check_duplicates(&evd_ids, "evidence")?;
91 Self::check_duplicates(&rel_ids, "relationship")?;
92
93 let prop_cbor = proof_format::proposition_to_cbor(&proposition);
94 let id = proof_id(&prop_cbor, &event_ids, &att_ids, &evd_ids, &rel_ids);
95
96 let proof = Proof {
97 v: 1,
98 proof_id: id.clone(),
99 proposition,
100 events: self.events.iter().map(|e| e.content.clone()).collect(),
101 attestations: self
102 .attestations
103 .iter()
104 .map(|a| proof_core::model::StoredAttestation {
105 content: a.content.clone(),
106 sign1: a.sign1.clone(),
107 })
108 .collect(),
109 evidence: self.evidence.iter().map(|e| e.content.clone()).collect(),
110 relationships: self
111 .relationships
112 .iter()
113 .map(|r| r.content.clone())
114 .collect(),
115 created_at: self.created_at,
116 };
117 let canonical = encode_canonical(&proof_to_cbor(&proof)?);
118 if canonical.len() > limits.max_proof_size {
119 return Err(ErrorCode::LimitExceeded.err("built proof exceeds max_proof_size"));
120 }
121 let back =
123 proof_format::cbor_to_proof(&decode_and_check_canonical(&canonical, limits)?, limits)?;
124 if back != proof {
125 return Err(ErrorCode::SchemaViolation.err("built proof failed self-check round-trip"));
126 }
127 Ok(BuiltProof {
128 proof,
129 canonical,
130 id,
131 })
132 }
133}