Skip to content

Commit b292a61

Browse files
committed
nostr: impl ToBech32 for CoordinateBorrow
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 16345d5 commit b292a61

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
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 `CoordinateBorrow`
4748
- Implement `From<&Event>` for `Nip19Event`
4849
- Add `CowTag::kind` and `CowTag::content`
4950
- Add `Timestamp::as_secs`

crates/nostr/src/nips/nip01.rs

Lines changed: 10 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::FromBech32;
20+
use super::nip19::{self, FromBech32, ToBech32};
2121
use super::nip21::FromNostrUri;
2222
use crate::types::Url;
2323
use crate::{key, Filter, JsonUtil, Kind, PublicKey, Tag};
@@ -246,6 +246,15 @@ impl CoordinateBorrow<'_> {
246246
}
247247
}
248248

249+
impl ToBech32 for CoordinateBorrow<'_> {
250+
type Err = nip19::Error;
251+
252+
#[inline]
253+
fn to_bech32(&self) -> Result<String, Self::Err> {
254+
nip19::coordinate_to_bech32(*self, &[])
255+
}
256+
}
257+
249258
/// Metadata
250259
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
251260
pub struct Metadata {

crates/nostr/src/nips/nip19.rs

Lines changed: 35 additions & 25 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};
@@ -761,37 +761,47 @@ impl FromBech32 for Nip19Coordinate {
761761
impl ToBech32 for Nip19Coordinate {
762762
type Err = Error;
763763

764+
#[inline]
764765
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-
);
766+
coordinate_to_bech32(self.coordinate.borrow(), &self.relays)
767+
}
768+
}
771769

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

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

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
783+
// Identifier
784+
bytes.push(SPECIAL); // Type
785+
bytes.push(identifier.len() as u8); // Len
786+
bytes.extend(identifier.as_bytes()); // Value
786787

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-
}
788+
// Author
789+
bytes.push(AUTHOR); // Type
790+
bytes.push(32); // Len
791+
bytes.extend(coordinate.public_key.as_bytes()); // Value
792+
793+
// Kind
794+
bytes.push(KIND); // Type
795+
bytes.push(4); // Len
796+
bytes.extend((coordinate.kind.as_u16() as u32).to_be_bytes()); // Value
792797

793-
Ok(bech32::encode::<Bech32>(HRP_COORDINATE, &bytes)?)
798+
for relay in relays.iter() {
799+
bytes.push(RELAY); // Type
800+
bytes.push(relay.as_str().len() as u8); // Len
801+
bytes.extend(relay.as_str().as_bytes()); // Value
794802
}
803+
804+
Ok(bech32::encode::<Bech32>(HRP_COORDINATE, &bytes)?)
795805
}
796806

797807
#[cfg(test)]

0 commit comments

Comments
 (0)