chore: fix nip4e implementation (#196)

* push

* debug

* disable verify sender

* .
This commit is contained in:
reya
2025-10-28 20:35:34 +07:00
committed by GitHub
parent b9297d3a01
commit b0fa98831d
4 changed files with 114 additions and 111 deletions

116
Cargo.lock generated
View File

@@ -82,6 +82,15 @@ version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "ar_archive_writer"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c269894b6fe5e9d7ada0cf69b5bf847ff35bc25fc271f08e1d080fce80339a"
dependencies = [
"object",
]
[[package]] [[package]]
name = "arbitrary" name = "arbitrary"
version = "1.4.2" version = "1.4.2"
@@ -1157,7 +1166,7 @@ dependencies = [
[[package]] [[package]]
name = "collections" name = "collections"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
@@ -1597,7 +1606,7 @@ dependencies = [
[[package]] [[package]]
name = "derive_refineable" name = "derive_refineable"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2494,7 +2503,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui" name = "gpui"
version = "0.2.2" version = "0.2.2"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"as-raw-xcb-connection", "as-raw-xcb-connection",
@@ -2589,7 +2598,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui_macros" name = "gpui_macros"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
@@ -2600,7 +2609,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui_tokio" name = "gpui_tokio"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"gpui", "gpui",
@@ -2829,7 +2838,7 @@ dependencies = [
[[package]] [[package]]
name = "http_client" name = "http_client"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-compression", "async-compression",
@@ -2854,7 +2863,7 @@ dependencies = [
[[package]] [[package]]
name = "http_client_tls" name = "http_client_tls"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"rustls", "rustls",
"rustls-platform-verifier", "rustls-platform-verifier",
@@ -3333,9 +3342,9 @@ dependencies = [
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.81" version = "0.3.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305" checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"wasm-bindgen", "wasm-bindgen",
@@ -3672,7 +3681,7 @@ dependencies = [
[[package]] [[package]]
name = "media" name = "media"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bindgen 0.71.1", "bindgen 0.71.1",
@@ -3789,9 +3798,9 @@ dependencies = [
[[package]] [[package]]
name = "moxcms" name = "moxcms"
version = "0.7.8" version = "0.7.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692af879e4d9383c0fd9dec15524af6b6977c8bf1c6b278a4526d5341347c574" checksum = "0fbdd3d7436f8b5e892b8b7ea114271ff0fa00bc5acae845d53b07d498616ef6"
dependencies = [ dependencies = [
"num-traits", "num-traits",
"pxfm", "pxfm",
@@ -3928,7 +3937,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr" name = "nostr"
version = "0.43.0" version = "0.43.0"
source = "git+https://github.com/rust-nostr/nostr#806c46e33b49b35cf93d4d892999384f554ecd98" source = "git+https://github.com/rust-nostr/nostr#4e08f80aa578c7f65802d1c307252a5be9aef8a7"
dependencies = [ dependencies = [
"aes", "aes",
"base64", "base64",
@@ -3952,7 +3961,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-connect" name = "nostr-connect"
version = "0.43.0" version = "0.43.0"
source = "git+https://github.com/rust-nostr/nostr#806c46e33b49b35cf93d4d892999384f554ecd98" source = "git+https://github.com/rust-nostr/nostr#4e08f80aa578c7f65802d1c307252a5be9aef8a7"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"nostr", "nostr",
@@ -3964,7 +3973,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-database" name = "nostr-database"
version = "0.43.0" version = "0.43.0"
source = "git+https://github.com/rust-nostr/nostr#806c46e33b49b35cf93d4d892999384f554ecd98" source = "git+https://github.com/rust-nostr/nostr#4e08f80aa578c7f65802d1c307252a5be9aef8a7"
dependencies = [ dependencies = [
"flatbuffers", "flatbuffers",
"lru", "lru",
@@ -3975,7 +3984,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-lmdb" name = "nostr-lmdb"
version = "0.43.0" version = "0.43.0"
source = "git+https://github.com/rust-nostr/nostr#806c46e33b49b35cf93d4d892999384f554ecd98" source = "git+https://github.com/rust-nostr/nostr#4e08f80aa578c7f65802d1c307252a5be9aef8a7"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"flume", "flume",
@@ -3989,7 +3998,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-relay-pool" name = "nostr-relay-pool"
version = "0.43.0" version = "0.43.0"
source = "git+https://github.com/rust-nostr/nostr#806c46e33b49b35cf93d4d892999384f554ecd98" source = "git+https://github.com/rust-nostr/nostr#4e08f80aa578c7f65802d1c307252a5be9aef8a7"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"async-wsocket", "async-wsocket",
@@ -4006,7 +4015,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-sdk" name = "nostr-sdk"
version = "0.43.0" version = "0.43.0"
source = "git+https://github.com/rust-nostr/nostr#806c46e33b49b35cf93d4d892999384f554ecd98" source = "git+https://github.com/rust-nostr/nostr#4e08f80aa578c7f65802d1c307252a5be9aef8a7"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"nostr", "nostr",
@@ -4280,6 +4289,15 @@ dependencies = [
"objc", "objc",
] ]
[[package]]
name = "object"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.3" version = "1.21.3"
@@ -4514,7 +4532,7 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]] [[package]]
name = "perf" name = "perf"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"collections", "collections",
"serde", "serde",
@@ -4787,10 +4805,11 @@ dependencies = [
[[package]] [[package]]
name = "psm" name = "psm"
version = "0.1.27" version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e66fcd288453b748497d8fb18bccc83a16b0518e3906d4b8df0a8d42d93dbb1c" checksum = "d11f2fedc3b7dafdc2851bc52f277377c5473d378859be234bc7ebb593144d01"
dependencies = [ dependencies = [
"ar_archive_writer",
"cc", "cc",
] ]
@@ -5130,7 +5149,7 @@ dependencies = [
[[package]] [[package]]
name = "refineable" name = "refineable"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"derive_refineable", "derive_refineable",
] ]
@@ -5237,7 +5256,7 @@ dependencies = [
[[package]] [[package]]
name = "reqwest_client" name = "reqwest_client"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
@@ -5291,12 +5310,11 @@ dependencies = [
[[package]] [[package]]
name = "rope" name = "rope"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"log", "log",
"rayon", "rayon",
"regex",
"sum_tree", "sum_tree",
"unicode-segmentation", "unicode-segmentation",
"util", "util",
@@ -5758,7 +5776,7 @@ checksum = "16c2f82143577edb4921b71ede051dac62ca3c16084e918bf7b40c96ae10eb33"
[[package]] [[package]]
name = "semantic_version" name = "semantic_version"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"serde", "serde",
@@ -6204,7 +6222,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "sum_tree" name = "sum_tree"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"log", "log",
@@ -7240,7 +7258,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]] [[package]]
name = "util" name = "util"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-fs", "async-fs",
@@ -7275,7 +7293,7 @@ dependencies = [
[[package]] [[package]]
name = "util_macros" name = "util_macros"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#f503c65924fff9799b359b6c83994b2eaf7af7c8" source = "git+https://github.com/zed-industries/zed#8a01e483396eec940e9af72ce8f900bb98381a4f"
dependencies = [ dependencies = [
"perf", "perf",
"quote", "quote",
@@ -7434,9 +7452,9 @@ checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.104" version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d" checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"once_cell", "once_cell",
@@ -7445,25 +7463,11 @@ dependencies = [
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn 2.0.108",
"wasm-bindgen-shared",
]
[[package]] [[package]]
name = "wasm-bindgen-futures" name = "wasm-bindgen-futures"
version = "0.4.54" version = "0.4.55"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e038d41e478cc73bae0ff9b36c60cff1c98b8f38f8d7e8061e79ee63608ac5c" checksum = "551f88106c6d5e7ccc7cd9a16f312dd3b5d36ea8b4954304657d5dfba115d4a0"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"js-sys", "js-sys",
@@ -7474,9 +7478,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.104" version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119" checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@@ -7484,22 +7488,22 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.104" version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7" checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc"
dependencies = [ dependencies = [
"bumpalo",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.108", "syn 2.0.108",
"wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.104" version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1" checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@@ -7616,9 +7620,9 @@ dependencies = [
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.81" version = "0.3.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9367c417a924a74cae129e6a2ae3b47fabb1f8995595ab474029da749a8be120" checksum = "3a1f95c0d03a47f4ae1f7a64643a6bb97465d9b740f0fa8f90ea33915c99a9a1"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",

View File

@@ -549,9 +549,10 @@ impl Room {
let receiver = Self::select_receiver(&signer_kind, member, encryption)?; let receiver = Self::select_receiver(&signer_kind, member, encryption)?;
let rumor = rumor.clone(); let rumor = rumor.clone();
let tags = vec![Tag::public_key(member)];
// Construct the gift wrap event // Construct the gift wrap event
let event = EventBuilder::gift_wrap(&signer, &receiver, rumor, []).await?; let event = EventBuilder::gift_wrap(&signer, &receiver, rumor, tags).await?;
// Send the gift wrap event to the messaging relays // Send the gift wrap event to the messaging relays
match client.send_event_to(urls, &event).await { match client.send_event_to(urls, &event).await {
@@ -593,9 +594,10 @@ impl Room {
let receiver = Self::select_receiver(&signer_kind, user_pubkey, encryption_pubkey)?; let receiver = Self::select_receiver(&signer_kind, user_pubkey, encryption_pubkey)?;
let rumor = rumor.clone(); let rumor = rumor.clone();
let tags = vec![Tag::public_key(user_pubkey)];
// Construct the gift-wrapped event // Construct the gift-wrapped event
let event = EventBuilder::gift_wrap(&signer, &receiver, rumor, []).await?; let event = EventBuilder::gift_wrap(&signer, &receiver, rumor, tags).await?;
// Only send a backup message to current user if sent successfully to others // Only send a backup message to current user if sent successfully to others
if opts.backup() && reports.iter().all(|r| r.is_sent_success()) { if opts.backup() && reports.iter().all(|r| r.is_sent_success()) {

View File

@@ -4,6 +4,7 @@ pub const APP_PUBKEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZX
pub const APP_UPDATER_ENDPOINT: &str = "https://coop-updater.reya.su/"; pub const APP_UPDATER_ENDPOINT: &str = "https://coop-updater.reya.su/";
pub const SETTINGS_IDENTIFIER: &str = "coop:settings"; pub const SETTINGS_IDENTIFIER: &str = "coop:settings";
pub const INBOX_SUB_ID: &str = "inbox";
/// Bootstrap Relays. /// Bootstrap Relays.
pub const BOOTSTRAP_RELAYS: [&str; 5] = [ pub const BOOTSTRAP_RELAYS: [&str; 5] = [

View File

@@ -10,13 +10,13 @@ use nostr_lmdb::NostrLMDB;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use smol::lock::RwLock; use smol::lock::RwLock;
use crate::app_name;
use crate::constants::{ use crate::constants::{
BOOTSTRAP_RELAYS, METADATA_BATCH_LIMIT, METADATA_BATCH_TIMEOUT, QUERY_TIMEOUT, SEARCH_RELAYS, BOOTSTRAP_RELAYS, METADATA_BATCH_LIMIT, METADATA_BATCH_TIMEOUT, QUERY_TIMEOUT, SEARCH_RELAYS,
}; };
use crate::paths::config_dir; use crate::paths::config_dir;
use crate::state::ingester::Ingester; use crate::state::ingester::Ingester;
use crate::state::tracker::EventTracker; use crate::state::tracker::EventTracker;
use crate::{app_name, INBOX_SUB_ID};
mod device; mod device;
mod ingester; mod ingester;
@@ -345,7 +345,7 @@ impl AppState {
} }
} }
RelayMessage::EndOfStoredEvents(subscription_id) => { RelayMessage::EndOfStoredEvents(subscription_id) => {
if subscription_id.as_ref() == &SubscriptionId::new("inbox") { if subscription_id.as_ref() == &SubscriptionId::new(INBOX_SUB_ID) {
self.signal self.signal
.send(SignalKind::GiftWrapStatus(UnwrappingStatus::Processing)) .send(SignalKind::GiftWrapStatus(UnwrappingStatus::Processing))
.await; .await;
@@ -687,8 +687,8 @@ impl AppState {
// Send the announcement event to the relays // Send the announcement event to the relays
self.client.send_event(&event).await?; self.client.send_event(&event).await?;
// Re-subscribes to gift wrap events // Resubscribe to gift wrap events that include the encryption public key
self.resubscribe_messages().await?; self.get_messages_with_encryption(public_key).await?;
Ok(()) Ok(())
} }
@@ -698,14 +698,16 @@ impl AppState {
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md /// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
pub async fn load_encryption_keys(&self, announcement: &Announcement) -> Result<(), Error> { pub async fn load_encryption_keys(&self, announcement: &Announcement) -> Result<(), Error> {
let keys = self.get_keys("encryption").await?; let keys = self.get_keys("encryption").await?;
let public_key = announcement.public_key();
// Check if the encryption keys match the announcement // Check if the encryption keys match the announcement
if announcement.public_key() == keys.public_key() { if public_key == keys.public_key() {
// Update encryption keys
let mut device = self.device.write().await; let mut device = self.device.write().await;
device.set_encryption(keys); device.set_encryption(keys);
// Re-subscribes to gift wrap events // Resubscribe to gift wrap events that include the encryption public key
self.resubscribe_messages().await?; self.get_messages_with_encryption(public_key).await?;
Ok(()) Ok(())
} else { } else {
@@ -751,15 +753,17 @@ impl AppState {
let secret = SecretKey::from_hex(&decrypted)?; let secret = SecretKey::from_hex(&decrypted)?;
let keys = Keys::new(secret); let keys = Keys::new(secret);
let public_key = keys.public_key();
// No longer need to hold the reader for device // No longer need to hold the reader for device
drop(device); drop(device);
// Update encryption keys
let mut device = self.device.write().await; let mut device = self.device.write().await;
device.set_encryption(keys); device.set_encryption(keys);
// Re-subscribes to gift wrap events // Resubscribe to gift wrap events that include the encryption public key
self.resubscribe_messages().await?; self.get_messages_with_encryption(public_key).await?;
} }
None => { None => {
// Construct encryption keys request event // Construct encryption keys request event
@@ -812,16 +816,19 @@ impl AppState {
// Decrypt the payload using the client keys // Decrypt the payload using the client keys
let decrypted = client_key.nip44_decrypt(&public_key, payload).await?; let decrypted = client_key.nip44_decrypt(&public_key, payload).await?;
let secret = SecretKey::parse(&decrypted)?; let secret = SecretKey::parse(&decrypted)?;
let keys = Keys::new(secret); let keys = Keys::new(secret);
let public_key = keys.public_key();
// No longer need to hold the reader for device // No longer need to hold the reader for device
drop(device); drop(device);
// Update encryption keys
let mut device = self.device.write().await; let mut device = self.device.write().await;
device.set_encryption(keys); device.set_encryption(keys);
// Re-subscribes to gift wrap events // Resubscribe to gift wrap events that include the encryption public key
self.resubscribe_messages().await?; self.get_messages_with_encryption(public_key).await?;
Ok(()) Ok(())
} }
@@ -938,7 +945,7 @@ impl AppState {
public_key: PublicKey, public_key: PublicKey,
urls: &[RelayUrl], urls: &[RelayUrl],
) -> Result<(), Error> { ) -> Result<(), Error> {
let id = SubscriptionId::new("inbox"); let id = SubscriptionId::new(INBOX_SUB_ID);
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key); let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
// Ensure user's have at least one relay // Ensure user's have at least one relay
@@ -960,6 +967,29 @@ impl AppState {
Ok(()) Ok(())
} }
/// Resubscribes to gift wrap events that include the encryption public key
pub async fn get_messages_with_encryption(&self, encryption: PublicKey) -> Result<(), Error> {
let signer = self.client.signer().await?;
let public_key = signer.get_public_key().await?;
let urls = self.messaging_relays(public_key).await;
let id = SubscriptionId::new(INBOX_SUB_ID);
let filter = Filter::new()
.kind(Kind::GiftWrap)
.pubkeys(vec![public_key, encryption]);
// Unsubscribe the previous subscription
self.client.unsubscribe(&id).await;
// Subscribe to gift wrap events
self.client
.subscribe_with_id_to(&urls, id, filter, None)
.await?;
Ok(())
}
/// Gets messaging relays for public key /// Gets messaging relays for public key
pub async fn messaging_relays(&self, public_key: PublicKey) -> Vec<RelayUrl> { pub async fn messaging_relays(&self, public_key: PublicKey) -> Vec<RelayUrl> {
let mut relay_urls = vec![]; let mut relay_urls = vec![];
@@ -986,26 +1016,6 @@ impl AppState {
relay_urls relay_urls
} }
/// Re-subscribes to gift wrap events
pub async fn resubscribe_messages(&self) -> Result<(), Error> {
let signer = self.client.signer().await?;
let public_key = signer.get_public_key().await?;
let urls = self.messaging_relays(public_key).await;
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let id = SubscriptionId::new("inbox");
// Unsubscribe the previous subscription
self.client.unsubscribe(&id).await;
// Subscribe to gift wrap events
self.client
.subscribe_with_id_to(&urls, id, filter, None)
.await?;
Ok(())
}
/// Stores an unwrapped event in local database with reference to original /// Stores an unwrapped event in local database with reference to original
async fn set_rumor(&self, id: EventId, rumor: &UnsignedEvent) -> Result<(), Error> { async fn set_rumor(&self, id: EventId, rumor: &UnsignedEvent) -> Result<(), Error> {
let rumor_id = rumor.id.context("Rumor is missing an event id")?; let rumor_id = rumor.id.context("Rumor is missing an event id")?;
@@ -1077,12 +1087,12 @@ impl AppState {
// Try to unwrap with the available signer // Try to unwrap with the available signer
let unwrapped = self.try_unwrap_gift_wrap(gift_wrap).await?; let unwrapped = self.try_unwrap_gift_wrap(gift_wrap).await?;
let sender = unwrapped.sender; //let sender = unwrapped.sender;
let mut rumor_unsigned = unwrapped.rumor; let mut rumor_unsigned = unwrapped.rumor;
if !self.verify_sender(sender, &rumor_unsigned).await { //if !self.verify_rumor_sender(sender, &rumor_unsigned) {
return Err(anyhow!("Invalid rumor")); // return Err(anyhow!("Cannot verify the sender"));
}; //};
// Generate event id for the rumor if it doesn't have one // Generate event id for the rumor if it doesn't have one
rumor_unsigned.ensure_id(); rumor_unsigned.ensure_id();
@@ -1098,7 +1108,7 @@ impl AppState {
// Helper method to try unwrapping with different signers // Helper method to try unwrapping with different signers
async fn try_unwrap_gift_wrap(&self, gift_wrap: &Event) -> Result<UnwrappedGift, Error> { async fn try_unwrap_gift_wrap(&self, gift_wrap: &Event) -> Result<UnwrappedGift, Error> {
// Try to unwrap with the device's encryption keys first // Try to unwrap with the encryption key first
// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md // NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
if let Some(signer) = self.device.read().await.encryption.as_ref() { if let Some(signer) = self.device.read().await.encryption.as_ref() {
match UnwrappedGift::from_gift_wrap(signer, gift_wrap).await { match UnwrappedGift::from_gift_wrap(signer, gift_wrap).await {
@@ -1152,22 +1162,8 @@ impl AppState {
} }
/// Verify that the sender of a rumor is the same as the sender of the event. /// Verify that the sender of a rumor is the same as the sender of the event.
async fn verify_sender(&self, sender: PublicKey, rumor: &UnsignedEvent) -> bool { #[allow(dead_code)]
// If we have encryption keys, verify the sender matches the device's public key fn verify_rumor_sender(&self, sender: PublicKey, rumor: &UnsignedEvent) -> bool {
if let Some(keys) = self.device.read().await.encryption.as_ref() {
if let Ok(public_key) = keys.get_public_key().await {
let status = public_key == sender;
// Only return if the status is true
// else fallback to basic sender verification
if status {
return status;
} else {
return rumor.pubkey == sender;
}
}
}
// Fall back to basic sender verification
rumor.pubkey == sender rumor.pubkey == sender
} }