diff --git a/native/native_rust_library/src/backup.rs b/native/native_rust_library/src/backup.rs --- a/native/native_rust_library/src/backup.rs +++ b/native/native_rust_library/src/backup.rs @@ -33,6 +33,39 @@ tokio::spawn(upload_handler::compaction::cleanup_files(backup_id)); } + async fn prepare_user_keys_backup( + backup_id: String, + backup_secret: String, + pickle_key: String, + pickled_account: String, + siwe_backup_msg: String, + ) -> Result<(), String> { + let result = create_user_keys_compaction( + backup_id.clone(), + backup_secret, + pickle_key, + pickled_account, + ) + .await + .map_err(|err| err.to_string()); + + if let Err(err) = result { + handle_backup_creation_error(backup_id.clone(), err.to_string()); + return Err(err); + } + + if !siwe_backup_msg.is_empty() { + if let Err(err) = + create_siwe_backup_msg_compaction(&backup_id, siwe_backup_msg).await + { + handle_backup_creation_error(backup_id.clone(), err.to_string()); + return Err(err.to_string()); + } + } + + Ok(()) + } + pub fn create_backup( backup_id: String, backup_secret: String, @@ -44,29 +77,19 @@ compaction_upload_promises::insert(backup_id.clone(), promise_id); RUNTIME.spawn(async move { - let result = create_userkeys_compaction( + if (prepare_user_keys_backup( backup_id.clone(), backup_secret, pickle_key, pickled_account, + siwe_backup_msg.clone(), ) - .await - .map_err(|err| err.to_string()); - - if let Err(err) = result { - handle_backup_creation_error(backup_id.clone(), err.to_string()); + .await) + .is_err() + { return; } - if !siwe_backup_msg.is_empty() { - if let Err(err) = - create_siwe_backup_msg_compaction(&backup_id, siwe_backup_msg).await - { - handle_backup_creation_error(backup_id.clone(), err.to_string()); - return; - } - } - let (future_id, future) = future_manager::new_future::<()>().await; create_main_compaction(&backup_id, future_id); if let Err(err) = future.await { @@ -75,7 +98,35 @@ } trigger_backup_file_upload(); + // The promise will be resolved when the backup is uploaded + }); + } + pub fn create_user_keys_backup( + backup_id: String, + backup_secret: String, + pickle_key: String, + pickled_account: String, + siwe_backup_msg: String, + promise_id: u32, + ) { + compaction_upload_promises::insert(backup_id.clone(), promise_id); + + RUNTIME.spawn(async move { + if (prepare_user_keys_backup( + backup_id.clone(), + backup_secret, + pickle_key, + pickled_account, + siwe_backup_msg.clone(), + ) + .await) + .is_err() + { + return; + } + + trigger_backup_file_upload(); // The promise will be resolved when the backup is uploaded }); } @@ -270,7 +321,7 @@ } } -pub async fn create_userkeys_compaction( +pub async fn create_user_keys_compaction( backup_id: String, backup_secret: String, pickle_key: String, diff --git a/native/native_rust_library/src/lib.rs b/native/native_rust_library/src/lib.rs --- a/native/native_rust_library/src/lib.rs +++ b/native/native_rust_library/src/lib.rs @@ -436,6 +436,16 @@ promise_id: u32, ); + #[cxx_name = "createUserKeysBackup"] + fn create_user_keys_backup( + backup_id: String, + backup_secret: String, + pickle_key: String, + pickled_account: String, + siwe_backup_msg: String, + promise_id: u32, + ); + #[cxx_name = "restoreBackup"] fn restore_backup( backup_secret: String,