use super::version;
use crate::stored_event::event_kind;
use nostr_sdk::prelude::*;
const TAG_SUBKIND: &str = "vsk";
const TAG_ENTITY: &str = "eid";
const TAG_EVERSION: &str = "ev";
const TAG_EPREV: &str = "ep";
const TAG_VERSION: &str = "v";
const PROTOCOL_VERSION: &str = "1";
pub const TAG_AUTHORITY_CITATION: &str = "vac";
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AuthorityCitation {
pub entity_id: [u8; 32],
pub version: u64,
pub edition_hash: [u8; 32],
}
impl AuthorityCitation {
pub fn to_tag(&self) -> Tag {
Tag::custom(
TagKind::Custom(TAG_AUTHORITY_CITATION.into()),
[
crate::simd::hex::bytes_to_hex_32(&self.entity_id),
self.version.to_string(),
crate::simd::hex::bytes_to_hex_32(&self.edition_hash),
],
)
}
pub fn from_tags(tags: &Tags) -> Option<AuthorityCitation> {
let s = tags.iter().find_map(|t| {
let s = t.as_slice();
(s.len() >= 4 && s[0] == TAG_AUTHORITY_CITATION).then(|| (s[1].clone(), s[2].clone(), s[3].clone()))
})?;
let valid_hex = |h: &str| h.len() == 64 && h.bytes().all(|b| b.is_ascii_hexdigit());
if !valid_hex(&s.0) || !valid_hex(&s.2) {
return None;
}
Some(AuthorityCitation {
entity_id: crate::simd::hex::hex_to_bytes_32(&s.0),
version: s.1.parse().ok()?,
edition_hash: crate::simd::hex::hex_to_bytes_32(&s.2),
})
}
}
pub fn build_edition_inner(
author: PublicKey,
vsk: &str,
entity_id: &[u8; 32],
version: u64,
prev_hash: Option<&[u8; 32]>,
content: &str,
created_at_secs: u64,
authority: Option<&AuthorityCitation>,
) -> UnsignedEvent {
let mut tags = vec![
Tag::custom(TagKind::Custom(TAG_SUBKIND.into()), [vsk.to_string()]),
Tag::custom(TagKind::Custom(TAG_ENTITY.into()), [crate::simd::hex::bytes_to_hex_32(entity_id)]),
Tag::custom(TagKind::Custom(TAG_EVERSION.into()), [version.to_string()]),
Tag::custom(TagKind::Custom(TAG_VERSION.into()), [PROTOCOL_VERSION.to_string()]),
];
if let Some(p) = prev_hash {
tags.push(Tag::custom(TagKind::Custom(TAG_EPREV.into()), [crate::simd::hex::bytes_to_hex_32(p)]));
}
if let Some(a) = authority {
tags.push(a.to_tag());
}
EventBuilder::new(Kind::Custom(event_kind::COMMUNITY_CONTROL), content)
.tags(tags)
.custom_created_at(Timestamp::from_secs(created_at_secs))
.build(author)
}
#[derive(Clone, Debug)]
pub struct ParsedEdition {
pub author: PublicKey,
pub vsk: String,
pub entity_id: [u8; 32],
pub version: u64,
pub prev_hash: Option<[u8; 32]>,
pub content: String,
pub self_hash: [u8; 32],
pub created_at: u64,
pub inner_id: [u8; 32],
pub authority: Option<AuthorityCitation>,
}
#[derive(Debug, PartialEq, Eq)]
pub enum EditionError {
BadSignature,
MissingField(&'static str),
BadField(&'static str),
}
fn decode_hash(hex: &str, field: &'static str) -> Result<[u8; 32], EditionError> {
if hex.len() != 64 || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err(EditionError::BadField(field));
}
Ok(crate::simd::hex::hex_to_bytes_32(hex))
}
pub fn parse_edition_inner(inner: &Event) -> Result<ParsedEdition, EditionError> {
inner.verify().map_err(|_| EditionError::BadSignature)?;
for name in [TAG_SUBKIND, TAG_ENTITY, TAG_EVERSION, TAG_EPREV, TAG_AUTHORITY_CITATION] {
let count = inner
.tags
.iter()
.filter(|t| t.as_slice().first().map(|s| s.as_str() == name).unwrap_or(false))
.count();
if count > 1 {
return Err(EditionError::BadField("duplicate authority tag"));
}
}
let get = |name: &str| -> Option<String> {
inner.tags.iter().find_map(|t| {
let s = t.as_slice();
(s.len() >= 2 && s[0] == name).then(|| s[1].clone())
})
};
let vsk = get(TAG_SUBKIND).ok_or(EditionError::MissingField("vsk"))?;
let entity_id = decode_hash(&get(TAG_ENTITY).ok_or(EditionError::MissingField("eid"))?, "eid")?;
let ev_raw = get(TAG_EVERSION).ok_or(EditionError::MissingField("ev"))?;
if ev_raw.is_empty() || !ev_raw.bytes().all(|b| b.is_ascii_digit()) {
return Err(EditionError::BadField("ev"));
}
let version: u64 = ev_raw.parse().map_err(|_| EditionError::BadField("ev"))?;
let prev_hash = match get(TAG_EPREV) {
Some(h) => Some(decode_hash(&h, "ep")?),
None => None,
};
let content = inner.content.clone();
let self_hash = version::edition_hash(&entity_id, version, prev_hash.as_ref(), content.as_bytes());
Ok(ParsedEdition {
author: inner.pubkey,
vsk,
entity_id,
version,
prev_hash,
content,
self_hash,
created_at: inner.created_at.as_secs(),
inner_id: inner.id.to_bytes(),
authority: AuthorityCitation::from_tags(&inner.tags),
})
}
impl ParsedEdition {
pub fn to_fold_edition(&self) -> version::Edition {
version::Edition {
version: self.version,
prev_hash: self.prev_hash,
self_hash: self.self_hash,
created_at: self.created_at,
tiebreak_id: self.inner_id,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const VSK_GRANT: &str = "3";
fn eid() -> [u8; 32] {
[0x42; 32]
}
#[test]
fn round_trips_authorship_version_and_chain_hash() {
let actor = Keys::generate();
let prev = version::edition_hash(&eid(), 1, None, b"{}");
let inner = build_edition_inner(actor.public_key(), VSK_GRANT, &eid(), 2, Some(&prev), "{\"role_ids\":[]}", 1_700_000_000, None)
.sign_with_keys(&actor)
.unwrap();
let parsed = parse_edition_inner(&inner).expect("valid edition parses");
assert_eq!(parsed.author, actor.public_key(), "authorship = the real signer");
assert_eq!(parsed.vsk, VSK_GRANT);
assert_eq!(parsed.entity_id, eid());
assert_eq!(parsed.version, 2);
assert_eq!(parsed.prev_hash, Some(prev));
assert_eq!(parsed.created_at, 1_700_000_000);
assert_eq!(
parsed.self_hash,
version::edition_hash(&eid(), 2, Some(&prev), b"{\"role_ids\":[]}")
);
let fe = parsed.to_fold_edition();
assert_eq!(fe.version, 2);
assert_eq!(fe.prev_hash, Some(prev));
}
#[test]
fn authority_citation_round_trips_on_an_edition() {
let actor = Keys::generate();
let cite = AuthorityCitation { entity_id: [0xab; 32], version: 7, edition_hash: [0xcd; 32] };
let inner = build_edition_inner(actor.public_key(), VSK_GRANT, &eid(), 1, None, "{}", 100, Some(&cite))
.sign_with_keys(&actor)
.unwrap();
let parsed = parse_edition_inner(&inner).unwrap();
assert_eq!(parsed.authority.as_ref(), Some(&cite), "citation round-trips");
assert_eq!(parsed.self_hash, version::edition_hash(&eid(), 1, None, b"{}"));
let owner = build_edition_inner(actor.public_key(), VSK_GRANT, &eid(), 1, None, "{}", 100, None)
.sign_with_keys(&actor)
.unwrap();
assert_eq!(parse_edition_inner(&owner).unwrap().authority, None);
}
#[test]
fn authority_citation_tag_layout_is_frozen() {
let cite = AuthorityCitation { entity_id: [0x11; 32], version: 9, edition_hash: [0x22; 32] };
let tag = cite.to_tag();
let s = tag.as_slice();
assert_eq!(s.len(), 4, "vac is a 4-element tag");
assert_eq!(s[0], TAG_AUTHORITY_CITATION);
assert_eq!(s[1], "11".repeat(32), "entity id is lowercase hex");
assert_eq!(s[2], "9", "version is the decimal string");
assert_eq!(s[3], "22".repeat(32), "edition hash is lowercase hex");
}
#[test]
fn genesis_edition_has_no_prev() {
let actor = Keys::generate();
let inner = build_edition_inner(actor.public_key(), "1", &eid(), 1, None, "{}", 100, None)
.sign_with_keys(&actor)
.unwrap();
let parsed = parse_edition_inner(&inner).unwrap();
assert_eq!(parsed.prev_hash, None, "first edition cites no predecessor");
assert_eq!(parsed.version, 1);
}
#[test]
fn tampered_content_fails_verification() {
let actor = Keys::generate();
let inner = build_edition_inner(actor.public_key(), "3", &eid(), 1, None, "{\"a\":1}", 100, None)
.sign_with_keys(&actor)
.unwrap();
let mut json: serde_json::Value = serde_json::from_str(&inner.as_json()).unwrap();
json["content"] = serde_json::Value::String("{\"a\":2}".into()); let tampered: Event = serde_json::from_value(json).unwrap();
assert!(matches!(parse_edition_inner(&tampered), Err(EditionError::BadSignature)));
}
#[test]
fn missing_required_field_is_rejected_not_panicked() {
let actor = Keys::generate();
let inner = EventBuilder::new(Kind::Custom(event_kind::COMMUNITY_CONTROL), "{}")
.tags([Tag::custom(TagKind::Custom("vsk".into()), ["3".to_string()])])
.sign_with_keys(&actor)
.unwrap();
assert!(matches!(parse_edition_inner(&inner), Err(EditionError::MissingField("eid"))));
}
#[test]
fn duplicate_authority_tag_is_rejected() {
let actor = Keys::generate();
let hash = crate::simd::hex::bytes_to_hex_32(&[0xAB; 32]);
let base = || -> Vec<Tag> {
vec![
Tag::custom(TagKind::Custom("vsk".into()), ["1".to_string()]),
Tag::custom(TagKind::Custom("eid".into()), [crate::simd::hex::bytes_to_hex_32(&eid())]),
Tag::custom(TagKind::Custom("ev".into()), ["1".to_string()]),
Tag::custom(TagKind::Custom("ep".into()), [hash.clone()]),
Tag::custom(TagKind::Custom("vac".into()), [crate::simd::hex::bytes_to_hex_32(&eid()), "1".to_string(), hash.clone()]),
]
};
let build = |tags: Vec<Tag>| EventBuilder::new(Kind::Custom(event_kind::COMMUNITY_CONTROL), "{}")
.tags(tags).sign_with_keys(&actor).unwrap();
assert!(parse_edition_inner(&build(base())).is_ok(), "a clean 5-tag base edition parses");
for name in ["vsk", "eid", "ev", "ep", "vac"] {
let mut tags = base();
let dup = tags.iter().find(|t| t.as_slice().first().map(|s| s == name).unwrap_or(false)).cloned().unwrap();
tags.push(dup);
assert!(
matches!(parse_edition_inner(&build(tags)), Err(EditionError::BadField("duplicate authority tag"))),
"a duplicate `{name}` tag must be rejected"
);
}
}
}