Skip to content

Commit 4e08f80

Browse files
committed
Merge "Impl ToBech32 and ToNostrUri for Coordinate, CoordinateBorrow and Event"
Closes nostr:nevent1qvzqqqqx25pzpepndn2jthmelfxn4umylktqp493ph8yy9d2fse76al2ppprgjcsqqsvf2k0leg7jptedx9pdt8s9ayrr32qmt06jhdzdrvsesw4j04pzgskh937n Pull-Request: #1113 Signed-off-by: Yuki Kishimoto <[email protected]>
2 parents 16345d5 + ab21b35 commit 4e08f80

File tree

5 files changed

+90
-45
lines changed

5 files changed

+90
-45
lines changed

crates/nostr/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
- Add NIP-C7 support (https://github.com/rust-nostr/nostr/pull/1067)
4545
- Add NIP-60 support (https://github.com/rust-nostr/nostr/pull/1092)
4646
- Implement `ToBech32` trait for `Nip21`
47+
- Implement `ToBech32` for `Coordinate`
48+
- Implement `ToNostrUri` for `Coordinate`
49+
- Implement `ToBech32` for `CoordinateBorrow`
50+
- Implement `ToNostrUri` for `CoordinateBorrow`
51+
- Implement `ToBech32` for `Event`
52+
- Implement `ToNostrUri` for `Event`
4753
- Implement `From<&Event>` for `Nip19Event`
4854
- Add `CowTag::kind` and `CowTag::content`
4955
- Add `Timestamp::as_secs`

crates/nostr/src/event/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub use self::kind::Kind;
3131
pub use self::tag::{Tag, TagKind, TagStandard, Tags};
3232
pub use self::unsigned::UnsignedEvent;
3333
use crate::nips::nip01::CoordinateBorrow;
34+
use crate::nips::nip19::{self, Nip19Event, ToBech32};
35+
use crate::nips::nip21::ToNostrUri;
3436
#[cfg(feature = "std")]
3537
use crate::types::time::Instant;
3638
use crate::types::time::TimeSupplier;
@@ -294,6 +296,19 @@ impl JsonUtil for Event {
294296
}
295297
}
296298

299+
impl ToBech32 for Event {
300+
type Err = nip19::Error;
301+
302+
fn to_bech32(&self) -> Result<String, Self::Err> {
303+
match self.coordinate() {
304+
Some(coordinate) => coordinate.to_bech32(),
305+
None => Nip19Event::from(self).to_bech32(),
306+
}
307+
}
308+
}
309+
310+
impl ToNostrUri for Event {}
311+
297312
impl TryFrom<&Event> for Metadata {
298313
type Error = serde_json::Error;
299314

crates/nostr/src/nips/nip01.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use serde::ser::{SerializeMap, Serializer};
1717
use serde::{Deserialize, Serialize};
1818
use serde_json::Value;
1919

20-
use super::nip19::FromBech32;
21-
use super::nip21::FromNostrUri;
20+
use super::nip19::{self, FromBech32, Nip19Coordinate, ToBech32};
21+
use super::nip21::{FromNostrUri, ToNostrUri};
2222
use crate::types::Url;
2323
use crate::{key, Filter, JsonUtil, Kind, PublicKey, Tag};
2424

@@ -222,6 +222,27 @@ impl FromStr for Coordinate {
222222
}
223223
}
224224

225+
impl ToBech32 for Coordinate {
226+
type Err = nip19::Error;
227+
228+
#[inline]
229+
fn to_bech32(&self) -> Result<String, Self::Err> {
230+
self.borrow().to_bech32()
231+
}
232+
}
233+
234+
impl FromBech32 for Coordinate {
235+
type Err = nip19::Error;
236+
237+
fn from_bech32(addr: &str) -> Result<Self, Self::Err> {
238+
let coordinate: Nip19Coordinate = Nip19Coordinate::from_bech32(addr)?;
239+
Ok(coordinate.coordinate)
240+
}
241+
}
242+
243+
impl ToNostrUri for Coordinate {}
244+
impl FromNostrUri for Coordinate {}
245+
225246
/// Borrowed coordinate
226247
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
227248
pub struct CoordinateBorrow<'a> {
@@ -246,6 +267,17 @@ impl CoordinateBorrow<'_> {
246267
}
247268
}
248269

270+
impl ToBech32 for CoordinateBorrow<'_> {
271+
type Err = nip19::Error;
272+
273+
#[inline]
274+
fn to_bech32(&self) -> Result<String, Self::Err> {
275+
nip19::coordinate_to_bech32(*self, &[])
276+
}
277+
}
278+
279+
impl ToNostrUri for CoordinateBorrow<'_> {}
280+
249281
/// Metadata
250282
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
251283
pub struct Metadata {

crates/nostr/src/nips/nip19.rs

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::str::FromStr;
1818

1919
use bech32::{self, Bech32, Hrp};
2020

21-
use super::nip01::Coordinate;
21+
use super::nip01::{Coordinate, CoordinateBorrow};
2222
use super::nip05::Nip05Profile;
2323
#[cfg(feature = "nip49")]
2424
use super::nip49::{self, EncryptedSecretKey};
@@ -647,22 +647,6 @@ impl FromBech32 for Nip19Profile {
647647
}
648648
}
649649

