Skip to main content

wire/
agent_card.rs

1//! Agent card — DID-anchored identity for a wire endpoint.
2//!
3//! An agent card binds:
4//!   - a handle (`paul`)
5//!   - to a DID (`did:wire:paul`)
6//!   - to one or more Ed25519 verify keys
7//!   - with a signature from the canonical key
8//!
9//! Bilateral pairing produces a 6-digit Short Authentication String (SAS) by
10//! HMAC'ing the two sorted public keys. Both peers compute the same digits
11//! independently from their own knowledge of both keys; the operator reads
12//! them aloud out-of-band (the magic-wormhole flow) to confirm.
13
14use ed25519_dalek::{Signer, SigningKey, Verifier, VerifyingKey};
15use serde_json::{Value, json};
16use sha2::{Digest, Sha256};
17use thiserror::Error;
18
19use crate::canonical::canonical;
20use crate::signing::{b64decode, b64encode, make_key_id};
21
22pub const CARD_SCHEMA_VERSION: &str = "v3.2";
23pub const DID_METHOD: &str = "did:wire";
24
25/// DID method prefix for operator anchor (RFC-001 §1). Distinct from
26/// `did:wire:` session DIDs so a session DID and an operator DID can
27/// never be confused at parse time.
28pub const DID_METHOD_OP: &str = "did:wire:op";
29
30/// DID method prefix for organization anchor (RFC-001 §1).
31pub const DID_METHOD_ORG: &str = "did:wire:org";
32
33/// Length of the hex tail on op_did / org_did (RFC-001 §1). 32 hex
34/// (128 bits) makes collision search 2^128, much harder than session
35/// DID's 2^32 — appropriate for long-lived identities that anchor
36/// trust scopes rather than ephemeral sessions.
37pub const LONG_FINGERPRINT_HEX_LEN: usize = 32;
38
39/// Build a DID from `handle` + `public_key`. Returns
40/// `did:wire:<handle>-<8-hex-of-sha256(public_key)>`. The pubkey suffix
41/// makes the DID uniquely tied to the keypair — two operators picking
42/// the same handle (e.g., both auto-init'ing as `<hostname>` on the same
43/// hostname) get distinct DIDs.
44///
45/// Pass-through for any string already starting with `did:*` (so callers
46/// can be lazy with mixed inputs).
47pub fn did_for_with_key(handle: &str, public_key: &[u8]) -> String {
48    if handle.starts_with("did:") {
49        return handle.to_string();
50    }
51    let suffix = crate::signing::fingerprint(public_key);
52    format!("{DID_METHOD}:{handle}-{suffix}")
53}
54
55/// Build an operator DID (`did:wire:op:<handle>-<32hex>`). RFC-001
56/// §1 calls for a 32-hex tail (16 bytes of sha256(pubkey)) so the
57/// long-lived operator anchor is collision-resistant at 2^128.
58///
59/// Pass-through for any string already starting with `did:wire:op:`
60/// so callers can be lazy with mixed inputs.
61pub fn did_for_op(handle: &str, public_key: &[u8]) -> String {
62    if handle.starts_with("did:wire:op:") {
63        return handle.to_string();
64    }
65    let suffix = long_fingerprint(public_key);
66    format!("{DID_METHOD_OP}:{handle}-{suffix}")
67}
68
69/// Build an organization DID (`did:wire:org:<handle>-<32hex>`). Same
70/// construction as `did_for_op` but under the org prefix; org_dids
71/// gate the eased-pair surface, so they share the longer hex tail.
72pub fn did_for_org(handle: &str, public_key: &[u8]) -> String {
73    if handle.starts_with("did:wire:org:") {
74        return handle.to_string();
75    }
76    let suffix = long_fingerprint(public_key);
77    format!("{DID_METHOD_ORG}:{handle}-{suffix}")
78}
79
80/// 32-hex (16-byte) fingerprint over the public key for op/org DIDs.
81/// Wider than `signing::fingerprint` (which returns 8 hex / 4 bytes)
82/// because op/org identities are long-lived and grant trust scope.
83pub fn long_fingerprint(public_key: &[u8]) -> String {
84    let digest = Sha256::digest(public_key);
85    hex::encode(&digest[..16])
86}
87
88/// True iff `did` is a well-formed `did:wire:op:<handle>-<32hex>`.
89/// Used at card-validation time to refuse a `did:wire:` session DID
90/// mistakenly placed in the `op_did` slot (and vice versa).
91pub fn is_op_did(did: &str) -> bool {
92    let Some(rest) = did.strip_prefix("did:wire:op:") else {
93        return false;
94    };
95    has_long_hex_suffix(rest)
96}
97
98/// True iff `did` is a well-formed `did:wire:org:<handle>-<32hex>`.
99pub fn is_org_did(did: &str) -> bool {
100    let Some(rest) = did.strip_prefix("did:wire:org:") else {
101        return false;
102    };
103    has_long_hex_suffix(rest)
104}
105
106fn has_long_hex_suffix(s: &str) -> bool {
107    let Some(idx) = s.rfind('-') else {
108        return false;
109    };
110    let suffix = &s[idx + 1..];
111    suffix.len() == LONG_FINGERPRINT_HEX_LEN && suffix.chars().all(|c| c.is_ascii_hexdigit())
112}
113
114/// True iff a session `did:wire:<handle>-<8hex>` actually commits to
115/// `public_key` — i.e. its trailing 8-hex fingerprint equals
116/// `signing::fingerprint(public_key)`.
117///
118/// This is the binding that makes a session DID self-certifying: without
119/// it, `verify_agent_card` only proves "this card is self-signed by
120/// SOME key", not "this card's DID belongs to that key". An attacker
121/// could otherwise self-sign a card claiming any victim's DID with an
122/// attacker-controlled key (the 32-bit suffix is brute-forceable for a
123/// targeted second-preimage; this check raises forgery from "free" to
124/// "must collide the fingerprint"). op/org DIDs use the wider
125/// `commits_to` (org_membership.rs); this is the session-DID analog.
126pub fn did_commits_to_key(did: &str, public_key: &[u8]) -> bool {
127    // Session DIDs are `did:wire:<handle>-<8hex>`. Reject op/org method
128    // prefixes here — those carry a 32-hex suffix and are bound elsewhere.
129    let Some(rest) = did.strip_prefix(&format!("{DID_METHOD}:")) else {
130        return false;
131    };
132    if rest.starts_with("op:") || rest.starts_with("org:") {
133        return false;
134    }
135    let Some(idx) = rest.rfind('-') else {
136        return false;
137    };
138    let suffix = &rest[idx + 1..];
139    suffix == crate::signing::fingerprint(public_key)
140}
141
142/// Strip the federation suffix (`@relay.example`) from a handle, returning
143/// the bare local-part. This is the canonical on-disk form: outbox/inbox
144/// files are keyed by bare handle (`paul-mac.jsonl`), and the pinned-peers
145/// map in `relay_state.json` is keyed by bare handle.
146///
147/// Why this exists (v0.5.13): `wire send paul-mac@wireup.net "..."` used
148/// to write the outbox to `paul-mac@wireup.net.jsonl`, but `wire push`
149/// only enumerated bare-handle filenames. Events stuck silently for 25
150/// minutes (issue #2). Normalizing here makes the on-disk contract the
151/// single source of truth — accepts both `paul-mac` and `paul-mac@host`,
152/// always writes to `paul-mac.jsonl`.
153pub fn bare_handle(handle: &str) -> &str {
154    handle.split_once('@').map(|(n, _)| n).unwrap_or(handle)
155}
156
157/// Extract the display-friendly handle from a DID. Handles both legacy
158/// (`did:wire:paul`) and v0.5.7+ (`did:wire:paul-abc12345`) forms. The
159/// v0.5.7 trailing `-<8-hex>` suffix is stripped when present.
160pub fn display_handle_from_did(did: &str) -> &str {
161    let stripped = did.strip_prefix("did:wire:").unwrap_or(did);
162    // v0.5.7+ form: `<handle>-<8-hex>`. Detect by trailing exactly 8 hex
163    // chars after a final `-`. Anything else passes through unchanged.
164    if let Some(idx) = stripped.rfind('-') {
165        let suffix = &stripped[idx + 1..];
166        if suffix.len() == 8 && suffix.chars().all(|c| c.is_ascii_hexdigit()) {
167            return &stripped[..idx];
168        }
169    }
170    stripped
171}
172
173/// Convenience type — at this stage we use serde_json::Value so the wire
174/// shape stays explicit. A typed struct can come in v0.2+.
175pub type AgentCard = Value;
176
177#[derive(Debug, Error)]
178pub enum CardError {
179    #[error("missing field: {0}")]
180    MissingField(&'static str),
181    #[error("verify_keys is empty or malformed")]
182    NoVerifyKeys,
183    #[error("signature decode failed")]
184    BadSignature,
185    #[error("signature did not verify")]
186    SignatureRejected,
187    #[error("card DID does not commit to its verify key (suffix mismatch)")]
188    DidKeyMismatch,
189}
190
191/// Build an unsigned agent card for `handle` with one verify key.
192///
193/// Optional overrides:
194///   - `name`: human-friendly display name (defaults to capitalized handle)
195///   - `capabilities`: list of capability strings (defaults to `["wire/v3.2"]`)
196///   - `max_body_kb`: per-message body cap in KB (defaults to 64)
197///
198/// v0.1 deliberately does NOT include `registries`, `onboard_endpoint`,
199/// `wire_raw_url_template`, or `revoked_at`. Those land in v0.2+ along
200/// with the registry feature itself (see ANTI_FEATURES.md).
201pub fn build_agent_card(
202    handle: &str,
203    public_key: &[u8],
204    name: Option<&str>,
205    capabilities: Option<Vec<String>>,
206    max_body_kb: Option<u64>,
207) -> AgentCard {
208    let display_name = name
209        .map(str::to_string)
210        .unwrap_or_else(|| capitalize(handle));
211    let caps = capabilities.unwrap_or_else(|| vec!["wire/v3.2".to_string()]);
212    let body_kb = max_body_kb.unwrap_or(64);
213
214    let key_id = make_key_id(handle, public_key);
215    let key_id_full = format!("ed25519:{key_id}");
216
217    json!({
218        "schema_version": CARD_SCHEMA_VERSION,
219        "did": did_for_with_key(handle, public_key),
220        "handle": handle,
221        "name": display_name,
222        "capabilities": caps,
223        "verify_keys": {
224            key_id_full: {
225                "key": b64encode(public_key),
226                "alg": "ed25519",
227                "active": true,
228            }
229        },
230        "policies": {
231            "max_message_body_kb": body_kb,
232        }
233    })
234}
235
236/// Capitalize the first character of an ASCII handle (`paul` → `Paul`).
237fn capitalize(s: &str) -> String {
238    let mut chars = s.chars();
239    match chars.next() {
240        Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
241        None => String::new(),
242    }
243}
244
245// ─── RFC-001 §1: identity claims (operator / organization / project) ───────
246//
247// Optional, orthogonal claims layered onto the agent card. Cards without
248// any of these verify and route exactly as before — the additions are
249// strictly additive. v3.1 cards remain readable; v3.2 cards may carry
250// any subset of these fields.
251
252/// One entry in `org_memberships[]` (RFC-001 §1). `member_cert` is the
253/// org's signature over the operator's `op_did` UTF-8 bytes. A peer
254/// verifies the cert by looking up the org's pubkey (from a roster
255/// pull or a previously-pinned org) and calling
256/// `identity::verify_member_cert`.
257#[derive(Debug, Clone)]
258pub struct OrgMembership {
259    pub org_did: String,
260    /// Base64 Ed25519 public key of the org, carried inline so a receiver
261    /// verifies the vouch fully offline — `org_did` commits to this key
262    /// (`did:wire:org:<h>-<32hex sha256(org_pubkey)>`) and `member_cert` is
263    /// checked against it (RFC-001 Phase 1, `org_membership::evaluate_card_membership`).
264    pub org_pubkey: String,
265    /// Base64 Ed25519 signature by the org's key over `op_did` UTF-8 bytes.
266    pub member_cert: String,
267}
268
269/// Identity claims that may be layered onto an agent card. Each field
270/// is independently optional — a card may declare an operator anchor
271/// without an org membership, or an org membership without a project
272/// tag. The fields are orthogonal axes per RFC-001.
273#[derive(Debug, Clone, Default)]
274pub struct IdentityClaims {
275    /// Operator DID — `did:wire:op:<handle>-<32hex>`. Must satisfy
276    /// `is_op_did(...)`. The operator's root key separately signs
277    /// `op_cert` over the *session* DID this card belongs to, anchoring
278    /// the session under the operator.
279    pub op_did: Option<String>,
280    /// Base64 Ed25519 signature by the operator's key over this card's
281    /// session DID (UTF-8 bytes). Verifiable with `identity::verify_op_cert`.
282    /// Meaningful only when `op_did` is set.
283    pub op_cert: Option<String>,
284    /// Base64 Ed25519 operator root public key, carried inline so the operator
285    /// binding verifies offline — `op_did` commits to this key and `op_cert` is
286    /// checked against it. Set whenever `op_did` is set; without it the operator
287    /// claim is unverifiable and a receiver fails it closed (RFC-001 Phase 1).
288    pub op_pubkey: Option<String>,
289    /// Zero or more org membership entries. An operator may sit in
290    /// multiple orgs simultaneously; each entry stands on its own.
291    pub org_memberships: Vec<OrgMembership>,
292    /// Opaque routing tag — NEVER trust-bearing. RFC-001 §6.
293    pub project: Option<String>,
294}
295
296/// Layer identity claims onto an existing (unsigned) card. The returned
297/// card is unsigned; the caller signs it with `sign_agent_card` after
298/// all claims are attached. Fields with `None`/empty values are not
299/// added to the JSON, keeping the canonical bytes minimal for v3.1-only
300/// peers and making round-trip semantics deterministic.
301///
302/// Returns `Err(ClaimError::InvalidOpDid)` if `op_did` is set but does
303/// not parse as `did:wire:op:<handle>-<32hex>`; same shape for
304/// `InvalidOrgDid`. The check is structural — cryptographic verification
305/// of `op_cert` / `member_cert` happens in `identity::verify_*`, which
306/// needs the pubkeys those certs are signed by.
307pub fn with_identity_claims(
308    card: &AgentCard,
309    claims: &IdentityClaims,
310) -> Result<AgentCard, ClaimError> {
311    if let Some(op_did) = &claims.op_did
312        && !is_op_did(op_did)
313    {
314        return Err(ClaimError::InvalidOpDid(op_did.clone()));
315    }
316    for m in &claims.org_memberships {
317        if !is_org_did(&m.org_did) {
318            return Err(ClaimError::InvalidOrgDid(m.org_did.clone()));
319        }
320    }
321
322    let mut out = card.as_object().cloned().unwrap_or_default();
323
324    if let Some(op_did) = &claims.op_did {
325        out.insert("op_did".into(), Value::String(op_did.clone()));
326    }
327    if let Some(op_cert) = &claims.op_cert {
328        out.insert("op_cert".into(), Value::String(op_cert.clone()));
329    }
330    if let Some(op_pubkey) = &claims.op_pubkey {
331        out.insert("op_pubkey".into(), Value::String(op_pubkey.clone()));
332    }
333    if !claims.org_memberships.is_empty() {
334        let arr: Vec<Value> = claims
335            .org_memberships
336            .iter()
337            .map(|m| {
338                json!({
339                    "org_did": m.org_did,
340                    "org_pubkey": m.org_pubkey,
341                    "member_cert": m.member_cert,
342                })
343            })
344            .collect();
345        out.insert("org_memberships".into(), Value::Array(arr));
346    }
347    if let Some(project) = &claims.project {
348        out.insert("project".into(), Value::String(project.clone()));
349    }
350
351    // v0.14.x retro-fix: when ANY RFC-001 op claim lands on the card,
352    // bump `schema_version` to at least `CARD_SCHEMA_VERSION` (currently
353    // "v3.2"). Existing cards minted at v3.1 keep their version field
354    // until republish hits this path — at which point the version
355    // matches the inline-fields shape. Monotonic (never downgrades): a
356    // card already at >= v3.2 is unchanged. Readers that key off
357    // `schema_version >= "v3.2"` to discriminate "carries op claims"
358    // now have a truthful signal. (The bug it closes: v0.14 stored
359    // op_did but kept emitting `schema_version: "v3.1"` — readers
360    // couldn't tell from the version alone whether the card had
361    // op claims; they had to probe the inline fields directly.)
362    let has_any_op_claim = claims.op_did.is_some()
363        || claims.op_cert.is_some()
364        || claims.op_pubkey.is_some()
365        || !claims.org_memberships.is_empty();
366    if has_any_op_claim {
367        let current = out
368            .get("schema_version")
369            .and_then(Value::as_str)
370            .unwrap_or("v3.0");
371        let target = max_schema_version(current, CARD_SCHEMA_VERSION);
372        out.insert("schema_version".into(), Value::String(target.to_string()));
373    }
374
375    Ok(Value::Object(out))
376}
377
378/// Compare two `vX.Y` schema-version strings as `(major, minor)` integer
379/// tuples and return the higher. Defensive: unparseable inputs fall back
380/// to the OTHER argument (so a malformed stored card doesn't poison the
381/// republish). `v3.10` correctly compares as > `v3.2`.
382fn max_schema_version<'a>(a: &'a str, b: &'a str) -> &'a str {
383    fn parse(s: &str) -> Option<(u32, u32)> {
384        let rest = s.strip_prefix('v')?;
385        let (maj, min) = rest.split_once('.')?;
386        Some((maj.parse().ok()?, min.parse().ok()?))
387    }
388    match (parse(a), parse(b)) {
389        (Some(pa), Some(pb)) => {
390            if pa >= pb {
391                a
392            } else {
393                b
394            }
395        }
396        // Bias toward the parseable one; if neither parses, keep `a`.
397        (Some(_), None) => a,
398        (None, Some(_)) => b,
399        (None, None) => a,
400    }
401}
402
403#[derive(Debug, Error)]
404pub enum ClaimError {
405    #[error("op_did is not a well-formed did:wire:op:<handle>-<32hex>: {0}")]
406    InvalidOpDid(String),
407    #[error("org_did is not a well-formed did:wire:org:<handle>-<32hex>: {0}")]
408    InvalidOrgDid(String),
409}
410
411/// Read `op_did` from a card. Returns `None` if absent or malformed.
412pub fn card_op_did(card: &AgentCard) -> Option<&str> {
413    card.get("op_did").and_then(Value::as_str)
414}
415
416/// Read `op_cert` from a card. Returns `None` if absent.
417pub fn card_op_cert(card: &AgentCard) -> Option<&str> {
418    card.get("op_cert").and_then(Value::as_str)
419}
420
421/// Read `project` routing tag from a card.
422pub fn card_project(card: &AgentCard) -> Option<&str> {
423    card.get("project").and_then(Value::as_str)
424}
425
426/// Read `org_memberships[]` from a card as a list of `(org_did,
427/// member_cert)` borrowed pairs. Returns empty if absent or malformed.
428pub fn card_org_memberships(card: &AgentCard) -> Vec<(&str, &str)> {
429    card.get("org_memberships")
430        .and_then(Value::as_array)
431        .map(|arr| {
432            arr.iter()
433                .filter_map(|entry| {
434                    let org = entry.get("org_did").and_then(Value::as_str)?;
435                    let cert = entry.get("member_cert").and_then(Value::as_str)?;
436                    Some((org, cert))
437                })
438                .collect()
439        })
440        .unwrap_or_default()
441}
442
443/// Canonical bytes of an agent card — strips `signature` before serialization.
444pub fn card_canonical(card: &AgentCard) -> Vec<u8> {
445    canonical(card, false)
446}
447
448/// Sign an agent card with `private_key`. Returns the card with `signature`
449/// field appended (base64 of Ed25519 signature over `card_canonical(card)`).
450pub fn sign_agent_card(card: &AgentCard, private_key: &[u8]) -> AgentCard {
451    let mut sk_bytes = [0u8; 32];
452    sk_bytes.copy_from_slice(&private_key[..32]);
453    let sk = SigningKey::from_bytes(&sk_bytes);
454    // D1 (RFC-006): attach the X25519 `dh_pubkey` derived from THIS signing seed
455    // before canonicalizing, so the self-signature covers it. Single chokepoint
456    // guaranteeing every signed card carries dh_pubkey; a stripped/substituted
457    // value breaks the signature (caught by verify_agent_card on the receiver).
458    let mut card_obj = card.as_object().cloned().unwrap_or_default();
459    card_obj.insert(
460        "dh_pubkey".into(),
461        Value::String(crate::enc::wire_x25519::self_dh_pubkey_b64(&sk_bytes)),
462    );
463    let card_with_dh = Value::Object(card_obj);
464    let sig = sk.sign(&card_canonical(&card_with_dh));
465    let mut out = card_with_dh.as_object().cloned().unwrap_or_default();
466    out.insert(
467        "signature".into(),
468        Value::String(b64encode(&sig.to_bytes())),
469    );
470    Value::Object(out)
471}
472
473/// Read `dh_pubkey` (base64 X25519) from a card. `None` ⇒ pre-D1/unsigned card.
474pub fn card_dh_pubkey(card: &AgentCard) -> Option<&str> {
475    card.get("dh_pubkey").and_then(Value::as_str)
476}
477
478/// Verify a signed card. Picks the first verify_key, validates the
479/// signature over `card_canonical(card)` (stripped of `signature`).
480pub fn verify_agent_card(card: &AgentCard) -> Result<(), CardError> {
481    let signature_b64 = card
482        .get("signature")
483        .and_then(Value::as_str)
484        .ok_or(CardError::MissingField("signature"))?;
485
486    let verify_keys = card
487        .get("verify_keys")
488        .and_then(Value::as_object)
489        .ok_or(CardError::MissingField("verify_keys"))?;
490
491    let (_kid, key_record) = verify_keys.iter().next().ok_or(CardError::NoVerifyKeys)?;
492    let pk_b64 = key_record
493        .get("key")
494        .and_then(Value::as_str)
495        .ok_or(CardError::MissingField("verify_keys[*].key"))?;
496    let pk_bytes = b64decode(pk_b64).map_err(|_| CardError::BadSignature)?;
497    if pk_bytes.len() != 32 {
498        return Err(CardError::BadSignature);
499    }
500    let mut pk_arr = [0u8; 32];
501    pk_arr.copy_from_slice(&pk_bytes);
502    let vk = VerifyingKey::from_bytes(&pk_arr).map_err(|_| CardError::BadSignature)?;
503
504    let sig_bytes = b64decode(signature_b64).map_err(|_| CardError::BadSignature)?;
505    if sig_bytes.len() != 64 {
506        return Err(CardError::BadSignature);
507    }
508    let mut sig_arr = [0u8; 64];
509    sig_arr.copy_from_slice(&sig_bytes);
510    let sig = ed25519_dalek::Signature::from_bytes(&sig_arr);
511
512    vk.verify(&card_canonical(card), &sig)
513        .map_err(|_| CardError::SignatureRejected)?;
514
515    // Binding: the card's session DID must commit to the key we just
516    // verified the signature with. A valid self-signature alone only
517    // proves the card was signed by SOME key — without this, an attacker
518    // can self-sign a card claiming any victim's DID under their own key.
519    // (op/org DIDs are bound separately via org_membership::commits_to;
520    // `did_commits_to_key` returns false for those prefixes, so a card
521    // whose top-level `did` is an op/org DID is correctly rejected here.)
522    let did = card
523        .get("did")
524        .and_then(Value::as_str)
525        .ok_or(CardError::MissingField("did"))?;
526    if !did_commits_to_key(did, &pk_arr) {
527        return Err(CardError::DidKeyMismatch);
528    }
529    Ok(())
530}
531
532/// 6-digit bilateral SAS over two raw 32-byte public keys.
533///
534/// `sha256(min(a, b) || max(a, b))` then take the last 6 decimal digits.
535/// Symmetric in `(a, b)` so either operator computes the same digits from
536/// independent knowledge of both keys.
537pub fn compute_sas(public_key_a: &[u8], public_key_b: &[u8]) -> String {
538    let (lo, hi) = if public_key_a <= public_key_b {
539        (public_key_a, public_key_b)
540    } else {
541        (public_key_b, public_key_a)
542    };
543    let mut h = Sha256::new();
544    h.update(lo);
545    h.update(hi);
546    let digest = h.finalize();
547    // Take low 4 bytes -> u32, mod 1_000_000 for 6 digits.
548    let n = u32::from_be_bytes([digest[28], digest[29], digest[30], digest[31]]);
549    format!("{:06}", n % 1_000_000)
550}
551
552#[cfg(test)]
553mod tests {
554    use super::*;
555    use crate::signing::generate_keypair;
556
557    #[test]
558    fn did_method_constant() {
559        assert_eq!(DID_METHOD, "did:wire");
560    }
561
562    #[test]
563    fn build_minimal_card() {
564        let (_, pk) = generate_keypair();
565        let card = build_agent_card("paul", &pk, None, None, None);
566        assert_eq!(card["schema_version"], CARD_SCHEMA_VERSION);
567        // v0.5.7+: DID is pubkey-suffixed for cross-operator uniqueness.
568        let did = card["did"].as_str().unwrap();
569        assert!(did.starts_with("did:wire:paul-"), "got: {did}");
570        assert_eq!(did.len(), "did:wire:paul-".len() + 8);
571        assert_eq!(card["handle"], "paul");
572        assert_eq!(card["name"], "Paul");
573        let vks = card["verify_keys"].as_object().unwrap();
574        assert_eq!(vks.len(), 1);
575        assert_eq!(card["policies"]["max_message_body_kb"], 64);
576    }
577
578    #[test]
579    fn build_card_with_overrides() {
580        let (_, pk) = generate_keypair();
581        let card = build_agent_card(
582            "carol",
583            &pk,
584            Some("Carol's Agent"),
585            Some(vec!["custom-cap".to_string()]),
586            Some(128),
587        );
588        assert_eq!(card["name"], "Carol's Agent");
589        assert_eq!(card["capabilities"], json!(["custom-cap"]));
590        assert_eq!(card["policies"]["max_message_body_kb"], 128);
591    }
592
593    #[test]
594    fn build_card_does_not_carry_v02_fields() {
595        let (_, pk) = generate_keypair();
596        let card = build_agent_card("paul", &pk, None, None, None);
597        let obj = card.as_object().unwrap();
598        for v02 in [
599            "registries",
600            "onboard_endpoint",
601            "wire_raw_url_template",
602            "revoked_at",
603        ] {
604            assert!(
605                !obj.contains_key(v02),
606                "v0.2+ field {v02} leaked into v0.1 card"
607            );
608        }
609    }
610
611    #[test]
612    fn card_canonical_excludes_signature() {
613        let v = json!({"schema_version": "v3.1", "did": "did:wire:paul", "signature": "sig"});
614        let bytes = card_canonical(&v);
615        assert!(!String::from_utf8_lossy(&bytes).contains("signature"));
616    }
617
618    #[test]
619    fn card_canonical_sort_keys_stable() {
620        let a = json!({"b": 1, "a": 2, "did": "did:wire:paul"});
621        let b = json!({"did": "did:wire:paul", "a": 2, "b": 1});
622        assert_eq!(card_canonical(&a), card_canonical(&b));
623    }
624
625    #[test]
626    fn sign_verify_roundtrip() {
627        let (sk, pk) = generate_keypair();
628        let card = build_agent_card("paul", &pk, None, None, None);
629        let signed = sign_agent_card(&card, &sk);
630        assert!(signed.get("signature").is_some());
631        verify_agent_card(&signed).unwrap();
632    }
633
634    #[test]
635    fn verify_rejects_unsigned_card() {
636        let (_, pk) = generate_keypair();
637        let card = build_agent_card("paul", &pk, None, None, None);
638        let err = verify_agent_card(&card).unwrap_err();
639        assert!(matches!(err, CardError::MissingField("signature")));
640    }
641
642    #[test]
643    fn verify_rejects_tampered_card() {
644        let (sk, pk) = generate_keypair();
645        let mut signed = sign_agent_card(&build_agent_card("paul", &pk, None, None, None), &sk);
646        signed["name"] = json!("TamperedName");
647        let err = verify_agent_card(&signed).unwrap_err();
648        assert!(matches!(err, CardError::SignatureRejected));
649    }
650
651    #[test]
652    fn verify_rejects_card_with_no_verify_keys() {
653        let (sk, _) = generate_keypair();
654        let card = json!({"schema_version": "v3.1", "did": "did:wire:paul", "verify_keys": {}});
655        let signed = sign_agent_card(&card, &sk);
656        let err = verify_agent_card(&signed).unwrap_err();
657        assert!(matches!(err, CardError::NoVerifyKeys));
658    }
659
660    #[test]
661    fn verify_rejects_did_claiming_foreign_key() {
662        // Attacker self-signs a card with their OWN key but claims a DID
663        // whose fingerprint suffix belongs to a victim's key. The
664        // signature verifies (it's the attacker's key over their own
665        // bytes) but the DID no longer commits to that key → reject.
666        let (victim_sk, victim_pk) = generate_keypair();
667        let (attacker_sk, attacker_pk) = generate_keypair();
668        assert_ne!(victim_pk, attacker_pk);
669        let victim_did = did_for_with_key("paul", &victim_pk);
670        // Build the attacker's card (their key in verify_keys) then
671        // overwrite the DID to claim the victim's, and re-sign so the
672        // self-signature is valid over the tampered bytes.
673        let mut card = build_agent_card("paul", &attacker_pk, None, None, None);
674        card["did"] = json!(victim_did);
675        let signed = sign_agent_card(&card, &attacker_sk);
676        // Sanity: the signature itself is valid (attacker signed it).
677        let err = verify_agent_card(&signed).unwrap_err();
678        assert!(
679            matches!(err, CardError::DidKeyMismatch),
680            "expected DidKeyMismatch, got {err:?}"
681        );
682        // And the genuine victim card (DID bound to victim key) verifies.
683        let real = sign_agent_card(
684            &build_agent_card("paul", &victim_pk, None, None, None),
685            &victim_sk,
686        );
687        verify_agent_card(&real).unwrap();
688    }
689
690    #[test]
691    fn did_commits_to_key_basic() {
692        let (_, pk) = generate_keypair();
693        let (_, other) = generate_keypair();
694        let did = did_for_with_key("alice", &pk);
695        assert!(did_commits_to_key(&did, &pk));
696        assert!(!did_commits_to_key(&did, &other));
697        // Handles containing hyphens still bind on the final segment.
698        let hdid = did_for_with_key("alice-bob", &pk);
699        assert!(did_commits_to_key(&hdid, &pk));
700        // op/org DIDs are bound elsewhere → not accepted by this helper.
701        assert!(!did_commits_to_key(&did_for_op("acme", &pk), &pk));
702        assert!(!did_commits_to_key(&did_for_org("acme", &pk), &pk));
703        // Suffix-less legacy DID → no binding.
704        assert!(!did_commits_to_key("did:wire:alice", &pk));
705    }
706
707    #[test]
708    fn compute_sas_is_6_digits() {
709        let (_, a) = generate_keypair();
710        let (_, b) = generate_keypair();
711        let sas = compute_sas(&a, &b);
712        assert_eq!(sas.len(), 6);
713        assert!(sas.chars().all(|c| c.is_ascii_digit()));
714    }
715
716    #[test]
717    fn compute_sas_bilateral_symmetric() {
718        let (_, a) = generate_keypair();
719        let (_, b) = generate_keypair();
720        assert_eq!(compute_sas(&a, &b), compute_sas(&b, &a));
721    }
722
723    #[test]
724    fn compute_sas_changes_with_inputs() {
725        let (_, a) = generate_keypair();
726        let (_, b) = generate_keypair();
727        let (_, c) = generate_keypair();
728        assert_ne!(compute_sas(&a, &b), compute_sas(&a, &c));
729    }
730
731    // ─── RFC-001 §1: identity claims ───────────────────────────────────────
732
733    fn op_did_for_test(handle: &str) -> (String, Vec<u8>, Vec<u8>) {
734        let (sk, pk) = generate_keypair();
735        (did_for_op(handle, &pk), sk.to_vec(), pk.to_vec())
736    }
737
738    fn org_did_for_test(handle: &str) -> (String, Vec<u8>, Vec<u8>) {
739        let (sk, pk) = generate_keypair();
740        (did_for_org(handle, &pk), sk.to_vec(), pk.to_vec())
741    }
742
743    #[test]
744    fn schema_version_is_v3_2() {
745        assert_eq!(CARD_SCHEMA_VERSION, "v3.2");
746    }
747
748    #[test]
749    fn op_did_has_long_hex_suffix_and_method_prefix() {
750        let (did, _, _) = op_did_for_test("darby");
751        assert!(did.starts_with("did:wire:op:darby-"), "got: {did}");
752        let tail = did.rsplit('-').next().unwrap();
753        assert_eq!(tail.len(), LONG_FINGERPRINT_HEX_LEN);
754        assert!(tail.chars().all(|c| c.is_ascii_hexdigit()));
755    }
756
757    #[test]
758    fn org_did_has_long_hex_suffix_and_method_prefix() {
759        let (did, _, _) = org_did_for_test("slanchaai");
760        assert!(did.starts_with("did:wire:org:slanchaai-"), "got: {did}");
761        let tail = did.rsplit('-').next().unwrap();
762        assert_eq!(tail.len(), LONG_FINGERPRINT_HEX_LEN);
763        assert!(tail.chars().all(|c| c.is_ascii_hexdigit()));
764    }
765
766    #[test]
767    fn op_did_passthrough_when_already_op_did() {
768        // Passing a fully-formed op_did back through `did_for_op` is a no-op;
769        // protects callers that mix raw handles + already-built DIDs.
770        let (_, pk) = generate_keypair();
771        let did = did_for_op("darby", &pk);
772        let again = did_for_op(&did, &pk);
773        assert_eq!(did, again);
774    }
775
776    #[test]
777    fn is_op_did_rejects_session_did() {
778        // The classification check exists precisely to refuse this confusion.
779        let (_, pk) = generate_keypair();
780        let session_did = did_for_with_key("darby", &pk);
781        assert!(!is_op_did(&session_did));
782        assert!(!is_org_did(&session_did));
783    }
784
785    #[test]
786    fn is_op_did_rejects_org_did_and_vice_versa() {
787        // Disjoint namespaces — an org_did is not an op_did even though both
788        // share the long-hex suffix shape.
789        let (op, _, _) = op_did_for_test("darby");
790        let (org, _, _) = org_did_for_test("slanchaai");
791        assert!(is_op_did(&op) && !is_org_did(&op));
792        assert!(is_org_did(&org) && !is_op_did(&org));
793    }
794
795    #[test]
796    fn is_op_did_rejects_short_hex_suffix() {
797        // An 8-hex tail (session-DID shape) under the op prefix would be a
798        // namespace squat. Refuse on syntax alone.
799        assert!(!is_op_did("did:wire:op:darby-deadbeef"));
800        assert!(!is_org_did("did:wire:org:slanchaai-deadbeef"));
801    }
802
803    #[test]
804    fn is_op_did_rejects_non_hex_suffix() {
805        let bad = format!("did:wire:op:darby-{}", "z".repeat(LONG_FINGERPRINT_HEX_LEN));
806        assert!(!is_op_did(&bad));
807    }
808
809    #[test]
810    fn with_identity_claims_attaches_all_fields() {
811        let (sk, pk) = generate_keypair();
812        let card = build_agent_card("vesper-valley", &pk, None, None, None);
813        let (op_did, _, op_pk) = op_did_for_test("darby");
814        let (org_did, _, org_pk) = org_did_for_test("slanchaai");
815        let op_pubkey = crate::signing::b64encode(&op_pk);
816        let org_pubkey = crate::signing::b64encode(&org_pk);
817        let claims = IdentityClaims {
818            op_did: Some(op_did.clone()),
819            op_cert: Some("AAAA".into()),
820            op_pubkey: Some(op_pubkey.clone()),
821            org_memberships: vec![OrgMembership {
822                org_did: org_did.clone(),
823                org_pubkey: org_pubkey.clone(),
824                member_cert: "BBBB".into(),
825            }],
826            project: Some("wire-codex-integration".into()),
827        };
828        let with = with_identity_claims(&card, &claims).unwrap();
829        assert_eq!(card_op_did(&with), Some(op_did.as_str()));
830        assert_eq!(card_op_cert(&with), Some("AAAA"));
831        assert_eq!(
832            with.get("op_pubkey").and_then(|v| v.as_str()),
833            Some(op_pubkey.as_str())
834        );
835        assert_eq!(card_project(&with), Some("wire-codex-integration"));
836        let orgs = card_org_memberships(&with);
837        assert_eq!(orgs.len(), 1);
838        assert_eq!(orgs[0], (org_did.as_str(), "BBBB"));
839        assert_eq!(
840            with.get("org_memberships").unwrap()[0]
841                .get("org_pubkey")
842                .and_then(|v| v.as_str()),
843            Some(org_pubkey.as_str())
844        );
845        // Card still signs + verifies after identity claims are layered.
846        let signed = sign_agent_card(&with, &sk);
847        verify_agent_card(&signed).unwrap();
848    }
849
850    #[test]
851    fn with_identity_claims_skips_absent_fields() {
852        // A card with no claims must not gain empty `op_did`/`project`/etc.
853        // entries — keeps canonical bytes minimal and v3.1-peer-friendly.
854        let (_, pk) = generate_keypair();
855        let card = build_agent_card("vesper-valley", &pk, None, None, None);
856        let with = with_identity_claims(&card, &IdentityClaims::default()).unwrap();
857        let obj = with.as_object().unwrap();
858        for field in ["op_did", "op_cert", "org_memberships", "project"] {
859            assert!(
860                !obj.contains_key(field),
861                "{field} leaked into claim-less card"
862            );
863        }
864    }
865
866    #[test]
867    fn with_identity_claims_rejects_malformed_op_did() {
868        let (_, pk) = generate_keypair();
869        let card = build_agent_card("vesper-valley", &pk, None, None, None);
870        let claims = IdentityClaims {
871            // Session-DID shape under op prefix → namespace confusion.
872            op_did: Some("did:wire:op:darby-deadbeef".into()),
873            ..Default::default()
874        };
875        let err = with_identity_claims(&card, &claims).unwrap_err();
876        assert!(matches!(err, ClaimError::InvalidOpDid(_)));
877    }
878
879    #[test]
880    fn with_identity_claims_rejects_malformed_org_did() {
881        let (_, pk) = generate_keypair();
882        let card = build_agent_card("vesper-valley", &pk, None, None, None);
883        let claims = IdentityClaims {
884            org_memberships: vec![OrgMembership {
885                org_did: "did:wire:slanchaai".into(),
886                org_pubkey: "AAAA".into(),
887                member_cert: "BBBB".into(),
888            }],
889            ..Default::default()
890        };
891        let err = with_identity_claims(&card, &claims).unwrap_err();
892        assert!(matches!(err, ClaimError::InvalidOrgDid(_)));
893    }
894
895    #[test]
896    fn build_agent_card_default_capability_advertises_v3_2() {
897        let (_, pk) = generate_keypair();
898        let card = build_agent_card("paul", &pk, None, None, None);
899        let caps = card["capabilities"].as_array().unwrap();
900        let has_v32 = caps.iter().any(|v| v.as_str() == Some("wire/v3.2"));
901        assert!(has_v32, "default caps should advertise wire/v3.2: {caps:?}");
902    }
903
904    // v0.14.x retro-fix tests: when op claims are attached, the card's
905    // `schema_version` field bumps to at least `CARD_SCHEMA_VERSION`. The
906    // bump is monotonic (never downgrades), conditional (claim-less
907    // attach leaves the field alone), and version-numeric (v3.10 > v3.2,
908    // not lexicographic).
909
910    #[test]
911    fn with_identity_claims_bumps_schema_version_when_op_did_attached() {
912        // A card that was minted at v3.1 (the pre-v0.14 emit version)
913        // must surface as >= v3.2 once op claims are attached — readers
914        // discriminate "card carries op_*" off the version field.
915        let (_, pk) = generate_keypair();
916        let mut card = build_agent_card("vesper-valley", &pk, None, None, None);
917        // Roll back to v3.1 to simulate a pre-v0.14 stored card.
918        card.as_object_mut()
919            .unwrap()
920            .insert("schema_version".into(), json!("v3.1"));
921        let (op_did, _, op_pk) = op_did_for_test("darby");
922        let claims = IdentityClaims {
923            op_did: Some(op_did),
924            op_pubkey: Some(crate::signing::b64encode(&op_pk)),
925            op_cert: Some("AAAA".into()),
926            ..Default::default()
927        };
928        let with = with_identity_claims(&card, &claims).unwrap();
929        assert_eq!(
930            with.get("schema_version").and_then(|v| v.as_str()),
931            Some(CARD_SCHEMA_VERSION),
932            "post-attach schema_version must bump to {CARD_SCHEMA_VERSION}",
933        );
934    }
935
936    #[test]
937    fn with_identity_claims_does_not_touch_schema_version_when_no_claims() {
938        // Claim-less attach (e.g. an unenrolled operator's republish)
939        // leaves the version field exactly as it was — no spurious bump
940        // for a v3.1 peer that has zero op_* fields to surface.
941        let (_, pk) = generate_keypair();
942        let mut card = build_agent_card("vesper-valley", &pk, None, None, None);
943        card.as_object_mut()
944            .unwrap()
945            .insert("schema_version".into(), json!("v3.1"));
946        let with = with_identity_claims(&card, &IdentityClaims::default()).unwrap();
947        assert_eq!(
948            with.get("schema_version").and_then(|v| v.as_str()),
949            Some("v3.1"),
950            "claim-less attach must NOT bump",
951        );
952    }
953
954    #[test]
955    fn with_identity_claims_never_downgrades_schema_version() {
956        // A hypothetical v3.5 card (future extension peer) attaching op
957        // claims via an older `CARD_SCHEMA_VERSION` build must NOT lose
958        // its higher version. Monotonic invariant.
959        let (_, pk) = generate_keypair();
960        let mut card = build_agent_card("vesper-valley", &pk, None, None, None);
961        card.as_object_mut()
962            .unwrap()
963            .insert("schema_version".into(), json!("v3.5"));
964        let (op_did, _, op_pk) = op_did_for_test("darby");
965        let claims = IdentityClaims {
966            op_did: Some(op_did),
967            op_pubkey: Some(crate::signing::b64encode(&op_pk)),
968            op_cert: Some("AAAA".into()),
969            ..Default::default()
970        };
971        let with = with_identity_claims(&card, &claims).unwrap();
972        assert_eq!(
973            with.get("schema_version").and_then(|v| v.as_str()),
974            Some("v3.5"),
975            "monotonic bump must not downgrade v3.5 to {CARD_SCHEMA_VERSION}",
976        );
977    }
978
979    #[test]
980    fn max_schema_version_compares_numerically_not_lexicographically() {
981        // Lexicographic compare would call "v3.10" < "v3.2" because '1' <
982        // '2'. The helper parses to (major, minor) ints so v3.10 > v3.2.
983        assert_eq!(max_schema_version("v3.10", "v3.2"), "v3.10");
984        assert_eq!(max_schema_version("v3.2", "v3.10"), "v3.10");
985        assert_eq!(max_schema_version("v3.2", "v3.2"), "v3.2");
986        assert_eq!(max_schema_version("v4.0", "v3.99"), "v4.0");
987    }
988
989    #[test]
990    fn max_schema_version_biases_to_parseable_on_malformed_input() {
991        // A malformed stored card must not poison the republish: parseable
992        // wins, both-malformed keeps `a` (deterministic, no panic).
993        assert_eq!(max_schema_version("garbage", "v3.2"), "v3.2");
994        assert_eq!(max_schema_version("v3.2", "garbage"), "v3.2");
995        assert_eq!(max_schema_version("garbage1", "garbage2"), "garbage1");
996    }
997}