Skip to main content

uts_core/codec/
v1.rs

1//! Components for the version 1 OpenTimestamps serialization format.
2
3mod attestation;
4mod detached_timestamp;
5mod digest;
6pub mod opcode;
7mod timestamp;
8
9pub use attestation::{
10    Attestation, AttestationTag, BitcoinAttestation, EASAttestation, EASTimestamped,
11    PendingAttestation, RawAttestation,
12};
13pub use detached_timestamp::DetachedTimestamp;
14pub use digest::DigestHeader;
15pub use timestamp::{Step, Timestamp, builder::TimestampBuilder};
16
17/// Error indicating that finalization of a timestamp failed due to conflicting inputs.
18#[derive(Debug)]
19pub struct FinalizationError;
20
21impl core::fmt::Display for FinalizationError {
22    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23        write!(f, "failed to finalize timestamp due to conflicting inputs")
24    }
25}
26
27impl std::error::Error for FinalizationError {}
28
29/// Trait for objects that may have input data.
30pub trait MayHaveInput {
31    /// Returns the input data for this object, if finalized.
32    fn input(&self) -> Option<&[u8]>;
33}
34
35trait ToInput {
36    fn to_input(&self) -> Option<&[u8]>;
37}
38impl<T: MayHaveInput> ToInput for T {
39    fn to_input(&self) -> Option<&[u8]> {
40        self.input()
41    }
42}
43impl ToInput for [u8] {
44    fn to_input(&self) -> Option<&[u8]> {
45        Some(self)
46    }
47}
48impl ToInput for Vec<u8> {
49    fn to_input(&self) -> Option<&[u8]> {
50        Some(self)
51    }
52}
53
54/// Trait for objects that can be checked for consistency with another object.
55#[allow(private_bounds)]
56pub trait ConsistentWith<T: ToInput + ?Sized>: MayHaveInput {
57    /// Checks if self is consistent with the given input.
58    ///
59    /// Note: Returns true if any of the inputs is not set.
60    fn is_consistent_with(&self, other: &T) -> bool {
61        self.input()
62            .zip(other.to_input())
63            .is_none_or(|(a, b)| a == b)
64    }
65
66    /// Checks if self is consistent with the given input.
67    ///
68    /// Note: Returns false if xor of the inputs is not set.
69    fn is_consistent_with_strict(&self, other: &T) -> bool {
70        self.input() == other.to_input()
71    }
72}
73
74impl<T: MayHaveInput, U: ToInput + ?Sized> ConsistentWith<U> for T {}