auths-core 0.1.3

Core cryptography and keychain integration for Auths
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Receipt collection from multiple witnesses.
//!
//! This module provides the `ReceiptCollector` which coordinates receipt
//! collection from multiple witnesses in parallel, enforcing threshold
//! requirements for security.
//!
//! # Threshold Semantics
//!
//! KERI uses k-of-n witness thresholds:
//! - n = total number of witnesses
//! - k = minimum receipts required (threshold)
//!
//! For example, with 3 witnesses and threshold 2:
//! - Need at least 2 receipts to succeed
//! - Can tolerate 1 witness being unavailable
//!
//! # Parallel Collection
//!
//! Receipts are collected in parallel for efficiency. The collector
//! returns as soon as the threshold is met, or waits for all witnesses
//! if the threshold cannot be met.

use std::sync::Arc;

use auths_keri::{Prefix, Said};
use tokio::time::{Duration, timeout};

use super::async_provider::AsyncWitnessProvider;
use super::error::{DuplicityEvidence, WitnessError};
use super::receipt::StoredReceipt;
use super::verify::{VerifiedReceipt, verify_receipt};

/// Error during receipt collection.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CollectionError {
    /// Duplicity detected during collection.
    #[error("duplicity detected: {0}")]
    Duplicity(DuplicityEvidence),

    /// Threshold not met - insufficient receipts collected.
    #[error("threshold not met: got {got} receipts, need {required}")]
    ThresholdNotMet {
        /// Number of receipts successfully collected
        got: usize,
        /// Number of receipts required
        required: usize,
        /// Errors from failed witness requests
        errors: Vec<(String, WitnessError)>,
    },

    /// All witnesses failed.
    #[error("all witnesses failed")]
    AllFailed {
        /// Errors from each witness
        errors: Vec<(String, WitnessError)>,
    },

    /// No witnesses configured.
    #[error("no witnesses configured")]
    NoWitnesses,
}

/// Collects receipts from multiple witnesses.
///
/// The collector queries witnesses in parallel and returns when either:
/// - Threshold receipts have been collected (success)
/// - Duplicity is detected (error)
/// - Not enough witnesses respond successfully (error)
///
/// Each witness is pinned to its CESR verkey AID. `collect` returns only
/// [`VerifiedReceipt`]s — receipts whose signatures verify against that pinned
/// AID for the submitted event — so forged or wrong-event receipts can never
/// reach the threshold.
///
/// # Example
///
/// ```rust,ignore
/// use auths_core::witness::{ReceiptCollector, VerifiedReceipt};
///
/// let witnesses: Vec<(Prefix, Arc<dyn AsyncWitnessProvider>)> = pinned_witnesses();
/// let collector = ReceiptCollector::new(witnesses, 2, 5000);
///
/// let receipts: Vec<VerifiedReceipt> =
///     collector.collect(&prefix, &event_said, &event_json).await?;
/// assert!(receipts.len() >= 2);
/// ```
pub struct ReceiptCollector {
    /// Witnesses to query, each paired with its pinned AID so collected
    /// receipts can be attributed without an extra `/health` round-trip.
    witnesses: Vec<(Prefix, Arc<dyn AsyncWitnessProvider>)>,
    /// Minimum receipts required
    threshold: usize,
    /// Timeout per witness in milliseconds
    timeout_ms: u64,
}

impl ReceiptCollector {
    /// Create a new receipt collector.
    ///
    /// # Arguments
    ///
    /// * `witnesses` - Witnesses to query, each as a `(witness_aid, provider)`
    ///   pair. The AID is the witness's pinned CESR verkey prefix; each collected
    ///   receipt's signature is verified against it before it counts toward quorum.
    /// * `threshold` - Minimum number of receipts required
    /// * `timeout_ms` - Timeout per witness operation in milliseconds
    pub fn new(
        witnesses: Vec<(Prefix, Arc<dyn AsyncWitnessProvider>)>,
        threshold: usize,
        timeout_ms: u64,
    ) -> Self {
        Self {
            witnesses,
            threshold,
            timeout_ms,
        }
    }

