cdk-ffi 0.17.0

FFI bindings for cdk wallet
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Quote-related FFI types

use serde::{Deserialize, Serialize};

use super::amount::{Amount, CurrencyUnit};
use super::mint::MintUrl;
use crate::error::FfiError;

/// FFI-compatible MintQuote
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MintQuote {
    /// Quote ID
    pub id: String,
    /// Requested or fixed quote amount, when defined by the payment method.
    ///
    /// Variable-amount methods such as onchain leave this unset and track
    /// funds through `amount_paid` and `amount_issued`.
    pub amount: Option<Amount>,
    /// Currency unit
    pub unit: CurrencyUnit,
    /// Payment request
    pub request: String,
    /// Quote state
    pub state: QuoteState,
    /// Expiry timestamp
    pub expiry: u64,
    /// Mint URL
    pub mint_url: MintUrl,
    /// Amount issued
    pub amount_issued: Amount,
    /// Amount paid
    pub amount_paid: Amount,
    /// Estimated confirmation target in blocks for onchain quotes
    pub estimated_blocks: Option<u32>,
    /// Payment method
    pub payment_method: PaymentMethod,
    /// Secret key (optional, hex-encoded)
    pub secret_key: Option<String>,
    /// Operation ID that reserved this quote
    pub used_by_operation: Option<String>,
    /// Version for optimistic locking
    #[serde(default)]
    pub version: u32,
}

impl From<cdk::wallet::MintQuote> for MintQuote {
    fn from(quote: cdk::wallet::MintQuote) -> Self {
        Self {
            id: quote.id.clone(),
            amount: quote.amount.map(Into::into),
            unit: quote.unit.clone().into(),
            request: quote.request.clone(),
            state: quote.state.into(),
            expiry: quote.expiry,
            mint_url: quote.mint_url.clone().into(),
            amount_issued: quote.amount_issued.into(),
            amount_paid: quote.amount_paid.into(),
            estimated_blocks: quote.estimated_blocks,
            payment_method: quote.payment_method.into(),
            secret_key: quote.secret_key.map(|sk| sk.to_secret_hex()),
            used_by_operation: quote.used_by_operation.map(|id| id.to_string()),
            version: quote.version,
        }
    }
}

impl TryFrom<MintQuote> for cdk::wallet::MintQuote {
    type Error = FfiError;

    fn try_from(quote: MintQuote) -> Result<Self, Self::Error> {
        let secret_key = quote
            .secret_key
            .map(|hex| cdk::nuts::SecretKey::from_hex(&hex))
            .transpose()
            .map_err(|e| FfiError::internal(format!("Invalid secret key: {}", e)))?;

        Ok(Self {
            id: quote.id,
            amount: quote.amount.map(Into::into),
            unit: quote.unit.into(),
            request: quote.request,
            state: quote.state.into(),
            expiry: quote.expiry,
            mint_url: quote.mint_url.try_into()?,
            amount_issued: quote.amount_issued.into(),
            amount_paid: quote.amount_paid.into(),
            estimated_blocks: quote.estimated_blocks,
            payment_method: quote.payment_method.into(),
            secret_key,
            used_by_operation: quote.used_by_operation,
            version: quote.version,
        })
    }
}

/// Get total amount for a mint quote (amount paid)
#[uniffi::export]
pub fn mint_quote_total_amount(quote: &MintQuote) -> Result<Amount, FfiError> {
    let cdk_quote: cdk::wallet::MintQuote = quote.clone().try_into()?;
    Ok(cdk_quote.total_amount().into())
}

/// Check if mint quote is expired
#[uniffi::export]
pub fn mint_quote_is_expired(quote: &MintQuote, current_time: u64) -> Result<bool, FfiError> {
    let cdk_quote: cdk::wallet::MintQuote = quote.clone().try_into()?;
    Ok(cdk_quote.is_expired(current_time))
}

/// Get amount that can be minted from a mint quote
#[uniffi::export]
pub fn mint_quote_amount_mintable(quote: &MintQuote) -> Result<Amount, FfiError> {
    let cdk_quote: cdk::wallet::MintQuote = quote.clone().try_into()?;
    Ok(cdk_quote.amount_mintable().into())
}

/// Decode MintQuote from JSON string
#[uniffi::export]
pub fn decode_mint_quote(json: String) -> Result<MintQuote, FfiError> {
    let quote: cdk::wallet::MintQuote = serde_json::from_str(&json)?;
    Ok(quote.into())
}

/// Encode MintQuote to JSON string
#[uniffi::export]
pub fn encode_mint_quote(quote: MintQuote) -> Result<String, FfiError> {
    Ok(serde_json::to_string(&quote)?)
}

/// FFI-compatible MintQuoteBolt11Response
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MintQuoteBolt11Response {
    /// Quote ID
    pub quote: String,
    /// Request string
    pub request: String,
    /// State of the quote
    pub state: QuoteState,
    /// Expiry timestamp (optional)
    pub expiry: Option<u64>,
    /// Amount (optional)
    pub amount: Option<Amount>,
    /// Unit (optional)
    pub unit: Option<CurrencyUnit>,
    /// Pubkey (optional)
    pub pubkey: Option<String>,
}

impl From<cdk::nuts::MintQuoteBolt11Response<String>> for MintQuoteBolt11Response {
    fn from(response: cdk::nuts::MintQuoteBolt11Response<String>) -> Self {
        Self {
            quote: response.quote,
            request: response.request,
            state: response.state.into(),
            expiry: response.expiry,
            amount: response.amount.map(Into::into),
            unit: response.unit.map(Into::into),
            pubkey: response.pubkey.map(|p| p.to_string()),
        }
    }
}

impl From<cdk::wallet::MintQuote> for MintQuoteBolt11Response {
    fn from(quote: cdk::wallet::MintQuote) -> Self {
        Self {
            quote: quote.id,
            request: quote.request,
            state: quote.state.into(),
            expiry: Some(quote.expiry),
            amount: quote.amount.map(Into::into),
            unit: Some(quote.unit.into()),
            pubkey: quote.secret_key.map(|sk| sk.public_key().to_string()),
        }
    }
}

/// FFI-compatible MintQuoteCustomResponse
///
/// This is a unified response type for custom payment methods that includes
/// extra fields for method-specific data (e.g., ehash share).
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MintQuoteCustomResponse {
    /// Quote ID
    pub quote: String,
    /// Request string
    pub request: String,
    /// Expiry timestamp (optional)
    pub expiry: Option<u64>,
    /// Amount (optional)
    pub amount: Option<Amount>,
    /// Amount paid
    pub amount_paid: Amount,
    /// Amount issued
    pub amount_issued: Amount,
    /// Unit (optional)
    pub unit: Option<CurrencyUnit>,
    /// Pubkey (optional)
    pub pubkey: Option<String>,
    /// Extra payment-method-specific fields as JSON string
    ///
    /// These fields are flattened into the JSON representation, allowing
    /// custom payment methods to include additional data without nesting.
    pub extra: Option<String>,
}

impl From<cdk::nuts::MintQuoteCustomResponse<String>> for MintQuoteCustomResponse {
    fn from(response: cdk::nuts::MintQuoteCustomResponse<String>) -> Self {
        let extra = if response.extra.is_null() {
            None
        } else {
            Some(response.extra.to_string())
        };

        Self {
            quote: response.quote,
            request: response.request,
            expiry: response.expiry,
            amount: response.amount.map(Into::into),
            amount_paid: response.amount_paid.into(),
            amount_issued: response.amount_issued.into(),
            unit: response.unit.map(Into::into),
            pubkey: response.pubkey.map(|p| p.to_string()),
            extra,
        }
    }
}

/// FFI-compatible MeltQuoteBolt11Response
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MeltQuoteBolt11Response {
    /// Quote ID
    pub quote: String,
    /// Amount
    pub amount: Amount,
    /// Fee reserve
    pub fee_reserve: Amount,
    /// State of the quote
    pub state: QuoteState,
    /// Expiry timestamp
    pub expiry: u64,
    /// Payment proof (optional)
    pub payment_proof: Option<String>,
    /// Request string (optional)
    pub request: Option<String>,
    /// Unit (optional)
    pub unit: Option<CurrencyUnit>,
}

