database.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. //! FFI Database bindings
  2. use std::collections::HashMap;
  3. use std::sync::Arc;
  4. use cdk::cdk_database::WalletDatabase as CdkWalletDatabase;
  5. use cdk_common::database::{DbTransactionFinalizer, WalletDatabaseTransaction};
  6. use crate::error::FfiError;
  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 get_mint(
  142. &self,
  143. mint_url: cdk::mint_url::MintUrl,
  144. ) -> Result<Option<cdk::nuts::MintInfo>, Self::Err> {
  145. let ffi_mint_url = mint_url.into();
  146. let result = self
  147. .ffi_db
  148. .get_mint(ffi_mint_url)
  149. .await
  150. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  151. Ok(result.map(Into::into))
  152. }
  153. async fn get_mints(
  154. &self,
  155. ) -> Result<HashMap<cdk::mint_url::MintUrl, Option<cdk::nuts::MintInfo>>, Self::Err> {
  156. let result = self
  157. .ffi_db
  158. .get_mints()
  159. .await
  160. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  161. let mut cdk_result = HashMap::new();
  162. for (ffi_mint_url, mint_info_opt) in result {
  163. let cdk_url = ffi_mint_url
  164. .try_into()
  165. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  166. cdk_result.insert(cdk_url, mint_info_opt.map(Into::into));
  167. }
  168. Ok(cdk_result)
  169. }
  170. // Keyset Management
  171. async fn get_mint_keysets(
  172. &self,
  173. mint_url: cdk::mint_url::MintUrl,
  174. ) -> Result<Option<Vec<cdk::nuts::KeySetInfo>>, Self::Err> {
  175. let ffi_mint_url = mint_url.into();
  176. let result = self
  177. .ffi_db
  178. .get_mint_keysets(ffi_mint_url)
  179. .await
  180. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  181. Ok(result.map(|keysets| keysets.into_iter().map(Into::into).collect()))
  182. }
  183. async fn get_keyset_by_id(
  184. &self,
  185. keyset_id: &cdk::nuts::Id,
  186. ) -> Result<Option<cdk::nuts::KeySetInfo>, Self::Err> {
  187. let ffi_id = (*keyset_id).into();
  188. let result = self
  189. .ffi_db
  190. .get_keyset_by_id(ffi_id)
  191. .await
  192. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  193. Ok(result.map(Into::into))
  194. }
  195. // Mint Quote Management
  196. async fn get_mint_quote(
  197. &self,
  198. quote_id: &str,
  199. ) -> Result<Option<cdk::wallet::MintQuote>, Self::Err> {
  200. let result = self
  201. .ffi_db
  202. .get_mint_quote(quote_id.to_string())
  203. .await
  204. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  205. Ok(result
  206. .map(|q| {
  207. q.try_into()
  208. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  209. })
  210. .transpose()?)
  211. }
  212. async fn get_mint_quotes(&self) -> Result<Vec<cdk::wallet::MintQuote>, Self::Err> {
  213. let result = self
  214. .ffi_db
  215. .get_mint_quotes()
  216. .await
  217. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  218. Ok(result
  219. .into_iter()
  220. .map(|q| {
  221. q.try_into()
  222. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  223. })
  224. .collect::<Result<Vec<_>, _>>()?)
  225. }
  226. // Melt Quote Management
  227. async fn get_melt_quote(
  228. &self,
  229. quote_id: &str,
  230. ) -> Result<Option<cdk::wallet::MeltQuote>, Self::Err> {
  231. let result = self
  232. .ffi_db
  233. .get_melt_quote(quote_id.to_string())
  234. .await
  235. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  236. Ok(result
  237. .map(|q| {
  238. q.try_into()
  239. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  240. })
  241. .transpose()?)
  242. }
  243. async fn get_melt_quotes(&self) -> Result<Vec<cdk::wallet::MeltQuote>, Self::Err> {
  244. let result = self
  245. .ffi_db
  246. .get_melt_quotes()
  247. .await
  248. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  249. Ok(result
  250. .into_iter()
  251. .map(|q| {
  252. q.try_into()
  253. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  254. })
  255. .collect::<Result<Vec<_>, _>>()?)
  256. }
  257. // Keys Management
  258. async fn get_keys(&self, id: &cdk::nuts::Id) -> Result<Option<cdk::nuts::Keys>, Self::Err> {
  259. let ffi_id: Id = (*id).into();
  260. let result = self
  261. .ffi_db
  262. .get_keys(ffi_id)
  263. .await
  264. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  265. // Convert FFI Keys back to CDK Keys using TryFrom
  266. result
  267. .map(|ffi_keys| {
  268. ffi_keys
  269. .try_into()
  270. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  271. })
  272. .transpose()
  273. }
  274. // Proof Management
  275. async fn get_proofs(
  276. &self,
  277. mint_url: Option<cdk::mint_url::MintUrl>,
  278. unit: Option<cdk::nuts::CurrencyUnit>,
  279. state: Option<Vec<cdk::nuts::State>>,
  280. spending_conditions: Option<Vec<cdk::nuts::SpendingConditions>>,
  281. ) -> Result<Vec<cdk::types::ProofInfo>, Self::Err> {
  282. let ffi_mint_url = mint_url.map(Into::into);
  283. let ffi_unit = unit.map(Into::into);
  284. let ffi_state = state.map(|s| s.into_iter().map(Into::into).collect());
  285. let ffi_spending_conditions =
  286. spending_conditions.map(|sc| sc.into_iter().map(Into::into).collect());
  287. let result = self
  288. .ffi_db
  289. .get_proofs(ffi_mint_url, ffi_unit, ffi_state, ffi_spending_conditions)
  290. .await
  291. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  292. // Convert back to CDK ProofInfo
  293. let cdk_result: Result<Vec<cdk::types::ProofInfo>, cdk::cdk_database::Error> = result
  294. .into_iter()
  295. .map(|info| {
  296. Ok(cdk::types::ProofInfo {
  297. proof: info.proof.try_into().map_err(|e: FfiError| {
  298. cdk::cdk_database::Error::Database(e.to_string().into())
  299. })?,
  300. y: info.y.try_into().map_err(|e: FfiError| {
  301. cdk::cdk_database::Error::Database(e.to_string().into())
  302. })?,
  303. mint_url: info.mint_url.try_into().map_err(|e: FfiError| {
  304. cdk::cdk_database::Error::Database(e.to_string().into())
  305. })?,
  306. state: info.state.into(),
  307. spending_condition: info
  308. .spending_condition
  309. .map(|sc| sc.try_into())
  310. .transpose()
  311. .map_err(|e: FfiError| {
  312. cdk::cdk_database::Error::Database(e.to_string().into())
  313. })?,
  314. unit: info.unit.into(),
  315. })
  316. })
  317. .collect();
  318. cdk_result
  319. }
  320. async fn get_balance(
  321. &self,
  322. mint_url: Option<cdk::mint_url::MintUrl>,
  323. unit: Option<cdk::nuts::CurrencyUnit>,
  324. state: Option<Vec<cdk::nuts::State>>,
  325. ) -> Result<u64, Self::Err> {
  326. let ffi_mint_url = mint_url.map(Into::into);
  327. let ffi_unit = unit.map(Into::into);
  328. let ffi_state = state.map(|s| s.into_iter().map(Into::into).collect());
  329. self.ffi_db
  330. .get_balance(ffi_mint_url, ffi_unit, ffi_state)
  331. .await
  332. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  333. }
  334. // Transaction Management
  335. async fn get_transaction(
  336. &self,
  337. transaction_id: cdk::wallet::types::TransactionId,
  338. ) -> Result<Option<cdk::wallet::types::Transaction>, Self::Err> {
  339. let ffi_id = transaction_id.into();
  340. let result = self
  341. .ffi_db
  342. .get_transaction(ffi_id)
  343. .await
  344. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  345. result
  346. .map(|tx| tx.try_into())
  347. .transpose()
  348. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  349. }
  350. async fn list_transactions(
  351. &self,
  352. mint_url: Option<cdk::mint_url::MintUrl>,
  353. direction: Option<cdk::wallet::types::TransactionDirection>,
  354. unit: Option<cdk::nuts::CurrencyUnit>,
  355. ) -> Result<Vec<cdk::wallet::types::Transaction>, Self::Err> {
  356. let ffi_mint_url = mint_url.map(Into::into);
  357. let ffi_direction = direction.map(Into::into);
  358. let ffi_unit = unit.map(Into::into);
  359. let result = self
  360. .ffi_db
  361. .list_transactions(ffi_mint_url, ffi_direction, ffi_unit)
  362. .await
  363. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  364. result
  365. .into_iter()
  366. .map(|tx| tx.try_into())
  367. .collect::<Result<Vec<_>, FfiError>>()
  368. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  369. }
  370. async fn begin_db_transaction<'a>(
  371. &'a self,
  372. ) -> Result<Box<dyn WalletDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Self::Err>
  373. {
  374. Ok(Box::new(WalletDatabaseTransactionBridge {
  375. ffi_db: Arc::clone(&self.ffi_db),
  376. }))
  377. }
  378. }
  379. /// Transaction bridge for FFI wallet database
  380. struct WalletDatabaseTransactionBridge {
  381. ffi_db: Arc<dyn WalletDatabase>,
  382. }
  383. #[async_trait::async_trait]
  384. impl<'a> WalletDatabaseTransaction<'a, cdk::cdk_database::Error>
  385. for WalletDatabaseTransactionBridge
  386. {
  387. async fn add_mint(
  388. &mut self,
  389. mint_url: cdk::mint_url::MintUrl,
  390. mint_info: Option<cdk::nuts::MintInfo>,
  391. ) -> Result<(), cdk::cdk_database::Error> {
  392. let ffi_mint_url = mint_url.into();
  393. let ffi_mint_info = mint_info.map(Into::into);
  394. self.ffi_db
  395. .add_mint(ffi_mint_url, ffi_mint_info)
  396. .await
  397. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  398. }
  399. async fn remove_mint(
  400. &mut self,
  401. mint_url: cdk::mint_url::MintUrl,
  402. ) -> Result<(), cdk::cdk_database::Error> {
  403. let ffi_mint_url = mint_url.into();
  404. self.ffi_db
  405. .remove_mint(ffi_mint_url)
  406. .await
  407. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  408. }
  409. async fn update_mint_url(
  410. &mut self,
  411. old_mint_url: cdk::mint_url::MintUrl,
  412. new_mint_url: cdk::mint_url::MintUrl,
  413. ) -> Result<(), cdk::cdk_database::Error> {
  414. let ffi_old_mint_url = old_mint_url.into();
  415. let ffi_new_mint_url = new_mint_url.into();
  416. self.ffi_db
  417. .update_mint_url(ffi_old_mint_url, ffi_new_mint_url)
  418. .await
  419. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  420. }
  421. async fn add_mint_keysets(
  422. &mut self,
  423. mint_url: cdk::mint_url::MintUrl,
  424. keysets: Vec<cdk::nuts::KeySetInfo>,
  425. ) -> Result<(), cdk::cdk_database::Error> {
  426. let ffi_mint_url = mint_url.into();
  427. let ffi_keysets: Vec<KeySetInfo> = keysets.into_iter().map(Into::into).collect();
  428. self.ffi_db
  429. .add_mint_keysets(ffi_mint_url, ffi_keysets)
  430. .await
  431. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  432. }
  433. async fn add_mint_quote(
  434. &mut self,
  435. quote: cdk::wallet::MintQuote,
  436. ) -> Result<(), cdk::cdk_database::Error> {
  437. let ffi_quote = quote.into();
  438. self.ffi_db
  439. .add_mint_quote(ffi_quote)
  440. .await
  441. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  442. }
  443. async fn remove_mint_quote(&mut self, quote_id: &str) -> Result<(), cdk::cdk_database::Error> {
  444. self.ffi_db
  445. .remove_mint_quote(quote_id.to_string())
  446. .await
  447. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  448. }
  449. async fn add_melt_quote(
  450. &mut self,
  451. quote: cdk::wallet::MeltQuote,
  452. ) -> Result<(), cdk::cdk_database::Error> {
  453. let ffi_quote = quote.into();
  454. self.ffi_db
  455. .add_melt_quote(ffi_quote)
  456. .await
  457. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  458. }
  459. async fn remove_melt_quote(&mut self, quote_id: &str) -> Result<(), cdk::cdk_database::Error> {
  460. self.ffi_db
  461. .remove_melt_quote(quote_id.to_string())
  462. .await
  463. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  464. }
  465. async fn add_keys(
  466. &mut self,
  467. keyset: cdk::nuts::KeySet,
  468. ) -> Result<(), cdk::cdk_database::Error> {
  469. let ffi_keyset: KeySet = keyset.into();
  470. self.ffi_db
  471. .add_keys(ffi_keyset)
  472. .await
  473. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  474. }
  475. async fn remove_keys(&mut self, id: &cdk::nuts::Id) -> Result<(), cdk::cdk_database::Error> {
  476. let ffi_id = (*id).into();
  477. self.ffi_db
  478. .remove_keys(ffi_id)
  479. .await
  480. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  481. }
  482. async fn update_proofs(
  483. &mut self,
  484. added: Vec<cdk::types::ProofInfo>,
  485. removed_ys: Vec<cdk::nuts::PublicKey>,
  486. ) -> Result<(), cdk::cdk_database::Error> {
  487. let ffi_added: Vec<ProofInfo> = added.into_iter().map(Into::into).collect();
  488. let ffi_removed_ys: Vec<PublicKey> = removed_ys.into_iter().map(Into::into).collect();
  489. self.ffi_db
  490. .update_proofs(ffi_added, ffi_removed_ys)
  491. .await
  492. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  493. }
  494. async fn update_proofs_state(
  495. &mut self,
  496. ys: Vec<cdk::nuts::PublicKey>,
  497. state: cdk::nuts::State,
  498. ) -> Result<(), cdk::cdk_database::Error> {
  499. let ffi_ys: Vec<PublicKey> = ys.into_iter().map(Into::into).collect();
  500. let ffi_state = state.into();
  501. self.ffi_db
  502. .update_proofs_state(ffi_ys, ffi_state)
  503. .await
  504. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  505. }
  506. async fn increment_keyset_counter(
  507. &mut self,
  508. keyset_id: &cdk::nuts::Id,
  509. count: u32,
  510. ) -> Result<u32, cdk::cdk_database::Error> {
  511. let ffi_id = (*keyset_id).into();
  512. self.ffi_db
  513. .increment_keyset_counter(ffi_id, count)
  514. .await
  515. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  516. }
  517. async fn add_transaction(
  518. &mut self,
  519. transaction: cdk::wallet::types::Transaction,
  520. ) -> Result<(), cdk::cdk_database::Error> {
  521. let ffi_transaction = transaction.into();
  522. self.ffi_db
  523. .add_transaction(ffi_transaction)
  524. .await
  525. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  526. }
  527. async fn remove_transaction(
  528. &mut self,
  529. transaction_id: cdk::wallet::types::TransactionId,
  530. ) -> Result<(), cdk::cdk_database::Error> {
  531. let ffi_id = transaction_id.into();
  532. self.ffi_db
  533. .remove_transaction(ffi_id)
  534. .await
  535. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))
  536. }
  537. // Read methods needed during transactions
  538. async fn get_keyset_by_id(
  539. &mut self,
  540. keyset_id: &cdk::nuts::Id,
  541. ) -> Result<Option<cdk::nuts::KeySetInfo>, cdk::cdk_database::Error> {
  542. let ffi_id = (*keyset_id).into();
  543. let result = self
  544. .ffi_db
  545. .get_keyset_by_id(ffi_id)
  546. .await
  547. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  548. Ok(result.map(Into::into))
  549. }
  550. async fn get_keys(
  551. &mut self,
  552. id: &cdk::nuts::Id,
  553. ) -> Result<Option<cdk::nuts::Keys>, cdk::cdk_database::Error> {
  554. let ffi_id = (*id).into();
  555. let result = self
  556. .ffi_db
  557. .get_keys(ffi_id)
  558. .await
  559. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  560. match result {
  561. Some(keys) => Ok(Some(keys.try_into().map_err(|e: FfiError| {
  562. cdk::cdk_database::Error::Database(e.to_string().into())
  563. })?)),
  564. None => Ok(None),
  565. }
  566. }
  567. async fn get_mint_quote(
  568. &mut self,
  569. quote_id: &str,
  570. ) -> Result<Option<cdk::wallet::MintQuote>, cdk::cdk_database::Error> {
  571. let result = self
  572. .ffi_db
  573. .get_mint_quote(quote_id.to_string())
  574. .await
  575. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  576. Ok(result
  577. .map(|q| {
  578. q.try_into()
  579. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  580. })
  581. .transpose()?)
  582. }
  583. async fn get_melt_quote(
  584. &mut self,
  585. quote_id: &str,
  586. ) -> Result<Option<cdk::wallet::MeltQuote>, cdk::cdk_database::Error> {
  587. let result = self
  588. .ffi_db
  589. .get_melt_quote(quote_id.to_string())
  590. .await
  591. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  592. Ok(result
  593. .map(|q| {
  594. q.try_into()
  595. .map_err(|e: FfiError| cdk::cdk_database::Error::Database(e.to_string().into()))
  596. })
  597. .transpose()?)
  598. }
  599. async fn get_proofs(
  600. &mut self,
  601. mint_url: Option<cdk::mint_url::MintUrl>,
  602. unit: Option<cdk::nuts::CurrencyUnit>,
  603. state: Option<Vec<cdk::nuts::State>>,
  604. spending_conditions: Option<Vec<cdk::nuts::SpendingConditions>>,
  605. ) -> Result<Vec<cdk::types::ProofInfo>, cdk::cdk_database::Error> {
  606. let ffi_mint_url = mint_url.map(Into::into);
  607. let ffi_unit = unit.map(Into::into);
  608. let ffi_state = state.map(|s| s.into_iter().map(Into::into).collect());
  609. let ffi_spending_conditions =
  610. spending_conditions.map(|sc| sc.into_iter().map(Into::into).collect());
  611. let result = self
  612. .ffi_db
  613. .get_proofs(ffi_mint_url, ffi_unit, ffi_state, ffi_spending_conditions)
  614. .await
  615. .map_err(|e| cdk::cdk_database::Error::Database(e.to_string().into()))?;
  616. // Convert back to CDK ProofInfo
  617. let cdk_result: Result<Vec<cdk::types::ProofInfo>, cdk::cdk_database::Error> = result
  618. .into_iter()
  619. .map(|info| {
  620. Ok(cdk::types::ProofInfo {
  621. proof: info.proof.try_into().map_err(|e: FfiError| {
  622. cdk::cdk_database::Error::Database(e.to_string().into())
  623. })?,
  624. y: info.y.try_into().map_err(|e: FfiError| {
  625. cdk::cdk_database::Error::Database(e.to_string().into())
  626. })?,
  627. mint_url: info.mint_url.try_into().map_err(|e: FfiError| {
  628. cdk::cdk_database::Error::Database(e.to_string().into())
  629. })?,
  630. state: info.state.into(),
  631. spending_condition: info
  632. .spending_condition
  633. .map(|sc| sc.try_into())
  634. .transpose()
  635. .map_err(|e: FfiError| {
  636. cdk::cdk_database::Error::Database(e.to_string().into())
  637. })?,
  638. unit: info.unit.into(),
  639. })
  640. })
  641. .collect();
  642. cdk_result
  643. }
  644. }
  645. #[async_trait::async_trait]
  646. impl DbTransactionFinalizer for WalletDatabaseTransactionBridge {
  647. type Err = cdk::cdk_database::Error;
  648. async fn commit(self: Box<Self>) -> Result<(), cdk::cdk_database::Error> {
  649. // FFI databases handle transactions internally, no-op here
  650. Ok(())
  651. }
  652. async fn rollback(self: Box<Self>) -> Result<(), cdk::cdk_database::Error> {
  653. // FFI databases handle transactions internally, no-op here
  654. Ok(())
  655. }
  656. }
  657. /// FFI-safe wallet database backend selection
  658. #[derive(uniffi::Enum)]
  659. pub enum WalletDbBackend {
  660. Sqlite {
  661. path: String,
  662. },
  663. #[cfg(feature = "postgres")]
  664. Postgres {
  665. url: String,
  666. },
  667. }
  668. /// Factory helpers returning a CDK wallet database behind the FFI trait
  669. #[uniffi::export]
  670. pub fn create_wallet_db(backend: WalletDbBackend) -> Result<Arc<dyn WalletDatabase>, FfiError> {
  671. match backend {
  672. WalletDbBackend::Sqlite { path } => {
  673. let sqlite = WalletSqliteDatabase::new(path)?;
  674. Ok(sqlite as Arc<dyn WalletDatabase>)
  675. }
  676. WalletDbBackend::Postgres { url } => {
  677. let pg = WalletPostgresDatabase::new(url)?;
  678. Ok(pg as Arc<dyn WalletDatabase>)
  679. }
  680. }
  681. }
  682. /// Helper function to create a CDK database from the FFI trait
  683. pub fn create_cdk_database_from_ffi(
  684. ffi_db: Arc<dyn WalletDatabase>,
  685. ) -> Arc<dyn CdkWalletDatabase<Err = cdk::cdk_database::Error> + Send + Sync> {
  686. Arc::new(WalletDatabaseBridge::new(ffi_db))
  687. }