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::event_ext::FinalizeUnsignedWithId;
15use crate::stored_event::event_kind;
16use nostr_sdk::prelude::*;
17
18/// Tag binding the attestation to its community. `["vco", community_id]`.
19const TAG_OWNER: &str = "vco";
20
21/// The unsigned owner-attestation event. Sign it with the owner's IDENTITY signer (local or
22/// bunker — it's a normal event, so NIP-46 works) at community creation.
23pub fn build_owner_attestation_unsigned(
24 owner_pubkey: PublicKey,
25 community_id: &str,
26) -> UnsignedEvent {
27 EventBuilder::new(Kind::Custom(event_kind::APPLICATION_SPECIFIC), "")
28 .tags([Tag::custom(
29 TAG_OWNER,
30 [community_id.to_string()],
31 )])
32 .finalize_unsigned_with_id(owner_pubkey)
33}
34
35/// Verify an owner-attestation event (JSON). Returns the PROVEN owner pubkey iff the signature
36/// is valid AND it binds exactly this `community_id`. `None` on any missing/mismatched/forged
37/// input — the caller then treats ownership as unverified (no crown).
38pub fn verify_owner_attestation(
39 attestation_json: &str,
40 community_id: &str,
41) -> Option<PublicKey> {
42 let ev: Event = serde_json::from_str(attestation_json).ok()?;
43 ev.verify().ok()?; // id + Schnorr signature
44 let bound = ev.tags.iter().find_map(|t| {
45 let s = t.as_slice();
46 (s.len() >= 2 && s[0] == TAG_OWNER).then(|| s[1].clone())
47 })?;
48 (bound == community_id).then_some(ev.pubkey)
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn attestation_round_trips_binds_and_rejects_forgery() {
57 let owner = Keys::generate();
58 let cid = "a".repeat(64);
59
60 let signed = build_owner_attestation_unsigned(owner.public_key(), &cid)
61 .finalize(&owner)
62 .unwrap();
63 let json = signed.as_json();
64
65 // Valid → the proven owner is the signer.
66 assert_eq!(verify_owner_attestation(&json, &cid), Some(owner.public_key()));
67 // Can't transplant to another community.
68 assert_eq!(verify_owner_attestation(&json, &"b".repeat(64)), None);
69 // Garbage in → None, never a panic.
70 assert_eq!(verify_owner_attestation("not json", &cid), None);
71
72 // Framing defense: a forger can only ever attest THEMSELVES — verify returns the
73 // forger's pubkey, never the victim's, so they can't make the UI crown someone else.
74 let mallory = Keys::generate();
75 let forged = build_owner_attestation_unsigned(mallory.public_key(), &cid)
76 .finalize(&mallory)
77 .unwrap()
78 .as_json();
79 assert_eq!(verify_owner_attestation(&forged, &cid), Some(mallory.public_key()));
80 assert_ne!(verify_owner_attestation(&forged, &cid), Some(owner.public_key()));
81 }
82
83 #[test]
84 fn attestation_tag_layout_is_frozen() {
85 // the attestation's signed form is FROZEN. It is verify-only (other clients check the
86 // owner's signature, never reconstruct the event — created_at is non-deterministic), so the
87 // pinned interop contract is the TAG layout: exactly one `["vco", <community_id>]` tag, empty
88 // content. A drift here (extra element, reordered, renamed) breaks every verifier reading idx 1.
89 let owner = Keys::generate();
90 let cid = "c".repeat(64);
91 let unsigned = build_owner_attestation_unsigned(owner.public_key(), &cid);
92 assert_eq!(unsigned.content, "");
93 let tags: Vec<Vec<String>> = unsigned.tags.iter().map(|t| t.as_slice().to_vec()).collect();
94 assert_eq!(tags, vec![vec!["vco".to_string(), cid]], "exactly one [\"vco\", community_id] tag");
95 }
96}