Skip to content

Commit

Permalink
nostr: change TagStandard::Relays variant inner value
Browse files Browse the repository at this point in the history
Change `TagStandard::Relays` variant inner value from `Vec<Url>` to `Vec<RelayUrl>`.

Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Feb 14, 2025
1 parent b7c1168 commit 43b9080
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* nostr: update `Nip19Event` relays field type from `Vec<String>` to `Vec<RelayUrl>` ([Yuki Kishimoto])
* nostr: update `Tags::new` signature ([Yuki Kishimoto])
* nostr: remove `WeakTag` ([Yuki Kishimoto])
* nostr: change `TagStandard::Relays` variant inner value from `Vec<Url>` to `Vec<RelayUrl>` ([Yuki Kishimoto])

### Changed

Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/protocol/event/tag/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,9 @@ impl TryFrom<TagStandard> for tag::TagStandard {
TagStandard::Bolt11 { bolt11 } => Ok(Self::Bolt11(bolt11)),
TagStandard::Preimage { preimage } => Ok(Self::Preimage(preimage)),
TagStandard::Relays { urls } => {
let mut parsed_urls: Vec<Url> = Vec::with_capacity(urls.len());
let mut parsed_urls: Vec<RelayUrl> = Vec::with_capacity(urls.len());
for url in urls.into_iter() {
parsed_urls.push(Url::parse(&url)?);
parsed_urls.push(RelayUrl::parse(&url)?);
}
Ok(Self::Relays(parsed_urls))
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/protocol/nips/nip34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;

use nostr::hashes::sha1::Hash as Sha1Hash;
use nostr::nips::nip34;
use nostr::Url;
use nostr::{RelayUrl, Url};
use uniffi::{Enum, Record};

use crate::error::NostrSdkError;
Expand Down Expand Up @@ -65,7 +65,7 @@ impl From<GitRepositoryAnnouncement> for nip34::GitRepositoryAnnouncement {
relays: value
.relays
.into_iter()
.filter_map(|u| Url::parse(&u).ok())
.filter_map(|u| RelayUrl::parse(&u).ok())
.collect(),
euc: value.euc,
maintainers: value.maintainers.into_iter().map(|p| **p).collect(),
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/src/protocol/nips/nip53.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl TryFrom<LiveEvent> for nip53::LiveEvent {
relays: value
.relays
.into_iter()
.filter_map(|u| Url::parse(&u).ok())
.filter_map(|u| RelayUrl::parse(&u).ok())
.collect(),
host: match value.host {
Some(h) => Some(h.try_into()?),
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/protocol/nips/nip57.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::ops::Deref;

use nostr::nips::nip57;
use nostr::Url;
use nostr::RelayUrl;
use uniffi::{Enum, Object};

use crate::error::Result;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl ZapRequestData {
Self {
inner: nip57::ZapRequestData::new(
**public_key,
relays.into_iter().filter_map(|u| Url::parse(&u).ok()),
relays.into_iter().filter_map(|u| RelayUrl::parse(&u).ok()),
),
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-js/src/protocol/nips/nip34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl From<JsGitRepositoryAnnouncement> for GitRepositoryAnnouncement {
relays: value
.relays
.into_iter()
.filter_map(|u| Url::parse(&u).ok())
.filter_map(|u| RelayUrl::parse(&u).ok())
.collect(),
euc: value.euc,
maintainers: value.maintainers.into_iter().map(|p| *p).collect(),
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-js/src/protocol/nips/nip57.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl JsZapRequestData {
public_key: **public_key,
relays: relays
.into_iter()
.filter_map(|r| Url::parse(&r).ok())
.filter_map(|r| RelayUrl::parse(&r).ok())
.collect(),
message: message.to_string(),
amount: amount.map(|n| n as u64),
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/examples/nip57.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> Result<()> {

let public_key =
PublicKey::from_bech32("npub14f8usejl26twx0dhuxjh9cas7keav9vr0v8nvtwtrjqx3vycc76qqh9nsy")?;
let relays = [Url::parse("wss://relay.damus.io").unwrap()];
let relays = [RelayUrl::parse("wss://relay.damus.io").unwrap()];
let data = ZapRequestData::new(public_key, relays).message("Zap!");

let public_zap: Event = EventBuilder::public_zap_request(data.clone()).sign_with_keys(&keys)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ impl EventBuilder {
/// # let public_key = PublicKey::from_bech32(
/// # "npub14f8usejl26twx0dhuxjh9cas7keav9vr0v8nvtwtrjqx3vycc76qqh9nsy",
/// # ).unwrap();
/// # let relays = [Url::parse("wss://relay.damus.io").unwrap()];
/// # let relays = [RelayUrl::parse("wss://relay.damus.io").unwrap()];
/// let data = ZapRequestData::new(public_key, relays).message("Zap!");
///
/// let anon_zap: Event = nip57::anonymous_zap_request(data.clone()).unwrap();
Expand Down
22 changes: 17 additions & 5 deletions crates/nostr/src/event/tag/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub enum TagStandard {
uppercase: bool,
},
Relay(RelayUrl),
Relays(Vec<RelayUrl>),
/// Proof of Work
///
/// <https://github.com/nostr-protocol/nips/blob/master/13.md>
Expand All @@ -159,7 +160,6 @@ pub enum TagStandard {
Description(String),
Bolt11(String),
Preimage(String),
Relays(Vec<Url>),
Amount {
millisats: u64,
bolt11: Option<String>,
Expand Down Expand Up @@ -324,7 +324,7 @@ impl TagStandard {
}
TagKind::Protected => return Ok(Self::Protected),
TagKind::Relays => {
let urls: Vec<Url> = extract_urls(tag)?;
let urls: Vec<RelayUrl> = extract_relay_urls(tag)?;
return Ok(Self::Relays(urls));
}
TagKind::Web => {
Expand Down Expand Up @@ -1244,6 +1244,18 @@ where
Ok(list)
}

fn extract_relay_urls<S>(tag: &[S]) -> Result<Vec<RelayUrl>, Error>
where
S: AsRef<str>,
{
// Skip index 0 because is the tag kind
let mut list: Vec<RelayUrl> = Vec::with_capacity(tag.len().saturating_sub(1));
for url in tag.iter().skip(1) {
list.push(RelayUrl::parse(url.as_ref())?);
}
Ok(list)
}

fn extract_public_keys<S>(tag: &[S]) -> Result<Vec<PublicKey>, Error>
where
S: AsRef<str>,
Expand Down Expand Up @@ -2252,9 +2264,9 @@ mod tests {
])
.unwrap(),
TagStandard::Relays(vec![
Url::parse("wss://relay.damus.io/").unwrap(),
Url::parse("wss://nostr-relay.wlvs.space/").unwrap(),
Url::parse("wss://nostr.fmt.wiz.biz").unwrap(),
RelayUrl::parse("wss://relay.damus.io/").unwrap(),
RelayUrl::parse("wss://nostr-relay.wlvs.space/").unwrap(),
RelayUrl::parse("wss://nostr.fmt.wiz.biz").unwrap(),
])
);

Expand Down
6 changes: 4 additions & 2 deletions crates/nostr/src/nips/nip34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use hashes::sha1::Hash as Sha1Hash;
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::{
EventBuilder, EventId, Kind, PublicKey, RelayUrl, Tag, TagKind, TagStandard, Timestamp,
};

/// Earlier unique commit ID
pub const EUC: &str = "euc";
Expand All @@ -45,7 +47,7 @@ pub struct GitRepositoryAnnouncement {
/// Urls for git-cloning
pub clone: Vec<Url>,
/// Relays that this repository will monitor for patches and issues
pub relays: Vec<Url>,
pub relays: Vec<RelayUrl>,
/// Earliest unique commit ID
///
/// `euc` marker should be the commit ID of the earliest unique commit of this repo,
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/src/nips/nip53.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub struct LiveEvent {
/// Total participants
pub total_participants: Option<u64>,
/// Relays
pub relays: Vec<Url>,
pub relays: Vec<RelayUrl>,
/// Host
pub host: Option<LiveEventHost>,
/// Speakers
Expand Down
10 changes: 5 additions & 5 deletions crates/nostr/src/nips/nip57.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::types::time::TimeSupplier;
#[cfg(feature = "std")]
use crate::SECP256K1;
use crate::{
event, util, Event, EventBuilder, EventId, JsonUtil, Keys, Kind, PublicKey, SecretKey, Tag,
TagStandard, Timestamp, Url,
event, util, Event, EventBuilder, EventId, JsonUtil, Keys, Kind, PublicKey, RelayUrl,
SecretKey, Tag, TagStandard, Timestamp,
};

type Aes256CbcEnc = Encryptor<Aes256>;
Expand Down Expand Up @@ -126,7 +126,7 @@ pub struct ZapRequestData {
/// Public key of the recipient
pub public_key: PublicKey,
/// List of relays the recipient's wallet should publish its zap receipt to
pub relays: Vec<Url>,
pub relays: Vec<RelayUrl>,
/// Message
pub message: String,
/// Amount in `millisats` the sender intends to pay
Expand All @@ -143,7 +143,7 @@ impl ZapRequestData {
/// New Zap Request Data
pub fn new<I>(public_key: PublicKey, relays: I) -> Self
where
I: IntoIterator<Item = Url>,
I: IntoIterator<Item = RelayUrl>,
{
Self {
public_key,
Expand Down Expand Up @@ -425,7 +425,7 @@ mod tests {
let alice_keys = Keys::generate();
let bob_keys = Keys::generate();

let relays = [Url::parse("wss://relay.damus.io").unwrap()];
let relays = [RelayUrl::parse("wss://relay.damus.io").unwrap()];
let msg = "Private Zap message!";
let data = ZapRequestData::new(bob_keys.public_key(), relays).message(msg);
let private_zap = private_zap_request(data, &alice_keys).unwrap();
Expand Down

0 comments on commit 43b9080

Please sign in to comment.