impl From<cdk::nuts::MeltQuoteBolt11Response<String>> for MeltQuoteBolt11Response {
    fn from(response: cdk::nuts::MeltQuoteBolt11Response<String>) -> Self {
        Self {
            quote: response.quote,
            amount: response.amount.into(),
            fee_reserve: response.fee_reserve.into(),
            state: response.state.into(),
            expiry: response.expiry,
            payment_proof: response.payment_preimage,
            request: response.request,
            unit: response.unit.map(Into::into),
        }
    }
}

/// FFI-compatible MeltQuoteCustomResponse
///
/// This is a unified response type for custom payment methods that includes
/// extra fields for method-specific data.
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MeltQuoteCustomResponse {
    /// Quote ID
    pub quote: String,
    /// Amount
    pub amount: Amount,
    /// Fee reserve
    pub fee_reserve: Option<Amount>,
    /// State of the quote
    pub state: QuoteState,
    /// Expiry timestamp
    pub expiry: u64,
    /// Payment proof (optional)
    pub payment_proof: Option<String>,
    /// Request string (optional)
    pub request: Option<String>,
    /// Unit (optional)
    pub unit: Option<CurrencyUnit>,
    /// Extra payment-method-specific fields as JSON string
    ///
    /// These fields are flattened into the JSON representation, allowing
    /// custom payment methods to include additional data without nesting.
    pub extra: Option<String>,
}

impl From<cdk::nuts::MeltQuoteCustomResponse<String>> for MeltQuoteCustomResponse {
    fn from(response: cdk::nuts::MeltQuoteCustomResponse<String>) -> Self {
        let extra = if response.extra.is_null() {
            None
        } else {
            Some(response.extra.to_string())
        };

        Self {
            quote: response.quote,
            amount: response.amount.into(),
            fee_reserve: response.fee_reserve.map(Into::into),
            state: response.state.into(),
            expiry: response.expiry,
            payment_proof: response.payment_preimage,
            request: response.request,
            unit: response.unit.map(Into::into),
            extra,
        }
    }
}

/// FFI-compatible PaymentMethod
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, uniffi::Enum)]
pub enum PaymentMethod {
    /// Bolt11 payment type
    Bolt11,
    /// Bolt12 payment type
    Bolt12,
    /// Onchain Bitcoin payment type
    Onchain,
    /// Custom payment type
    Custom { method: String },
}

impl From<cdk::nuts::PaymentMethod> for PaymentMethod {
    fn from(method: cdk::nuts::PaymentMethod) -> Self {
        match method.as_str() {
            "bolt11" => Self::Bolt11,
            "bolt12" => Self::Bolt12,
            "onchain" => Self::Onchain,
            s => Self::Custom {
                method: s.to_string(),
            },
        }
    }
}

impl From<PaymentMethod> for cdk::nuts::PaymentMethod {
    fn from(method: PaymentMethod) -> Self {
        match method {
            PaymentMethod::Bolt11 => Self::from("bolt11"),
            PaymentMethod::Bolt12 => Self::from("bolt12"),
            PaymentMethod::Onchain => Self::from("onchain"),
            PaymentMethod::Custom { method } => Self::from(method),
        }
    }
}

/// FFI-compatible MintQuoteOnchainResponse.
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MintQuoteOnchainResponse {
    /// Quote ID
    pub quote: String,
    /// Bitcoin address to pay
    pub request: String,
    /// Unit
    pub unit: CurrencyUnit,
    /// Expiry timestamp
    pub expiry: Option<u64>,
    /// NUT-20 public key
    pub pubkey: String,
    /// Total confirmed amount paid to the onchain address
    pub amount_paid: Amount,
    /// Amount already issued for this quote
    pub amount_issued: Amount,
}

impl From<cdk::nuts::MintQuoteOnchainResponse<String>> for MintQuoteOnchainResponse {
    fn from(response: cdk::nuts::MintQuoteOnchainResponse<String>) -> Self {
        Self {
            quote: response.quote,
            request: response.request,
            unit: response.unit.into(),
            expiry: response.expiry,
            pubkey: response.pubkey.to_string(),
            amount_paid: response.amount_paid.into(),
            amount_issued: response.amount_issued.into(),
        }
    }
}

