database.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. //! FFI Database bindings
  2. use std::collections::HashMap;
  3. use std::sync::Arc;
  4. use cdk::cdk_database::WalletDatabase as CdkWalletDatabase;
  5. use crate::error::FfiError;
  6. #[cfg(feature = "postgres")]
  7. use crate::postgres::WalletPostgresDatabase;
  8. use crate::sqlite::WalletSqliteDatabase;
  9. use crate::types::*;
  10. /// FFI-compatible trait for wallet database operations
  11. /// This trait mirrors the CDK WalletDatabase trait but uses FFI-compatible types
  12. #[uniffi::export(with_foreign)]
  13. #[async_trait::async_trait]
  14. pub trait WalletDatabase: Send + Sync {
  15. // Mint Management
  16. /// Add Mint to storage
  17. async fn add_mint(
  18. &self,
  19. mint_url: MintUrl,
  20. mint_info: Option<MintInfo>,
  21. ) -> Result<(), FfiError>;
  22. /// Remove Mint from storage
  23. async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), FfiError>;
  24. /// Get mint from storage
  25. async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, FfiError>;
  26. /// Get all mints from storage
  27. async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, FfiError>;
  28. /// Update mint url
  29. async fn update_mint_url(
  30. &self,
  31. old_mint_url: MintUrl,
  32. new_mint_url: MintUrl,
  33. ) -> Result<(), FfiError>;
  34. // Keyset Management
  35. /// Add mint keyset to storage
  36. async fn add_mint_keysets(
  37. &self,
  38. mint_url: MintUrl,
  39. keysets: Vec<KeySetInfo>,
  40. ) -> Result<(), FfiError>;
  41. /// Get mint keysets for mint url
  42. async fn get_mint_keysets(
  43. &self,
  44. mint_url: MintUrl,
  45. ) -> Result<Option<Vec<KeySetInfo>>, FfiError>;
  46. /// Get mint keyset by id
  47. async fn get_keyset_by_id(&self, keyset_id: Id) -> Result<Option<KeySetInfo>, FfiError>;
  48. // Mint Quote Management
  49. /// Add mint quote to storage
  50. async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), FfiError>;
  51. /// Get mint quote from storage
  52. async fn get_mint_quote(&self, quote_id: String) -> Result<Option<MintQuote>, FfiError>;
  53. /// Get mint quotes from storage
  54. async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, FfiError>;
  55. /// Remove mint quote from storage
  56. async fn remove_mint_quote(&self, quote_id: String) -> Result<(), FfiError>;
  57. // Melt Quote Management
  58. /// Add melt quote to storage
  59. async fn add_melt_quote(&self, quote: MeltQuote) -> Result<(), FfiError>;
  60. /// Get melt quote from storage
  61. async fn get_melt_quote(&self, quote_id: String) -> Result<Option<MeltQuote>, FfiError>;
  62. /// Get melt quotes from storage
  63. async fn get_melt_quotes(&self) -> Result<Vec<MeltQuote>, FfiError>;
  64. /// Remove melt quote from storage
  65. async fn remove_melt_quote(&self, quote_id: String) -> Result<(), FfiError>;
  66. // Keys Management
  67. /// Add Keys to storage
  68. async fn add_keys(&self, keyset: KeySet) -> Result<(), FfiError>;
  69. /// Get Keys from storage
  70. async fn get_keys(&self, id: Id) -> Result<Option<Keys>, FfiError>;
  71. /// Remove Keys from storage
  72. async fn remove_keys(&self, id: Id) -> Result<(), FfiError>;
  73. // Proof Management
  74. /// Update the proofs in storage by adding new proofs or removing proofs by their Y value
  75. async fn update_proofs(
  76. &self,
  77. added: Vec<ProofInfo>,
  78. removed_ys: Vec<PublicKey>,
  79. ) -> Result<(), FfiError>;
  80. /// Get proofs from storage
  81. async fn get_proofs(
  82. &self,
  83. mint_url: Option<MintUrl>,
  84. unit: Option<CurrencyUnit>,
  85. state: Option<Vec<ProofState>>,
  86. spending_conditions: Option<Vec<SpendingConditions>>,
  87. ) -> Result<Vec<ProofInfo>, FfiError>;
  88. /// Get balance efficiently using SQL aggregation
  89. async fn get_balance(
  90. &self,
  91. mint_url: Option<MintUrl>,
  92. unit: Option<CurrencyUnit>,
  93. state: Option<Vec<ProofState>>,
  94. ) -> Result<u64, FfiError>;
  95. /// Update proofs state in storage
  96. async fn update_proofs_state(
  97. &self,
  98. ys: Vec<PublicKey>,
  99. state: ProofState,
  100. ) -> Result<(), FfiError>;
  101. // Keyset Counter Management
  102. /// Increment Keyset counter
  103. async fn increment_keyset_counter(&self, keyset_id: Id, count: u32) -> Result<u32, FfiError>;
  104. // Transaction Management
  105. /// Add transaction to storage
  106. async fn add_transaction(&self, transaction: Transaction) -> Result<(), FfiError>;
  107. /// Get transaction from storage
  108. async fn get_transaction(
  109. &self,
  110. transaction_id: TransactionId,
  111. ) -> Result<Option<Transaction>, FfiError>;
  112. /// List transactions from storage
  113. async fn list_transactions(
  114. &self,
  115. mint_url: Option<MintUrl>,
  116. direction: Option<TransactionDirection>,
  117. unit: Option<CurrencyUnit>,
  118. ) -> Result<Vec<Transaction>, FfiError>;
  119. /// Remove transaction from storage
  120. async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), FfiError>;
  121. }
  122. /// Internal bridge trait to convert from the FFI trait to the CDK database trait
  123. /// This allows us to bridge between the UniFFI trait and the CDK's internal database trait
  124. struct WalletDatabaseBridge {
  125. ffi_db: Arc<dyn WalletDatabase>,
  126. }
  127. impl WalletDatabaseBridge {
  128. fn new(ffi_db: Arc<dyn WalletDatabase>) -> Self {
  129. Self { ffi_db }
  130. }
  131. }
  132. impl std::fmt::Debug for WalletDatabaseBridge {
  133. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  134. write!(f, "WalletDatabaseBridge")
  135. }
  136. }
  137. #[async_trait::async_trait]
  138. impl CdkWalletDatabase for WalletDatabaseBridge {
  139. type Err = cdk::cdk_database::Error;
  140. // Mint Management
  141. async fn add_mint(
  142. &self,
  143. mint_url: cdk::mint_url::MintUrl,
  144. mint_info: Option<cdk::nuts::MintInfo>,
  145. ) -> Result<(), Self::Err> {
  146. let ffi_mint_url = mint_url.into();
  147. let ffi_mint_info = mint_info.map(Into::into);
  148. self.ffi_db
  149. .add_mint(ffi_mint_url, ffi_mint_info)
  150. .await
  151. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  152. }
  153. async fn remove_mint(&self, mint_url: cdk::mint_url::MintUrl) -> Result<(), Self::Err> {
  154. let ffi_mint_url = mint_url.into();
  155. self.ffi_db
  156. .remove_mint(ffi_mint_url)
  157. .await
  158. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  159. }
  160. async fn get_mint(
  161. &self,
  162. mint_url: cdk::mint_url::MintUrl,
  163. ) -> Result<Option<cdk::nuts::MintInfo>, Self::Err> {
  164. let ffi_mint_url = mint_url.into();
  165. let result = self
  166. .ffi_db
  167. .get_mint(ffi_mint_url)
  168. .await
  169. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  170. Ok(result.map(Into::into))
  171. }
  172. async fn get_mints(
  173. &self,
  174. ) -> Result<HashMap<cdk::mint_url::MintUrl, Option<cdk::nuts::MintInfo>>, Self::Err> {
  175. let result = self
  176. .ffi_db
  177. .get_mints()
  178. .await
  179. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  180. let mut cdk_result = HashMap::new();
  181. for (ffi_mint_url, mint_info_opt) in result {
  182. let cdk_url = ffi_mint_url
  183. .try_into()
  184. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  185. cdk_result.insert(cdk_url, mint_info_opt.map(Into::into));
  186. }
  187. Ok(cdk_result)
  188. }
  189. async fn update_mint_url(
  190. &self,
  191. old_mint_url: cdk::mint_url::MintUrl,
  192. new_mint_url: cdk::mint_url::MintUrl,
  193. ) -> Result<(), Self::Err> {
  194. let ffi_old_mint_url = old_mint_url.into();
  195. let ffi_new_mint_url = new_mint_url.into();
  196. self.ffi_db
  197. .update_mint_url(ffi_old_mint_url, ffi_new_mint_url)
  198. .await
  199. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  200. }
  201. // Keyset Management
  202. async fn add_mint_keysets(
  203. &self,
  204. mint_url: cdk::mint_url::MintUrl,
  205. keysets: Vec<cdk::nuts::KeySetInfo>,
  206. ) -> Result<(), Self::Err> {
  207. let ffi_mint_url = mint_url.into();
  208. let ffi_keysets: Vec<KeySetInfo> = keysets.into_iter().map(Into::into).collect();
  209. self.ffi_db
  210. .add_mint_keysets(ffi_mint_url, ffi_keysets)
  211. .await
  212. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  213. }
  214. async fn get_mint_keysets(
  215. &self,
  216. mint_url: cdk::mint_url::MintUrl,
  217. ) -> Result<Option<Vec<cdk::nuts::KeySetInfo>>, Self::Err> {
  218. let ffi_mint_url = mint_url.into();
  219. let result = self
  220. .ffi_db
  221. .get_mint_keysets(ffi_mint_url)
  222. .await
  223. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  224. Ok(result.map(|keysets| keysets.into_iter().map(Into::into).collect()))
  225. }
  226. async fn get_keyset_by_id(
  227. &self,
  228. keyset_id: &cdk::nuts::Id,
  229. ) -> Result<Option<cdk::nuts::KeySetInfo>, Self::Err> {
  230. let ffi_id = (*keyset_id).into();
  231. let result = self
  232. .ffi_db
  233. .get_keyset_by_id(ffi_id)
  234. .await
  235. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  236. Ok(result.map(Into::into))
  237. }
  238. // Mint Quote Management
  239. async fn add_mint_quote(&self, quote: cdk::wallet::MintQuote) -> Result<(), Self::Err> {
  240. let ffi_quote = quote.into();
  241. self.ffi_db
  242. .add_mint_quote(ffi_quote)
  243. .await
  244. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  245. }
  246. async fn get_mint_quote(
  247. &self,
  248. quote_id: &str,
  249. ) -> Result<Option<cdk::wallet::MintQuote>, Self::Err> {
  250. let result = self
  251. .ffi_db
  252. .get_mint_quote(quote_id.to_string())
  253. .await
  254. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  255. Ok(result
  256. .map(|q| {
  257. q.try_into()
  258. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  259. })
  260. .transpose()?)
  261. }
  262. async fn get_mint_quotes(&self) -> Result<Vec<cdk::wallet::MintQuote>, Self::Err> {
  263. let result = self
  264. .ffi_db
  265. .get_mint_quotes()
  266. .await
  267. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  268. Ok(result
  269. .into_iter()
  270. .map(|q| {
  271. q.try_into()
  272. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  273. })
  274. .collect::<Result<Vec<_>, _>>()?)
  275. }
  276. async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err> {
  277. self.ffi_db
  278. .remove_mint_quote(quote_id.to_string())
  279. .await
  280. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  281. }
  282. // Melt Quote Management
  283. async fn add_melt_quote(&self, quote: cdk::wallet::MeltQuote) -> Result<(), Self::Err> {
  284. let ffi_quote = quote.into();
  285. self.ffi_db
  286. .add_melt_quote(ffi_quote)
  287. .await
  288. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  289. }
  290. async fn get_melt_quote(
  291. &self,
  292. quote_id: &str,
  293. ) -> Result<Option<cdk::wallet::MeltQuote>, Self::Err> {
  294. let result = self
  295. .ffi_db
  296. .get_melt_quote(quote_id.to_string())
  297. .await
  298. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  299. Ok(result
  300. .map(|q| {
  301. q.try_into()
  302. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  303. })
  304. .transpose()?)
  305. }
  306. async fn get_melt_quotes(&self) -> Result<Vec<cdk::wallet::MeltQuote>, Self::Err> {
  307. let result = self
  308. .ffi_db
  309. .get_melt_quotes()
  310. .await
  311. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  312. Ok(result
  313. .into_iter()
  314. .map(|q| {
  315. q.try_into()
  316. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  317. })
  318. .collect::<Result<Vec<_>, _>>()?)
  319. }
  320. async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err> {
  321. self.ffi_db
  322. .remove_melt_quote(quote_id.to_string())
  323. .await
  324. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  325. }
  326. // Keys Management
  327. async fn add_keys(&self, keyset: cdk::nuts::KeySet) -> Result<(), Self::Err> {
  328. let ffi_keyset: KeySet = keyset.into();
  329. self.ffi_db
  330. .add_keys(ffi_keyset)
  331. .await
  332. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  333. }
  334. async fn get_keys(&self, id: &cdk::nuts::Id) -> Result<Option<cdk::nuts::Keys>, Self::Err> {
  335. let ffi_id: Id = (*id).into();
  336. let result = self
  337. .ffi_db
  338. .get_keys(ffi_id)
  339. .await
  340. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  341. // Convert FFI Keys back to CDK Keys using TryFrom
  342. result
  343. .map(|ffi_keys| {
  344. ffi_keys
  345. .try_into()
  346. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  347. })
  348. .transpose()
  349. }
  350. async fn remove_keys(&self, id: &cdk::nuts::Id) -> Result<(), Self::Err> {
  351. let ffi_id = (*id).into();
  352. self.ffi_db
  353. .remove_keys(ffi_id)
  354. .await
  355. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  356. }
  357. // Proof Management
  358. async fn update_proofs(
  359. &self,
  360. added: Vec<cdk::types::ProofInfo>,
  361. removed_ys: Vec<cdk::nuts::PublicKey>,
  362. ) -> Result<(), Self::Err> {
  363. let ffi_added: Vec<ProofInfo> = added.into_iter().map(Into::into).collect();
  364. let ffi_removed_ys: Vec<PublicKey> = removed_ys.into_iter().map(Into::into).collect();
  365. self.ffi_db
  366. .update_proofs(ffi_added, ffi_removed_ys)
  367. .await
  368. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  369. }
  370. async fn get_proofs(
  371. &self,
  372. mint_url: Option<cdk::mint_url::MintUrl>,
  373. unit: Option<cdk::nuts::CurrencyUnit>,
  374. state: Option<Vec<cdk::nuts::State>>,
  375. spending_conditions: Option<Vec<cdk::nuts::SpendingConditions>>,
  376. ) -> Result<Vec<cdk::types::ProofInfo>, Self::Err> {
  377. let ffi_mint_url = mint_url.map(Into::into);
  378. let ffi_unit = unit.map(Into::into);
  379. let ffi_state = state.map(|s| s.into_iter().map(Into::into).collect());
  380. let ffi_spending_conditions =
  381. spending_conditions.map(|sc| sc.into_iter().map(Into::into).collect());
  382. let result = self
  383. .ffi_db
  384. .get_proofs(ffi_mint_url, ffi_unit, ffi_state, ffi_spending_conditions)
  385. .await
  386. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  387. // Convert back to CDK ProofInfo
  388. let cdk_result: Result<Vec<cdk::types::ProofInfo>, cdk::cdk_database::Error> = result
  389. .into_iter()
  390. .map(|info| {
  391. Ok(cdk::types::ProofInfo {
  392. proof: info.proof.try_into().map_err(|e: FfiError| {
  393. cdk::cdk_database::Error::Database(e.to_string().into())
  394. })?,
  395. y: info.y.try_into().map_err(|e: FfiError| {
  396. cdk::cdk_database::Error::Database(e.to_string().into())
  397. })?,
  398. mint_url: info.mint_url.try_into().map_err(|e: FfiError| {
  399. cdk::cdk_database::Error::Database(e.to_string().into())
  400. })?,
  401. state: info.state.into(),
  402. spending_condition: info
  403. .spending_condition
  404. .map(|sc| sc.try_into())
  405. .transpose()
  406. .map_err(|e: FfiError| {
  407. cdk::cdk_database::Error::Database(e.to_string().into())
  408. })?,
  409. unit: info.unit.into(),
  410. })
  411. })
  412. .collect();
  413. cdk_result
  414. }
  415. async fn get_balance(
  416. &self,
  417. mint_url: Option<cdk::mint_url::MintUrl>,
  418. unit: Option<cdk::nuts::CurrencyUnit>,
  419. state: Option<Vec<cdk::nuts::State>>,
  420. ) -> Result<u64, Self::Err> {
  421. let ffi_mint_url = mint_url.map(Into::into);
  422. let ffi_unit = unit.map(Into::into);
  423. let ffi_state = state.map(|s| s.into_iter().map(Into::into).collect());
  424. self.ffi_db
  425. .get_balance(ffi_mint_url, ffi_unit, ffi_state)
  426. .await
  427. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  428. }
  429. async fn update_proofs_state(
  430. &self,
  431. ys: Vec<cdk::nuts::PublicKey>,
  432. state: cdk::nuts::State,
  433. ) -> Result<(), Self::Err> {
  434. let ffi_ys: Vec<PublicKey> = ys.into_iter().map(Into::into).collect();
  435. let ffi_state = state.into();
  436. self.ffi_db
  437. .update_proofs_state(ffi_ys, ffi_state)
  438. .await
  439. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  440. }
  441. // Keyset Counter Management
  442. async fn increment_keyset_counter(
  443. &self,
  444. keyset_id: &cdk::nuts::Id,
  445. count: u32,
  446. ) -> Result<u32, Self::Err> {
  447. let ffi_id = (*keyset_id).into();
  448. self.ffi_db
  449. .increment_keyset_counter(ffi_id, count)
  450. .await
  451. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  452. }
  453. // Transaction Management
  454. async fn add_transaction(
  455. &self,
  456. transaction: cdk::wallet::types::Transaction,
  457. ) -> Result<(), Self::Err> {
  458. let ffi_transaction = transaction.into();
  459. self.ffi_db
  460. .add_transaction(ffi_transaction)
  461. .await
  462. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  463. }
  464. async fn get_transaction(
  465. &self,
  466. transaction_id: cdk::wallet::types::TransactionId,
  467. ) -> Result<Option<cdk::wallet::types::Transaction>, Self::Err> {
  468. let ffi_id = transaction_id.into();
  469. let result = self
  470. .ffi_db
  471. .get_transaction(ffi_id)
  472. .await
  473. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  474. result
  475. .map(|tx| tx.try_into())
  476. .transpose()
  477. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  478. }
  479. async fn list_transactions(
  480. &self,
  481. mint_url: Option<cdk::mint_url::MintUrl>,
  482. direction: Option<cdk::wallet::types::TransactionDirection>,
  483. unit: Option<cdk::nuts::CurrencyUnit>,
  484. ) -> Result<Vec<cdk::wallet::types::Transaction>, Self::Err> {
  485. let ffi_mint_url = mint_url.map(Into::into);
  486. let ffi_direction = direction.map(Into::into);
  487. let ffi_unit = unit.map(Into::into);
  488. let result = self
  489. .ffi_db
  490. .list_transactions(ffi_mint_url, ffi_direction, ffi_unit)
  491. .await
  492. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  493. result
  494. .into_iter()
  495. .map(|tx| tx.try_into())
  496. .collect::<Result<Vec<_>, FfiError>>()
  497. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  498. }
  499. async fn remove_transaction(
  500. &self,
  501. transaction_id: cdk::wallet::types::TransactionId,
  502. ) -> Result<(), Self::Err> {
  503. let ffi_id = transaction_id.into();
  504. self.ffi_db
  505. .remove_transaction(ffi_id)
  506. .await
  507. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  508. }
  509. }
  510. /// FFI-safe wallet database backend selection
  511. #[derive(uniffi::Enum)]
  512. pub enum WalletDbBackend {
  513. Sqlite {
  514. path: String,
  515. },
  516. #[cfg(feature = "postgres")]
  517. Postgres {
  518. url: String,
  519. },
  520. }
  521. /// Factory helpers returning a CDK wallet database behind the FFI trait
  522. #[uniffi::export]
  523. pub fn create_wallet_db(backend: WalletDbBackend) -> Result<Arc<dyn WalletDatabase>, FfiError> {
  524. match backend {
  525. WalletDbBackend::Sqlite { path } => {
  526. let sqlite = WalletSqliteDatabase::new(path)?;
  527. Ok(sqlite as Arc<dyn WalletDatabase>)
  528. }
  529. #[cfg(feature = "postgres")]
  530. WalletDbBackend::Postgres { url } => {
  531. let pg = WalletPostgresDatabase::new(url)?;
  532. Ok(pg as Arc<dyn WalletDatabase>)
  533. }
  534. }
  535. }
  536. /// Helper function to create a CDK database from the FFI trait
  537. pub fn create_cdk_database_from_ffi(
  538. ffi_db: Arc<dyn WalletDatabase>,
  539. ) -> Arc<dyn CdkWalletDatabase<Err = cdk::cdk_database::Error> + Send + Sync> {
  540. Arc::new(WalletDatabaseBridge::new(ffi_db))
  541. }