tenzro-types 0.1.0

Core types and constants for Tenzro Network
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
492
493
494
495
496
//! `tenzro://` URI scheme — unified content + node addressing.
//!
//! All Tenzro-native URIs use the `tenzro://` scheme. The path prefix
//! dispatches to the right resolver (blobs, nodes, DIDs, models, gradients,
//! sealed shards, manifests, agent memory, receipts). Transport details
//! (iroh-blobs, libp2p, etc.) are deliberately hidden behind the scheme so
//! the underlying transport can be swapped without breaking URIs.
//!
//! # Canonical forms
//!
//! ```text
//! tenzro://blob/<blake3-hex>                            — content fetch (default: iroh-blobs)
//! tenzro://blob/<blake3-hex>?n=<node-id>                — content fetch with provider hint
//! tenzro://node/<node-id>                               — dial a node by key
//! tenzro://did/<did:tenzro:human|machine:...>           — dial by TDIP DID
//! tenzro://model/<model-id>@<blake3-hex>                — content-addressed model artifact
//! tenzro://gradient/<run-id>/<round>/<blake3-hex>       — outer-gradient artifact (Tenzro Train)
//! tenzro://shard/<manifest-hash>/<envelope-hash>        — sealed shard (Confidential tier)
//! tenzro://manifest/<manifest-hash>                     — sealed-dataset manifest
//! tenzro://memory/<agent-did>/<record-uuid>             — agent-memory archived record
//! tenzro://receipt/<receipt-kind>/<blake3-hex>          — offloaded receipt
//! ```
//!
//! Parsing this module returns a `TenzroUri` enum whose variants match the
//! prefixes above. Resolvers (in `tenzro-iroh`, RPC handlers, etc.) match
//! on the variant and dispatch to the right backend.

use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use thiserror::Error;

/// The leading scheme expected on every Tenzro-native URI.
pub const TENZRO_URI_SCHEME: &str = "tenzro";

/// Parsed Tenzro URI.
///
/// Construct via `TenzroUri::parse(&str)` or the `FromStr` impl. The variants
/// carry parsed components rather than raw substrings; round-tripping via
/// `Display` always emits the canonical form.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TenzroUri {
    /// `tenzro://blob/<blake3-hex>[?n=<node-id>]`
    Blob {
        /// 64-character lowercase hex BLAKE3 hash of the blob.
        hash: String,
        /// Optional provider hint — node-id (hex Ed25519 public key) of a
        /// peer believed to have the blob. The resolver MAY use this as a
        /// preferred dial target before falling back to discovery.
        provider_hint: Option<String>,
    },
    /// `tenzro://node/<node-id>` — dial a node by its (iroh / libp2p) public key.
    Node {
        /// Hex-encoded node public key.
        node_id: String,
    },
    /// `tenzro://did/<did:tenzro:...>` — dial by TDIP DID. Resolution to a
    /// node-id happens via the on-chain identity registry.
    Did {
        /// Full DID string starting with `did:tenzro:`.
        did: String,
    },
    /// `tenzro://model/<model-id>@<blake3-hex>` — content-addressed model
    /// artifact. `model_id` is the human-readable identifier; the hash
    /// pins a specific binary representation.
    Model {
        /// Model registry identifier (e.g. `qwen3-0.6b`).
        model_id: String,
        /// 64-character lowercase hex BLAKE3 hash of the artifact.
        hash: String,
    },
    /// `tenzro://gradient/<run-id>/<round>/<blake3-hex>` — Tenzro Train
    /// outer-gradient artifact.
    Gradient {
        /// Training run identifier.
        run_id: String,
        /// Sync round number.
        round: u64,
        /// 64-character lowercase hex BLAKE3 hash of the gradient blob.
        hash: String,
    },
    /// `tenzro://shard/<manifest-hash>/<envelope-hash>` — Confidential-tier
    /// sealed shard. `manifest_hash` binds it to its parent manifest.
    Shard {
        /// 64-character lowercase hex hash of the parent sealed manifest.
        manifest_hash: String,
        /// 64-character lowercase hex hash of the sealed envelope.
        envelope_hash: String,
    },
    /// `tenzro://manifest/<manifest-hash>` — sealed-dataset manifest. The
    /// signed manifest itself is distributed on the `tenzro/training`
    /// libp2p gossipsub topic (Phase B2, #217); this URI is the durable
    /// content-addressed identifier that pins the binding.
    Manifest {
        /// 64-character lowercase hex hash of the manifest.
        manifest_hash: String,
    },
    /// `tenzro://memory/<agent-did>/<record-uuid>` — agent-memory archived
    /// record (DA pointer).
    Memory {
        /// Owning agent's DID (`did:tenzro:machine:...`).
        agent_did: String,
        /// UUID v4 of the memory record.
        record_uuid: String,
    },
    /// `tenzro://receipt/<receipt-kind>/<blake3-hex>` — offloaded receipt
    /// (settlement / inference / agent-message).
    Receipt {
        /// Receipt kind, matches `tenzro_storage::da::ReceiptKind` lowercase.
        kind: String,
        /// 64-character lowercase hex BLAKE3 hash of the canonical receipt payload.
        hash: String,
    },
}

