refactor encryption dropdown menu
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m43s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m43s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
This commit is contained in:
@@ -151,7 +151,7 @@ impl DeviceRegistry {
|
||||
}
|
||||
|
||||
/// Set the decoupled encryption key for the current user
|
||||
fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
|
||||
pub fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
|
||||
where
|
||||
S: NostrSigner + 'static,
|
||||
{
|
||||
@@ -242,7 +242,7 @@ impl DeviceRegistry {
|
||||
}
|
||||
|
||||
/// Get device announcement for current user
|
||||
fn get_announcement(&mut self, cx: &mut Context<Self>) {
|
||||
pub fn get_announcement(&mut self, cx: &mut Context<Self>) {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
@@ -307,8 +307,8 @@ impl DeviceRegistry {
|
||||
}));
|
||||
}
|
||||
|
||||
/// Create a new device signer and announce it
|
||||
fn announce(&mut self, cx: &mut Context<Self>) {
|
||||
/// Create new encryption keys
|
||||
pub fn create_encryption(&self, cx: &App) -> Task<Result<Keys, Error>> {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
@@ -323,7 +323,7 @@ impl DeviceRegistry {
|
||||
let secret = keys.secret_key().to_secret_hex();
|
||||
let n = keys.public_key();
|
||||
|
||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||
cx.background_spawn(async move {
|
||||
let urls = write_relays.await;
|
||||
|
||||
// Construct an announcement event
|
||||
@@ -340,23 +340,29 @@ impl DeviceRegistry {
|
||||
// Save device keys to the database
|
||||
set_keys(&client, &secret).await?;
|
||||
|
||||
Ok(())
|
||||
});
|
||||
Ok(keys)
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new device signer and announce it
|
||||
fn announce(&mut self, cx: &mut Context<Self>) {
|
||||
let task = self.create_encryption(cx);
|
||||
|
||||
self.tasks.push(cx.spawn(async move |this, cx| {
|
||||
if task.await.is_ok() {
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_signer(keys, cx);
|
||||
this.listen_request(cx);
|
||||
})?;
|
||||
}
|
||||
let keys = task.await?;
|
||||
|
||||
// Update signer
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_signer(keys, cx);
|
||||
this.listen_request(cx);
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
}
|
||||
|
||||
/// Initialize device signer (decoupled encryption key) for the current user
|
||||
fn new_signer(&mut self, event: &Event, cx: &mut Context<Self>) {
|
||||
pub fn new_signer(&mut self, event: &Event, cx: &mut Context<Self>) {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
@@ -375,31 +381,29 @@ impl DeviceRegistry {
|
||||
}
|
||||
});
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
self.tasks.push(cx.spawn(async move |this, cx| {
|
||||
match task.await {
|
||||
Ok(keys) => {
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_signer(keys, cx);
|
||||
this.listen_request(cx);
|
||||
})
|
||||
.ok();
|
||||
})?;
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to initialize device signer: {e}");
|
||||
this.update(cx, |this, cx| {
|
||||
this.request(cx);
|
||||
this.listen_approval(cx);
|
||||
})
|
||||
.ok();
|
||||
|
||||
log::warn!("Failed to initialize device signer: {e}");
|
||||
})?;
|
||||
}
|
||||
};
|
||||
})
|
||||
.detach();
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
}
|
||||
|
||||
/// Listen for device key requests on user's write relays
|
||||
fn listen_request(&mut self, cx: &mut Context<Self>) {
|
||||
pub fn listen_request(&mut self, cx: &mut Context<Self>) {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user