sqlite.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. use std::collections::HashMap;
  2. use std::sync::Arc;
  3. use cdk_sqlite::wallet::WalletSqliteDatabase as CdkWalletSqliteDatabase;
  4. use cdk_sqlite::SqliteConnectionManager;
  5. use crate::{
  6. CurrencyUnit, FfiError, FfiWalletSQLDatabase, Id, KeySetInfo, Keys, MeltQuote, MintInfo,
  7. MintQuote, MintUrl, ProofInfo, ProofState, PublicKey, SpendingConditions, Transaction,
  8. TransactionDirection, TransactionId, WalletDatabase,
  9. };
  10. /// FFI-compatible WalletSqliteDatabase implementation that implements the WalletDatabaseFfi trait
  11. #[derive(uniffi::Object)]
  12. pub struct WalletSqliteDatabase {
  13. inner: Arc<FfiWalletSQLDatabase<SqliteConnectionManager>>,
  14. }
  15. #[uniffi::export]
  16. impl WalletSqliteDatabase {
  17. /// Create a new WalletSqliteDatabase with the given work directory
  18. #[uniffi::constructor]
  19. pub fn new(file_path: String) -> Result<Arc<Self>, FfiError> {
  20. let db = match tokio::runtime::Handle::try_current() {
  21. Ok(handle) => tokio::task::block_in_place(|| {
  22. handle
  23. .block_on(async move { CdkWalletSqliteDatabase::new(file_path.as_str()).await })
  24. }),
  25. Err(_) => {
  26. // No current runtime, create a new one
  27. tokio::runtime::Runtime::new()
  28. .map_err(|e| FfiError::Database {
  29. msg: format!("Failed to create runtime: {}", e),
  30. })?
  31. .block_on(async move { CdkWalletSqliteDatabase::new(file_path.as_str()).await })
  32. }
  33. }
  34. .map_err(|e| FfiError::Database { msg: e.to_string() })?;
  35. Ok(Arc::new(Self {
  36. inner: FfiWalletSQLDatabase::new(db),
  37. }))
  38. }
  39. /// Create an in-memory database
  40. #[uniffi::constructor]
  41. pub fn new_in_memory() -> Result<Arc<Self>, FfiError> {
  42. let db = match tokio::runtime::Handle::try_current() {
  43. Ok(handle) => tokio::task::block_in_place(|| {
  44. handle.block_on(async move { cdk_sqlite::wallet::memory::empty().await })
  45. }),
  46. Err(_) => {
  47. // No current runtime, create a new one
  48. tokio::runtime::Runtime::new()
  49. .map_err(|e| FfiError::Database {
  50. msg: format!("Failed to create runtime: {}", e),
  51. })?
  52. .block_on(async move { cdk_sqlite::wallet::memory::empty().await })
  53. }
  54. }
  55. .map_err(|e| FfiError::Database { msg: e.to_string() })?;
  56. Ok(Arc::new(Self {
  57. inner: FfiWalletSQLDatabase::new(db),
  58. }))
  59. }
  60. }
  61. #[uniffi::export(async_runtime = "tokio")]
  62. #[async_trait::async_trait]
  63. impl WalletDatabase for WalletSqliteDatabase {
  64. async fn begin_db_transaction(
  65. &self,
  66. ) -> Result<Arc<crate::database::WalletDatabaseTransactionWrapper>, FfiError> {
  67. self.inner.begin_db_transaction().await
  68. }
  69. async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, FfiError> {
  70. self.inner.get_mint(mint_url).await
  71. }
  72. async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, FfiError> {
  73. self.inner.get_mints().await
  74. }
  75. async fn get_mint_keysets(
  76. &self,
  77. mint_url: MintUrl,
  78. ) -> Result<Option<Vec<KeySetInfo>>, FfiError> {
  79. self.inner.get_mint_keysets(mint_url).await
  80. }
  81. async fn get_keyset_by_id(&self, keyset_id: Id) -> Result<Option<KeySetInfo>, FfiError> {
  82. self.inner.get_keyset_by_id(keyset_id).await
  83. }
  84. async fn get_mint_quote(&self, quote_id: String) -> Result<Option<MintQuote>, FfiError> {
  85. self.inner.get_mint_quote(quote_id).await
  86. }
  87. async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, FfiError> {
  88. self.inner.get_mint_quotes().await
  89. }
  90. async fn get_melt_quote(&self, quote_id: String) -> Result<Option<MeltQuote>, FfiError> {
  91. self.inner.get_melt_quote(quote_id).await
  92. }
  93. async fn get_melt_quotes(&self) -> Result<Vec<MeltQuote>, FfiError> {
  94. self.inner.get_melt_quotes().await
  95. }
  96. async fn get_keys(&self, id: Id) -> Result<Option<Keys>, FfiError> {
  97. self.inner.get_keys(id).await
  98. }
  99. async fn get_proofs(
  100. &self,
  101. mint_url: Option<MintUrl>,
  102. unit: Option<CurrencyUnit>,
  103. state: Option<Vec<ProofState>>,
  104. spending_conditions: Option<Vec<SpendingConditions>>,
  105. ) -> Result<Vec<ProofInfo>, FfiError> {
  106. self.inner
  107. .get_proofs(mint_url, unit, state, spending_conditions)
  108. .await
  109. }
  110. async fn get_proofs_by_ys(&self, ys: Vec<PublicKey>) -> Result<Vec<ProofInfo>, FfiError> {
  111. self.inner.get_proofs_by_ys(ys).await
  112. }
  113. async fn get_balance(
  114. &self,
  115. mint_url: Option<MintUrl>,
  116. unit: Option<CurrencyUnit>,
  117. state: Option<Vec<ProofState>>,
  118. ) -> Result<u64, FfiError> {
  119. self.inner.get_balance(mint_url, unit, state).await
  120. }
  121. async fn get_transaction(
  122. &self,
  123. transaction_id: TransactionId,
  124. ) -> Result<Option<Transaction>, FfiError> {
  125. self.inner.get_transaction(transaction_id).await
  126. }
  127. async fn list_transactions(
  128. &self,
  129. mint_url: Option<MintUrl>,
  130. direction: Option<TransactionDirection>,
  131. unit: Option<CurrencyUnit>,
  132. ) -> Result<Vec<Transaction>, FfiError> {
  133. self.inner
  134. .list_transactions(mint_url, direction, unit)
  135. .await
  136. }
  137. }