vector_core/community/owner.rs
1//! Owner attestation — the unforgeable binding of a Community to its owner's identity (
2//! "anchored to the owner's identity key").
3//!
4//! At creation the owner signs, with their IDENTITY key, a statement binding the Community's random
5//! `community_id`. The proven owner is then DERIVED from the signature (`event.pubkey`), never
6//! asserted separately — so:
7//! - you cannot frame an innocent npub (that requires their key to sign), and
8//! - the binding can't be transplanted to another community (the unique id is inside the signed
9//! payload, so an attestation for community X can't be replayed as community Y's).
10//! Members verify it against the very community id they already hold. Server-root encrypted in
11//! transit, so outsiders learn nothing; only members see who the owner is. (The community is
12//! keyless — there is no management/authority key to bind; `community_id` alone is the anchor.)
13
14use crate::stored_event::event_kind;
15use nostr_sdk::prelude::*;
16
17/// Tag binding the attestation to its community. `["vco", community_id]`.
18const TAG_OWNER: &str = "vco";
19
20/// The unsigned owner-attestation event. Sign it with the owner's IDENTITY signer (local or
21/// bunker — it's a normal event, so NIP-46 works) at community creation.
22pub fn build_owner_attestation_unsigned(
23 owner_pubkey: PublicKey,
24 community_id: &str,
25) -> UnsignedEvent {
26 EventBuilder::new(Kind::Custom(event_kind::APPLICATION_SPECIFIC), "")
27 .tags([Tag::custom(
28 TagKind::Custom(TAG_OWNER.into()),
29 [community_id.to_string()],
30 )])
31 .build(owner_pubkey)
32}
33
34/// Verify an owner-attestation event (JSON). Returns the PROVEN owner pubkey iff the signature
35/// is valid AND it binds exactly this `community_id`. `None` on any missing/mismatched/forged
36/// input — the caller then treats ownership as unverified (no crown).
37pub fn verify_owner_attestation(
38 attestation_json: &str,
39 community_id: &str,
40) -> Option<PublicKey> {
41 let ev: Event = serde_json::from_str(attestation_json).ok()?;
42 ev.verify().ok()?; // id + Schnorr signature
43 let bound = ev.tags.iter().find_map(|t| {
44 let s = t.as_slice();
45 (s.len() >= 2 && s[0] == TAG_OWNER).then(|| s[1].clone())
46 })?;
47 (bound == community_id).then_some(ev.pubkey)
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn attestation_round_trips_binds_and_rejects_forgery() {
56 let owner = Keys::generate();
57 let cid = "a".repeat(64);
58
59 let signed = build_owner_attestation_unsigned(owner.public_key(), &cid)
60 .sign_with_keys(&owner)
61 .unwrap();
62 let json = signed.as_json();
63
64 // Valid → the proven owner is the signer.
65 assert_eq!(verify_owner_attestation(&json, &cid), Some(owner.public_key()));
66 // Can't transplant to another community.
67 assert_eq!(verify_owner_attestation(&json, &"b".repeat(64)), None);
68 // Garbage in → None, never a panic.
69 assert_eq!(verify_owner_attestation("not json", &cid), None);
70
71 // Framing defense: a forger can only ever attest THEMSELVES — verify returns the
72 // forger's pubkey, never the victim's, so they can't make the UI crown someone else.
73 let mallory = Keys::generate();
74 let forged = build_owner_attestation_unsigned(mallory.public_key(), &cid)
75 .sign_with_keys(&mallory)
76 .unwrap()
77 .as_json();
78 assert_eq!(verify_owner_attestation(&forged, &cid), Some(mallory.public_key()));
79 assert_ne!(verify_owner_attestation(&forged, &cid), Some(owner.public_key()));
80 }
81
82 #[test]
83 fn attestation_tag_layout_is_frozen() {
84 // the attestation's signed form is FROZEN. It is verify-only (other clients check the
85 // owner's signature, never reconstruct the event — created_at is non-deterministic), so the
86 // pinned interop contract is the TAG layout: exactly one `["vco", <community_id>]` tag, empty
87 // content. A drift here (extra element, reordered, renamed) breaks every verifier reading idx 1.
88 let owner = Keys::generate();
89 let cid = "c".repeat(64);
90 let unsigned = build_owner_attestation_unsigned(owner.public_key(), &cid);
91 assert_eq!(unsigned.content, "");
92 let tags: Vec<Vec<String>> = unsigned.tags.iter().map(|t| t.as_slice().to_vec()).collect();
93 assert_eq!(tags, vec![vec!["vco".to_string(), cid]], "exactly one [\"vco\", community_id] tag");
94 }
95}