/// Errors returned when parsing a `tenzro://` URI.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum TenzroUriError {
    #[error("expected scheme `tenzro://`, got `{0}`")]
    WrongScheme(String),
    #[error("empty path after `tenzro://`")]
    EmptyPath,
    #[error("unknown path prefix `{0}` — expected one of blob, node, did, model, gradient, shard, manifest, memory, receipt")]
    UnknownPrefix(String),
    #[error("missing required segment in `{prefix}` URI: {detail}")]
    MissingSegment { prefix: &'static str, detail: &'static str },
    #[error("invalid BLAKE3 hash `{0}` — expected 64 lowercase hex characters")]
    InvalidHash(String),
    #[error("invalid round number `{0}` in gradient URI: {1}")]
    InvalidRound(String, std::num::ParseIntError),
    #[error("invalid `?n=<node-id>` query in blob URI: {0}")]
    InvalidQuery(String),
}

impl TenzroUri {
    /// Parse a `tenzro://...` URI string.
    pub fn parse(input: &str) -> Result<Self, TenzroUriError> {
        let rest = input
            .strip_prefix("tenzro://")
            .ok_or_else(|| TenzroUriError::WrongScheme(input.to_string()))?;

        if rest.is_empty() {
            return Err(TenzroUriError::EmptyPath);
        }

        // Split path from optional query.
        let (path, query) = match rest.split_once('?') {
            Some((p, q)) => (p, Some(q)),
            None => (rest, None),
        };

        let mut segments = path.split('/');
        let prefix = segments
            .next()
            .ok_or(TenzroUriError::EmptyPath)?;

        match prefix {
            "blob" => {
                let hash = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "blob",
                    detail: "BLAKE3 hash",
                })?;
                validate_blake3_hex(hash)?;
                let provider_hint = match query {
                    None => None,
                    Some(q) => Some(parse_node_query(q)?),
                };
                Ok(TenzroUri::Blob {
                    hash: hash.to_string(),
                    provider_hint,
                })
            }
            "node" => {
                let node_id = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "node",
                    detail: "node-id",
                })?;
                Ok(TenzroUri::Node {
                    node_id: node_id.to_string(),
                })
            }
            "did" => {
                // Reassemble: DID strings contain ':' which split('/') leaves intact
                // in the first segment. There should be exactly one segment.
                let did = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "did",
                    detail: "DID string",
                })?;
                if !did.starts_with("did:tenzro:") && !did.starts_with("did:pdis:") {
                    return Err(TenzroUriError::MissingSegment {
                        prefix: "did",
                        detail: "DID must start with `did:tenzro:` or `did:pdis:`",
                    });
                }
                Ok(TenzroUri::Did {
                    did: did.to_string(),
                })
            }
            "model" => {
                let spec = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "model",
                    detail: "<model-id>@<hash>",
                })?;
                let (model_id, hash) = spec
                    .split_once('@')
                    .ok_or(TenzroUriError::MissingSegment {
                        prefix: "model",
                        detail: "expected <model-id>@<hash>",
                    })?;
                validate_blake3_hex(hash)?;
                Ok(TenzroUri::Model {
                    model_id: model_id.to_string(),
                    hash: hash.to_string(),
                })
            }
            "gradient" => {
                let run_id = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "gradient",
                    detail: "run-id",
                })?;
                let round_str = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "gradient",
                    detail: "round",
                })?;
                let round: u64 = round_str
                    .parse()
                    .map_err(|e| TenzroUriError::InvalidRound(round_str.to_string(), e))?;
                let hash = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "gradient",
                    detail: "hash",
                })?;
                validate_blake3_hex(hash)?;
                Ok(TenzroUri::Gradient {
                    run_id: run_id.to_string(),
                    round,
                    hash: hash.to_string(),
                })
            }
            "shard" => {
                let manifest_hash = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "shard",
                    detail: "manifest-hash",
                })?;
                let envelope_hash = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "shard",
                    detail: "envelope-hash",
                })?;
                validate_blake3_hex(manifest_hash)?;
                validate_blake3_hex(envelope_hash)?;
                Ok(TenzroUri::Shard {
                    manifest_hash: manifest_hash.to_string(),
                    envelope_hash: envelope_hash.to_string(),
                })
            }
            "manifest" => {
                let manifest_hash = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "manifest",
                    detail: "manifest-hash",
                })?;
                validate_blake3_hex(manifest_hash)?;
                Ok(TenzroUri::Manifest {
                    manifest_hash: manifest_hash.to_string(),
                })
            }
            "memory" => {
                let agent_did = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "memory",
                    detail: "agent-did",
                })?;
                let record_uuid = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "memory",
                    detail: "record-uuid",
                })?;
                Ok(TenzroUri::Memory {
                    agent_did: agent_did.to_string(),
                    record_uuid: record_uuid.to_string(),
                })
            }
            "receipt" => {
                let kind = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "receipt",
                    detail: "kind",
                })?;
                let hash = segments.next().ok_or(TenzroUriError::MissingSegment {
                    prefix: "receipt",
                    detail: "hash",
                })?;
                validate_blake3_hex(hash)?;
                Ok(TenzroUri::Receipt {
                    kind: kind.to_string(),
                    hash: hash.to_string(),
                })
            }
            other => Err(TenzroUriError::UnknownPrefix(other.to_string())),
        }
    }
}

