Skip to content

Commit df8dc4a

Browse files
committed
nostr: re-impl FromBech32 for Coordinate and impl ToBech32
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent a396ba4 commit df8dc4a

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

crates/nostr/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
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`
4748
- Implement `ToBech32` for `CoordinateBorrow`
4849
- Implement `ToNostrUri` for `CoordinateBorrow`
4950
- Implement `From<&Event>` for `Nip19Event`

crates/nostr/src/nips/nip01.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use serde::ser::{SerializeMap, Serializer};
1717
use serde::{Deserialize, Serialize};
1818
use serde_json::Value;
1919

20-
use super::nip19::{self, FromBech32, ToBech32};
20+
use super::nip19::{self, FromBech32, Nip19Coordinate, ToBech32};
2121
use super::nip21::{FromNostrUri, ToNostrUri};
2222
use crate::types::Url;
2323
use crate::{key, Filter, JsonUtil, Kind, PublicKey, Tag};
@@ -222,6 +222,24 @@ 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+
225243
/// Borrowed coordinate
226244
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
227245
pub struct CoordinateBorrow<'a> {

crates/nostr/src/nips/nip19.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

0 commit comments

Comments
 (0)