Skip to content

Commit bb31377

Browse files
TheAwitebyukibtc
authored andcommitted
nostr: use Infallible error for bare entities bech32 encoding
Change the `Err` type of `ToBech32` to `Infallible` for `SecretKey`, `PublicKey` and `EventId`. They are always less than `1023`, so will never be returned an error. Pull-Request: #786 Acked-by: Yuki Kishimoto <[email protected]> Signed-off-by: Awiteb <[email protected]> Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent db4a241 commit bb31377

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
### Breaking changes
3333

3434
* nostr: update `Nip19Event` relays field type from `Vec<String>` to `Vec<RelayUrl>` ([Yuki Kishimoto])
35+
* nostr: change the `Err` type of `ToBech32` to `Infallible` for `SecretKey`, `PublicKey` and `EventId` ([awiteb])
3536
* nostr: update `Tags::new` signature ([Yuki Kishimoto])
3637
* nostr: remove `WeakTag` ([Yuki Kishimoto])
3738
* nostr: change `TagStandard::Relays` variant inner value from `Vec<Url>` to `Vec<RelayUrl>` ([Yuki Kishimoto])

crates/nostr/src/nips/nip19.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use alloc::borrow::Cow;
1212
use alloc::string::{String, ToString};
1313
use alloc::vec::Vec;
14+
use core::convert::Infallible;
1415
use core::fmt;
1516
use core::ops::Deref;
1617
use core::str::FromStr;
@@ -131,6 +132,12 @@ impl From<event::Error> for Error {
131132
}
132133
}
133134

135+
impl From<Infallible> for Error {
136+
fn from(_: Infallible) -> Self {
137+
unreachable!()
138+
}
139+
}
140+
134141
#[cfg(feature = "nip49")]
135142
impl From<nip49::Error> for Error {
136143
fn from(e: nip49::Error) -> Self {
@@ -264,13 +271,13 @@ impl ToBech32 for Nip19 {
264271

265272
fn to_bech32(&self) -> Result<String, Self::Err> {
266273
match self {
267-
Nip19::Secret(sec) => sec.to_bech32(),
274+
Nip19::Secret(sec) => Ok(sec.to_bech32()?),
268275
#[cfg(feature = "nip49")]
269276
Nip19::EncryptedSecret(cryptsec) => cryptsec.to_bech32(),
270-
Nip19::Pubkey(pubkey) => pubkey.to_bech32(),
277+
Nip19::Pubkey(pubkey) => Ok(pubkey.to_bech32()?),
271278
Nip19::Event(event) => event.to_bech32(),
272279
Nip19::Profile(profile) => profile.to_bech32(),
273-
Nip19::EventId(event_id) => event_id.to_bech32(),
280+
Nip19::EventId(event_id) => Ok(event_id.to_bech32()?),
274281
Nip19::Coordinate(coordinate) => coordinate.to_bech32(),
275282
}
276283
}
@@ -291,13 +298,13 @@ impl FromBech32 for SecretKey {
291298
}
292299

293300
impl ToBech32 for SecretKey {
294-
type Err = Error;
301+
type Err = Infallible;
295302

296303
fn to_bech32(&self) -> Result<String, Self::Err> {
297-
Ok(bech32::encode::<Bech32>(
298-
HRP_SECRET_KEY,
299-
self.as_secret_bytes(),
300-
)?)
304+
Ok(
305+
bech32::encode::<Bech32>(HRP_SECRET_KEY, self.as_secret_bytes())
306+
.expect("Less than 1023"),
307+
)
301308
}
302309
}
303310

@@ -343,10 +350,10 @@ impl FromBech32 for PublicKey {
343350
}
344351

345352
impl ToBech32 for PublicKey {
346-
type Err = Error;
353+
type Err = Infallible;
347354

348355
fn to_bech32(&self) -> Result<String, Self::Err> {
349-
Ok(bech32::encode::<Bech32>(HRP_PUBLIC_KEY, self.as_bytes())?)
356+
Ok(bech32::encode::<Bech32>(HRP_PUBLIC_KEY, self.as_bytes()).expect("Less than 1023"))
350357
}
351358
}
352359

@@ -365,10 +372,10 @@ impl FromBech32 for EventId {
365372
}
366373

367374
impl ToBech32 for EventId {
368-
type Err = Error;
375+
type Err = Infallible;
369376

370377
fn to_bech32(&self) -> Result<String, Self::Err> {
371-
Ok(bech32::encode::<Bech32>(HRP_NOTE_ID, self.as_bytes())?)
378+
Ok(bech32::encode::<Bech32>(HRP_NOTE_ID, self.as_bytes()).expect("Less than 1023"))
372379
}
373380
}
374381

crates/nostr/src/nips/nip21.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use alloc::string::String;
1010
use alloc::vec::Vec;
11+
use core::convert::Infallible;
1112
use core::fmt;
1213
use core::str::FromStr;
1314

@@ -68,6 +69,12 @@ impl From<nip19::Error> for Error {
6869
}
6970
}
7071

72+
impl From<Infallible> for Error {
73+
fn from(_: Infallible) -> Self {
74+
unreachable!()
75+
}
76+
}
77+
7178
fn split_uri(uri: &str) -> Result<&str, Error> {
7279
let mut splitted = uri.split(':');
7380
let prefix: &str = splitted.next().ok_or(Error::InvalidURI)?;

0 commit comments

Comments
 (0)