From 2f230ce805e219a5ce888771a0877dfb03cf0021 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Sat, 24 Aug 2024 19:52:53 -0400 Subject: [PATCH] nostr: improve `NostrWalletConnectURI` parsing * Avoid unnecessary allocation Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 1 + crates/nostr/src/nips/nip47.rs | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76c014810..6fd211e5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ * nostr: change `Keys::secret_key` and `Keys::sign_schnorr` methods fingerprint ([Yuki Kishimoto]) * nostr: deprecate `Keys::generate_without_keypair` ([Yuki Kishimoto]) * nostr: change NIP-26 functions fingerprint ([Yuki Kishimoto]) +* nostr: improve `NostrWalletConnectURI` parsing ([Yuki Kishimoto]) * database: update `NostrDatabase` supertraits ([Yuki Kishimoto]) * signer: update NIP-04 and NIP-44 methods signature ([Yuki Kishimoto]) * webln: bump `webln` to `v0.3` ([Yuki Kishimoto]) diff --git a/crates/nostr/src/nips/nip47.rs b/crates/nostr/src/nips/nip47.rs index 8c6f159a7..815e03db9 100644 --- a/crates/nostr/src/nips/nip47.rs +++ b/crates/nostr/src/nips/nip47.rs @@ -925,7 +925,7 @@ impl FromStr for NostrWalletConnectURI { } if let Some(pubkey) = url.domain() { - let public_key = PublicKey::from_str(pubkey)?; + let public_key = PublicKey::from_hex(pubkey)?; let mut relay_url: Option = None; let mut secret: Option = None; @@ -934,12 +934,10 @@ impl FromStr for NostrWalletConnectURI { for (key, value) in url.query_pairs() { match key { Cow::Borrowed("relay") => { - let value = value.to_string(); - relay_url = Some(Url::parse(&value)?); + relay_url = Some(Url::parse(value.as_ref())?); } Cow::Borrowed("secret") => { - let value = value.to_string(); - secret = Some(SecretKey::from_str(&value)?); + secret = Some(SecretKey::from_hex(value.as_ref())?); } Cow::Borrowed("lud16") => { lud16 = Some(value.to_string()); @@ -948,15 +946,13 @@ impl FromStr for NostrWalletConnectURI { } } - if let Some(relay_url) = relay_url { - if let Some(secret) = secret { - return Ok(Self { - public_key, - relay_url, - secret, - lud16, - }); - } + if let (Some(relay_url), Some(secret)) = (relay_url, secret) { + return Ok(Self { + public_key, + relay_url, + secret, + lud16, + }); } }