multi_mint_wallet.rs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. //! FFI MultiMintWallet bindings
  2. use std::collections::HashMap;
  3. use std::str::FromStr;
  4. use std::sync::Arc;
  5. use bip39::Mnemonic;
  6. use cdk::wallet::multi_mint_wallet::{
  7. MultiMintReceiveOptions as CdkMultiMintReceiveOptions,
  8. MultiMintSendOptions as CdkMultiMintSendOptions, MultiMintWallet as CdkMultiMintWallet,
  9. TokenData as CdkTokenData, TransferMode as CdkTransferMode,
  10. TransferResult as CdkTransferResult,
  11. };
  12. use crate::error::FfiError;
  13. use crate::token::Token;
  14. use crate::types::payment_request::{
  15. CreateRequestParams, CreateRequestResult, NostrWaitInfo, PaymentRequest,
  16. };
  17. use crate::types::*;
  18. /// FFI-compatible MultiMintWallet
  19. #[derive(uniffi::Object)]
  20. pub struct MultiMintWallet {
  21. inner: Arc<CdkMultiMintWallet>,
  22. }
  23. #[uniffi::export(async_runtime = "tokio")]
  24. impl MultiMintWallet {
  25. /// Create a new MultiMintWallet from mnemonic using WalletDatabaseFfi trait
  26. #[uniffi::constructor]
  27. pub fn new(
  28. unit: CurrencyUnit,
  29. mnemonic: String,
  30. db: Arc<dyn crate::database::WalletDatabase>,
  31. ) -> Result<Self, FfiError> {
  32. // Parse mnemonic and generate seed without passphrase
  33. let m = Mnemonic::parse(&mnemonic)
  34. .map_err(|e| FfiError::internal(format!("Invalid mnemonic: {}", e)))?;
  35. let seed = m.to_seed_normalized("");
  36. // Convert the FFI database trait to a CDK database implementation
  37. let localstore = crate::database::create_cdk_database_from_ffi(db);
  38. let wallet = match tokio::runtime::Handle::try_current() {
  39. Ok(handle) => tokio::task::block_in_place(|| {
  40. handle.block_on(async move {
  41. CdkMultiMintWallet::new(localstore, seed, unit.into()).await
  42. })
  43. }),
  44. Err(_) => {
  45. // No current runtime, create a new one
  46. tokio::runtime::Runtime::new()
  47. .map_err(|e| FfiError::internal(format!("Failed to create runtime: {}", e)))?
  48. .block_on(async move {
  49. CdkMultiMintWallet::new(localstore, seed, unit.into()).await
  50. })
  51. }
  52. }?;
  53. Ok(Self {
  54. inner: Arc::new(wallet),
  55. })
  56. }
  57. /// Create a new MultiMintWallet with proxy configuration
  58. #[uniffi::constructor]
  59. pub fn new_with_proxy(
  60. unit: CurrencyUnit,
  61. mnemonic: String,
  62. db: Arc<dyn crate::database::WalletDatabase>,
  63. proxy_url: String,
  64. ) -> Result<Self, FfiError> {
  65. // Parse mnemonic and generate seed without passphrase
  66. let m = Mnemonic::parse(&mnemonic)
  67. .map_err(|e| FfiError::internal(format!("Invalid mnemonic: {}", e)))?;
  68. let seed = m.to_seed_normalized("");
  69. // Convert the FFI database trait to a CDK database implementation
  70. let localstore = crate::database::create_cdk_database_from_ffi(db);
  71. // Parse proxy URL
  72. let proxy_url = url::Url::parse(&proxy_url)
  73. .map_err(|e| FfiError::internal(format!("Invalid URL: {}", e)))?;
  74. let wallet = match tokio::runtime::Handle::try_current() {
  75. Ok(handle) => tokio::task::block_in_place(|| {
  76. handle.block_on(async move {
  77. CdkMultiMintWallet::new_with_proxy(localstore, seed, unit.into(), proxy_url)
  78. .await
  79. })
  80. }),
  81. Err(_) => {
  82. // No current runtime, create a new one
  83. tokio::runtime::Runtime::new()
  84. .map_err(|e| FfiError::internal(format!("Failed to create runtime: {}", e)))?
  85. .block_on(async move {
  86. CdkMultiMintWallet::new_with_proxy(localstore, seed, unit.into(), proxy_url)
  87. .await
  88. })
  89. }
  90. }?;
  91. Ok(Self {
  92. inner: Arc::new(wallet),
  93. })
  94. }
  95. /// Get the currency unit for this wallet
  96. pub fn unit(&self) -> CurrencyUnit {
  97. self.inner.unit().clone().into()
  98. }
  99. /// Set metadata cache TTL (time-to-live) in seconds for a specific mint
  100. ///
  101. /// Controls how long cached mint metadata (keysets, keys, mint info) is considered fresh
  102. /// before requiring a refresh from the mint server for a specific mint.
  103. ///
  104. /// # Arguments
  105. ///
  106. /// * `mint_url` - The mint URL to set the TTL for
  107. /// * `ttl_secs` - Optional TTL in seconds. If None, cache never expires.
  108. pub async fn set_metadata_cache_ttl_for_mint(
  109. &self,
  110. mint_url: MintUrl,
  111. ttl_secs: Option<u64>,
  112. ) -> Result<(), FfiError> {
  113. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  114. let wallets = self.inner.get_wallets().await;
  115. if let Some(wallet) = wallets.iter().find(|w| w.mint_url == cdk_mint_url) {
  116. let ttl = ttl_secs.map(std::time::Duration::from_secs);
  117. wallet.set_metadata_cache_ttl(ttl);
  118. Ok(())
  119. } else {
  120. Err(FfiError::internal(format!(
  121. "Mint not found: {}",
  122. cdk_mint_url
  123. )))
  124. }
  125. }
  126. /// Set metadata cache TTL (time-to-live) in seconds for all mints
  127. ///
  128. /// Controls how long cached mint metadata is considered fresh for all mints
  129. /// in this MultiMintWallet.
  130. ///
  131. /// # Arguments
  132. ///
  133. /// * `ttl_secs` - Optional TTL in seconds. If None, cache never expires for any mint.
  134. pub async fn set_metadata_cache_ttl_for_all_mints(&self, ttl_secs: Option<u64>) {
  135. let wallets = self.inner.get_wallets().await;
  136. let ttl = ttl_secs.map(std::time::Duration::from_secs);
  137. for wallet in wallets.iter() {
  138. wallet.set_metadata_cache_ttl(ttl);
  139. }
  140. }
  141. /// Add a mint to this MultiMintWallet
  142. pub async fn add_mint(
  143. &self,
  144. mint_url: MintUrl,
  145. target_proof_count: Option<u32>,
  146. ) -> Result<(), FfiError> {
  147. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  148. if let Some(count) = target_proof_count {
  149. let config = cdk::wallet::multi_mint_wallet::WalletConfig::new()
  150. .with_target_proof_count(count as usize);
  151. self.inner
  152. .add_mint_with_config(cdk_mint_url, config)
  153. .await?;
  154. } else {
  155. self.inner.add_mint(cdk_mint_url).await?;
  156. }
  157. Ok(())
  158. }
  159. /// Remove mint from MultiMintWallet
  160. ///
  161. /// # Panics
  162. ///
  163. /// Panics if the hardcoded fallback URL is invalid (should never happen).
  164. pub async fn remove_mint(&self, mint_url: MintUrl) {
  165. let url_str = mint_url.url.clone();
  166. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into().unwrap_or_else(|_| {
  167. // If conversion fails, we can't remove the mint, but we shouldn't panic
  168. // This is a best-effort operation
  169. cdk::mint_url::MintUrl::from_str(&url_str).unwrap_or_else(|_| {
  170. // Last resort: create a dummy URL that won't match anything
  171. cdk::mint_url::MintUrl::from_str("https://invalid.mint")
  172. .expect("Valid hardcoded URL")
  173. })
  174. });
  175. self.inner.remove_mint(&cdk_mint_url).await;
  176. }
  177. /// Check if mint is in wallet
  178. pub async fn has_mint(&self, mint_url: MintUrl) -> bool {
  179. if let Ok(cdk_mint_url) = mint_url.try_into() {
  180. self.inner.has_mint(&cdk_mint_url).await
  181. } else {
  182. false
  183. }
  184. }
  185. pub async fn get_mint_keysets(&self, mint_url: MintUrl) -> Result<Vec<KeySetInfo>, FfiError> {
  186. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  187. let keysets = self.inner.get_mint_keysets(&cdk_mint_url).await?;
  188. let keysets = keysets.into_iter().map(|k| k.into()).collect();
  189. Ok(keysets)
  190. }
  191. /// Get token data (mint URL and proofs) from a token
  192. ///
  193. /// This method extracts the mint URL and proofs from a token. It will automatically
  194. /// fetch the keysets from the mint if needed to properly decode the proofs.
  195. ///
  196. /// The mint must already be added to the wallet. If the mint is not in the wallet,
  197. /// use `add_mint` first.
  198. pub async fn get_token_data(&self, token: Arc<Token>) -> Result<TokenData, FfiError> {
  199. let token_data = self.inner.get_token_data(&token.inner).await?;
  200. Ok(token_data.into())
  201. }
  202. /// Get wallet balances for all mints
  203. pub async fn get_balances(&self) -> Result<BalanceMap, FfiError> {
  204. let balances = self.inner.get_balances().await?;
  205. let mut balance_map = HashMap::new();
  206. for (mint_url, amount) in balances {
  207. balance_map.insert(mint_url.to_string(), amount.into());
  208. }
  209. Ok(balance_map)
  210. }
  211. /// Get total balance across all mints
  212. pub async fn total_balance(&self) -> Result<Amount, FfiError> {
  213. let total = self.inner.total_balance().await?;
  214. Ok(total.into())
  215. }
  216. /// List proofs for all mints
  217. pub async fn list_proofs(&self) -> Result<ProofsByMint, FfiError> {
  218. let proofs = self.inner.list_proofs().await?;
  219. let mut proofs_by_mint = HashMap::new();
  220. for (mint_url, mint_proofs) in proofs {
  221. let ffi_proofs: Vec<Proof> = mint_proofs.into_iter().map(|p| p.into()).collect();
  222. proofs_by_mint.insert(mint_url.to_string(), ffi_proofs);
  223. }
  224. Ok(proofs_by_mint)
  225. }
  226. /// Check the state of proofs at a specific mint
  227. pub async fn check_proofs_state(
  228. &self,
  229. mint_url: MintUrl,
  230. proofs: Proofs,
  231. ) -> Result<Vec<ProofState>, FfiError> {
  232. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  233. let cdk_proofs: Result<Vec<cdk::nuts::Proof>, _> =
  234. proofs.into_iter().map(|p| p.try_into()).collect();
  235. let cdk_proofs = cdk_proofs?;
  236. let states = self
  237. .inner
  238. .check_proofs_state(&cdk_mint_url, cdk_proofs)
  239. .await?;
  240. Ok(states.into_iter().map(|s| s.into()).collect())
  241. }
  242. /// Receive token
  243. pub async fn receive(
  244. &self,
  245. token: Arc<Token>,
  246. options: MultiMintReceiveOptions,
  247. ) -> Result<Amount, FfiError> {
  248. let amount = self
  249. .inner
  250. .receive(&token.to_string(), options.into())
  251. .await?;
  252. Ok(amount.into())
  253. }
  254. /// Restore wallets for a specific mint
  255. pub async fn restore(&self, mint_url: MintUrl) -> Result<Restored, FfiError> {
  256. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  257. let restored = self.inner.restore(&cdk_mint_url).await?;
  258. Ok(restored.into())
  259. }
  260. /// Get all pending send operations across all mints
  261. pub async fn get_pending_sends(&self) -> Result<Vec<PendingSend>, FfiError> {
  262. let sends = self.inner.get_pending_sends().await?;
  263. Ok(sends
  264. .into_iter()
  265. .map(|(mint_url, id)| PendingSend {
  266. mint_url: mint_url.into(),
  267. operation_id: id.to_string(),
  268. })
  269. .collect())
  270. }
  271. /// Revoke a pending send operation for a specific mint
  272. pub async fn revoke_send(
  273. &self,
  274. mint_url: MintUrl,
  275. operation_id: String,
  276. ) -> Result<Amount, FfiError> {
  277. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  278. let uuid = uuid::Uuid::parse_str(&operation_id)
  279. .map_err(|e| FfiError::internal(format!("Invalid operation ID: {}", e)))?;
  280. let amount = self.inner.revoke_send(cdk_mint_url, uuid).await?;
  281. Ok(amount.into())
  282. }
  283. /// Check status of a pending send operation for a specific mint
  284. pub async fn check_send_status(
  285. &self,
  286. mint_url: MintUrl,
  287. operation_id: String,
  288. ) -> Result<bool, FfiError> {
  289. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  290. let uuid = uuid::Uuid::parse_str(&operation_id)
  291. .map_err(|e| FfiError::internal(format!("Invalid operation ID: {}", e)))?;
  292. let claimed = self.inner.check_send_status(cdk_mint_url, uuid).await?;
  293. Ok(claimed)
  294. }
  295. /// Send tokens from a specific mint
  296. ///
  297. /// This method prepares and confirms the send in one step.
  298. /// For more control over the send process, use the single-mint Wallet.
  299. pub async fn send(
  300. &self,
  301. mint_url: MintUrl,
  302. amount: Amount,
  303. options: MultiMintSendOptions,
  304. ) -> Result<Token, FfiError> {
  305. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  306. let token = self
  307. .inner
  308. .send(cdk_mint_url, amount.into(), options.into())
  309. .await?;
  310. Ok(token.into())
  311. }
  312. /// Get a mint quote from a specific mint
  313. pub async fn mint_quote(
  314. &self,
  315. mint_url: MintUrl,
  316. amount: Amount,
  317. description: Option<String>,
  318. ) -> Result<MintQuote, FfiError> {
  319. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  320. let quote = self
  321. .inner
  322. .mint_quote(&cdk_mint_url, amount.into(), description)
  323. .await?;
  324. Ok(quote.into())
  325. }
  326. /// Refresh a specific mint quote status from the mint.
  327. /// Updates local store with current state from mint.
  328. /// Does NOT mint tokens - use mint() to mint a specific quote.
  329. pub async fn refresh_mint_quote(
  330. &self,
  331. mint_url: MintUrl,
  332. quote_id: String,
  333. ) -> Result<MintQuote, FfiError> {
  334. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  335. let quote = self
  336. .inner
  337. .refresh_mint_quote(&cdk_mint_url, &quote_id)
  338. .await?;
  339. Ok(quote.into())
  340. }
  341. /// Mint tokens at a specific mint
  342. pub async fn mint(
  343. &self,
  344. mint_url: MintUrl,
  345. quote_id: String,
  346. spending_conditions: Option<SpendingConditions>,
  347. ) -> Result<Proofs, FfiError> {
  348. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  349. let conditions = spending_conditions.map(|sc| sc.try_into()).transpose()?;
  350. let proofs = self
  351. .inner
  352. .mint(
  353. &cdk_mint_url,
  354. &quote_id,
  355. cdk::amount::SplitTarget::default(),
  356. conditions,
  357. )
  358. .await?;
  359. Ok(proofs.into_iter().map(|p| p.into()).collect())
  360. }
  361. /// Wait for a mint quote to be paid and automatically mint the proofs
  362. #[cfg(not(target_arch = "wasm32"))]
  363. pub async fn wait_for_mint_quote(
  364. &self,
  365. mint_url: MintUrl,
  366. quote_id: String,
  367. split_target: SplitTarget,
  368. spending_conditions: Option<SpendingConditions>,
  369. timeout_secs: u64,
  370. ) -> Result<Proofs, FfiError> {
  371. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  372. let conditions = spending_conditions.map(|sc| sc.try_into()).transpose()?;
  373. let timeout = std::time::Duration::from_secs(timeout_secs);
  374. let proofs = self
  375. .inner
  376. .wait_for_mint_quote(
  377. &cdk_mint_url,
  378. &quote_id,
  379. split_target.into(),
  380. conditions,
  381. timeout,
  382. )
  383. .await?;
  384. Ok(proofs.into_iter().map(|p| p.into()).collect())
  385. }
  386. /// Get a melt quote from a specific mint
  387. pub async fn melt_quote(
  388. &self,
  389. mint_url: MintUrl,
  390. payment_method: PaymentMethod,
  391. request: String,
  392. options: Option<MeltOptions>,
  393. extra: Option<String>,
  394. ) -> Result<MeltQuote, FfiError> {
  395. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  396. let cdk_options = options.map(Into::into);
  397. let quote = self
  398. .inner
  399. .melt_quote(&cdk_mint_url, payment_method, request, cdk_options, extra)
  400. .await?;
  401. Ok(quote.into())
  402. }
  403. /// Get a melt quote for a BIP353 human-readable address
  404. ///
  405. /// This method resolves a BIP353 address (e.g., "alice@example.com") to a Lightning offer
  406. /// and then creates a melt quote for that offer at the specified mint.
  407. ///
  408. /// # Arguments
  409. ///
  410. /// * `mint_url` - The mint to use for creating the melt quote
  411. /// * `bip353_address` - Human-readable address in the format "user@domain.com"
  412. /// * `amount_msat` - Amount to pay in millisatoshis
  413. #[cfg(not(target_arch = "wasm32"))]
  414. pub async fn melt_bip353_quote(
  415. &self,
  416. mint_url: MintUrl,
  417. bip353_address: String,
  418. amount_msat: u64,
  419. ) -> Result<MeltQuote, FfiError> {
  420. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  421. let cdk_amount = cdk::Amount::from(amount_msat);
  422. let quote = self
  423. .inner
  424. .melt_bip353_quote(&cdk_mint_url, &bip353_address, cdk_amount)
  425. .await?;
  426. Ok(quote.into())
  427. }
  428. /// Get a melt quote for a Lightning address
  429. ///
  430. /// This method resolves a Lightning address (e.g., "alice@example.com") to a Lightning invoice
  431. /// and then creates a melt quote for that invoice at the specified mint.
  432. ///
  433. /// # Arguments
  434. ///
  435. /// * `mint_url` - The mint to use for creating the melt quote
  436. /// * `lightning_address` - Lightning address in the format "user@domain.com"
  437. /// * `amount_msat` - Amount to pay in millisatoshis
  438. pub async fn melt_lightning_address_quote(
  439. &self,
  440. mint_url: MintUrl,
  441. lightning_address: String,
  442. amount_msat: u64,
  443. ) -> Result<MeltQuote, FfiError> {
  444. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  445. let cdk_amount = cdk::Amount::from(amount_msat);
  446. let quote = self
  447. .inner
  448. .melt_lightning_address_quote(&cdk_mint_url, &lightning_address, cdk_amount)
  449. .await?;
  450. Ok(quote.into())
  451. }
  452. /// Get a melt quote for a human-readable address
  453. ///
  454. /// This method accepts a human-readable address that could be either a BIP353 address
  455. /// or a Lightning address. It intelligently determines which to try based on mint support:
  456. ///
  457. /// 1. If the mint supports Bolt12, it tries BIP353 first
  458. /// 2. Falls back to Lightning address only if BIP353 DNS resolution fails
  459. /// 3. If BIP353 resolves but fails at the mint, it does NOT fall back to Lightning address
  460. /// 4. If the mint doesn't support Bolt12, it tries Lightning address directly
  461. ///
  462. /// # Arguments
  463. ///
  464. /// * `mint_url` - The mint to use for creating the melt quote
  465. /// * `address` - Human-readable address (BIP353 or Lightning address)
  466. /// * `amount_msat` - Amount to pay in millisatoshis
  467. #[cfg(not(target_arch = "wasm32"))]
  468. pub async fn melt_human_readable_quote(
  469. &self,
  470. mint_url: MintUrl,
  471. address: String,
  472. amount_msat: u64,
  473. ) -> Result<MeltQuote, FfiError> {
  474. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  475. let cdk_amount = cdk::Amount::from(amount_msat);
  476. let quote = self
  477. .inner
  478. .melt_human_readable_quote(&cdk_mint_url, &address, cdk_amount)
  479. .await?;
  480. Ok(quote.into())
  481. }
  482. /// Melt tokens
  483. pub async fn melt_with_mint(
  484. &self,
  485. mint_url: MintUrl,
  486. quote_id: String,
  487. ) -> Result<FinalizedMelt, FfiError> {
  488. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  489. let finalized = self.inner.melt_with_mint(&cdk_mint_url, &quote_id).await?;
  490. Ok(finalized.into())
  491. }
  492. /// Melt specific proofs from a specific mint
  493. ///
  494. /// This method allows melting proofs that may not be in the wallet's database,
  495. /// similar to how `receive_proofs` handles external proofs. The proofs will be
  496. /// added to the database and used for the melt operation.
  497. ///
  498. /// # Arguments
  499. ///
  500. /// * `mint_url` - The mint to use for the melt operation
  501. /// * `quote_id` - The melt quote ID (obtained from `melt_quote`)
  502. /// * `proofs` - The proofs to melt (can be external proofs not in the wallet's database)
  503. ///
  504. /// # Returns
  505. ///
  506. /// A `FinalizedMelt` result containing the payment details and any change proofs
  507. pub async fn melt_proofs(
  508. &self,
  509. mint_url: MintUrl,
  510. quote_id: String,
  511. proofs: Proofs,
  512. ) -> Result<FinalizedMelt, FfiError> {
  513. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  514. let cdk_proofs: Result<Vec<cdk::nuts::Proof>, _> =
  515. proofs.into_iter().map(|p| p.try_into()).collect();
  516. let cdk_proofs = cdk_proofs?;
  517. let finalized = self
  518. .inner
  519. .melt_proofs(&cdk_mint_url, &quote_id, cdk_proofs)
  520. .await?;
  521. Ok(finalized.into())
  522. }
  523. /// Check melt quote status
  524. pub async fn check_melt_quote(
  525. &self,
  526. mint_url: MintUrl,
  527. quote_id: String,
  528. ) -> Result<MeltQuote, FfiError> {
  529. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  530. let quote = self
  531. .inner
  532. .check_melt_quote(&cdk_mint_url, &quote_id)
  533. .await?;
  534. Ok(quote.into())
  535. }
  536. /// Melt tokens (pay a bolt11 invoice)
  537. pub async fn melt(
  538. &self,
  539. bolt11: String,
  540. options: Option<MeltOptions>,
  541. max_fee: Option<Amount>,
  542. ) -> Result<FinalizedMelt, FfiError> {
  543. let cdk_options = options.map(Into::into);
  544. let cdk_max_fee = max_fee.map(Into::into);
  545. let finalized = self.inner.melt(&bolt11, cdk_options, cdk_max_fee).await?;
  546. Ok(finalized.into())
  547. }
  548. /// Transfer funds between mints
  549. pub async fn transfer(
  550. &self,
  551. source_mint: MintUrl,
  552. target_mint: MintUrl,
  553. transfer_mode: TransferMode,
  554. ) -> Result<TransferResult, FfiError> {
  555. let source_cdk: cdk::mint_url::MintUrl = source_mint.try_into()?;
  556. let target_cdk: cdk::mint_url::MintUrl = target_mint.try_into()?;
  557. let result = self
  558. .inner
  559. .transfer(&source_cdk, &target_cdk, transfer_mode.into())
  560. .await?;
  561. Ok(result.into())
  562. }
  563. /// Swap proofs with automatic wallet selection
  564. pub async fn swap(
  565. &self,
  566. amount: Option<Amount>,
  567. spending_conditions: Option<SpendingConditions>,
  568. ) -> Result<Option<Proofs>, FfiError> {
  569. let conditions = spending_conditions.map(|sc| sc.try_into()).transpose()?;
  570. let result = self.inner.swap(amount.map(Into::into), conditions).await?;
  571. Ok(result.map(|proofs| proofs.into_iter().map(|p| p.into()).collect()))
  572. }
  573. /// List transactions from all mints
  574. pub async fn list_transactions(
  575. &self,
  576. direction: Option<TransactionDirection>,
  577. ) -> Result<Vec<Transaction>, FfiError> {
  578. let cdk_direction = direction.map(Into::into);
  579. let transactions = self.inner.list_transactions(cdk_direction).await?;
  580. Ok(transactions.into_iter().map(Into::into).collect())
  581. }
  582. /// Get proofs for a transaction by transaction ID
  583. ///
  584. /// This retrieves all proofs associated with a transaction. If `mint_url` is provided,
  585. /// it will only check that specific mint's wallet. Otherwise, it searches across all
  586. /// wallets to find which mint the transaction belongs to.
  587. ///
  588. /// # Arguments
  589. ///
  590. /// * `id` - The transaction ID
  591. /// * `mint_url` - Optional mint URL to check directly, avoiding iteration over all wallets
  592. pub async fn get_proofs_for_transaction(
  593. &self,
  594. id: TransactionId,
  595. mint_url: Option<MintUrl>,
  596. ) -> Result<Vec<Proof>, FfiError> {
  597. let cdk_id = id.try_into()?;
  598. let cdk_mint_url = mint_url.map(|url| url.try_into()).transpose()?;
  599. let proofs = self
  600. .inner
  601. .get_proofs_for_transaction(cdk_id, cdk_mint_url)
  602. .await?;
  603. Ok(proofs.into_iter().map(Into::into).collect())
  604. }
  605. /// Refresh all unissued mint quote states
  606. /// Does NOT mint - use mint_unissued_quotes() for that
  607. pub async fn refresh_all_mint_quotes(
  608. &self,
  609. mint_url: Option<MintUrl>,
  610. ) -> Result<Vec<MintQuote>, FfiError> {
  611. let cdk_mint_url = mint_url.map(|url| url.try_into()).transpose()?;
  612. let quotes = self.inner.refresh_all_mint_quotes(cdk_mint_url).await?;
  613. Ok(quotes.into_iter().map(Into::into).collect())
  614. }
  615. /// Refresh states and mint all unissued quotes
  616. /// Returns total amount minted across all wallets
  617. pub async fn mint_unissued_quotes(
  618. &self,
  619. mint_url: Option<MintUrl>,
  620. ) -> Result<Amount, FfiError> {
  621. let cdk_mint_url = mint_url.map(|url| url.try_into()).transpose()?;
  622. let amount = self.inner.mint_unissued_quotes(cdk_mint_url).await?;
  623. Ok(amount.into())
  624. }
  625. /// Consolidate proofs across all mints
  626. pub async fn consolidate(&self) -> Result<Amount, FfiError> {
  627. let amount = self.inner.consolidate().await?;
  628. Ok(amount.into())
  629. }
  630. /// Get list of mint URLs
  631. pub async fn get_mint_urls(&self) -> Vec<String> {
  632. let wallets = self.inner.get_wallets().await;
  633. wallets.iter().map(|w| w.mint_url.to_string()).collect()
  634. }
  635. /// Get all wallets from MultiMintWallet
  636. pub async fn get_wallets(&self) -> Vec<Arc<crate::wallet::Wallet>> {
  637. let wallets = self.inner.get_wallets().await;
  638. wallets
  639. .into_iter()
  640. .map(|w| Arc::new(crate::wallet::Wallet::from_inner(w)))
  641. .collect()
  642. }
  643. /// Get a specific wallet from MultiMintWallet by mint URL
  644. pub async fn get_wallet(&self, mint_url: MintUrl) -> Option<Arc<crate::wallet::Wallet>> {
  645. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into().ok()?;
  646. let wallet = self.inner.get_wallet(&cdk_mint_url).await?;
  647. Some(Arc::new(crate::wallet::Wallet::from_inner(wallet)))
  648. }
  649. /// Verify token DLEQ proofs
  650. pub async fn verify_token_dleq(&self, token: Arc<Token>) -> Result<(), FfiError> {
  651. let cdk_token = token.inner.clone();
  652. self.inner.verify_token_dleq(&cdk_token).await?;
  653. Ok(())
  654. }
  655. /// Query mint for current mint information
  656. pub async fn fetch_mint_info(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, FfiError> {
  657. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  658. let mint_info = self.inner.fetch_mint_info(&cdk_mint_url).await?;
  659. Ok(mint_info.map(Into::into))
  660. }
  661. /// Get mint info for all wallets
  662. ///
  663. /// This method loads the mint info for each wallet in the MultiMintWallet
  664. /// and returns a map of mint URLs to their corresponding mint info.
  665. ///
  666. /// Uses cached mint info when available, only fetching from the mint if the cache
  667. /// has expired.
  668. pub async fn get_all_mint_info(&self) -> Result<MintInfoMap, FfiError> {
  669. let mint_infos = self.inner.get_all_mint_info().await?;
  670. let mut result = HashMap::new();
  671. for (mint_url, mint_info) in mint_infos {
  672. result.insert(mint_url.to_string(), mint_info.into());
  673. }
  674. Ok(result)
  675. }
  676. }
  677. /// Payment request methods for MultiMintWallet
  678. #[uniffi::export(async_runtime = "tokio")]
  679. impl MultiMintWallet {
  680. /// Pay a NUT-18 PaymentRequest
  681. ///
  682. /// This method handles paying a payment request by selecting an appropriate mint:
  683. /// - If `mint_url` is provided, it verifies the payment request accepts that mint
  684. /// and uses it to pay.
  685. /// - If `mint_url` is None, it automatically selects the mint that:
  686. /// 1. Is accepted by the payment request (matches one of the request's mints, or request accepts any mint)
  687. /// 2. Has the highest balance among matching mints
  688. ///
  689. /// # Arguments
  690. ///
  691. /// * `payment_request` - The NUT-18 payment request to pay
  692. /// * `mint_url` - Optional specific mint to use. If None, automatically selects the best matching mint.
  693. /// * `custom_amount` - Custom amount to pay (required if payment request has no amount)
  694. ///
  695. /// # Errors
  696. ///
  697. /// Returns an error if:
  698. /// - The payment request has no amount and no custom amount is provided
  699. /// - The specified mint is not accepted by the payment request
  700. /// - No matching mint has sufficient balance
  701. /// - No transport is available in the payment request
  702. pub async fn pay_request(
  703. &self,
  704. payment_request: Arc<PaymentRequest>,
  705. mint_url: Option<MintUrl>,
  706. custom_amount: Option<Amount>,
  707. ) -> Result<(), FfiError> {
  708. let cdk_mint_url = mint_url.map(|url| url.try_into()).transpose()?;
  709. let cdk_amount = custom_amount.map(Into::into);
  710. self.inner
  711. .pay_request(payment_request.inner().clone(), cdk_mint_url, cdk_amount)
  712. .await?;
  713. Ok(())
  714. }
  715. /// Create a NUT-18 payment request
  716. ///
  717. /// Creates a payment request that can be shared to receive Cashu tokens.
  718. /// The request can include optional amount, description, and spending conditions.
  719. ///
  720. /// # Arguments
  721. ///
  722. /// * `params` - Parameters for creating the payment request
  723. ///
  724. /// # Transport Options
  725. ///
  726. /// - `"nostr"` - Uses Nostr relays for privacy-preserving delivery (requires nostr_relays)
  727. /// - `"http"` - Uses HTTP POST for delivery (requires http_url)
  728. /// - `"none"` - No transport; token must be delivered out-of-band
  729. ///
  730. /// # Example
  731. ///
  732. /// ```ignore
  733. /// let params = CreateRequestParams {
  734. /// amount: Some(100),
  735. /// unit: "sat".to_string(),
  736. /// description: Some("Coffee payment".to_string()),
  737. /// transport: "http".to_string(),
  738. /// http_url: Some("https://example.com/callback".to_string()),
  739. /// ..Default::default()
  740. /// };
  741. /// let result = wallet.create_request(params).await?;
  742. /// println!("Share this request: {}", result.payment_request.to_string_encoded());
  743. ///
  744. /// // If using Nostr transport, wait for payment:
  745. /// if let Some(nostr_info) = result.nostr_wait_info {
  746. /// let amount = wallet.wait_for_nostr_payment(nostr_info).await?;
  747. /// println!("Received {} sats", amount);
  748. /// }
  749. /// ```
  750. pub async fn create_request(
  751. &self,
  752. params: CreateRequestParams,
  753. ) -> Result<CreateRequestResult, FfiError> {
  754. let (payment_request, nostr_wait_info) = self.inner.create_request(params.into()).await?;
  755. Ok(CreateRequestResult {
  756. payment_request: Arc::new(PaymentRequest::from_inner(payment_request)),
  757. nostr_wait_info: nostr_wait_info.map(|info| Arc::new(NostrWaitInfo::from_inner(info))),
  758. })
  759. }
  760. /// Wait for a Nostr payment and receive it into the wallet
  761. ///
  762. /// This method connects to the Nostr relays specified in the `NostrWaitInfo`,
  763. /// subscribes for incoming payment events, and receives the first valid
  764. /// payment into the wallet.
  765. ///
  766. /// # Arguments
  767. ///
  768. /// * `info` - The Nostr wait info returned from `create_request` when using Nostr transport
  769. ///
  770. /// # Returns
  771. ///
  772. /// The amount received from the payment.
  773. ///
  774. /// # Example
  775. ///
  776. /// ```ignore
  777. /// let result = wallet.create_request(params).await?;
  778. /// if let Some(nostr_info) = result.nostr_wait_info {
  779. /// let amount = wallet.wait_for_nostr_payment(nostr_info).await?;
  780. /// println!("Received {} sats", amount);
  781. /// }
  782. /// ```
  783. pub async fn wait_for_nostr_payment(
  784. &self,
  785. info: Arc<NostrWaitInfo>,
  786. ) -> Result<Amount, FfiError> {
  787. // We need to clone the inner NostrWaitInfo since we can't consume the Arc
  788. let info_inner = cdk::wallet::payment_request::NostrWaitInfo {
  789. keys: info.inner().keys.clone(),
  790. relays: info.inner().relays.clone(),
  791. pubkey: info.inner().pubkey,
  792. };
  793. let amount = self
  794. .inner
  795. .wait_for_nostr_payment(info_inner)
  796. .await
  797. .map_err(FfiError::internal)?;
  798. Ok(amount.into())
  799. }
  800. }
  801. /// Auth methods for MultiMintWallet
  802. #[uniffi::export(async_runtime = "tokio")]
  803. impl MultiMintWallet {
  804. /// Set Clear Auth Token (CAT) for a specific mint
  805. pub async fn set_cat(&self, mint_url: MintUrl, cat: String) -> Result<(), FfiError> {
  806. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  807. self.inner.set_cat(&cdk_mint_url, cat).await?;
  808. Ok(())
  809. }
  810. /// Set refresh token for a specific mint
  811. pub async fn set_refresh_token(
  812. &self,
  813. mint_url: MintUrl,
  814. refresh_token: String,
  815. ) -> Result<(), FfiError> {
  816. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  817. self.inner
  818. .set_refresh_token(&cdk_mint_url, refresh_token)
  819. .await?;
  820. Ok(())
  821. }
  822. /// Refresh access token for a specific mint using the stored refresh token
  823. pub async fn refresh_access_token(&self, mint_url: MintUrl) -> Result<(), FfiError> {
  824. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  825. self.inner.refresh_access_token(&cdk_mint_url).await?;
  826. Ok(())
  827. }
  828. /// Mint blind auth tokens at a specific mint
  829. pub async fn mint_blind_auth(
  830. &self,
  831. mint_url: MintUrl,
  832. amount: Amount,
  833. ) -> Result<Proofs, FfiError> {
  834. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  835. let proofs = self
  836. .inner
  837. .mint_blind_auth(&cdk_mint_url, amount.into())
  838. .await?;
  839. Ok(proofs.into_iter().map(|p| p.into()).collect())
  840. }
  841. /// Get unspent auth proofs for a specific mint
  842. pub async fn get_unspent_auth_proofs(
  843. &self,
  844. mint_url: MintUrl,
  845. ) -> Result<Vec<AuthProof>, FfiError> {
  846. let cdk_mint_url: cdk::mint_url::MintUrl = mint_url.try_into()?;
  847. let auth_proofs = self.inner.get_unspent_auth_proofs(&cdk_mint_url).await?;
  848. Ok(auth_proofs.into_iter().map(Into::into).collect())
  849. }
  850. }
  851. /// Transfer mode for mint-to-mint transfers
  852. #[derive(Debug, Clone, uniffi::Enum)]
  853. pub enum TransferMode {
  854. /// Transfer exact amount to target (target receives specified amount)
  855. ExactReceive { amount: Amount },
  856. /// Transfer all available balance (source will be emptied)
  857. FullBalance,
  858. }
  859. impl From<TransferMode> for CdkTransferMode {
  860. fn from(mode: TransferMode) -> Self {
  861. match mode {
  862. TransferMode::ExactReceive { amount } => CdkTransferMode::ExactReceive(amount.into()),
  863. TransferMode::FullBalance => CdkTransferMode::FullBalance,
  864. }
  865. }
  866. }
  867. /// Result of a transfer operation with detailed breakdown
  868. #[derive(Debug, Clone, uniffi::Record)]
  869. pub struct TransferResult {
  870. /// Amount deducted from source mint
  871. pub amount_sent: Amount,
  872. /// Amount received at target mint
  873. pub amount_received: Amount,
  874. /// Total fees paid for the transfer
  875. pub fees_paid: Amount,
  876. /// Remaining balance in source mint after transfer
  877. pub source_balance_after: Amount,
  878. /// New balance in target mint after transfer
  879. pub target_balance_after: Amount,
  880. }
  881. impl From<CdkTransferResult> for TransferResult {
  882. fn from(result: CdkTransferResult) -> Self {
  883. Self {
  884. amount_sent: result.amount_sent.into(),
  885. amount_received: result.amount_received.into(),
  886. fees_paid: result.fees_paid.into(),
  887. source_balance_after: result.source_balance_after.into(),
  888. target_balance_after: result.target_balance_after.into(),
  889. }
  890. }
  891. }
  892. /// Represents a pending send operation
  893. #[derive(Debug, Clone, uniffi::Record)]
  894. pub struct PendingSend {
  895. /// The mint URL where the send is pending
  896. pub mint_url: MintUrl,
  897. /// The operation ID of the pending send
  898. pub operation_id: String,
  899. }
  900. /// Data extracted from a token including mint URL, proofs, and memo
  901. #[derive(Debug, Clone, uniffi::Record)]
  902. pub struct TokenData {
  903. /// The mint URL from the token
  904. pub mint_url: MintUrl,
  905. /// The proofs contained in the token
  906. pub proofs: Proofs,
  907. /// The memo from the token, if present
  908. pub memo: Option<String>,
  909. /// Value of token
  910. pub value: Amount,
  911. /// Unit of token
  912. pub unit: CurrencyUnit,
  913. /// Fee to redeem
  914. ///
  915. /// If the token is for a proof that we do not know, we cannot get the fee.
  916. /// To avoid just erroring and still allow decoding, this is an option.
  917. /// None does not mean there is no fee, it means we do not know the fee.
  918. pub redeem_fee: Option<Amount>,
  919. }
  920. impl From<CdkTokenData> for TokenData {
  921. fn from(data: CdkTokenData) -> Self {
  922. Self {
  923. mint_url: data.mint_url.into(),
  924. proofs: data.proofs.into_iter().map(|p| p.into()).collect(),
  925. memo: data.memo,
  926. value: data.value.into(),
  927. unit: data.unit.into(),
  928. redeem_fee: data.redeem_fee.map(|a| a.into()),
  929. }
  930. }
  931. }
  932. /// Options for receiving tokens in multi-mint context
  933. #[derive(Debug, Clone, Default, uniffi::Record)]
  934. pub struct MultiMintReceiveOptions {
  935. /// Whether to allow receiving from untrusted (not yet added) mints
  936. pub allow_untrusted: bool,
  937. /// Mint URL to transfer tokens to from untrusted mints (None means keep in original mint)
  938. pub transfer_to_mint: Option<MintUrl>,
  939. /// Base receive options to apply to the wallet receive
  940. pub receive_options: ReceiveOptions,
  941. }
  942. impl From<MultiMintReceiveOptions> for CdkMultiMintReceiveOptions {
  943. fn from(options: MultiMintReceiveOptions) -> Self {
  944. let mut opts = CdkMultiMintReceiveOptions::new();
  945. opts.allow_untrusted = options.allow_untrusted;
  946. opts.transfer_to_mint = options.transfer_to_mint.and_then(|url| url.try_into().ok());
  947. opts.receive_options = options.receive_options.into();
  948. opts
  949. }
  950. }
  951. /// Options for sending tokens in multi-mint context
  952. #[derive(Debug, Clone, Default, uniffi::Record)]
  953. pub struct MultiMintSendOptions {
  954. /// Whether to allow transferring funds from other mints if needed
  955. pub allow_transfer: bool,
  956. /// Maximum amount to transfer from other mints (optional limit)
  957. pub max_transfer_amount: Option<Amount>,
  958. /// Specific mint URLs allowed for transfers (empty means all mints allowed)
  959. pub allowed_mints: Vec<MintUrl>,
  960. /// Specific mint URLs to exclude from transfers
  961. pub excluded_mints: Vec<MintUrl>,
  962. /// Base send options to apply to the wallet send
  963. pub send_options: SendOptions,
  964. }
  965. impl From<MultiMintSendOptions> for CdkMultiMintSendOptions {
  966. fn from(options: MultiMintSendOptions) -> Self {
  967. let mut opts = CdkMultiMintSendOptions::new();
  968. opts.allow_transfer = options.allow_transfer;
  969. opts.max_transfer_amount = options.max_transfer_amount.map(Into::into);
  970. opts.allowed_mints = options
  971. .allowed_mints
  972. .into_iter()
  973. .filter_map(|url| url.try_into().ok())
  974. .collect();
  975. opts.excluded_mints = options
  976. .excluded_mints
  977. .into_iter()
  978. .filter_map(|url| url.try_into().ok())
  979. .collect();
  980. opts.send_options = options.send_options.into();
  981. opts
  982. }
  983. }
  984. /// Nostr backup methods for MultiMintWallet (NUT-XX)
  985. #[uniffi::export(async_runtime = "tokio")]
  986. impl MultiMintWallet {
  987. /// Get the hex-encoded public key used for Nostr mint backup
  988. ///
  989. /// This key is deterministically derived from the wallet seed and can be used
  990. /// to identify and decrypt backup events on Nostr relays.
  991. pub fn backup_public_key(&self) -> Result<String, FfiError> {
  992. let keys = self.inner.backup_keys()?;
  993. Ok(keys.public_key().to_hex())
  994. }
  995. /// Backup the current mint list to Nostr relays
  996. ///
  997. /// Creates an encrypted NIP-78 addressable event containing all mint URLs
  998. /// and publishes it to the specified relays.
  999. ///
  1000. /// # Arguments
  1001. ///
  1002. /// * `relays` - List of Nostr relay URLs (e.g., "wss://relay.damus.io")
  1003. /// * `options` - Backup options including optional client name
  1004. ///
  1005. /// # Example
  1006. ///
  1007. /// ```ignore
  1008. /// let relays = vec!["wss://relay.damus.io".to_string(), "wss://nos.lol".to_string()];
  1009. /// let options = BackupOptions { client: Some("my-wallet".to_string()) };
  1010. /// let result = wallet.backup_mints(relays, options).await?;
  1011. /// println!("Backup published with event ID: {}", result.event_id);
  1012. /// ```
  1013. pub async fn backup_mints(
  1014. &self,
  1015. relays: Vec<String>,
  1016. options: BackupOptions,
  1017. ) -> Result<BackupResult, FfiError> {
  1018. let result = self.inner.backup_mints(relays, options.into()).await?;
  1019. Ok(result.into())
  1020. }
  1021. /// Restore mint list from Nostr relays
  1022. ///
  1023. /// Fetches the most recent backup event from the specified relays,
  1024. /// decrypts it, and optionally adds the discovered mints to the wallet.
  1025. ///
  1026. /// # Arguments
  1027. ///
  1028. /// * `relays` - List of Nostr relay URLs to fetch from
  1029. /// * `add_mints` - If true, automatically add discovered mints to the wallet
  1030. /// * `options` - Restore options including timeout
  1031. ///
  1032. /// # Example
  1033. ///
  1034. /// ```ignore
  1035. /// let relays = vec!["wss://relay.damus.io".to_string()];
  1036. /// let result = wallet.restore_mints(relays, true, RestoreOptions::default()).await?;
  1037. /// println!("Restored {} mints, {} newly added", result.mint_count, result.mints_added);
  1038. /// ```
  1039. pub async fn restore_mints(
  1040. &self,
  1041. relays: Vec<String>,
  1042. add_mints: bool,
  1043. options: RestoreOptions,
  1044. ) -> Result<RestoreResult, FfiError> {
  1045. let result = self
  1046. .inner
  1047. .restore_mints(relays, add_mints, options.into())
  1048. .await?;
  1049. Ok(result.into())
  1050. }
  1051. /// Fetch the backup without adding mints to the wallet
  1052. ///
  1053. /// This is useful for previewing what mints are in the backup before
  1054. /// deciding to add them.
  1055. ///
  1056. /// # Arguments
  1057. ///
  1058. /// * `relays` - List of Nostr relay URLs to fetch from
  1059. /// * `options` - Restore options including timeout
  1060. pub async fn fetch_backup(
  1061. &self,
  1062. relays: Vec<String>,
  1063. options: RestoreOptions,
  1064. ) -> Result<MintBackup, FfiError> {
  1065. let backup = self.inner.fetch_backup(relays, options.into()).await?;
  1066. Ok(backup.into())
  1067. }
  1068. }
  1069. /// Type alias for balances by mint URL
  1070. pub type BalanceMap = HashMap<String, Amount>;
  1071. /// Type alias for proofs by mint URL
  1072. pub type ProofsByMint = HashMap<String, Vec<Proof>>;
  1073. /// Type alias for mint info by mint URL
  1074. pub type MintInfoMap = HashMap<String, MintInfo>;