Skip to main content

cdk_ffi/
sqlite.rs

1use std::sync::Arc;
2
3use cdk_common::database::Error as CdkDatabaseError;
4use cdk_sqlite::wallet::WalletSqliteDatabase as CdkWalletSqliteDatabase;
5
6use crate::{
7    CurrencyUnit, FfiError, FfiWalletDatabaseWrapper, Id, KeySet, KeySetInfo, Keys, MeltQuote,
8    MintInfo, MintQuote, MintUrl, P2PKSigningKey, ProofInfo, ProofState, PublicKey,
9    SpendingConditions, Transaction, TransactionDirection, TransactionId, WalletDatabase,
10};
11
12/// FFI-compatible WalletSqliteDatabase implementation that implements the WalletDatabaseFfi trait
13#[derive(uniffi::Object)]
14pub struct WalletSqliteDatabase {
15    inner: Arc<FfiWalletDatabaseWrapper<CdkWalletSqliteDatabase, CdkDatabaseError>>,
16    // Keep the runtime alive so async pool operations work in FFI contexts.
17    _runtime: crate::runtime::RuntimeGuard,
18}
19
20#[uniffi::export]
21impl WalletSqliteDatabase {
22    /// Create a new WalletSqliteDatabase with the given work directory
23    #[uniffi::constructor]
24    pub fn new(file_path: String) -> Result<Arc<Self>, FfiError> {
25        let rt = crate::runtime::RuntimeGuard::new().map_err(FfiError::internal)?;
26        let db = rt
27            .block_on(async move { CdkWalletSqliteDatabase::new(file_path.as_str()).await })
28            .map_err(FfiError::internal)?;
29        Ok(Arc::new(Self {
30            inner: FfiWalletDatabaseWrapper::new(db),
31            _runtime: rt,
32        }))
33    }
34
35    /// Create an in-memory database
36    #[uniffi::constructor]
37    pub fn new_in_memory() -> Result<Arc<Self>, FfiError> {
38        let rt = crate::runtime::RuntimeGuard::new().map_err(FfiError::internal)?;
39        let db = rt
40            .block_on(async move { cdk_sqlite::wallet::memory::empty().await })
41            .map_err(FfiError::internal)?;
42        Ok(Arc::new(Self {
43            inner: FfiWalletDatabaseWrapper::new(db),
44            _runtime: rt,
45        }))
46    }
47}
48
49// Use macro to implement WalletDatabase trait - delegates all methods to inner
50crate::impl_ffi_wallet_database!(WalletSqliteDatabase);