update nostr sdk

This commit is contained in:
2026-05-17 14:38:07 +07:00
parent 6d60726f27
commit d2a17e54c4
19 changed files with 630 additions and 612 deletions

View File

@@ -16,14 +16,15 @@ impl From<&Event> for Announcement {
let public_key = val
.tags
.iter()
.find(|tag| tag.kind().as_str() == "n")
.find(|tag| tag.kind() == "n")
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.unwrap_or(val.pubkey);
let client_name = val
.tags
.find(TagKind::Client)
.iter()
.find(|tag| tag.kind() == "client")
.and_then(|tag| tag.content())
.map(|c| c.to_string());

View File

@@ -3,7 +3,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error, anyhow};
use anyhow::{Error, anyhow};
use common::config_dir;
use gpui::{App, AppContext, Context, Entity, EventEmitter, Global, SharedString, Task, Window};
use nostr_connect::prelude::*;
@@ -130,10 +130,8 @@ impl NostrRegistry {
// Construct the nostr client
let client = ClientBuilder::default()
.signer(signer.clone())
.database(lmdb)
.gossip(NostrGossipMemory::unbounded())
.automatic_authentication(false)
.connect_timeout(Duration::from_secs(10))
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600),
@@ -236,10 +234,7 @@ impl NostrRegistry {
}
// Connect to all added relays
client
.connect()
.and_wait(Duration::from_secs(TIMEOUT))
.await;
client.connect().await;
Ok(())
});
@@ -262,70 +257,73 @@ impl NostrRegistry {
}));
}
/// Get the secret for a given npub.
pub fn get_secret(
&self,
public_key: PublicKey,
cx: &App,
) -> Task<Result<Arc<dyn NostrSigner>, Error>> {
/// Switch to a different account by public key
pub fn switch_account(&self, public_key: PublicKey, cx: &App) -> Result<(), Error> {
let client = self.client();
let signer = self.signer();
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone();
let app_signer: Arc<dyn NostrSigner> = Arc::new(app_keys.clone());
if let Ok(payload) = std::fs::read_to_string(key_path) {
if !payload.is_empty() {
cx.background_spawn(async move {
let decrypted = app_keys.nip44_decrypt(&public_key, &payload).await?;
let secret = SecretKey::parse(&decrypted)?;
let keys = Keys::new(secret);
if let Ok(payload) = std::fs::read_to_string(key_path)
&& !payload.is_empty()
{
let decrypted = app_signer.nip44_decrypt(&public_key, &payload)?;
Ok(keys.into_nostr_signer())
if let Ok(secret) = SecretKey::parse(&decrypted) {
let keys = Keys::new(secret);
cx.spawn(async move |_cx| {
signer.switch(keys).await;
client.unsubscribe_all().await.ok();
})
} else {
self.get_secret_keyring(&npub, cx)
.detach();
return Ok(());
} else if let Ok(uri) = NostrConnectUri::parse(decrypted) {
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
nip46.auth_url_handler(CoopAuthUrlHandler);
cx.spawn(async move |_cx| {
signer.switch(nip46).await;
client.unsubscribe_all().await.ok();
})
.detach();
return Ok(());
}
} else {
self.get_secret_keyring(&npub, cx)
}
Err(anyhow!("Secret not found"))
}
/// Get the secret for a given npub in the OS credentials store.
#[deprecated = "Use get_secret instead"]
fn get_secret_keyring(
&self,
user: &str,
cx: &App,
) -> Task<Result<Arc<dyn NostrSigner>, Error>> {
let read = cx.read_credentials(user);
/// Get the secret for a given npub.
pub fn get_secret(&self, public_key: PublicKey) -> Result<Arc<dyn AsyncNostrSigner>, Error> {
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone();
let app_signer: Arc<dyn NostrSigner> = Arc::new(self.app_keys.clone());
cx.background_spawn(async move {
let (_, secret) = read
.await
.map_err(|_| anyhow!("Failed to get signer. Please re-import the secret key"))?
.ok_or_else(|| anyhow!("Failed to get signer. Please re-import the secret key"))?;
if let Ok(payload) = std::fs::read_to_string(key_path)
&& !payload.is_empty()
{
let decrypted = app_signer.nip44_decrypt(&public_key, &payload)?;
// Try to parse as a direct secret key first
if let Ok(secret_key) = SecretKey::from_slice(&secret) {
return Ok(Keys::new(secret_key).into_nostr_signer());
if let Ok(secret) = SecretKey::parse(&decrypted) {
let keys = Keys::new(secret);
return Ok(Arc::new(keys));
} else if let Ok(uri) = NostrConnectUri::parse(decrypted) {
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
nip46.auth_url_handler(CoopAuthUrlHandler);
return Ok(Arc::new(nip46));
}
}
// Convert the secret into string
let sec = String::from_utf8(secret)
.map_err(|_| anyhow!("Failed to parse secret as UTF-8"))?;
// Try to parse as a NIP-46 URI
let uri =
NostrConnectUri::parse(&sec).map_err(|_| anyhow!("Failed to parse NIP-46 URI"))?;
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
// Set the auth URL handler
nip46.auth_url_handler(CoopAuthUrlHandler);
Ok(nip46.into_nostr_signer())
})
Err(anyhow!("Secret not found"))
}
/// Add a new npub to the keys directory
@@ -337,7 +335,7 @@ impl NostrRegistry {
) -> Task<Result<(), Error>> {
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone();
let app_keys: Arc<dyn AsyncNostrSigner> = Arc::new(self.app_keys.clone());
cx.background_spawn(async move {
// If the secret starts with "bunker://" (nostr connect), use it directly; otherwise, encrypt it
@@ -386,11 +384,11 @@ impl NostrRegistry {
// Run async tasks in background
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let signer = async_keys.into_nostr_signer();
// Construct relay list event
let relay_list = default_relay_list();
let event = EventBuilder::relay_list(relay_list).sign(&signer).await?;
let event = EventBuilder::relay_list(relay_list)
.sign_async(&async_keys)
.await?;
// Publish relay list
client
@@ -403,7 +401,9 @@ impl NostrRegistry {
let name = petname::petname(2, "-").unwrap_or("Cooper".to_string());
let avatar = Url::parse(&format!("https://avatar.vercel.sh/{name}")).unwrap();
let metadata = Metadata::new().display_name(&name).picture(avatar);
let event = EventBuilder::metadata(&metadata).sign(&signer).await?;
let event = EventBuilder::metadata(&metadata)
.sign_async(&async_keys)
.await?;
// Publish metadata event
client
@@ -414,7 +414,9 @@ impl NostrRegistry {
// Construct the default contact list
let contacts = vec![Contact::new(PublicKey::parse(COOP_PUBKEY).unwrap())];
let event = EventBuilder::contact_list(contacts).sign(&signer).await?;
let event = EventBuilder::contact_list(contacts)
.sign_async(&async_keys)
.await?;
// Publish contact list event
client
@@ -425,7 +427,9 @@ impl NostrRegistry {
// Construct the default messaging relay list
let relays = default_messaging_relays();
let event = EventBuilder::nip17_relay_list(relays).sign(&signer).await?;
let event = EventBuilder::nip17_relay_list(relays)
.sign_async(&async_keys)
.await?;
// Publish messaging relay list event
client.send_event(&event).to_nip65().await?;
@@ -440,56 +444,7 @@ impl NostrRegistry {
match task.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
})?;
}
Err(e) => {
this.update(cx, |_this, cx| {
cx.emit(StateEvent::error(e.to_string()));
})?;
}
};
Ok(())
}));
}
/// Set the signer for the nostr client and verify the public key
pub fn set_signer<T>(&mut self, new: T, cx: &mut Context<Self>)
where
T: NostrSigner + 'static,
{
let client = self.client();
let signer = self.signer();
// Create a task to update the signer and verify the public key
let task: Task<Result<PublicKey, Error>> = cx.background_spawn(async move {
// Update signer and unsubscribe
signer.switch(new).await;
client.unsubscribe_all().await?;
// Verify and get public key
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
log::info!("Signer's public key: {}", public_key);
Ok(public_key)
});
self.tasks.push(cx.spawn(async move |this, cx| {
match task.await {
Ok(public_key) => {
this.update(cx, |this, cx| {
// Add public key to npubs if not already present
this.npubs.update(cx, |this, cx| {
if !this.contains(&public_key) {
this.push(public_key);
cx.notify();
}
});
// Emit signer changed event
cx.emit(StateEvent::SignerSet);
this.switch_account(keys.public_key(), cx);
})?;
}
Err(e) => {
@@ -513,7 +468,7 @@ impl NostrRegistry {
match write_secret.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
this.switch_account(keys.public_key(), cx);
})?;
}
Err(e) => {
@@ -552,7 +507,7 @@ impl NostrRegistry {
match write_secret.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_signer(nip46, cx);
this.switch_account(public_key, cx);
})?;
}
Err(e) => {
@@ -670,14 +625,19 @@ impl NostrRegistry {
pub fn wot_search(&self, query: &str, cx: &App) -> Task<Result<Vec<PublicKey>, Error>> {
let client = self.client();
let query = query.to_string();
let signer = self.signer();
cx.background_spawn(async move {
let signer = signer.get().await;
// Construct a vertex request event
let builder = EventBuilder::new(Kind::Custom(5315), "").tags(vec![
Tag::custom(TagKind::custom("param"), vec!["search", &query]),
Tag::custom(TagKind::custom("param"), vec!["limit", "10"]),
]);
let event = client.sign_event_builder(builder).await?;
let event = EventBuilder::new(Kind::Custom(5315), "")
.tags(vec![
Tag::custom("param", vec!["search", &query]),
Tag::custom("param", vec!["limit", "10"]),
])
.sign_async(&signer)
.await?;
// Send the event to vertex relays
let output = client.send_event(&event).to(WOT_RELAYS).await?;

View File

@@ -7,38 +7,42 @@ use smol::lock::RwLock;
#[derive(Debug)]
pub struct CoopSigner {
/// User's signer
signer: RwLock<Arc<dyn NostrSigner>>,
signer: Arc<RwLock<Arc<dyn AsyncNostrSigner>>>,
/// User's signer public key
signer_pkey: RwLock<Option<PublicKey>>,
signer_pkey: Arc<RwLock<Option<PublicKey>>>,
/// Specific signer for encryption purposes
encryption_signer: RwLock<Option<Arc<dyn NostrSigner>>>,
encryption_signer: Arc<RwLock<Option<Arc<dyn AsyncNostrSigner>>>>,
}
impl CoopSigner {
pub fn new<T>(signer: T) -> Self
where
T: IntoNostrSigner,
T: AsyncNostrSigner,
{
Self {
signer: RwLock::new(signer.into_nostr_signer()),
signer_pkey: RwLock::new(None),
encryption_signer: RwLock::new(None),
signer: Arc::new(RwLock::new(Arc::new(signer))),
signer_pkey: Arc::new(RwLock::new(None)),
encryption_signer: Arc::new(RwLock::new(None)),
}
}
/// Get the current signer.
pub async fn get(&self) -> Arc<dyn NostrSigner> {
pub async fn get(&self) -> Arc<dyn AsyncNostrSigner> {
self.signer.read().await.clone()
}
/// Get the encryption signer.
pub async fn get_encryption_signer(&self) -> Option<Arc<dyn NostrSigner>> {
pub async fn get_encryption_signer(&self) -> Option<Arc<dyn AsyncNostrSigner>> {
self.encryption_signer.read().await.clone()
}
/// Check if the encryption signer is available.
pub async fn has_encryption_signer(&self) -> bool {
self.encryption_signer.read().await.is_some()
}
/// Get public key
///
/// Ensure to call this method after the signer has been initialized.
@@ -50,19 +54,17 @@ impl CoopSigner {
/// Switch the current signer to a new signer.
pub async fn switch<T>(&self, new: T)
where
T: IntoNostrSigner,
T: AsyncNostrSigner,
{
let new_signer = new.into_nostr_signer();
let public_key = new_signer.get_public_key().await.ok();
let mut signer = self.signer.write().await;
let mut signer_pkey = self.signer_pkey.write().await;
let mut encryption_signer = self.encryption_signer.write().await;
// Switch to the new signer
*signer = new_signer;
// Update the public key
*signer_pkey = public_key;
*signer_pkey = new.get_public_key().await.ok();
// Switch to the new signer
*signer = Arc::new(new);
// Reset the encryption signer
*encryption_signer = None;
@@ -71,35 +73,33 @@ impl CoopSigner {
/// Set the encryption signer.
pub async fn set_encryption_signer<T>(&self, new: T)
where
T: IntoNostrSigner,
T: AsyncNostrSigner,
{
let mut encryption_signer = self.encryption_signer.write().await;
*encryption_signer = Some(new.into_nostr_signer());
*encryption_signer = Some(Arc::new(new));
}
}
impl NostrSigner for CoopSigner {
#[allow(mismatched_lifetime_syntaxes)]
fn backend(&self) -> SignerBackend {
SignerBackend::Custom(Cow::Borrowed("custom"))
}
fn get_public_key<'a>(&'a self) -> BoxedFuture<'a, Result<PublicKey, SignerError>> {
impl AsyncGetPublicKey for CoopSigner {
fn get_public_key(&self) -> BoxedFuture<'_, Result<PublicKey, SignerError>> {
Box::pin(async move { self.get().await.get_public_key().await })
}
}
fn sign_event<'a>(
&'a self,
unsigned: UnsignedEvent,
) -> BoxedFuture<'a, Result<Event, SignerError>> {
impl AsyncSignEvent for CoopSigner {
fn sign_event(&self, unsigned: UnsignedEvent) -> BoxedFuture<'_, Result<Event, SignerError>> {
Box::pin(async move { self.get().await.sign_event(unsigned).await })
}
}
impl AsyncNip04 for CoopSigner {
type Error = SignerError;
fn nip04_encrypt<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.get().await.nip04_encrypt(public_key, content).await })
}
@@ -107,7 +107,7 @@ impl NostrSigner for CoopSigner {
&'a self,
public_key: &'a PublicKey,
encrypted_content: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move {
self.get()
.await
@@ -115,12 +115,16 @@ impl NostrSigner for CoopSigner {
.await
})
}
}
impl AsyncNip44 for CoopSigner {
type Error = SignerError;
fn nip44_encrypt<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.get().await.nip44_encrypt(public_key, content).await })
}
@@ -128,7 +132,13 @@ impl NostrSigner for CoopSigner {
&'a self,
public_key: &'a PublicKey,
payload: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.get().await.nip44_decrypt(public_key, payload).await })
}
}
impl AsyncNostrSigner for CoopSigner {
fn backend(&self) -> SignerBackend<'_> {
SignerBackend::Custom(Cow::Borrowed("custom"))
}
}