sqlite.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use std::sync::Arc;
  2. use cdk_sqlite::wallet::WalletSqliteDatabase as CdkWalletSqliteDatabase;
  3. use cdk_sqlite::SqliteConnectionManager;
  4. use crate::{
  5. CurrencyUnit, FfiError, FfiWalletSQLDatabase, Id, KeySet, KeySetInfo, Keys, MeltQuote,
  6. MintInfo, MintQuote, MintUrl, ProofInfo, ProofState, PublicKey, SpendingConditions,
  7. Transaction, TransactionDirection, TransactionId, WalletDatabase,
  8. };
  9. /// FFI-compatible WalletSqliteDatabase implementation that implements the WalletDatabaseFfi trait
  10. #[derive(uniffi::Object)]
  11. pub struct WalletSqliteDatabase {
  12. inner: Arc<FfiWalletSQLDatabase<SqliteConnectionManager>>,
  13. }
  14. #[uniffi::export]
  15. impl WalletSqliteDatabase {
  16. /// Create a new WalletSqliteDatabase with the given work directory
  17. #[uniffi::constructor]
  18. pub fn new(file_path: String) -> Result<Arc<Self>, FfiError> {
  19. let db = match tokio::runtime::Handle::try_current() {
  20. Ok(handle) => tokio::task::block_in_place(|| {
  21. handle
  22. .block_on(async move { CdkWalletSqliteDatabase::new(file_path.as_str()).await })
  23. }),
  24. Err(_) => {
  25. // No current runtime, create a new one
  26. tokio::runtime::Runtime::new()
  27. .map_err(|e| FfiError::internal(format!("Failed to create runtime: {}", e)))?
  28. .block_on(async move { CdkWalletSqliteDatabase::new(file_path.as_str()).await })
  29. }
  30. }
  31. .map_err(FfiError::internal)?;
  32. Ok(Arc::new(Self {
  33. inner: FfiWalletSQLDatabase::new(db),
  34. }))
  35. }
  36. /// Create an in-memory database
  37. #[uniffi::constructor]
  38. pub fn new_in_memory() -> Result<Arc<Self>, FfiError> {
  39. let db = match tokio::runtime::Handle::try_current() {
  40. Ok(handle) => tokio::task::block_in_place(|| {
  41. handle.block_on(async move { cdk_sqlite::wallet::memory::empty().await })
  42. }),
  43. Err(_) => {
  44. // No current runtime, create a new one
  45. tokio::runtime::Runtime::new()
  46. .map_err(|e| FfiError::internal(format!("Failed to create runtime: {}", e)))?
  47. .block_on(async move { cdk_sqlite::wallet::memory::empty().await })
  48. }
  49. }
  50. .map_err(FfiError::internal)?;
  51. Ok(Arc::new(Self {
  52. inner: FfiWalletSQLDatabase::new(db),
  53. }))
  54. }
  55. }
  56. // Use macro to implement WalletDatabase trait - delegates all methods to inner
  57. crate::impl_ffi_wallet_database!(WalletSqliteDatabase);