    /// Get the number of witnesses.
    pub fn witness_count(&self) -> usize {
        self.witnesses.len()
    }

    /// Get the threshold.
    pub fn threshold(&self) -> usize {
        self.threshold
    }

    /// Collect receipts from witnesses.
    ///
    /// Queries all witnesses in parallel and returns when threshold is met
    /// or all witnesses have responded.
    ///
    /// # Arguments
    ///
    /// * `prefix` - The KERI prefix of the identity
    /// * `event_said` - SAID of the event being receipted; every counted receipt
    ///   must reference exactly this event
    /// * `event_json` - The canonicalized JSON bytes of the event
    ///
    /// # Returns
    ///
    /// * `Ok(receipts)` - At least `threshold` *verified* receipts, each carrying
    ///   the AID of the witness that produced it
    /// * `Err(CollectionError::Duplicity(_))` - A witness reported duplicity
    /// * `Err(CollectionError::ThresholdNotMet { .. })` - Too few verified receipts
    pub async fn collect(
        &self,
        prefix: &Prefix,
        event_said: &Said,
        event_json: &[u8],
    ) -> Result<Vec<VerifiedReceipt>, CollectionError> {
        if self.witnesses.is_empty() {
            return Err(CollectionError::NoWitnesses);
        }

        // Spawn tasks for all witnesses
        let mut handles = Vec::with_capacity(self.witnesses.len());
        let timeout_duration = Duration::from_millis(self.timeout_ms);

        for (idx, (aid, witness)) in self.witnesses.iter().enumerate() {
            let witness = Arc::clone(witness);
            let aid = aid.clone();
            let prefix = prefix.clone();
            let event_json = event_json.to_vec();

            let handle = tokio::spawn(async move {
                let result =
                    timeout(timeout_duration, witness.submit_event(&prefix, &event_json)).await;

                match result {
                    Ok(Ok(signed)) => Ok((
                        idx,
                        StoredReceipt {
                            signed,
                            witness: aid,
                        },
                    )),
                    Ok(Err(e)) => Err((idx, e)),
                    Err(_) => Err((
                        idx,
                        WitnessError::Timeout(timeout_duration.as_millis() as u64),
                    )),
                }
            });

            handles.push(handle);
        }

        // Collect results, verifying each receipt before it can count.
        let mut receipts: Vec<VerifiedReceipt> = Vec::new();
        let mut errors: Vec<(String, WitnessError)> = Vec::new();

        for handle in handles {
            match handle.await {
                Ok(Ok((idx, stored))) => match verify_receipt(stored, event_said) {
                    Ok(verified) => receipts.push(verified),
                    // Forged, foreign-key, or wrong-event receipts are dropped
                    // here — never counted toward the threshold.
                    Err(e) => errors.push((format!("witness_{idx}"), e)),
                },
                Ok(Err((idx, e))) => {
                    // A witness that itself reports duplicity is a hard stop.
                    if let WitnessError::Duplicity(evidence) = e {
                        return Err(CollectionError::Duplicity(evidence));
                    }
                    errors.push((format!("witness_{}", idx), e));
                }
                Err(join_err) => {
                    errors.push((
                        "unknown".to_string(),
                        WitnessError::Network(format!("task join error: {}", join_err)),
                    ));
                }
            }
        }

        // Check if we met the threshold
        if receipts.len() >= self.threshold {
            Ok(receipts)
        } else if receipts.is_empty() && !errors.is_empty() {
            Err(CollectionError::AllFailed { errors })
        } else {
            Err(CollectionError::ThresholdNotMet {
                got: receipts.len(),
                required: self.threshold,
                errors,
            })
        }
    }
}

/// Builder for ReceiptCollector.
#[derive(Default)]
pub struct ReceiptCollectorBuilder {
    witnesses: Vec<(Prefix, Arc<dyn AsyncWitnessProvider>)>,
    threshold: Option<usize>,
    timeout_ms: Option<u64>,
}

