What is Nostr?
DanConwayDev
npub15qy…yejr
2024-11-13 12:50:22
in reply to nevent1q…hukv

DanConwayDev on Nostr: thanks. I've suggested so `bootstrap` shouldn't fail after `set_user_public_key` is ...

thanks. I've suggested so `bootstrap` shouldn't fail after `set_user_public_key` is called.
From 151f0abb89e6ded8fa60557ca9d3c2469a68ce47 Mon Sep 17 00:00:00 2001
From: DanConwayDev
Date: Wed, 13 Nov 2024 12:34:21 +0000
Subject: [PATCH] connect: prevent err after `set_user_public_key`

prevent bootstrap from resulting in an error when triggered after
`set_user_public_key` is called. eg.
```
let signer = NostrConnect::new(...);
signer.set_user_public_key(pk);
// first usage of signer will trigger bootstrap eg:
signer.sign_event(e);
```

throw a error during bootstrap if the remote signer public key is
different to the value given to `set_user_public_key`. this would
happen in the unlikely event that the remote signer was reconfigured
to use the same app keys with a user key
---
crates/nostr-connect/src/client.rs | 8 +++++++-
crates/nostr-connect/src/error.rs | 3 +++
2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/crates/nostr-connect/src/client.rs b/crates/nostr-connect/src/client.rs
index 6bae456..c0dfffb 100644
--- a/crates/nostr-connect/src/client.rs
+++ b/crates/nostr-connect/src/client.rs
@@ -127,7 +127,13 @@ impl NostrConnect {
GetRemoteSignerPublicKey::RemoteOnly(public_key) => public_key,
GetRemoteSignerPublicKey::WithUserPublicKey { remote, user } => {
// Set user public key
- self.user_public_key.set(user)?; // This shouldn't fails
+ if let Some(stored_user_public_key) = self.user_public_key.get() {
+ if !user.eq(stored_user_public_key) {
+ return Err(Error::UserPublicKeyChanged(stored_user_public_key, user))
+ }
+ } else {
+ self.user_public_key.set(user)?; // This shouldn't fails
+ }

// Return remote signer public key
remote
diff --git a/crates/nostr-connect/src/error.rs b/crates/nostr-connect/src/error.rs
index 8a63543..8c6f2ba 100644
--- a/crates/nostr-connect/src/error.rs
+++ b/crates/nostr-connect/src/error.rs
@@ -37,6 +37,9 @@ pub enum Error {
/// Set user public key error
#[error(transparent)]
SetUserPublicKey(#[from] SetError),
+ /// User public key has changed
+ #[error("user public key changed from: {0} to: {1}")]
+ UserPublicKeyChanged(PublicKey, PublicKey),
/// NIP46 response error
#[error("response error: {0}")]
Response(String),
--
libgit2 1.8.1

Author Public Key
npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr