cdk 0.16.0

Core Cashu Development Kit library implementing the Cashu protocol
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
//! BIP 321 Payment Instruction Helpers
//!
//! This module provides helper functions for reading and creating BIP 321 `bitcoin:` URIs
//! that can include cashu payment requests (NUT-26 `creq` parameter) alongside other
//! payment methods (BOLT11, BOLT12, on-chain addresses).
//!
//! # Parsing
//!
//! Use [`parse_payment_instruction`] to parse a BIP 321 URI or standalone
//! payment string into [`ParsedPaymentInstruction`]. A [`bitcoin::Network`]
//! must be provided so that on-chain addresses are validated against the
//! correct network.
//!
//! # Creating
//!
//! Use [`Bip321UriBuilder`] to construct a BIP 321 `bitcoin:` URI via the builder
//! pattern and call `.to_string()` to produce the final URI string.
//! A [`PaymentRequest`] can also be converted directly via the
//! [`PaymentRequestBip321Ext::to_bip321`] extension method.
//!
//! # Example
//!
//! ```no_run
//! use cdk::wallet::bip321::{parse_payment_instruction, Bip321UriBuilder};
//!
//! # async fn example() -> Result<(), cdk::error::Error> {
//! // Create a BIP 321 URI with a cashu payment request
//! let uri = Bip321UriBuilder::new()
//!     .with_cashu_request_str("CREQB1...".to_string())
//!     .to_string();
//!
//! // Parse it back (network is required for on-chain address validation)
//! let parsed = parse_payment_instruction(&uri, bitcoin::Network::Bitcoin).await?;
//! assert_eq!(parsed.cashu_requests.len(), 1);
//! # Ok(())
//! # }
//! ```

use core::fmt;
use std::str::FromStr;
#[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
use std::sync::Arc;

use bitcoin_payment_instructions::hrn_resolution::DummyHrnResolver;
use bitcoin_payment_instructions::{
    PaymentInstructions, PaymentMethod as BpiPaymentMethod, PossiblyResolvedPaymentMethod,
};
use tracing::instrument;
use url::form_urlencoded;

#[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
use crate::bip353::Bip353Address;
use crate::error::Error;
use crate::nuts::PaymentRequest;
#[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
use crate::wallet::MintConnector;

/// Parsed payment instruction with all available payment methods.
#[derive(Debug, Clone, Default)]
pub struct ParsedPaymentInstruction {
    /// Cashu NUT-26 payment requests found in the instruction.
    pub cashu_requests: Vec<PaymentRequest>,
    /// BOLT11 invoice strings found.
    pub bolt11_invoices: Vec<String>,
    /// BOLT12 offer strings found.
    pub bolt12_offers: Vec<String>,
    /// On-chain bitcoin addresses found.
    pub onchain_addresses: Vec<String>,
    /// Description / label / message from the URI.
    pub description: Option<String>,
    /// Amount in millisatoshis (if a fixed-amount instruction).
    pub amount_msats: Option<u64>,
    /// Whether the amount is configurable (vs fixed).
    pub is_configurable_amount: bool,
}

/// Builder for BIP 321 `bitcoin:` URIs.
///
/// # Example
///
/// ```no_run
/// use cdk::wallet::bip321::Bip321UriBuilder;
///
/// let uri = Bip321UriBuilder::new()
///     .with_onchain_address("bc1q...".to_string())
///     .with_amount_sats(100_000)
///     .with_message("Coffee payment".to_string())
///     .to_string();
/// ```
#[derive(Debug, Clone, Default)]
pub struct Bip321UriBuilder {
    /// A cashu payment request as a CREQB1 bech32m string.
    cashu_request_str: Option<String>,
    /// A BOLT11 invoice string.
    bolt11_invoice: Option<String>,
    /// A BOLT12 offer string.
    bolt12_offer: Option<String>,
    /// An on-chain bitcoin address.
    onchain_address: Option<String>,
    /// Amount in satoshis.
    amount_sats: Option<u64>,
    /// Label for the payment (shown to user, not sent to payee).
    label: Option<String>,
    /// Message for the payment (displayed to user as a note).
    message: Option<String>,
}