/// Fee option for an onchain melt quote.
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MeltQuoteOnchainFeeOption {
    /// Server-assigned identifier the wallet echoes back to select this option
    pub fee_index: u32,
    /// Maximum onchain transaction fee the mint may charge
    pub fee_reserve: Amount,
    /// Estimated confirmation target in blocks
    pub estimated_blocks: u32,
}

impl From<cdk::nuts::nut30::MeltQuoteOnchainFeeOption> for MeltQuoteOnchainFeeOption {
    fn from(option: cdk::nuts::nut30::MeltQuoteOnchainFeeOption) -> Self {
        Self {
            fee_index: option.fee_index,
            fee_reserve: option.fee_reserve.into(),
            estimated_blocks: option.estimated_blocks,
        }
    }
}

/// FFI-compatible MeltQuoteOnchainResponse.
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MeltQuoteOnchainResponse {
    /// Quote ID
    pub quote: String,
    /// Amount being paid to the onchain address
    pub amount: Amount,
    /// Unit
    pub unit: CurrencyUnit,
    /// Quote state
    pub state: QuoteState,
    /// Expiry timestamp
    pub expiry: u64,
    /// Bitcoin address to pay
    pub request: String,
    /// Available onchain fee options
    pub fee_options: Vec<MeltQuoteOnchainFeeOption>,
    /// Selected fee option index, once execution has started
    pub selected_fee_index: Option<u32>,
    /// Broadcast outpoint (`txid:vout`), once available
    pub outpoint: Option<String>,
    /// Change blind signatures as JSON, when the mint returns change
    pub change: Option<String>,
}

impl From<cdk::nuts::MeltQuoteOnchainResponse<String>> for MeltQuoteOnchainResponse {
    fn from(response: cdk::nuts::MeltQuoteOnchainResponse<String>) -> Self {
        let change = response
            .change
            .as_ref()
            .and_then(|change| serde_json::to_string(change).ok());

        Self {
            quote: response.quote,
            amount: response.amount.into(),
            unit: response.unit.into(),
            state: response.state.into(),
            expiry: response.expiry,
            request: response.request,
            fee_options: response.fee_options.into_iter().map(Into::into).collect(),
            selected_fee_index: response.selected_fee_index,
            outpoint: response.outpoint,
            change,
        }
    }
}

/// FFI-compatible MeltQuote
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MeltQuote {
    /// Quote ID
    pub id: String,
    /// Mint URL
    pub mint_url: Option<MintUrl>,
    /// Quote amount
    pub amount: Amount,
    /// Currency unit
    pub unit: CurrencyUnit,
    /// Payment request
    pub request: String,
    /// Fee reserve
    pub fee_reserve: Amount,
    /// Quote state
    pub state: QuoteState,
    /// Expiry timestamp
    pub expiry: u64,
    /// Payment proof (e.g. Lightning preimage or onchain outpoint)
    pub payment_proof: Option<String>,
    /// Estimated confirmation target in blocks for onchain quotes
    pub estimated_blocks: Option<u32>,
    /// Selected fee option index for onchain quotes
    pub fee_index: Option<u32>,
    /// Payment method
    pub payment_method: PaymentMethod,
    /// Operation ID that reserved this quote
    pub used_by_operation: Option<String>,
    /// Version for optimistic locking
    #[serde(default)]
    pub version: u32,
}

impl From<cdk::wallet::MeltQuote> for MeltQuote {
    fn from(quote: cdk::wallet::MeltQuote) -> Self {
        Self {
            id: quote.id.clone(),
            mint_url: quote.mint_url.map(Into::into),
            amount: quote.amount.into(),
            unit: quote.unit.clone().into(),
            request: quote.request.clone(),
            fee_reserve: quote.fee_reserve.into(),
            state: quote.state.into(),
            expiry: quote.expiry,
            payment_proof: quote.payment_proof.clone(),
            estimated_blocks: quote.estimated_blocks,
            fee_index: quote.fee_index,
            payment_method: quote.payment_method.into(),
            used_by_operation: quote.used_by_operation.map(|id| id.to_string()),
            version: quote.version,
        }
    }
}