impl std::fmt::Debug for ReceiptCollectorBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ReceiptCollectorBuilder")
            .field("witnesses_count", &self.witnesses.len())
            .field("threshold", &self.threshold)
            .field("timeout_ms", &self.timeout_ms)
            .finish()
    }
}

impl ReceiptCollectorBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a witness paired with its pinned AID.
    pub fn witness(mut self, aid: Prefix, witness: Arc<dyn AsyncWitnessProvider>) -> Self {
        self.witnesses.push((aid, witness));
        self
    }

    /// Add multiple `(witness_aid, provider)` pairs.
    pub fn witnesses(
        mut self,
        witnesses: impl IntoIterator<Item = (Prefix, Arc<dyn AsyncWitnessProvider>)>,
    ) -> Self {
        self.witnesses.extend(witnesses);
        self
    }

    /// Set the threshold.
    pub fn threshold(mut self, threshold: usize) -> Self {
        self.threshold = Some(threshold);
        self
    }

    /// Set the timeout in milliseconds.
    pub fn timeout_ms(mut self, timeout_ms: u64) -> Self {
        self.timeout_ms = Some(timeout_ms);
        self
    }

    /// Build the collector.
    ///
    /// Uses default timeout of 5000ms if not specified.
    /// Threshold defaults to 1 if not specified.
    pub fn build(self) -> ReceiptCollector {
        ReceiptCollector {
            witnesses: self.witnesses,
            threshold: self.threshold.unwrap_or(1),
            timeout_ms: self.timeout_ms.unwrap_or(5000),
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::witness::NoOpAsyncWitness;
    use async_trait::async_trait;
    use auths_keri::KeriPublicKey;
    use auths_keri::witness::{EventHash, Receipt, ReceiptTag, SignedReceipt};
    use auths_keri::{KeriSequence, VersionString};
    use ring::signature::{Ed25519KeyPair, KeyPair};

    const EVENT_SAID: &str = "EEvent00000000000000000000000000000000000000";
    const OTHER_SAID: &str = "EOther00000000000000000000000000000000000000";

    fn event_said() -> Said {
        Said::new_unchecked(EVENT_SAID.to_string())
    }

    /// A fresh Ed25519 keypair and its CESR `D…` verkey AID.
    fn keypair_and_aid() -> (Arc<Ed25519KeyPair>, Prefix) {
        let rng = ring::rand::SystemRandom::new();
        let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
        let kp = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
        let aid = KeriPublicKey::ed25519(kp.public_key().as_ref())
            .unwrap()
            .to_qb64()
            .unwrap();
        (Arc::new(kp), Prefix::new_unchecked(aid))
    }

    /// A witness that signs a receipt for `said` with `kp` on every submission.
    struct SigningWitness {
        kp: Arc<Ed25519KeyPair>,
        said: Said,
    }

    #[async_trait]
    impl AsyncWitnessProvider for SigningWitness {
        async fn submit_event(
            &self,
            prefix: &Prefix,
            _event_json: &[u8],
        ) -> Result<SignedReceipt, WitnessError> {
            let receipt = Receipt {
                v: VersionString::placeholder(),
                t: ReceiptTag,
                d: self.said.clone(),
                i: prefix.clone(),
                s: KeriSequence::new(0),
            };
            let payload = serde_json::to_vec(&receipt)
                .map_err(|e| WitnessError::Serialization(e.to_string()))?;
            let signature = self.kp.sign(&payload).as_ref().to_vec();
            Ok(SignedReceipt { receipt, signature })
        }

        async fn observe_identity_head(
            &self,
            _prefix: &Prefix,
        ) -> Result<Option<EventHash>, WitnessError> {
            Ok(None)
        }

        async fn get_receipt(
            &self,
            _prefix: &Prefix,
            _event_said: &Said,
        ) -> Result<Option<Receipt>, WitnessError> {
            Ok(None)
        }

        fn quorum(&self) -> usize {
            0
        }
    }

    fn witness_pair(
        kp: Arc<Ed25519KeyPair>,
        aid: Prefix,
        said: Said,
    ) -> (Prefix, Arc<dyn AsyncWitnessProvider>) {
        (
            aid,
            Arc::new(SigningWitness { kp, said }) as Arc<dyn AsyncWitnessProvider>,
        )
    }

    /// An honest witness: signs the expected event with the key its AID pins.
    fn honest() -> (Prefix, Arc<dyn AsyncWitnessProvider>) {
        let (kp, aid) = keypair_and_aid();
        witness_pair(kp, aid, event_said())
    }

    /// A witness pinned to one AID but signing with a *different* key (forged).
    fn forged() -> (Prefix, Arc<dyn AsyncWitnessProvider>) {
        let (signing_kp, _signing_aid) = keypair_and_aid();
        let (_pinned_kp, pinned_aid) = keypair_and_aid();
        witness_pair(signing_kp, pinned_aid, event_said())
    }

    #[tokio::test]
    async fn verified_receipts_reach_quorum() {
        let collector = ReceiptCollector::new(vec![honest(), honest(), honest()], 2, 5000);
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let receipts = collector
            .collect(&prefix, &event_said(), b"{}")
            .await
            .unwrap();

        assert!(receipts.len() >= 2);
        assert!(receipts.iter().all(|r| !r.witness.as_str().is_empty()));
    }

    #[tokio::test]
    async fn forged_receipt_does_not_count() {
        // Two honest + one forged; threshold 2 succeeds with only the honest two.
        let collector = ReceiptCollector::new(vec![honest(), honest(), forged()], 2, 5000);
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let receipts = collector
            .collect(&prefix, &event_said(), b"{}")
            .await
            .unwrap();

        assert_eq!(receipts.len(), 2, "forged receipt must be dropped");
    }

    #[tokio::test]
    async fn forged_receipt_drops_below_threshold() {
        // One honest + one forged, threshold 2: only 1 verifies → not met.
        let collector = ReceiptCollector::new(vec![honest(), forged()], 2, 5000);
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let result = collector.collect(&prefix, &event_said(), b"{}").await;

        assert!(matches!(
            result,
            Err(CollectionError::ThresholdNotMet {
                got: 1,
                required: 2,
                ..
            })
        ));
    }

    #[tokio::test]
    async fn wrong_said_receipt_dropped() {
        let (kp, aid) = keypair_and_aid();
        let wrong = witness_pair(kp, aid, Said::new_unchecked(OTHER_SAID.to_string()));
        let collector = ReceiptCollector::new(vec![honest(), wrong], 2, 5000);
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let result = collector.collect(&prefix, &event_said(), b"{}").await;

        assert!(matches!(
            result,
            Err(CollectionError::ThresholdNotMet { got: 1, .. })
        ));
    }

    #[tokio::test]
    async fn no_witnesses_error() {
        let collector = ReceiptCollector::new(vec![], 1, 5000);
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let result = collector.collect(&prefix, &event_said(), b"{}").await;

        assert!(matches!(result, Err(CollectionError::NoWitnesses)));
    }

    #[tokio::test]
    async fn builder_pattern() {
        let (_, aid0) = keypair_and_aid();
        let (_, aid1) = keypair_and_aid();
        let collector = ReceiptCollectorBuilder::new()
            .witness(aid0, Arc::new(NoOpAsyncWitness))
            .witness(aid1, Arc::new(NoOpAsyncWitness))
            .threshold(2)
            .timeout_ms(1000)
            .build();

        assert_eq!(collector.witness_count(), 2);
        assert_eq!(collector.threshold(), 2);
    }

    #[test]
    fn collection_error_display() {
        let err = CollectionError::ThresholdNotMet {
            got: 1,
            required: 2,
            errors: vec![],
        };
        assert!(format!("{}", err).contains("threshold not met"));

        let err = CollectionError::NoWitnesses;
        assert!(format!("{}", err).contains("no witnesses"));
    }
}