From 73e1198f2ec99b2e3ec02716d58216eaf849fbff Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 14 Feb 2025 10:36:17 +0100 Subject: [PATCH] nostr: fix `EventBuilder::git_issue` - Enforce repository address kind - Tag public key of repo owner - Remove git issue `alt` tag - Remove `public_keys` field from `GitIssue` since any kind of tag can be added with `EventBuilder::tags` - Change `EventBuilder::git_issue` signature Fixes 631a966c783adb1fbd94b2fc9b5f3c5f1f31b7e5 Closes https://github.com/rust-nostr/nostr/issues/758 Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 3 ++ .../nostr-sdk-ffi/src/protocol/nips/nip34.rs | 9 ++--- .../nostr-sdk-js/src/protocol/nips/nip34.rs | 10 ++---- crates/nostr/src/event/builder.rs | 2 +- crates/nostr/src/nips/nip34.rs | 34 +++++++++++-------- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8528a2e35..0a6ac5a6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ * nostr: update `Nip19Event` relays field type from `Vec` to `Vec` ([Yuki Kishimoto]) * nostr: update `Tags::new` signature ([Yuki Kishimoto]) * nostr: remove `WeakTag` ([Yuki Kishimoto]) +* nostr: change `EventBuilder::git_issue` signature ([Yuki Kishimoto]) ### Changed @@ -65,6 +66,8 @@ ### Fixed +* fix `EventBuilder::git_issue` according to last NIP34 rev ([Yuki Kishimoto]) + ### Removed * database: remove deprecated ([Yuki Kishimoto]) diff --git a/bindings/nostr-sdk-ffi/src/protocol/nips/nip34.rs b/bindings/nostr-sdk-ffi/src/protocol/nips/nip34.rs index edc1b5af2..5d87e82f5 100644 --- a/bindings/nostr-sdk-ffi/src/protocol/nips/nip34.rs +++ b/bindings/nostr-sdk-ffi/src/protocol/nips/nip34.rs @@ -76,12 +76,10 @@ impl From for nip34::GitRepositoryAnnouncement { /// Git Issue #[derive(Record)] pub struct GitIssue { - /// The issue content (markdown) - pub content: String, /// The repository address pub repository: Arc, - /// Public keys (owners or other users) - pub public_keys: Vec>, + /// The issue content (markdown) + pub content: String, /// Subject pub subject: Option, /// Labels @@ -91,9 +89,8 @@ pub struct GitIssue { impl From for nip34::GitIssue { fn from(value: GitIssue) -> Self { Self { - content: value.content, repository: value.repository.as_ref().deref().clone(), - public_keys: value.public_keys.into_iter().map(|p| **p).collect(), + content: value.content, subject: value.subject, labels: value.labels, } diff --git a/bindings/nostr-sdk-js/src/protocol/nips/nip34.rs b/bindings/nostr-sdk-js/src/protocol/nips/nip34.rs index 36def5412..bb7bcd00a 100644 --- a/bindings/nostr-sdk-js/src/protocol/nips/nip34.rs +++ b/bindings/nostr-sdk-js/src/protocol/nips/nip34.rs @@ -77,15 +77,12 @@ impl From for GitRepositoryAnnouncement { /// Git Issue #[wasm_bindgen(js_name = GitIssue)] pub struct JsGitIssue { - /// The issue content (markdown) - #[wasm_bindgen(getter_with_clone)] - pub content: String, /// The repository address #[wasm_bindgen(getter_with_clone)] pub repository: JsCoordinate, - /// Public keys (owners or other users) + /// The issue content (markdown) #[wasm_bindgen(getter_with_clone)] - pub public_keys: Vec, + pub content: String, /// Subject #[wasm_bindgen(getter_with_clone)] pub subject: Option, @@ -97,9 +94,8 @@ pub struct JsGitIssue { impl From for GitIssue { fn from(value: JsGitIssue) -> Self { Self { - content: value.content, repository: value.repository.deref().clone(), - public_keys: value.public_keys.into_iter().map(|p| *p).collect(), + content: value.content, subject: value.subject, labels: value.labels, } diff --git a/crates/nostr/src/event/builder.rs b/crates/nostr/src/event/builder.rs index 25b55481f..e686480ef 100644 --- a/crates/nostr/src/event/builder.rs +++ b/crates/nostr/src/event/builder.rs @@ -1691,7 +1691,7 @@ impl EventBuilder { /// /// #[inline] - pub fn git_issue(issue: GitIssue) -> Self { + pub fn git_issue(issue: GitIssue) -> Result { issue.to_event_builder() } diff --git a/crates/nostr/src/nips/nip34.rs b/crates/nostr/src/nips/nip34.rs index 9fea291e7..849b7f65a 100644 --- a/crates/nostr/src/nips/nip34.rs +++ b/crates/nostr/src/nips/nip34.rs @@ -15,16 +15,16 @@ use core::fmt; use hashes::sha1::Hash as Sha1Hash; +use crate::event::builder::{Error, EventBuilder, WrongKindError}; use crate::nips::nip01::Coordinate; use crate::nips::nip10::Marker; use crate::types::url::Url; -use crate::{EventBuilder, EventId, Kind, PublicKey, Tag, TagKind, TagStandard, Timestamp}; +use crate::{EventId, Kind, PublicKey, Tag, TagKind, TagStandard, Timestamp}; /// Earlier unique commit ID pub const EUC: &str = "euc"; const GIT_REPO_ANNOUNCEMENT_ALT: &str = "git repository"; -const GIT_ISSUE_ALT: &str = "git issue"; const GIT_PATCH_ALT: &str = "git patch"; const GIT_PATCH_COVER_LETTER_ALT: &str = "git patch cover letter"; @@ -122,12 +122,10 @@ impl GitRepositoryAnnouncement { /// Git Issue #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct GitIssue { - /// The issue content (markdown) - pub content: String, /// The repository address pub repository: Coordinate, - /// Public keys (owners or other users) - pub public_keys: Vec, + /// The issue content (markdown) + pub content: String, /// Subject pub subject: Option, /// Labels @@ -135,14 +133,25 @@ pub struct GitIssue { } impl GitIssue { - pub(crate) fn to_event_builder(self) -> EventBuilder { - let mut tags: Vec = Vec::with_capacity(1); + /// Based on revision. + pub(crate) fn to_event_builder(self) -> Result { + // Check if repository address kind is wrong + if self.repository.kind != Kind::GitRepoAnnouncement { + return Err(Error::WrongKind { + received: self.repository.kind, + expected: WrongKindError::Single(Kind::GitRepoAnnouncement), + }); + } + + let owner_public_key: PublicKey = self.repository.public_key; + + let mut tags: Vec = Vec::with_capacity(2); // Add coordinate tags.push(Tag::coordinate(self.repository)); - // Add public keys - tags.extend(self.public_keys.into_iter().map(Tag::public_key)); + // Add owner public key + tags.push(Tag::public_key(owner_public_key)); // Add subject if let Some(subject) = self.subject { @@ -154,11 +163,8 @@ impl GitIssue { // Add labels tags.extend(self.labels.into_iter().map(Tag::hashtag)); - // Add alt tag - tags.push(Tag::alt(GIT_ISSUE_ALT)); - // Build - EventBuilder::new(Kind::GitIssue, self.content).tags(tags) + Ok(EventBuilder::new(Kind::GitIssue, self.content).tags(tags)) } }