Skip to main content

cdk_ffi/types/
nostr_backup.rs

1//! FFI types for Nostr mint backup (NUT-27)
2
3use cdk::wallet::{
4    BackupOptions as CdkBackupOptions, BackupResult as CdkBackupResult,
5    RestoreOptions as CdkRestoreOptions, RestoreResult as CdkRestoreResult,
6};
7
8use super::MintUrl;
9
10/// Options for backup operations
11#[derive(Debug, Clone, Default, uniffi::Record)]
12pub struct BackupOptions {
13    /// Client name to include in the event tags
14    pub client: Option<String>,
15}
16
17impl From<BackupOptions> for CdkBackupOptions {
18    fn from(options: BackupOptions) -> Self {
19        let mut opts = CdkBackupOptions::new();
20        if let Some(client) = options.client {
21            opts = opts.client(client);
22        }
23        opts
24    }
25}
26
27/// Options for restore operations
28#[derive(Debug, Clone, uniffi::Record)]
29pub struct RestoreOptions {
30    /// Timeout in seconds for waiting for relay responses
31    pub timeout_secs: u64,
32}
33
34impl Default for RestoreOptions {
35    fn default() -> Self {
36        Self { timeout_secs: 10 }
37    }
38}
39
40impl From<RestoreOptions> for CdkRestoreOptions {
41    fn from(options: RestoreOptions) -> Self {
42        CdkRestoreOptions::new().timeout(std::time::Duration::from_secs(options.timeout_secs))
43    }
44}
45
46/// Result of a backup operation
47#[derive(Debug, Clone, uniffi::Record)]
48pub struct BackupResult {
49    /// The event ID of the published backup (hex encoded)
50    pub event_id: String,
51    /// The public key used for the backup (hex encoded)
52    pub public_key: String,
53    /// Number of mints backed up
54    pub mint_count: u64,
55}
56
57impl From<CdkBackupResult> for BackupResult {
58    fn from(result: CdkBackupResult) -> Self {
59        Self {
60            event_id: result.event_id.to_hex(),
61            public_key: result.public_key.to_hex(),
62            mint_count: result.mint_count as u64,
63        }
64    }
65}
66
67/// Result of a restore operation
68#[derive(Debug, Clone, uniffi::Record)]
69pub struct RestoreResult {
70    /// The restored mint backup data
71    pub backup: MintBackup,
72    /// Number of mints found in the backup
73    pub mint_count: u64,
74    /// Number of mints that were newly added (not already in wallet)
75    pub mints_added: u64,
76}
77
78impl From<CdkRestoreResult> for RestoreResult {
79    fn from(result: CdkRestoreResult) -> Self {
80        Self {
81            backup: result.backup.into(),
82            mint_count: result.mint_count as u64,
83            mints_added: result.mints_added as u64,
84        }
85    }
86}
87
88/// Mint backup data containing the list of mints and timestamp
89#[derive(Debug, Clone, uniffi::Record)]
90pub struct MintBackup {
91    /// List of mint URLs in the backup
92    pub mints: Vec<MintUrl>,
93    /// Unix timestamp of when the backup was created
94    pub timestamp: u64,
95}
96
97impl From<cdk::nuts::nut27::MintBackup> for MintBackup {
98    fn from(backup: cdk::nuts::nut27::MintBackup) -> Self {
99        Self {
100            mints: backup.mints.into_iter().map(|m| m.into()).collect(),
101            timestamp: backup.timestamp,
102        }
103    }
104}