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
use crate::{operator::OperatorRecord, package::PackageRecord, ProtoEnvelope};
use anyhow::bail;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use std::time::SystemTime;
use warg_crypto::hash::{AnyHash, Hash, HashAlgorithm, SupportedDigest};
use warg_crypto::prefix::VisitPrefixEncode;
use warg_crypto::{prefix, ByteVisitor, Signable, VisitBytes};
use wasmparser::names::KebabStr;

/// Type alias for registry log index
pub type RegistryIndex = usize;

/// Type alias for registry log length
pub type RegistryLen = RegistryIndex;

#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Checkpoint {
    pub log_root: AnyHash,
    pub log_length: RegistryLen,
    pub map_root: AnyHash,
}

impl prefix::VisitPrefixEncode for Checkpoint {
    fn visit_pe<BV: ?Sized + ByteVisitor>(&self, visitor: &mut prefix::PrefixEncodeVisitor<BV>) {
        visitor.visit_str_raw("WARG-CHECKPOINT-V0");
        visitor.visit_unsigned(self.log_length as u64);
        visitor.visit_str(&self.log_root.to_string());
        visitor.visit_str(&self.map_root.to_string());
    }
}

// Manual impls of VisitBytes for VisitPrefixEncode to avoid conflict with blanket impls
impl VisitBytes for Checkpoint {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.visit_bv(visitor);
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TimestampedCheckpoint {
    #[serde(flatten)]
    pub checkpoint: Checkpoint,
    pub timestamp: u64,
}

impl TimestampedCheckpoint {
    pub fn new(checkpoint: Checkpoint, time: SystemTime) -> anyhow::Result<Self> {
        Ok(Self {
            checkpoint,
            timestamp: time.duration_since(std::time::UNIX_EPOCH)?.as_secs(),
        })
    }

    pub fn now(checkpoint: Checkpoint) -> anyhow::Result<Self> {
        Self::new(checkpoint, SystemTime::now())
    }
}

impl Signable for TimestampedCheckpoint {
    const PREFIX: &'static [u8] = b"WARG-CHECKPOINT-SIGNATURE-V0";
}

impl prefix::VisitPrefixEncode for TimestampedCheckpoint {
    fn visit_pe<BV: ?Sized + ByteVisitor>(&self, visitor: &mut prefix::PrefixEncodeVisitor<BV>) {
        visitor.visit_str_raw("WARG-TIMESTAMPED-CHECKPOINT-V0");
        visitor.visit_unsigned(self.checkpoint.log_length as u64);
        visitor.visit_str(&self.checkpoint.log_root.to_string());
        visitor.visit_str(&self.checkpoint.map_root.to_string());
        visitor.visit_unsigned(self.timestamp);
    }
}

// Manual impls of VisitBytes for VisitPrefixEncode to avoid conflict with blanket impls
impl VisitBytes for TimestampedCheckpoint {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.visit_bv(visitor);
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct MapLeaf {
    pub record_id: RecordId,
}

impl prefix::VisitPrefixEncode for MapLeaf {
    fn visit_pe<BV: ?Sized + ByteVisitor>(&self, visitor: &mut prefix::PrefixEncodeVisitor<BV>) {
        visitor.visit_str_raw("WARG-MAP-LEAF-V0");
        visitor.visit_str(&self.record_id.0.to_string());
    }
}

// Manual impls of VisitBytes for VisitPrefixEncode to avoid conflict with blanket impls
impl VisitBytes for MapLeaf {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.visit_bv(visitor);
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogLeaf {
    pub log_id: LogId,
    pub record_id: RecordId,
}

impl prefix::VisitPrefixEncode for LogLeaf {
    fn visit_pe<BV: ?Sized + ByteVisitor>(&self, visitor: &mut prefix::PrefixEncodeVisitor<BV>) {
        visitor.visit_str_raw("WARG-LOG-LEAF-V0");
        visitor.visit_str(&self.log_id.0.to_string());
        visitor.visit_str(&self.record_id.0.to_string());
    }
}

// Manual impls of VisitBytes for VisitPrefixEncode to avoid conflict with blanket impls
impl VisitBytes for LogLeaf {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.visit_bv(visitor);
    }
}

/// Represents a valid package identifier in the registry.
///
/// Valid package identifiers conform to the component model specification
/// for identifiers.
///
/// A valid component model identifier is the format `<namespace>:<name>`,
/// where both parts are also valid WIT identifiers (i.e. kebab-cased).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct PackageId {
    id: String,
    colon: usize,
}

impl PackageId {
    /// Creates a package identifier from the given string.
    ///
    /// Returns an error if the given string is not a valid package identifier.
    pub fn new(id: impl Into<String>) -> anyhow::Result<Self> {
        let id = id.into();

        if let Some(colon) = id.find(':') {
            // Validate the namespace and name parts are valid kebab strings
            if KebabStr::new(&id[..colon]).is_some() && KebabStr::new(&id[colon + 1..]).is_some() {
                return Ok(Self { id, colon });
            }
        }

        bail!("invalid package identifier `{id}`: expected format is `<namespace>:<name>`")
    }

