chore(repo-announce): Utils to creates the bech32
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -16,13 +16,9 @@
|
||||
|
||||
use clap::Args;
|
||||
use nostr::{
|
||||
event::{EventBuilder, Kind},
|
||||
event::EventBuilder,
|
||||
key::PublicKey,
|
||||
nips::{
|
||||
nip01::Coordinate,
|
||||
nip19::{Nip19Coordinate, Nip19Event, ToBech32},
|
||||
nip65::{self, RelayMetadata},
|
||||
},
|
||||
nips::nip65::{self, RelayMetadata},
|
||||
types::Url,
|
||||
};
|
||||
|
||||
@@ -63,17 +59,9 @@ impl CommandRunner for AnnounceArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
let client = NostrClient::init(&options).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 mut write_relays = options.relays.clone();
|
||||
let mut maintainers = vec![user_pubk];
|
||||
write_relays.sort_unstable();
|
||||
write_relays.dedup();
|
||||
maintainers.extend(self.maintainers);
|
||||
|
||||
if let Some(event) = relays_list.clone() {
|
||||
@@ -89,23 +77,21 @@ impl CommandRunner for AnnounceArgs {
|
||||
self.description.map(utils::str_trim),
|
||||
self.web,
|
||||
self.clone,
|
||||
options.relays,
|
||||
options.relays.clone(),
|
||||
maintainers,
|
||||
self.labels.into_iter().map(utils::str_trim).collect(),
|
||||
)?
|
||||
.pow(options.pow)
|
||||
.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
|
||||
.send_event_to(event, relays_list.as_ref(), &write_relays)
|
||||
.await?;
|
||||
|
||||
println!("Event: {nevent}",);
|
||||
println!("Repo Address: {}", naddr.to_bech32()?);
|
||||
println!("Repo Address: {naddr}",);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -22,13 +22,13 @@ pub mod utils;
|
||||
use std::time::Duration;
|
||||
|
||||
use nostr::{
|
||||
event::{Event, EventId, Kind, UnsignedEvent},
|
||||
event::{Event, Kind, UnsignedEvent},
|
||||
filter::Filter,
|
||||
key::{Keys, PublicKey},
|
||||
nips::{nip19::Nip19Coordinate, nip34::GitRepositoryAnnouncement},
|
||||
types::RelayUrl,
|
||||
};
|
||||
use nostr_sdk::{Client, pool::Output};
|
||||
use nostr_sdk::Client;
|
||||
|
||||
use crate::{
|
||||
cli::CliOptions,
|
||||
|
||||
@@ -17,12 +17,19 @@
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
use nostr::{
|
||||
event::{Event, TagKind, TagStandard},
|
||||
event::{Event, EventId, Kind, TagKind, TagStandard},
|
||||
filter::{Alphabet, SingleLetterTag},
|
||||
nips::nip34::GitRepositoryAnnouncement,
|
||||
key::PublicKey,
|
||||
nips::{
|
||||
nip01::Coordinate,
|
||||
nip19::{Nip19Coordinate, Nip19Event, ToBech32},
|
||||
nip34::GitRepositoryAnnouncement,
|
||||
},
|
||||
types::RelayUrl,
|
||||
};
|
||||
|
||||
use super::traits::TagsExt;
|
||||
use crate::error::{N34Error, N34Result};
|
||||
|
||||
/// Returns the value of the given tag
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user