impl Bip321UriBuilder {
    /// Create a new empty builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the cashu payment request from a raw CREQB1 bech32m string.
    pub fn with_cashu_request_str(mut self, creq: String) -> Self {
        self.cashu_request_str = Some(creq);
        self
    }

    /// Set the cashu payment request from a CDK [`PaymentRequest`].
    ///
    /// The request is encoded to bech32m format internally.
    pub fn with_cashu_request(mut self, request: &PaymentRequest) -> Result<Self, Error> {
        let bech32_str = request
            .to_bech32_string()
            .map_err(|e| Error::Bip321Encode(e.to_string()))?;
        self.cashu_request_str = Some(bech32_str);
        Ok(self)
    }

    /// Set the BOLT11 invoice string.
    pub fn with_bolt11_invoice(mut self, invoice: String) -> Self {
        self.bolt11_invoice = Some(invoice);
        self
    }

    /// Set the BOLT12 offer string.
    pub fn with_bolt12_offer(mut self, offer: String) -> Self {
        self.bolt12_offer = Some(offer);
        self
    }

    /// Set the on-chain bitcoin address.
    pub fn with_onchain_address(mut self, address: String) -> Self {
        self.onchain_address = Some(address);
        self
    }

    /// Set the amount in satoshis.
    ///
    /// BIP 21 encodes amount values in BTC, so satoshis are formatted at
    /// render time.
    pub fn with_amount_sats(mut self, sats: u64) -> Self {
        self.amount_sats = Some(sats);
        self
    }

    /// Set the label for the payment (shown to user, not sent to payee).
    pub fn with_label(mut self, label: String) -> Self {
        self.label = Some(label);
        self
    }

    /// Set the message for the payment (displayed to user as a note).
    pub fn with_message(mut self, message: String) -> Self {
        self.message = Some(message);
        self
    }
}

/// Extension trait for converting [`PaymentRequest`] values to BIP 321 builders.
pub trait PaymentRequestBip321Ext {
    /// Convert this request into a builder with the `creq=` field pre-populated.
    fn to_bip321(&self) -> Result<Bip321UriBuilder, Error>;
}

impl PaymentRequestBip321Ext for PaymentRequest {
    fn to_bip321(&self) -> Result<Bip321UriBuilder, Error> {
        Bip321UriBuilder::new().with_cashu_request(self)
    }
}

/// Parse a BIP 321 `bitcoin:` URI or standalone payment instruction string,
/// validating on-chain addresses against the given [`bitcoin::Network`].
///
/// Supports BIP 321 URIs and standalone Cashu, BOLT11, BOLT12, and on-chain inputs.
/// Human-readable name resolution (`user@domain.com`, BIP 353, LNURL) is not supported.
#[instrument(skip_all)]
pub async fn parse_payment_instruction(
    instruction: &str,
    network: bitcoin::Network,
) -> Result<ParsedPaymentInstruction, Error> {
    let resolver = DummyHrnResolver;

    let parsed = PaymentInstructions::parse(instruction, network, &resolver, false)
        .await
        .map_err(|e| Error::Bip321Parse(format!("{e:?}")))?;

    convert_payment_instructions(parsed)
}