650-
impl FromBech32 for Coordinate {
651-
type Err = Error;
652-
653-
fn from_bech32(addr: &str) -> Result<Self, Self::Err> {
654-
let (hrp, data) = bech32::decode(addr)?;
655-
656-
if hrp != HRP_COORDINATE {
657-
return Err(Error::WrongPrefix);
658-
}
659-
660-
let coordinate = Nip19Coordinate::from_bech32_data(data)?;
661-
662-
Ok(coordinate.coordinate)
663-
}
664-
}
665-
666650
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
667651
pub struct Nip19Coordinate {
668652
pub coordinate: Coordinate,
@@ -761,37 +745,47 @@ impl FromBech32 for Nip19Coordinate {
761745
impl ToBech32 for Nip19Coordinate {
762746
type Err = Error;
763747

748+
#[inline]
764749
fn to_bech32(&self) -> Result<String, Self::Err> {
765-
// Allocate capacity
766-
let identifier_len: usize = 2 + self.identifier.len();
767-
let relays_len: usize = self.relays.iter().map(|u| 2 + u.as_str().len()).sum();
768-
let mut bytes: Vec<u8> = Vec::with_capacity(
769-
identifier_len + FIXED_1_1_32_BYTES_TVL + FIXED_KIND_BYTES_TVL + relays_len,
770-
);
750+
coordinate_to_bech32(self.coordinate.borrow(), &self.relays)
751+
}
752+
}
771753

772-
// Identifier
773-
bytes.push(SPECIAL); // Type
774-
bytes.push(self.identifier.len() as u8); // Len
775-
bytes.extend(self.identifier.as_bytes()); // Value
754+
pub(super) fn coordinate_to_bech32<'a>(
755+
coordinate: CoordinateBorrow<'a>,
756+
relays: &[RelayUrl],
757+
) -> Result<String, Error> {
758+
let identifier: &'a str = coordinate.identifier.unwrap_or_default();
776759

777-
// Author
778-
bytes.push(AUTHOR); // Type
779-
bytes.push(32); // Len
780-
bytes.extend(self.public_key.as_bytes()); // Value
760+
// Allocate capacity
761+
let identifier_len: usize = 2 + identifier.len();
762+
let relays_len: usize = relays.iter().map(|u| 2 + u.as_str().len()).sum();
763+
let mut bytes: Vec<u8> = Vec::with_capacity(
764+
identifier_len + FIXED_1_1_32_BYTES_TVL + FIXED_KIND_BYTES_TVL + relays_len,
765+
);
781766

782-
// Kind
783-
bytes.push(KIND); // Type
784-
bytes.push(4); // Len
785-
bytes.extend((self.kind.as_u16() as u32).to_be_bytes()); // Value
767+
// Identifier
768+
bytes.push(SPECIAL); // Type
769+
bytes.push(identifier.len() as u8); // Len
770+
bytes.extend(identifier.as_bytes()); // Value
786771

787-
for relay in self.relays.iter() {
788-
bytes.push(RELAY); // Type
789-
bytes.push(relay.as_str().len() as u8); // Len
790-
bytes.extend(relay.as_str().as_bytes()); // Value
791-
}
772+
// Author
773+
bytes.push(AUTHOR); // Type
774+
bytes.push(32); // Len
775+
bytes.extend(coordinate.public_key.as_bytes()); // Value
776+
777+
// Kind
778+
bytes.push(KIND); // Type
779+
bytes.push(4); // Len
780+
bytes.extend((coordinate.kind.as_u16() as u32).to_be_bytes()); // Value
792781

793-
Ok(bech32::encode::<Bech32>(HRP_COORDINATE, &bytes)?)
782+
for relay in relays.iter() {
783+
bytes.push(RELAY); // Type
784+
bytes.push(relay.as_str().len() as u8); // Len
785+
bytes.extend(relay.as_str().as_bytes()); // Value
794786
}
787+
788+
Ok(bech32::encode::<Bech32>(HRP_COORDINATE, &bytes)?)
795789
}
796790

797791
#[cfg(test)]

crates/nostr/src/nips/nip21.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use core::convert::Infallible;
1111
use core::fmt;
1212

1313
use super::nip19::{self, FromBech32, Nip19, Nip19Coordinate, Nip19Event, Nip19Profile, ToBech32};
14-
use crate::nips::nip01::Coordinate;
1514
use crate::{EventId, PublicKey};
1615

1716
/// URI scheme
@@ -114,7 +113,6 @@ impl ToNostrUri for Nip19Profile {}
114113
impl FromNostrUri for Nip19Profile {}
115114
impl ToNostrUri for Nip19Event {}
116115
impl FromNostrUri for Nip19Event {}
117-
impl FromNostrUri for Coordinate {}
118116
impl ToNostrUri for Nip19Coordinate {}
119117
impl FromNostrUri for Nip19Coordinate {}
120118

0 commit comments

Comments
 (0)