-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sdk: allow to change Keys
#170
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ use nostr::{ | |
Metadata, Result, Tag, | ||
}; | ||
use nostr_sdk_net::futures_util::Future; | ||
use tokio::sync::broadcast; | ||
use tokio::sync::{broadcast, RwLock}; | ||
|
||
#[cfg(feature = "blocking")] | ||
pub mod blocking; | ||
|
@@ -111,7 +111,7 @@ pub enum Error { | |
#[derive(Debug, Clone)] | ||
pub struct Client { | ||
pool: RelayPool, | ||
keys: Keys, | ||
keys: Arc<RwLock<Keys>>, | ||
opts: Options, | ||
dropped: Arc<AtomicBool>, | ||
#[cfg(feature = "nip46")] | ||
|
@@ -167,7 +167,7 @@ impl Client { | |
pub fn with_opts(keys: &Keys, opts: Options) -> Self { | ||
Self { | ||
pool: RelayPool::new(opts.pool), | ||
keys: keys.clone(), | ||
keys: Arc::new(RwLock::new(keys.clone())), | ||
opts, | ||
dropped: Arc::new(AtomicBool::new(false)), | ||
#[cfg(feature = "nip46")] | ||
|
@@ -190,7 +190,7 @@ impl Client { | |
) -> Self { | ||
Self { | ||
pool: RelayPool::new(opts.pool), | ||
keys: app_keys.clone(), | ||
keys: Arc::new(RwLock::new(app_keys.clone())), | ||
opts, | ||
dropped: Arc::new(AtomicBool::new(false)), | ||
remote_signer: Some(remote_signer), | ||
|
@@ -203,8 +203,15 @@ impl Client { | |
} | ||
|
||
/// Get current [`Keys`] | ||
pub fn keys(&self) -> Keys { | ||
self.keys.clone() | ||
pub async fn keys(&self) -> Keys { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't turning a synchronous function into an asynchronous one in the public API a breaking change? |
||
let keys = self.keys.read().await; | ||
keys.clone() | ||
} | ||
|
||
/// Change [`Keys`] | ||
pub async fn set_keys(&self, keys: &Keys) { | ||
let mut current_keys = self.keys.write().await; | ||
*current_keys = keys.clone(); | ||
} | ||
|
||
/// Get [`RelayPool`] | ||
|
@@ -214,16 +221,17 @@ impl Client { | |
|
||
/// Get NIP46 uri | ||
#[cfg(feature = "nip46")] | ||
pub fn nostr_connect_uri( | ||
pub async fn nostr_connect_uri( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't turning a synchronous function into an asynchronous one in the public API a breaking change? |
||
&self, | ||
metadata: NostrConnectMetadata, | ||
) -> Result<NostrConnectURI, Error> { | ||
let signer = self | ||
.remote_signer | ||
.as_ref() | ||
.ok_or(Error::SignerNotConfigured)?; | ||
let keys = self.keys.read().await; | ||
Ok(NostrConnectURI::new( | ||
self.keys.public_key(), | ||
keys.public_key(), | ||
signer.relay_url(), | ||
metadata.name, | ||
)) | ||
|
@@ -725,20 +733,22 @@ impl Client { | |
} | ||
} else { | ||
let difficulty: u8 = self.opts.get_difficulty(); | ||
let keys = self.keys.read().await; | ||
if difficulty > 0 { | ||
builder.to_pow_event(&self.keys, difficulty)? | ||
builder.to_pow_event(&keys, difficulty)? | ||
} else { | ||
builder.to_event(&self.keys)? | ||
builder.to_event(&keys)? | ||
} | ||
}; | ||
|
||
#[cfg(not(feature = "nip46"))] | ||
let event: Event = { | ||
let difficulty: u8 = self.opts.get_difficulty(); | ||
let keys = self.keys.read().await; | ||
if difficulty > 0 { | ||
builder.to_pow_event(&self.keys, difficulty)? | ||
builder.to_pow_event(&keys, difficulty)? | ||
} else { | ||
builder.to_event(&self.keys)? | ||
builder.to_event(&keys)? | ||
} | ||
}; | ||
|
||
|
@@ -847,17 +857,21 @@ impl Client { | |
|
||
filter = filter.author(signer_public_key.to_string()); | ||
} else { | ||
filter = filter.author(self.keys.public_key().to_string()); | ||
let keys = self.keys.read().await; | ||
filter = filter.author(keys.public_key().to_string()); | ||
} | ||
|
||
filter | ||
}; | ||
|
||
#[cfg(not(feature = "nip46"))] | ||
let filter = Filter::new() | ||
.author(self.keys.public_key().to_string()) | ||
.kind(Kind::ContactList) | ||
.limit(1); | ||
let filter: Filter = { | ||
let keys = self.keys.read().await; | ||
Filter::new() | ||
.author(keys.public_key().to_string()) | ||
.kind(Kind::ContactList) | ||
.limit(1) | ||
}; | ||
|
||
Ok(vec![filter]) | ||
} | ||
|
@@ -1012,7 +1026,8 @@ impl Client { | |
return Err(Error::ResponseNotMatchRequest); | ||
} | ||
} else { | ||
EventBuilder::new_encrypted_direct_msg(&self.keys, receiver, msg, reply_to)? | ||
let keys = self.keys.read().await; | ||
EventBuilder::new_encrypted_direct_msg(&keys, receiver, msg, reply_to)? | ||
}; | ||
|
||
#[cfg(not(feature = "nip46"))] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't turning a synchronous function into an asynchronous one in the public API a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's a breaking change. I usually try to avoid breaking change, but in the last versions happened more frequently to implement new features.
In the README.md I written that, since the library it's in ALPHA state, the APIs could/will change in breaking ways.