/// Resolve a BIP353 address and parse the resulting concrete payment instruction.
///
/// This is the shared low-level helper for "resolve first, inspect later" flows. It accepts
/// any [`MintConnector`] so callers do not need a full [`crate::wallet::Wallet`] just to resolve and inspect a
/// human-readable payment instruction.
///
/// The `network` parameter is forwarded to [`parse_payment_instruction`] and controls
/// which on-chain address prefixes are accepted in the resolved URI.
#[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
#[instrument(skip_all)]
pub async fn resolve_bip353_payment_instruction(
    client: &Arc<dyn MintConnector + Send + Sync>,
    bip353_address: &str,
    network: bitcoin::Network,
) -> Result<ParsedPaymentInstruction, Error> {
    let address = Bip353Address::from_str(bip353_address).map_err(|e| {
        tracing::error!("Failed to parse BIP353 address '{}': {}", bip353_address, e);
        Error::Bip353Parse(e.to_string())
    })?;

    tracing::debug!("Resolving BIP353 address: {}", address);
    let address_string = address.to_string();

    let resolved_uri = address.resolve(client).await.map_err(|e| {
        tracing::error!(
            "Failed to resolve BIP353 address '{}': {}",
            address_string,
            e
        );
        Error::Bip353Resolve(e.to_string())
    })?;

    parse_payment_instruction(&resolved_uri, network)
        .await
        .map_err(|e| {
            tracing::error!(
                "Failed to parse resolved BIP353 payment instruction '{}': {}",
                resolved_uri,
                e
            );
            Error::Bip321Parse(format!("Invalid resolved bitcoin URI: {e}"))
        })
}

fn convert_payment_instructions(
    parsed: PaymentInstructions,
) -> Result<ParsedPaymentInstruction, Error> {
    match parsed {
        PaymentInstructions::FixedAmount(fixed) => {
            let max_amount_msats = fixed.max_amount().map(|a| a.milli_sats());
            extract_payment_methods(
                fixed.methods(),
                fixed.recipient_description().map(|d| d.to_string()),
                max_amount_msats,
                false,
            )
        }
        PaymentInstructions::ConfigurableAmount(configurable) => extract_payment_methods(
            configurable.methods().filter_map(|method| match method {
                PossiblyResolvedPaymentMethod::Resolved(method) => Some(method),
                _ => None,
            }),
            configurable.recipient_description().map(|d| d.to_string()),
            None,
            true,
        ),
    }
}

/// Extract CDK types from BPI payment methods.
fn extract_payment_methods<'a>(
    methods: impl IntoIterator<Item = &'a BpiPaymentMethod>,
    description: Option<String>,
    max_amount_msats: Option<u64>,
    is_configurable: bool,
) -> Result<ParsedPaymentInstruction, Error> {
    let mut result = ParsedPaymentInstruction {
        description,
        amount_msats: max_amount_msats,
        is_configurable_amount: is_configurable,
        ..Default::default()
    };

    for method in methods {
        match method {
            BpiPaymentMethod::Cashu(cashu_req) => {
                // Round-trip through bech32m string for safe conversion
                let bech32_str = cashu_req.to_string();
                let cdk_req = PaymentRequest::from_str(&bech32_str)
                    .map_err(|e| Error::Bip321Parse(e.to_string()))?;
                result.cashu_requests.push(cdk_req);
            }
            BpiPaymentMethod::LightningBolt11(invoice) => {
                result.bolt11_invoices.push(invoice.to_string());
            }
            BpiPaymentMethod::LightningBolt12(offer) => {
                result.bolt12_offers.push(offer.to_string());
            }
            BpiPaymentMethod::OnChain(address) => {
                result.onchain_addresses.push(address.to_string());
            }
        }
    }

    Ok(result)
}

impl fmt::Display for Bip321UriBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("bitcoin:")?;

        // On-chain address goes in the path
        if let Some(ref addr) = self.onchain_address {
            f.write_str(addr)?;
        }

        let mut query_params = form_urlencoded::Serializer::new(String::new());

        if let Some(amount_sats) = self.amount_sats {
            let amount_btc = format_btc_amount_from_sats(amount_sats);
            query_params.append_pair("amount", &amount_btc);
        }

        if let Some(ref label) = self.label {
            query_params.append_pair("label", label);
        }

        if let Some(ref message) = self.message {
            query_params.append_pair("message", message);
        }

        if let Some(ref bolt11) = self.bolt11_invoice {
            query_params.append_pair("lightning", bolt11);
        }

        if let Some(ref bolt12) = self.bolt12_offer {
            query_params.append_pair("lno", bolt12);
        }

        if let Some(ref creq) = self.cashu_request_str {
            query_params.append_pair("creq", creq);
        }

        let query_string = query_params.finish();
        if !query_string.is_empty() {
            f.write_str("?")?;
            f.write_str(&query_string)?;
        }

        Ok(())
    }
}