fn validate_blake3_hex(s: &str) -> Result<(), TenzroUriError> {
    if s.len() != 64 || !s.bytes().all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) {
        return Err(TenzroUriError::InvalidHash(s.to_string()));
    }
    Ok(())
}

fn parse_node_query(q: &str) -> Result<String, TenzroUriError> {
    let (k, v) = q
        .split_once('=')
        .ok_or_else(|| TenzroUriError::InvalidQuery(q.to_string()))?;
    if k != "n" || v.is_empty() {
        return Err(TenzroUriError::InvalidQuery(q.to_string()));
    }
    Ok(v.to_string())
}

impl fmt::Display for TenzroUri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TenzroUri::Blob { hash, provider_hint } => match provider_hint {
                None => write!(f, "tenzro://blob/{hash}"),
                Some(n) => write!(f, "tenzro://blob/{hash}?n={n}"),
            },
            TenzroUri::Node { node_id } => write!(f, "tenzro://node/{node_id}"),
            TenzroUri::Did { did } => write!(f, "tenzro://did/{did}"),
            TenzroUri::Model { model_id, hash } => write!(f, "tenzro://model/{model_id}@{hash}"),
            TenzroUri::Gradient { run_id, round, hash } => {
                write!(f, "tenzro://gradient/{run_id}/{round}/{hash}")
            }
            TenzroUri::Shard {
                manifest_hash,
                envelope_hash,
            } => write!(f, "tenzro://shard/{manifest_hash}/{envelope_hash}"),
            TenzroUri::Manifest { manifest_hash } => write!(f, "tenzro://manifest/{manifest_hash}"),
            TenzroUri::Memory { agent_did, record_uuid } => {
                write!(f, "tenzro://memory/{agent_did}/{record_uuid}")
            }
            TenzroUri::Receipt { kind, hash } => write!(f, "tenzro://receipt/{kind}/{hash}"),
        }
    }
}

impl FromStr for TenzroUri {
    type Err = TenzroUriError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const ZERO_HASH: &str = "0000000000000000000000000000000000000000000000000000000000000000";

    fn roundtrip(uri: &str) -> TenzroUri {
        let parsed = TenzroUri::parse(uri).unwrap_or_else(|e| panic!("parse({uri}) failed: {e}"));
        assert_eq!(parsed.to_string(), uri, "round-trip mismatch");
        parsed
    }

