Skip to content

Commit

Permalink
nostr: improve NostrWalletConnectURI parsing
Browse files Browse the repository at this point in the history
* Avoid unnecessary allocation

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Aug 24, 2024
1 parent b646536 commit 2f230ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
24 changes: 10 additions & 14 deletions crates/nostr/src/nips/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Url> = None;
let mut secret: Option<SecretKey> = None;
Expand All @@ -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());
Expand All @@ -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,
});
}
}

Expand Down

0 comments on commit 2f230ce

Please sign in to comment.