sqlite.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_unissued_mint_quotes(&self) -> Result<Vec<MintQuote>, FfiError> {
  91. self.inner.get_unissued_mint_quotes().await
  92. }
  93. async fn get_melt_quote(&self, quote_id: String) -> Result<Option<MeltQuote>, FfiError> {
  94. self.inner.get_melt_quote(quote_id).await
  95. }
  96. async fn get_melt_quotes(&self) -> Result<Vec<MeltQuote>, FfiError> {
  97. self.inner.get_melt_quotes().await
  98. }
  99. async fn get_keys(&self, id: Id) -> Result<Option<Keys>, FfiError> {
  100. self.inner.get_keys(id).await
  101. }
  102. async fn get_proofs(
  103. &self,
  104. mint_url: Option<MintUrl>,
  105. unit: Option<CurrencyUnit>,
  106. state: Option<Vec<ProofState>>,
  107. spending_conditions: Option<Vec<SpendingConditions>>,
  108. ) -> Result<Vec<ProofInfo>, FfiError> {
  109. self.inner
  110. .get_proofs(mint_url, unit, state, spending_conditions)
  111. .await
  112. }
  113. async fn get_proofs_by_ys(&self, ys: Vec<PublicKey>) -> Result<Vec<ProofInfo>, FfiError> {
  114. self.inner.get_proofs_by_ys(ys).await
  115. }
  116. async fn get_balance(
  117. &self,
  118. mint_url: Option<MintUrl>,
  119. unit: Option<CurrencyUnit>,
  120. state: Option<Vec<ProofState>>,
  121. ) -> Result<u64, FfiError> {
  122. self.inner.get_balance(mint_url, unit, state).await
  123. }
  124. async fn get_transaction(
  125. &self,
  126. transaction_id: TransactionId,
  127. ) -> Result<Option<Transaction>, FfiError> {
  128. self.inner.get_transaction(transaction_id).await
  129. }
  130. async fn list_transactions(
  131. &self,
  132. mint_url: Option<MintUrl>,
  133. direction: Option<TransactionDirection>,
  134. unit: Option<CurrencyUnit>,
  135. ) -> Result<Vec<Transaction>, FfiError> {
  136. self.inner
  137. .list_transactions(mint_url, direction, unit)
  138. .await
  139. }
  140. }