chore(repo-announce): Utils to creates the bech32

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-05-07 06:04:28 +00:00
parent 3857753b7d
commit 577de41054
3 changed files with 50 additions and 24 deletions

View File

@@ -16,13 +16,9 @@
use clap::Args; use clap::Args;
use nostr::{ use nostr::{
event::{EventBuilder, Kind}, event::EventBuilder,
key::PublicKey, key::PublicKey,
nips::{ nips::nip65::{self, RelayMetadata},
nip01::Coordinate,
nip19::{Nip19Coordinate, Nip19Event, ToBech32},
nip65::{self, RelayMetadata},
},
types::Url, types::Url,
}; };
@@ -63,17 +59,9 @@ impl CommandRunner for AnnounceArgs {
async fn run(self, options: CliOptions) -> N34Result<()> { async fn run(self, options: CliOptions) -> N34Result<()> {
let client = NostrClient::init(&options).await; let client = NostrClient::init(&options).await;
let user_pubk = options.pubkey().await?; let user_pubk = options.pubkey().await?;
let naddr = Nip19Coordinate::new(
Coordinate::new(Kind::GitRepoAnnouncement, user_pubk),
options.relays.iter().take(3),
)
.expect("Valid relays");
let relays_list = client.user_relays_list(user_pubk).await?; let relays_list = client.user_relays_list(user_pubk).await?;
let mut write_relays = options.relays.clone(); let mut write_relays = options.relays.clone();
let mut maintainers = vec![user_pubk]; let mut maintainers = vec![user_pubk];
write_relays.sort_unstable();
write_relays.dedup();
maintainers.extend(self.maintainers); maintainers.extend(self.maintainers);
if let Some(event) = relays_list.clone() { if let Some(event) = relays_list.clone() {
@@ -89,23 +77,21 @@ impl CommandRunner for AnnounceArgs {
self.description.map(utils::str_trim), self.description.map(utils::str_trim),
self.web, self.web,
self.clone, self.clone,
options.relays, options.relays.clone(),
maintainers, maintainers,
self.labels.into_iter().map(utils::str_trim).collect(), self.labels.into_iter().map(utils::str_trim).collect(),
)? )?
.pow(options.pow) .pow(options.pow)
.build(user_pubk); .build(user_pubk);
let nevent = Nip19Event::new(event.id.expect("There is an id"))
.relays(write_relays.iter().take(3).cloned())
.to_bech32()?;
let nevent = utils::new_nevent(event.id.expect("There is an id"), &write_relays)?;
let naddr = utils::repo_naddr(user_pubk, &options.relays)?;
client client
.send_event_to(event, relays_list.as_ref(), &write_relays) .send_event_to(event, relays_list.as_ref(), &write_relays)
.await?; .await?;
println!("Event: {nevent}",); println!("Event: {nevent}",);
println!("Repo Address: {}", naddr.to_bech32()?); println!("Repo Address: {naddr}",);
Ok(()) Ok(())
} }

View File

@@ -22,13 +22,13 @@ pub mod utils;
use std::time::Duration; use std::time::Duration;
use nostr::{ use nostr::{
event::{Event, EventId, Kind, UnsignedEvent}, event::{Event, Kind, UnsignedEvent},
filter::Filter, filter::Filter,
key::{Keys, PublicKey}, key::{Keys, PublicKey},
nips::{nip19::Nip19Coordinate, nip34::GitRepositoryAnnouncement}, nips::{nip19::Nip19Coordinate, nip34::GitRepositoryAnnouncement},
types::RelayUrl, types::RelayUrl,
}; };
use nostr_sdk::{Client, pool::Output}; use nostr_sdk::Client;
use crate::{ use crate::{
cli::CliOptions, cli::CliOptions,

View File

@@ -17,12 +17,19 @@
use std::{fmt, str::FromStr}; use std::{fmt, str::FromStr};
use nostr::{ use nostr::{
event::{Event, TagKind, TagStandard}, event::{Event, EventId, Kind, TagKind, TagStandard},
filter::{Alphabet, SingleLetterTag}, filter::{Alphabet, SingleLetterTag},
nips::nip34::GitRepositoryAnnouncement, key::PublicKey,
nips::{
nip01::Coordinate,
nip19::{Nip19Coordinate, Nip19Event, ToBech32},
nip34::GitRepositoryAnnouncement,
},
types::RelayUrl,
}; };
use super::traits::TagsExt; use super::traits::TagsExt;
use crate::error::{N34Error, N34Result};
/// Returns the value of the given tag /// Returns the value of the given tag
fn tag_value(tag: &TagStandard) -> String { fn tag_value(tag: &TagStandard) -> String {
@@ -80,3 +87,36 @@ pub fn event_into_repo(event: Event, repo_id: impl Into<String>) -> GitRepositor
pub fn str_trim(s: String) -> String { pub fn str_trim(s: String) -> String {
s.trim().to_owned() s.trim().to_owned()
} }
/// Returns a vector with duplicate elements removed.
pub fn dedup<I, T>(iter: I) -> Vec<T>
where
T: std::cmp::Ord,
I: Iterator<Item = T>,
{
let mut vector: Vec<T> = iter.collect();
vector.sort_unstable();
vector.dedup();
vector
}
/// Creates a new NIP-19 nevent string from an event ID and up to 3 unique relay
/// URLs.
pub fn new_nevent(event_id: EventId, relays: &[RelayUrl]) -> N34Result<String> {
Nip19Event::new(event_id)
.relays(dedup(relays.iter().take(3).cloned()))
.to_bech32()
.map_err(N34Error::from)
}
/// Creates a NIP-19 naddr string for a git repository announcement and up to 3
/// unique relay URLs.
pub fn repo_naddr(pubk: PublicKey, relays: &[RelayUrl]) -> N34Result<String> {
Nip19Coordinate::new(
Coordinate::new(Kind::GitRepoAnnouncement, pubk),
dedup(relays.iter().take(3)),
)
.expect("Valid relays")
.to_bech32()
.map_err(N34Error::from)
}