Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/nostr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
- Add `a` tag of replaceable and addressable events in `EventBuilder::repost` (https://github.com/rust-nostr/nostr/pull/1184)
- Handle legacy events with `mention` marker (https://github.com/rust-nostr/nostr/pull/1193)
- Parse "HEAD" as `TagKing::Head` (https://github.com/rust-nostr/nostr/pull/1215)
- Optimize NIP-21 URI parsing in `PublicKey::parse` (https://github.com/rust-nostr/nostr/pull/1308)

## v0.44.2 - 2025/12/04

Expand Down
27 changes: 10 additions & 17 deletions crates/nostr/src/key/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use secp256k1::{Secp256k1, Signing, XOnlyPublicKey};
use serde::{Deserialize, Deserializer, Serialize};

use super::{Error, SecretKey};
use crate::nips::nip19::FromBech32;
use crate::nips::nip21::FromNostrUri;
use crate::nips::nip19::{FromBech32, PREFIX_BECH32_PROFILE, PREFIX_BECH32_PUBLIC_KEY};
use crate::nips::nip21::{FromNostrUri, SCHEME_WITH_COLON};

/// Public Key
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -81,22 +81,15 @@ impl PublicKey {

/// Parse from `hex`, `bech32` or [NIP21](https://github.com/nostr-protocol/nips/blob/master/21.md) uri
pub fn parse(public_key: &str) -> Result<Self, Error> {
// Try from hex
if let Ok(public_key) = Self::from_hex(public_key) {
return Ok(public_key);
if public_key.starts_with(PREFIX_BECH32_PUBLIC_KEY)
|| public_key.starts_with(PREFIX_BECH32_PROFILE)
{
Self::from_bech32(public_key).map_err(|_| Error::InvalidPublicKey)
} else if public_key.starts_with(SCHEME_WITH_COLON) {
Self::from_nostr_uri(public_key).map_err(|_| Error::InvalidPublicKey)
} else {
Self::from_hex(public_key).map_err(|_| Error::InvalidPublicKey)
}

// Try from bech32
if let Ok(public_key) = Self::from_bech32(public_key) {
return Ok(public_key);
}

// Try from NIP21 URI
if let Ok(public_key) = Self::from_nostr_uri(public_key) {
return Ok(public_key);
}

Err(Error::InvalidPublicKey)
}

/// Parse from hex string
Expand Down
Loading