Add scan_blocks api

This commit is contained in:
Sosthene 2025-08-23 15:43:13 +02:00
parent 13f6f8b751
commit e252ea2fd3

View File

@ -6,7 +6,7 @@ use std::ops::Index;
use std::str::FromStr; use std::str::FromStr;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError}; use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
use std::time::{Duration, Instant}; use web_time::{Duration, Instant};
use std::u32; use std::u32;
use rand::{thread_rng, Fill, Rng, RngCore}; 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::aes_gcm::Nonce;
use sdk_common::hash::AnkPcdHash; use sdk_common::hash::AnkPcdHash;
use sdk_common::log::{self, debug, info, warn}; use sdk_common::log::{self, debug, info, warn};
use sdk_common::backend_blindbit_wasm::wasm_bindgen_futures;
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use anyhow::Error as AnyhowError; use anyhow::Error as AnyhowError;
@ -68,7 +69,7 @@ use sdk_common::pcd::{
DataType, FileBlob, Member, Pcd, PcdCommitments, RoleDefinition, Roles, ValidationRule, PCD_VERSION, PcdSerializable DataType, FileBlob, Member, Pcd, PcdCommitments, RoleDefinition, Roles, ValidationRule, PCD_VERSION, PcdSerializable
}; };
use sdk_common::prd::{AnkPrdHash, Prd, PrdType}; 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::sp_client::{FeeRate, OutputSpendStatus, OwnedOutput, Recipient, RecipientAddress, SilentPaymentUnsignedTransaction, SpClient, SpendKey};
use sdk_common::secrets::SecretsStore; use sdk_common::secrets::SecretsStore;
@ -293,6 +294,19 @@ pub fn create_new_device(birthday: u32, network_str: String) -> ApiResult<String
Ok(our_address) Ok(our_address)
} }
#[wasm_bindgen]
pub async fn scan_blocks(tip_height: u32, blindbit_url: String) -> 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] #[wasm_bindgen]
pub fn is_paired() -> ApiResult<bool> { pub fn is_paired() -> ApiResult<bool> {
let local_device = lock_local_device()?; let local_device = lock_local_device()?;
@ -456,6 +470,7 @@ pub fn get_opreturn(transaction: String) -> ApiResult<String> {
#[wasm_bindgen] #[wasm_bindgen]
pub fn process_commit_new_state(mut process: Process, state_id: String, new_tip: String) -> ApiResult<Process> { pub fn process_commit_new_state(mut process: Process, state_id: String, new_tip: String) -> ApiResult<Process> {
let state_id_array: [u8; 32] = Vec::from_hex(&state_id)?.try_into().unwrap(); 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; let new_state: ProcessState;
if let Ok(commited_state) = process.get_state_for_id(&state_id_array) { if let Ok(commited_state) = process.get_state_for_id(&state_id_array) {
new_state = commited_state.clone(); 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.remove_all_concurrent_states()?;
process.insert_concurrent_state(new_state)?; process.insert_concurrent_state(new_state)?;
process.update_states_tip(OutPoint::from_str(&new_tip)?)?; process.update_states_tip(new_tip)?;
Ok(process) Ok(process)
} }