    /// Gets the namespace part of the package identifier.
    pub fn namespace(&self) -> &str {
        &self.id[..self.colon]
    }

    /// Gets the name part of the package identifier.
    pub fn name(&self) -> &str {
        &self.id[self.colon + 1..]
    }
}

impl AsRef<str> for PackageId {
    fn as_ref(&self) -> &str {
        &self.id
    }
}

impl FromStr for PackageId {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl fmt::Display for PackageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{id}", id = self.id)
    }
}

impl prefix::VisitPrefixEncode for PackageId {
    fn visit_pe<BV: ?Sized + ByteVisitor>(&self, visitor: &mut prefix::PrefixEncodeVisitor<BV>) {
        visitor.visit_str_raw("WARG-PACKAGE-ID-V0");
        visitor.visit_str(&self.id);
    }
}

impl VisitBytes for PackageId {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.visit_bv(visitor);
    }
}

impl Serialize for PackageId {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.id)
    }
}

impl<'de> Deserialize<'de> for PackageId {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let id = String::deserialize(deserializer)?;
        PackageId::new(id).map_err(serde::de::Error::custom)
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct LogId(AnyHash);

impl LogId {
    pub fn operator_log<D: SupportedDigest>() -> Self {
        let prefix: &[u8] = b"WARG-OPERATOR-LOG-ID-V0".as_slice();
        let hash: Hash<D> = Hash::of(prefix);
        Self(hash.into())
    }

    pub fn package_log<D: SupportedDigest>(id: &PackageId) -> Self {
        let prefix: &[u8] = b"WARG-PACKAGE-LOG-ID-V0:".as_slice();
        let hash: Hash<D> = Hash::of((prefix, id));
        Self(hash.into())
    }
}

impl fmt::Display for LogId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl VisitBytes for LogId {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        visitor.visit_bytes(self.0.bytes())
    }
}

impl From<AnyHash> for LogId {
    fn from(value: AnyHash) -> Self {
        Self(value)
    }
}

impl From<LogId> for AnyHash {
    fn from(id: LogId) -> Self {
        id.0
    }
}

impl AsRef<[u8]> for LogId {
    fn as_ref(&self) -> &[u8] {
        self.0.bytes()
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RecordId(AnyHash);

impl RecordId {
    pub fn algorithm(&self) -> HashAlgorithm {
        self.0.algorithm()
    }

    pub fn operator_record<D: SupportedDigest>(record: &ProtoEnvelope<OperatorRecord>) -> Self {
        let prefix: &[u8] = b"WARG-OPERATOR-LOG-RECORD-V0:".as_slice();
        let hash: Hash<D> = Hash::of((prefix, record.content_bytes()));
        Self(hash.into())
    }

    pub fn package_record<D: SupportedDigest>(record: &ProtoEnvelope<PackageRecord>) -> Self {
        let prefix: &[u8] = b"WARG-PACKAGE-LOG-RECORD-V0:".as_slice();
        let hash: Hash<D> = Hash::of((prefix, record.content_bytes()));
        Self(hash.into())
    }
}

impl fmt::Display for RecordId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<AnyHash> for RecordId {
    fn from(value: AnyHash) -> Self {
        Self(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use warg_crypto::hash::Sha256;
    use warg_transparency::map::Map;

    #[test]
    fn log_id() {
        let first = Map::<Sha256, LogId, &'static str>::default();
        let second = first.insert(LogId::operator_log::<Sha256>(), "foobar");
        let proof = second.prove(LogId::operator_log::<Sha256>()).unwrap();
        assert_eq!(
            second.root().clone(),
            proof.evaluate(&LogId::operator_log::<Sha256>(), &"foobar")
        );
    }
}