impl TryFrom<MeltQuote> for cdk::wallet::MeltQuote {
    type Error = FfiError;

    fn try_from(quote: MeltQuote) -> Result<Self, Self::Error> {
        Ok(Self {
            id: quote.id,
            mint_url: quote.mint_url.map(|m| m.try_into()).transpose()?,
            amount: quote.amount.into(),
            unit: quote.unit.into(),
            request: quote.request,
            fee_reserve: quote.fee_reserve.into(),
            state: quote.state.into(),
            expiry: quote.expiry,
            payment_proof: quote.payment_proof,
            estimated_blocks: quote.estimated_blocks,
            fee_index: quote.fee_index,
            payment_method: quote.payment_method.into(),
            used_by_operation: quote.used_by_operation,
            version: quote.version,
        })
    }
}

impl MeltQuote {
    /// Convert MeltQuote to JSON string
    pub fn to_json(&self) -> Result<String, FfiError> {
        Ok(serde_json::to_string(self)?)
    }
}

/// Decode MeltQuote from JSON string
#[uniffi::export]
pub fn decode_melt_quote(json: String) -> Result<MeltQuote, FfiError> {
    let quote: cdk::wallet::MeltQuote = serde_json::from_str(&json)?;
    Ok(quote.into())
}

/// Encode MeltQuote to JSON string
#[uniffi::export]
pub fn encode_melt_quote(quote: MeltQuote) -> Result<String, FfiError> {
    Ok(serde_json::to_string(&quote)?)
}

/// FFI-compatible QuoteState
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, uniffi::Enum)]
pub enum QuoteState {
    Unpaid,
    Paid,
    Pending,
    Issued,
}

impl From<cdk::nuts::nut05::QuoteState> for QuoteState {
    fn from(state: cdk::nuts::nut05::QuoteState) -> Self {
        match state {
            cdk::nuts::nut05::QuoteState::Unpaid => QuoteState::Unpaid,
            cdk::nuts::nut05::QuoteState::Paid => QuoteState::Paid,
            cdk::nuts::nut05::QuoteState::Pending => QuoteState::Pending,
            cdk::nuts::nut05::QuoteState::Unknown => QuoteState::Unpaid,
            cdk::nuts::nut05::QuoteState::Failed => QuoteState::Unpaid,
        }
    }
}

impl From<QuoteState> for cdk::nuts::nut05::QuoteState {
    fn from(state: QuoteState) -> Self {
        match state {
            QuoteState::Unpaid => cdk::nuts::nut05::QuoteState::Unpaid,
            QuoteState::Paid => cdk::nuts::nut05::QuoteState::Paid,
            QuoteState::Pending => cdk::nuts::nut05::QuoteState::Pending,
            QuoteState::Issued => cdk::nuts::nut05::QuoteState::Paid, // Map issued to paid for melt quotes
        }
    }
}

impl From<cdk::nuts::MintQuoteState> for QuoteState {
    fn from(state: cdk::nuts::MintQuoteState) -> Self {
        match state {
            cdk::nuts::MintQuoteState::Unpaid => QuoteState::Unpaid,
            cdk::nuts::MintQuoteState::Paid => QuoteState::Paid,
            cdk::nuts::MintQuoteState::Issued => QuoteState::Issued,
        }
    }
}

impl From<QuoteState> for cdk::nuts::MintQuoteState {
    fn from(state: QuoteState) -> Self {
        match state {
            QuoteState::Unpaid => cdk::nuts::MintQuoteState::Unpaid,
            QuoteState::Paid => cdk::nuts::MintQuoteState::Paid,
            QuoteState::Issued => cdk::nuts::MintQuoteState::Issued,
            QuoteState::Pending => cdk::nuts::MintQuoteState::Unpaid,
        }
    }
}

// Note: MeltQuoteState is the same as nut05::QuoteState, so we don't need a separate impl