    #[test]
    fn parses_blob() {
        let u = roundtrip(&format!("tenzro://blob/{ZERO_HASH}"));
        match u {
            TenzroUri::Blob { hash, provider_hint } => {
                assert_eq!(hash, ZERO_HASH);
                assert!(provider_hint.is_none());
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_blob_with_provider_hint() {
        let u = roundtrip(&format!("tenzro://blob/{ZERO_HASH}?n=abc123"));
        match u {
            TenzroUri::Blob { provider_hint, .. } => assert_eq!(provider_hint.as_deref(), Some("abc123")),
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_node() {
        roundtrip("tenzro://node/12D3KooWGb6bkYsibMapPBMWjDTiTCGDnXZkkZK6mjzbx4HpaKC8");
    }

    #[test]
    fn parses_did() {
        roundtrip("tenzro://did/did:tenzro:human:00000000-0000-0000-0000-000000000000");
    }

    #[test]
    fn parses_did_pdis_secondary() {
        roundtrip("tenzro://did/did:pdis:guardian:00000000-0000-0000-0000-000000000000");
    }

    #[test]
    fn parses_model() {
        roundtrip(&format!("tenzro://model/qwen3-0.6b@{ZERO_HASH}"));
    }

    #[test]
    fn parses_gradient() {
        roundtrip(&format!("tenzro://gradient/run42/7/{ZERO_HASH}"));
    }

    #[test]
    fn parses_shard() {
        roundtrip(&format!("tenzro://shard/{ZERO_HASH}/{ZERO_HASH}"));
    }

    #[test]
    fn parses_manifest() {
        roundtrip(&format!("tenzro://manifest/{ZERO_HASH}"));
    }

    #[test]
    fn parses_memory() {
        roundtrip(
            "tenzro://memory/did:tenzro:machine:agent42/01234567-89ab-cdef-0123-456789abcdef",
        );
    }

    #[test]
    fn parses_receipt() {
        roundtrip(&format!("tenzro://receipt/settlement/{ZERO_HASH}"));
    }

    #[test]
    fn rejects_wrong_scheme() {
        assert!(matches!(
            TenzroUri::parse("iroh://blob/abc"),
            Err(TenzroUriError::WrongScheme(_))
        ));
        assert!(matches!(
            TenzroUri::parse("http://example.com"),
            Err(TenzroUriError::WrongScheme(_))
        ));
    }

    #[test]
    fn rejects_unknown_prefix() {
        assert!(matches!(
            TenzroUri::parse("tenzro://unknownprefix/abc"),
            Err(TenzroUriError::UnknownPrefix(_))
        ));
    }

    #[test]
    fn rejects_bad_hash() {
        // Too short.
        assert!(matches!(
            TenzroUri::parse("tenzro://blob/abc"),
            Err(TenzroUriError::InvalidHash(_))
        ));
        // Uppercase letters rejected — canonical form is lowercase. Pick a
        // hash that actually contains letters so case matters (a pure-digit
        // hash like 0000... uppercases to itself).
        let mixed_lower = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789";
        let mixed_upper = mixed_lower.to_uppercase();
        assert!(TenzroUri::parse(&format!("tenzro://blob/{mixed_lower}")).is_ok());
        assert!(matches!(
            TenzroUri::parse(&format!("tenzro://blob/{mixed_upper}")),
            Err(TenzroUriError::InvalidHash(_))
        ));
        // Non-hex characters rejected.
        let non_hex = "g".repeat(64);
        assert!(matches!(
            TenzroUri::parse(&format!("tenzro://blob/{non_hex}")),
            Err(TenzroUriError::InvalidHash(_))
        ));
    }

    #[test]
    fn rejects_empty_path() {
        assert!(matches!(
            TenzroUri::parse("tenzro://"),
            Err(TenzroUriError::EmptyPath)
        ));
    }

    #[test]
    fn rejects_bad_query() {
        // Unknown query parameter name.
        assert!(matches!(
            TenzroUri::parse(&format!("tenzro://blob/{ZERO_HASH}?x=abc")),
            Err(TenzroUriError::InvalidQuery(_))
        ));
        // Missing value.
        assert!(matches!(
            TenzroUri::parse(&format!("tenzro://blob/{ZERO_HASH}?n=")),
            Err(TenzroUriError::InvalidQuery(_))
        ));
    }
}