From d889f9b25d14b0c0d9011b11390613758dfa2a45 Mon Sep 17 00:00:00 2001 From: reya <123083837+reyamir@users.noreply.github.com> Date: Sun, 28 Sep 2025 07:57:04 +0700 Subject: [PATCH] chore: always call get_public_key on nip46 (#171) * get public key on login * . --- Cargo.lock | 44 ++++++++-------- crates/coop/src/views/login.rs | 80 ++++++++++++++--------------- crates/coop/src/views/onboarding.rs | 57 ++++++++++---------- 3 files changed, 88 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19afb16..a6acc44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1129,7 +1129,7 @@ dependencies = [ [[package]] name = "collections" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "indexmap", "rustc-hash 2.1.1", @@ -1570,7 +1570,7 @@ dependencies = [ [[package]] name = "derive_refineable" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "proc-macro2", "quote", @@ -2506,7 +2506,7 @@ dependencies = [ [[package]] name = "gpui" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "as-raw-xcb-connection", @@ -2600,7 +2600,7 @@ dependencies = [ [[package]] name = "gpui_macros" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -2612,7 +2612,7 @@ dependencies = [ [[package]] name = "gpui_tokio" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "gpui", @@ -2832,7 +2832,7 @@ dependencies = [ [[package]] name = "http_client" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "bytes", @@ -2852,7 +2852,7 @@ dependencies = [ [[package]] name = "http_client_tls" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "rustls", "rustls-platform-verifier", @@ -3657,7 +3657,7 @@ dependencies = [ [[package]] name = "media" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "bindgen 0.71.1", @@ -4500,7 +4500,7 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "perf" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "collections", "serde", @@ -5108,7 +5108,7 @@ dependencies = [ [[package]] name = "refineable" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "derive_refineable", "workspace-hack", @@ -5262,7 +5262,7 @@ dependencies = [ [[package]] name = "reqwest_client" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "bytes", @@ -5317,7 +5317,7 @@ dependencies = [ [[package]] name = "rope" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "arrayvec", "log", @@ -5812,7 +5812,7 @@ checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749" [[package]] name = "semantic_version" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "serde", @@ -5827,9 +5827,9 @@ checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" [[package]] name = "serde" -version = "1.0.227" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80ece43fc6fbed4eb5392ab50c07334d3e577cbf40997ee896fe7af40bba4245" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", "serde_derive", @@ -5837,18 +5837,18 @@ dependencies = [ [[package]] name = "serde_core" -version = "1.0.227" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a576275b607a2c86ea29e410193df32bc680303c82f31e275bbfcafe8b33be5" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.227" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51e694923b8824cf0e9b382adf0f60d4e05f348f357b38833a3fa5ed7c2ede04" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", @@ -6264,7 +6264,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sum_tree" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "arrayvec", "log", @@ -7310,7 +7310,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] name = "util" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "anyhow", "async-fs", @@ -7345,7 +7345,7 @@ dependencies = [ [[package]] name = "util_macros" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#ead38fd1bec5aa5db2ed8462cb804fe525963bca" +source = "git+https://github.com/zed-industries/zed#78098f6809346ba8b45db7ffa768fba75578a263" dependencies = [ "perf", "quote", diff --git a/crates/coop/src/views/login.rs b/crates/coop/src/views/login.rs index f330f99..0477123 100644 --- a/crates/coop/src/views/login.rs +++ b/crates/coop/src/views/login.rs @@ -6,7 +6,8 @@ use global::nostr_client; use gpui::prelude::FluentBuilder; use gpui::{ div, relative, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, - Focusable, IntoElement, ParentElement, Render, SharedString, Styled, Subscription, Window, + Focusable, IntoElement, ParentElement, Render, SharedString, Styled, Subscription, Task, + Window, }; use i18n::{shared_t, t}; use nostr_connect::prelude::*; @@ -292,30 +293,22 @@ impl Login { // Handle connection cx.spawn_in(window, async move |this, cx| { - let client = nostr_client(); - match signer.bunker_uri().await { Ok(uri) => { this.update(cx, |this, cx| { - this.write_uri_to_disk(&uri, cx); + this.write_uri_to_disk(signer, uri, cx); }) .ok(); - - // Set the client's signer with the current nostr connect instance - client.set_signer(signer).await; } Err(error) => { - cx.update(|window, cx| { - this.update(cx, |this, cx| { - this.set_error(error.to_string(), window, cx); - // Force reset the client keys - // - // This step is necessary to ensure that user can retry the connection - client_keys.update(cx, |this, cx| { - this.force_new_keys(cx); - }); - }) - .ok(); + this.update_in(cx, |this, window, cx| { + this.set_error(error.to_string(), window, cx); + // Force reset the client keys + // + // This step is necessary to ensure that user can retry the connection + client_keys.update(cx, |this, cx| { + this.force_new_keys(cx); + }); }) .ok(); } @@ -324,38 +317,41 @@ impl Login { .detach(); } - fn write_uri_to_disk(&mut self, uri: &NostrConnectURI, cx: &mut Context) { - let Some(public_key) = uri.remote_signer_public_key().cloned() else { - log::error!("Remote Signer's public key not found"); - return; - }; + fn write_uri_to_disk( + &mut self, + signer: NostrConnect, + uri: NostrConnectURI, + cx: &mut Context, + ) { + let mut uri_without_secret = uri.to_string(); - let mut value = uri.to_string(); - - // Clear the secret param if it exists + // Clear the secret parameter in the URI if it exists if let Some(secret) = uri.secret() { - value = value.replace(secret, ""); + uri_without_secret = uri_without_secret.replace(secret, ""); } - cx.background_spawn(async move { + let task: Task> = cx.background_spawn(async move { let client = nostr_client(); - let keys = Keys::generate(); - let tags = vec![Tag::identifier(ACCOUNT_IDENTIFIER)]; - let kind = Kind::ApplicationSpecificData; - let builder = EventBuilder::new(kind, value) - .tags(tags) + // Update the client's signer + client.set_signer(signer).await; + + let signer = client.signer().await?; + let public_key = signer.get_public_key().await?; + + let event = EventBuilder::new(Kind::ApplicationSpecificData, uri_without_secret) + .tags(vec![Tag::identifier(ACCOUNT_IDENTIFIER)]) .build(public_key) - .sign(&keys) - .await; + .sign(&Keys::generate()) + .await?; - if let Ok(event) = builder { - if let Err(e) = client.database().save_event(&event).await { - log::error!("Failed to save event: {e}"); - }; - } - }) - .detach(); + // Save the event to the database + client.database().save_event(&event).await?; + + Ok(()) + }); + + task.detach(); } pub fn write_keys_to_disk(&self, keys: &Keys, password: String, cx: &mut Context) { diff --git a/crates/coop/src/views/onboarding.rs b/crates/coop/src/views/onboarding.rs index 2b1b776..bbe17f6 100644 --- a/crates/coop/src/views/onboarding.rs +++ b/crates/coop/src/views/onboarding.rs @@ -135,7 +135,6 @@ impl Onboarding { self._tasks.push( // Wait for Nostr Connect approval cx.spawn_in(window, async move |this, cx| { - let client = nostr_client(); let connect = this.read_with(cx, |this, cx| this.nostr_connect.read(cx).clone()); if let Ok(Some(signer)) = connect { @@ -143,12 +142,9 @@ impl Onboarding { Ok(uri) => { this.update(cx, |this, cx| { this.set_connecting(cx); - this.write_uri_to_disk(&uri, cx); + this.write_uri_to_disk(signer, uri, cx); }) .ok(); - - // Set the client's signer with the current nostr connect instance - client.set_signer(signer).await; } Err(e) => { this.update_in(cx, |_, window, cx| { @@ -169,38 +165,41 @@ impl Onboarding { ChatSpace::proxy_signer(window, cx); } - fn write_uri_to_disk(&mut self, uri: &NostrConnectURI, cx: &mut Context) { - let Some(public_key) = uri.remote_signer_public_key().cloned() else { - log::error!("Remote Signer's public key not found"); - return; - }; + fn write_uri_to_disk( + &mut self, + signer: NostrConnect, + uri: NostrConnectURI, + cx: &mut Context, + ) { + let mut uri_without_secret = uri.to_string(); - let mut value = uri.to_string(); - - // Clear the secret param if it exists + // Clear the secret parameter in the URI if it exists if let Some(secret) = uri.secret() { - value = value.replace(secret, ""); + uri_without_secret = uri_without_secret.replace(secret, ""); } - cx.background_spawn(async move { + let task: Task> = cx.background_spawn(async move { let client = nostr_client(); - let keys = Keys::generate(); - let tags = vec![Tag::identifier(ACCOUNT_IDENTIFIER)]; - let kind = Kind::ApplicationSpecificData; - let builder = EventBuilder::new(kind, value) - .tags(tags) + // Update the client's signer + client.set_signer(signer).await; + + let signer = client.signer().await?; + let public_key = signer.get_public_key().await?; + + let event = EventBuilder::new(Kind::ApplicationSpecificData, uri_without_secret) + .tags(vec![Tag::identifier(ACCOUNT_IDENTIFIER)]) .build(public_key) - .sign(&keys) - .await; + .sign(&Keys::generate()) + .await?; - if let Ok(event) = builder { - if let Err(e) = client.database().save_event(&event).await { - log::error!("Failed to save event: {e}"); - }; - } - }) - .detach(); + // Save the event to the database + client.database().save_event(&event).await?; + + Ok(()) + }); + + task.detach(); } fn copy_uri(&mut self, window: &mut Window, cx: &mut Context) {