/// Format satoshis to BTC string without trailing zeros, per BIP 21.
fn format_btc_amount_from_sats(sats: u64) -> String {
    let whole = sats / 100_000_000;
    let fractional = sats % 100_000_000;

    if fractional == 0 {
        return whole.to_string();
    }

    let formatted = format!("{whole}.{fractional:08}");
    formatted.trim_end_matches('0').to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_CREQ: &str = "CREQB1QYQQWER9D4HNZV3NQGQQSQQQQQQQQQQRAQPSQQGQQSQQZQG9QQVXSAR5WPEN5TE0D45KUAPWV4UXZMTSD3JJUCM0D5RQQRJRDANXVET9YPCXZ7TDV4H8GXHR3TQ";
    const TEST_BOLT11: &str = "lnbc1pvjluezsp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq9qrsgq357wnc5r2ueh7ck6q93dj32dlqnls087fxdwk8qakdyafkq3yap9us6v52vjjsrvywa6rt52cm9r9zqt8r2t7mlcwspyetp5h2tztugp9lfyql";

    fn test_payment_request() -> PaymentRequest {
        PaymentRequest::from_str(TEST_CREQ).expect("should parse test vector")
    }

    fn assert_has_creq(uri: &str) {
        assert!(uri.starts_with("bitcoin:?") || uri.starts_with("bitcoin:bc1"));
        assert!(uri.contains("creq="));
    }

    fn assert_demo_cashu(parsed: &ParsedPaymentInstruction) {
        assert_eq!(parsed.cashu_requests.len(), 1);
        assert!(!parsed.is_configurable_amount);

        let req = &parsed.cashu_requests[0];
        assert_eq!(req.payment_id, Some("demo123".to_string()));
        assert_eq!(req.amount, Some(1000.into()));
    }

    #[test]
    fn test_bip321_uri_cashu_only() {
        let uri = Bip321UriBuilder::new()
            .with_cashu_request_str(TEST_CREQ.to_string())
            .to_string();

        assert_has_creq(&uri);
        assert!(uri.contains(TEST_CREQ));
    }

    #[test]
    fn test_bip321_uri_multi_method() {
        let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
        let uri = Bip321UriBuilder::new()
            .with_cashu_request_str(TEST_CREQ.to_string())
            .with_onchain_address(addr.to_string())
            .with_amount_sats(100_000)
            .with_message("Coffee payment".to_string())
            .to_string();

        assert!(uri.starts_with(&format!("bitcoin:{addr}?")));
        assert!(uri.contains("creq="));
        assert!(uri.contains("amount=0.001"));
        assert!(uri.contains("message=Coffee+payment"));
    }

    #[test]
    fn test_bip321_uri_empty_params() {
        let uri = Bip321UriBuilder::new().to_string();
        assert_eq!(uri, "bitcoin:");
    }

    #[test]
    fn test_format_btc_amount_from_sats() {
        assert_eq!(format_btc_amount_from_sats(100_000_000), "1");
        assert_eq!(format_btc_amount_from_sats(100_000), "0.001");
        assert_eq!(format_btc_amount_from_sats(1), "0.00000001");
        assert_eq!(format_btc_amount_from_sats(2_100_000_000), "21");
    }

    #[test]
    fn test_query_params_are_encoded() {
        let uri = Bip321UriBuilder::new()
            .with_label("a=b&c=d".to_string())
            .with_message("hello world".to_string())
            .to_string();
        assert_eq!(uri, "bitcoin:?label=a%3Db%26c%3Dd&message=hello+world");
    }

    #[tokio::test]
    async fn test_parse_standalone_cashu_request() {
        let parsed = parse_payment_instruction(TEST_CREQ, bitcoin::Network::Bitcoin)
            .await
            .expect("should parse standalone CREQB1");
        assert_demo_cashu(&parsed);
    }

    #[tokio::test]
    async fn test_parse_bip321_uri_with_cashu() {
        let uri = format!("bitcoin:?creq={TEST_CREQ}");
        let parsed = parse_payment_instruction(&uri, bitcoin::Network::Bitcoin)
            .await
            .expect("should parse bitcoin: URI with creq");
        assert_demo_cashu(&parsed);
    }

    #[tokio::test]
    async fn test_parse_bip321_uri_with_cashu_and_amount() {
        let uri = format!("bitcoin:?creq={TEST_CREQ}&amount=0.00001");
        let parsed = parse_payment_instruction(&uri, bitcoin::Network::Bitcoin)
            .await
            .expect("should parse bitcoin: URI with creq and amount");

        assert_eq!(parsed.cashu_requests.len(), 1);
        assert_eq!(parsed.amount_msats, Some(1_000_000));
    }

    #[tokio::test]
    async fn test_roundtrip_create_then_parse() {
        let uri = Bip321UriBuilder::new()
            .with_cashu_request_str(TEST_CREQ.to_string())
            .to_string();

        let parsed = parse_payment_instruction(&uri, bitcoin::Network::Bitcoin)
            .await
            .expect("should parse created URI");
        assert_demo_cashu(&parsed);
    }

    #[tokio::test]
    async fn test_parse_standalone_bolt11() {
        let result = parse_payment_instruction(TEST_BOLT11, bitcoin::Network::Bitcoin).await;
        // This specific invoice may or may not parse depending on checksum
        // validity, but we verify the parser at least tries.
        if let Ok(parsed) = result {
            assert!(!parsed.bolt11_invoices.is_empty());
        }
    }

    #[test]
    fn test_bip321_uri_with_payment_request() {
        let payment_request = test_payment_request();

        let uri = Bip321UriBuilder::new()
            .with_cashu_request(&payment_request)
            .expect("should encode request")
            .to_string();
        assert_has_creq(&uri);
    }

    #[test]
    fn test_payment_request_to_bip321() {
        let payment_request = test_payment_request();

        let uri = payment_request
            .to_bip321()
            .expect("should create builder")
            .to_string();
        assert_has_creq(&uri);
    }

    #[test]
    fn test_payment_request_to_bip321_then_customise() {
        let payment_request = test_payment_request();

        let uri = payment_request
            .to_bip321()
            .expect("should create builder")
            .with_onchain_address("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq".to_string())
            .with_amount_sats(100_000)
            .to_string();
        assert!(uri.contains("creq="));
        assert!(uri.contains("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"));
        assert!(uri.contains("amount=0.001"));
    }

    #[tokio::test]
    async fn test_parse_garbage_input_returns_error() {
        let result =
            parse_payment_instruction("not-a-valid-anything", bitcoin::Network::Bitcoin).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_format_btc_amount_from_sats_zero() {
        assert_eq!(format_btc_amount_from_sats(0), "0");
    }

    #[test]
    fn test_bip321_uri_bolt12_only() {
        let offer = "lno1qgsqtest";
        let uri = Bip321UriBuilder::new()
            .with_bolt12_offer(offer.to_string())
            .to_string();
        assert!(uri.starts_with("bitcoin:?"));
        assert!(uri.contains(&format!("lno={offer}")));
        assert!(!uri.contains("creq="));
        assert!(!uri.contains("lightning="));
    }

    #[test]
    fn test_bip321_uri_with_label() {
        let uri = Bip321UriBuilder::new()
            .with_label("Donation".to_string())
            .to_string();
        assert!(uri.contains("label=Donation"));
    }

    #[tokio::test]
    async fn test_parse_for_different_networks_both_succeed_for_non_onchain() {
        // Cashu payment requests have no on-chain component, so they should
        // parse successfully regardless of the network parameter.
        let mainnet_result = parse_payment_instruction(TEST_CREQ, bitcoin::Network::Bitcoin).await;
        let testnet_result = parse_payment_instruction(TEST_CREQ, bitcoin::Network::Testnet).await;

        assert!(mainnet_result.is_ok());
        assert!(testnet_result.is_ok());
    }
}