From e252ea2fd34def582f09fedf973a4457b3129081 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Sat, 23 Aug 2025 15:43:13 +0200 Subject: [PATCH] Add scan_blocks api --- src/api.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/api.rs b/src/api.rs index fd90e6d..fe6d023 100644 --- a/src/api.rs +++ b/src/api.rs @@ -6,7 +6,7 @@ use std::ops::Index; use std::str::FromStr; use std::string::FromUtf8Error; use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError}; -use std::time::{Duration, Instant}; +use web_time::{Duration, Instant}; use std::u32; use rand::{thread_rng, Fill, Rng, RngCore}; @@ -15,6 +15,7 @@ use sdk_common::aes_gcm::aes::cipher::ArrayLength; use sdk_common::aes_gcm::Nonce; use sdk_common::hash::AnkPcdHash; use sdk_common::log::{self, debug, info, warn}; +use sdk_common::backend_blindbit_wasm::wasm_bindgen_futures; use anyhow::{anyhow, Context}; use anyhow::Error as AnyhowError; @@ -68,7 +69,7 @@ use sdk_common::pcd::{ DataType, FileBlob, Member, Pcd, PcdCommitments, RoleDefinition, Roles, ValidationRule, PCD_VERSION, PcdSerializable }; use sdk_common::prd::{AnkPrdHash, Prd, PrdType}; -use sdk_common::silentpayments::{create_transaction as internal_create_transaction, sign_transaction as internal_sign_tx, TsUnsignedTransaction}; +use sdk_common::silentpayments::{create_transaction as internal_create_transaction, sign_transaction as internal_sign_tx, SpWallet, TsUnsignedTransaction}; use sdk_common::sp_client::{FeeRate, OutputSpendStatus, OwnedOutput, Recipient, RecipientAddress, SilentPaymentUnsignedTransaction, SpClient, SpendKey}; use sdk_common::secrets::SecretsStore; @@ -293,6 +294,19 @@ pub fn create_new_device(birthday: u32, network_str: String) -> ApiResult ApiResult<()> { + let local_device = lock_local_device()?; + + let sp_wallet = local_device.get_sp_wallet(); + + let last_scan = sp_wallet.get_last_scan(); + let n_blocks_to_scan = tip_height - last_scan; + crate::wallet::scan_blocks(n_blocks_to_scan, &blindbit_url, sp_wallet, tip_height, last_scan).await?; + + Ok(()) +} + #[wasm_bindgen] pub fn is_paired() -> ApiResult { let local_device = lock_local_device()?; @@ -456,6 +470,7 @@ pub fn get_opreturn(transaction: String) -> ApiResult { #[wasm_bindgen] pub fn process_commit_new_state(mut process: Process, state_id: String, new_tip: String) -> ApiResult { let state_id_array: [u8; 32] = Vec::from_hex(&state_id)?.try_into().unwrap(); + let new_tip = OutPoint::from_str(&new_tip)?; let new_state: ProcessState; if let Ok(commited_state) = process.get_state_for_id(&state_id_array) { new_state = commited_state.clone(); @@ -469,7 +484,7 @@ pub fn process_commit_new_state(mut process: Process, state_id: String, new_tip: process.remove_all_concurrent_states()?; process.insert_concurrent_state(new_state)?; - process.update_states_tip(OutPoint::from_str(&new_tip)?)?; + process.update_states_tip(new_tip)?; Ok(process) }