add restore encryption
This commit is contained in:
@@ -37,6 +37,8 @@ pub enum DeviceEvent {
|
||||
Set,
|
||||
/// The device is requesting an encryption key
|
||||
Requesting,
|
||||
/// The device is creating a new encryption key
|
||||
Creating,
|
||||
/// Encryption key is not set
|
||||
NotSet { reason: SharedString },
|
||||
/// An event to notify that Coop isn't subscribed to gift wrap events
|
||||
@@ -345,7 +347,7 @@ impl DeviceRegistry {
|
||||
Err(_) => {
|
||||
// User has no announcement, create a new one
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_announcement(cx);
|
||||
this.set_announcement(Keys::generate(), cx);
|
||||
})?;
|
||||
}
|
||||
}
|
||||
@@ -355,8 +357,11 @@ impl DeviceRegistry {
|
||||
}
|
||||
|
||||
/// Create a new device signer and announce it to user's relay list
|
||||
pub fn set_announcement(&mut self, cx: &mut Context<Self>) {
|
||||
let task = self.new_encryption(cx);
|
||||
pub fn set_announcement(&mut self, keys: Keys, cx: &mut Context<Self>) {
|
||||
let task = self.create_encryption(keys, cx);
|
||||
|
||||
// Notify that we're creating a new encryption key
|
||||
cx.emit(DeviceEvent::Creating);
|
||||
|
||||
self.tasks.push(cx.spawn(async move |this, cx| {
|
||||
match task.await {
|
||||
@@ -376,12 +381,11 @@ impl DeviceRegistry {
|
||||
}));
|
||||
}
|
||||
|
||||
/// Create new encryption keys
|
||||
fn new_encryption(&self, cx: &App) -> Task<Result<Keys, Error>> {
|
||||
/// Create new encryption key and announce it to user's relay list
|
||||
fn create_encryption(&self, keys: Keys, cx: &App) -> Task<Result<Keys, Error>> {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
let keys = Keys::generate();
|
||||
let secret = keys.secret_key().to_secret_hex();
|
||||
let n = keys.public_key();
|
||||
|
||||
@@ -396,7 +400,11 @@ impl DeviceRegistry {
|
||||
let event = client.sign_event_builder(builder).await?;
|
||||
|
||||
// Publish announcement
|
||||
client.send_event(&event).to_nip65().await?;
|
||||
client
|
||||
.send_event(&event)
|
||||
.to_nip65()
|
||||
.ack_policy(AckPolicy::none())
|
||||
.await?;
|
||||
|
||||
// Save device keys to the database
|
||||
set_keys(&client, &secret).await?;
|
||||
|
||||
Reference in New Issue
Block a user