cashu 0.17.3

Cashu shared types and crypto utilities, used as the foundation for the CDK and their crates
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
//! Specific Subscription for the cdk crate
use serde::de::{DeserializeOwned, Error as DeError};
use serde::{Deserialize, Serialize};

use super::PublicKey;
use crate::nut00::KnownMethod;
use crate::nut25::MeltQuoteBolt12Response;
use crate::nut30::{MeltQuoteOnchainResponse, MintQuoteOnchainResponse};
use crate::nuts::{
    CurrencyUnit, MeltQuoteBolt11Response, MeltQuoteCustomResponse, MintQuoteBolt11Response,
    MintQuoteCustomResponse, PaymentMethod, ProofState,
};
use crate::quote_id::QuoteIdError;
use crate::MintQuoteBolt12Response;

pub mod ws;

/// Subscription Parameter according to the standard
#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash, Deserialize)]
#[serde(bound = "I: DeserializeOwned + Serialize")]
pub struct Params<I> {
    /// Kind
    pub kind: Kind,
    /// Filters
    pub filters: Vec<String>,
    /// Subscription Id
    #[serde(rename = "subId")]
    pub id: I,
}

/// Check state Settings
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SupportedSettings {
    /// Supported methods
    pub supported: Vec<SupportedMethods>,
}

/// Supported WS Methods
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SupportedMethods {
    /// Payment Method
    pub method: PaymentMethod,
    /// Unit
    pub unit: CurrencyUnit,
    /// Command
    pub commands: Vec<WsCommand>,
}

impl SupportedMethods {
    /// Create [`SupportedMethods`]
    pub fn new(method: PaymentMethod, unit: CurrencyUnit, commands: Vec<WsCommand>) -> Self {
        Self {
            method,
            unit,
            commands,
        }
    }

    /// Create [`SupportedMethods`] for Bolt11 with all supported commands
    pub fn default_bolt11(unit: CurrencyUnit) -> Self {
        let commands = vec![
            WsCommand::Bolt11MintQuote,
            WsCommand::Bolt11MeltQuote,
            WsCommand::ProofState,
        ];

        Self {
            method: PaymentMethod::Known(KnownMethod::Bolt11),
            unit,
            commands,
        }
    }

    /// Create [`SupportedMethods`] for Bolt12 with all supported commands
    pub fn default_bolt12(unit: CurrencyUnit) -> Self {
        let commands = vec![
            WsCommand::Bolt12MintQuote,
            WsCommand::Bolt12MeltQuote,
            WsCommand::ProofState,
        ];

        Self {
            method: PaymentMethod::Known(KnownMethod::Bolt12),
            unit,
            commands,
        }
    }

    /// Create [`SupportedMethods`] for custom payment method with all supported commands
    pub fn default_custom(method: PaymentMethod, unit: CurrencyUnit) -> Self {
        let method_name = method.to_string();
        let commands = vec![
            WsCommand::Custom(format!("{}_mint_quote", method_name)),
            WsCommand::Custom(format!("{}_melt_quote", method_name)),
            WsCommand::ProofState,
        ];

        Self {
            method,
            unit,
            commands,
        }
    }
}

impl WsCommand {
    /// Create a custom mint quote command for a payment method
    pub fn custom_mint_quote(method: &str) -> Self {
        WsCommand::Custom(format!("{}_mint_quote", method))
    }

    /// Create a custom melt quote command for a payment method
    pub fn custom_melt_quote(method: &str) -> Self {
        WsCommand::Custom(format!("{}_melt_quote", method))
    }
}

/// WebSocket commands supported by the Cashu mint
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum WsCommand {
    /// Command to request a Lightning invoice for minting tokens
    Bolt11MintQuote,
    /// Command to request a Lightning payment for melting tokens
    Bolt11MeltQuote,
    /// Websocket support for Bolt12 Mint Quote
    Bolt12MintQuote,
    /// Websocket support for Bolt12 Melt Quote
    Bolt12MeltQuote,
    /// Command to check the state of a proof
    ProofState,
    /// Custom payment method command
    Custom(String),
}

impl Serialize for WsCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = match self {
            WsCommand::Bolt11MintQuote => "bolt11_mint_quote",
            WsCommand::Bolt11MeltQuote => "bolt11_melt_quote",
            WsCommand::Bolt12MintQuote => "bolt12_mint_quote",
            WsCommand::Bolt12MeltQuote => "bolt12_melt_quote",
            WsCommand::ProofState => "proof_state",
            WsCommand::Custom(custom) => custom.as_str(),
        };
        serializer.serialize_str(s)
    }
}

impl<'de> Deserialize<'de> for WsCommand {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Ok(match s.as_str() {
            "bolt11_mint_quote" => WsCommand::Bolt11MintQuote,
            "bolt11_melt_quote" => WsCommand::Bolt11MeltQuote,
            "bolt12_mint_quote" => WsCommand::Bolt12MintQuote,
            "bolt12_melt_quote" => WsCommand::Bolt12MeltQuote,
            "proof_state" => WsCommand::ProofState,
            custom => WsCommand::Custom(custom.to_string()),
        })
    }
}

impl<T> From<MintQuoteBolt12Response<T>> for NotificationPayload<T>
where
    T: Clone,
{
    fn from(mint_quote: MintQuoteBolt12Response<T>) -> NotificationPayload<T> {
        NotificationPayload::MintQuoteBolt12Response(mint_quote)
    }
}

impl<T> From<MeltQuoteBolt12Response<T>> for NotificationPayload<T>
where
    T: Clone,
{
    fn from(melt_quote: MeltQuoteBolt12Response<T>) -> NotificationPayload<T> {
        NotificationPayload::MeltQuoteBolt12Response(melt_quote)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(bound(serialize = "T: Serialize + DeserializeOwned"))]
#[serde(untagged)]
/// Subscription response
pub enum NotificationPayload<T>
where
    T: Clone,
{
    /// Proof State
    ProofState(ProofState),
    /// Mint Quote Onchain Response
    MintQuoteOnchainResponse(MintQuoteOnchainResponse<T>),
    /// Melt Quote Onchain Response
    MeltQuoteOnchainResponse(MeltQuoteOnchainResponse<T>),
    /// Melt Quote Bolt11 Response
    MeltQuoteBolt11Response(MeltQuoteBolt11Response<T>),
    /// Mint Quote Bolt11 Response
    MintQuoteBolt11Response(MintQuoteBolt11Response<T>),
    /// Mint Quote Bolt12 Response
    MintQuoteBolt12Response(MintQuoteBolt12Response<T>),
    /// Melt Quote Bolt12 Response
    MeltQuoteBolt12Response(MeltQuoteBolt12Response<T>),
    /// Custom Mint Quote Response (method, response)
    CustomMintQuoteResponse(String, MintQuoteCustomResponse<T>),
    /// Custom Melt Quote Response (method, response)
    CustomMeltQuoteResponse(String, MeltQuoteCustomResponse<T>),
}

fn fill_response_method<E>(value: &mut serde_json::Value, method: &str) -> Result<(), E>
where
    E: DeError,
{
    if let serde_json::Value::Object(object) = value {
        match object.get("method") {
            Some(serde_json::Value::String(existing))
                if PaymentMethod::new(existing.to_string())
                    == PaymentMethod::new(method.to_string()) => {}
            Some(serde_json::Value::String(existing)) => {
                return Err(E::custom(format!(
                    "notification payload method {existing} does not match kind method {method}"
                )));
            }
            Some(_) => {
                return Err(E::custom("notification payload method must be a string"));
            }
            None => {
                object.insert(
                    "method".to_string(),
                    serde_json::Value::String(method.to_string()),
                );
            }
        }
    }

    Ok(())
}

fn fill_kind_response_method<E>(
    mut value: serde_json::Value,
    method: &str,
) -> Result<serde_json::Value, E>
where
    E: DeError,
{
    fill_response_method::<E>(&mut value, method)?;
    Ok(value)
}

/// Deserialize a notification payload using the subscription kind that produced
/// it.
///
/// NUT-17 notification payloads are not self-describing. Quote responses share
/// many fields and tolerate unknown fields, so deserializing the payload without
/// the subscription kind can silently select the wrong variant.
pub fn deserialize_payload_for_kind<T, E>(
    kind: &Kind,
    value: serde_json::Value,
) -> Result<NotificationPayload<T>, E>
where
    T: Clone + Serialize + DeserializeOwned,
    E: DeError,
{
    fn from_value<V, E>(value: serde_json::Value) -> Result<V, E>
    where
        V: DeserializeOwned,
        E: DeError,
    {
        serde_json::from_value(value).map_err(E::custom)
    }

    fn custom_response<V, E>(
        kind: &str,
        expected_method: &str,
        mut value: serde_json::Value,
    ) -> Result<(String, V), E>
    where
        V: DeserializeOwned,
        E: DeError,
    {
        match &mut value {
            serde_json::Value::Array(items) if items.len() == 2 => {
                let payload_method = items
                    .first()
                    .and_then(serde_json::Value::as_str)
                    .ok_or_else(|| E::custom("custom notification method must be a string"))?
                    .to_string();
                if payload_method != expected_method {
                    return Err(E::custom(format!(
                        "custom notification method {payload_method} does not match kind {kind}"
                    )));
                }

                let response = items
                    .get_mut(1)
                    .ok_or_else(|| E::custom("custom notification payload is missing response"))?;
                if !response.is_object() {
                    return Err(E::custom("custom notification response must be an object"));
                }
                fill_response_method::<E>(response, expected_method)?;

                from_value(value)
            }
            serde_json::Value::Array(_) => {
                Err(E::custom("custom notification payload must have two items"))
            }
            serde_json::Value::Object(_) => {
                fill_response_method::<E>(&mut value, expected_method)?;
                from_value(value).map(|response| (expected_method.to_string(), response))
            }
            _ => Err(E::custom("custom notification response must be an object")),
        }
    }

    match kind {
        Kind::ProofState => from_value(value).map(NotificationPayload::ProofState),
        Kind::Bolt11MintQuote => fill_kind_response_method::<E>(value, "bolt11")
            .and_then(from_value)
            .map(NotificationPayload::MintQuoteBolt11Response),
        Kind::Bolt11MeltQuote => fill_kind_response_method::<E>(value, "bolt11")
            .and_then(from_value)
            .map(NotificationPayload::MeltQuoteBolt11Response),
        Kind::Bolt12MintQuote => fill_kind_response_method::<E>(value, "bolt12")
            .and_then(from_value)
            .map(NotificationPayload::MintQuoteBolt12Response),
        Kind::Bolt12MeltQuote => fill_kind_response_method::<E>(value, "bolt12")
            .and_then(from_value)
            .map(NotificationPayload::MeltQuoteBolt12Response),
        Kind::OnchainMintQuote => fill_kind_response_method::<E>(value, "onchain")
            .and_then(from_value)
            .map(NotificationPayload::MintQuoteOnchainResponse),
        Kind::OnchainMeltQuote => fill_kind_response_method::<E>(value, "onchain")
            .and_then(from_value)
            .map(NotificationPayload::MeltQuoteOnchainResponse),
        Kind::Custom(method) => {
            if let Some(expected_method) = method.strip_suffix("_mint_quote") {
                custom_response(method, expected_method, value).map(|(method, response)| {
                    NotificationPayload::CustomMintQuoteResponse(method, response)
                })
            } else if let Some(expected_method) = method.strip_suffix("_melt_quote") {
                custom_response(method, expected_method, value).map(|(method, response)| {
                    NotificationPayload::CustomMeltQuoteResponse(method, response)
                })
            } else {
                Err(E::custom(format!(
                    "unsupported custom notification kind: {method}"
                )))
            }
        }
    }
}

/// Classify a notification payload by its discriminating fields instead of
/// serde `untagged` variant order. The quote responses for different payment
/// methods share most field names, and the structs tolerate unknown fields
/// for forward compatibility, so untagged trial-and-error would pick the
/// wrong variant.
fn deserialize_payload<T, E>(value: serde_json::Value) -> Result<NotificationPayload<T>, E>
where
    T: Clone + Serialize + DeserializeOwned,
    E: DeError,
{
    fn from_value<V, E>(value: serde_json::Value) -> Result<V, E>
    where
        V: DeserializeOwned,
        E: DeError,
    {
        serde_json::from_value(value).map_err(E::custom)
    }

    match &value {
        serde_json::Value::Object(fields) => {
            if fields.contains_key("Y") {
                return from_value(value).map(NotificationPayload::ProofState);
            }

            if fields.contains_key("fee_options") {
                return from_value(value).map(NotificationPayload::MeltQuoteOnchainResponse);
            }

            if fields.contains_key("fee_reserve") {
                if fields.get("method").and_then(serde_json::Value::as_str) == Some("bolt12") {
                    return from_value(value).map(NotificationPayload::MeltQuoteBolt12Response);
                }

                return from_value(value).map(NotificationPayload::MeltQuoteBolt11Response);
            }

            if fields.contains_key("state") {
                return from_value(value).map(NotificationPayload::MintQuoteBolt11Response);
            }

            if fields.contains_key("amount") {
                return from_value(value).map(NotificationPayload::MintQuoteBolt12Response);
            }

            from_value(value).map(NotificationPayload::MintQuoteOnchainResponse)
        }
        serde_json::Value::Array(items) if items.len() == 2 => {
            let method = items
                .first()
                .and_then(serde_json::Value::as_str)
                .ok_or_else(|| E::custom("custom notification method must be a string"))?
                .to_owned();
            let response = items
                .get(1)
                .ok_or_else(|| E::custom("custom notification payload is missing response"))?;
            let is_melt_quote = match response.as_object() {
                Some(fields) => fields.contains_key("state"),
                None => return Err(E::custom("custom notification response must be an object")),
            };

            let mut value = value;
            if let serde_json::Value::Array(items) = &mut value {
                if let Some(response) = items.get_mut(1) {
                    fill_response_method::<E>(response, &method)?;
                }
            }

            if is_melt_quote {
                from_value(value).map(|(method, response)| {
                    NotificationPayload::CustomMeltQuoteResponse(method, response)
                })
            } else {
                from_value(value).map(|(method, response)| {
                    NotificationPayload::CustomMintQuoteResponse(method, response)
                })
            }
        }
        _ => Err(E::custom("invalid notification payload")),
    }
}

impl<'de, T> Deserialize<'de> for NotificationPayload<T>
where
    T: Clone + Serialize + DeserializeOwned,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        deserialize_payload(value)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Hash, Serialize)]
#[serde(bound = "T: Serialize + DeserializeOwned")]
/// A parsed notification
pub enum NotificationId<T>
where
    T: Clone,
{
    /// ProofState id is a Pubkey
    ProofState(PublicKey),
    /// MeltQuote id is an QuoteId
    MeltQuoteBolt11(T),
    /// MintQuote id is an QuoteId
    MintQuoteBolt11(T),
    /// MintQuote id is an QuoteId
    MintQuoteBolt12(T),
    /// MintQuote id is an QuoteId
    MeltQuoteBolt12(T),
    /// MintQuote id is an QuoteId
    MintQuoteOnchain(T),
    /// MintQuote id is an QuoteId
    MeltQuoteOnchain(T),
    /// MintQuote id is an QuoteId
    MintQuoteCustom(String, T),
    /// MintQuote id is an QuoteId
    MeltQuoteCustom(String, T),
}

/// Kind
#[derive(Debug, Clone, Eq, Ord, PartialOrd, PartialEq, Hash)]
pub enum Kind {
    /// Bolt 11 Melt Quote
    Bolt11MeltQuote,
    /// Bolt 11 Mint Quote
    Bolt11MintQuote,
    /// Proof State
    ProofState,
    /// Bolt 12 Mint Quote
    Bolt12MintQuote,
    /// Bolt 12 Melt Quote
    Bolt12MeltQuote,
    /// Onchain Mint Quote
    OnchainMintQuote,
    /// Onchain Melt Quote
    OnchainMeltQuote,
    /// Custom
    Custom(String),
}

impl Serialize for Kind {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = match self {
            Kind::Bolt11MintQuote => "bolt11_mint_quote",
            Kind::Bolt11MeltQuote => "bolt11_melt_quote",
            Kind::Bolt12MintQuote => "bolt12_mint_quote",
            Kind::Bolt12MeltQuote => "bolt12_melt_quote",
            Kind::OnchainMintQuote => "onchain_mint_quote",
            Kind::OnchainMeltQuote => "onchain_melt_quote",
            Kind::ProofState => "proof_state",
            Kind::Custom(custom) => custom.as_str(),
        };
        serializer.serialize_str(s)
    }
}

impl<'de> Deserialize<'de> for Kind {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Ok(match s.as_str() {
            "bolt11_mint_quote" => Kind::Bolt11MintQuote,
            "bolt11_melt_quote" => Kind::Bolt11MeltQuote,
            "bolt12_mint_quote" => Kind::Bolt12MintQuote,
            "bolt12_melt_quote" => Kind::Bolt12MeltQuote,
            "onchain_mint_quote" => Kind::OnchainMintQuote,
            "onchain_melt_quote" => Kind::OnchainMeltQuote,
            "proof_state" => Kind::ProofState,
            custom => Kind::Custom(custom.to_string()),
        })
    }
}

impl<I> AsRef<I> for Params<I> {
    fn as_ref(&self) -> &I {
        &self.id
    }
}

/// Parsing error
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Uuid Error: {0}")]
    /// Uuid Error
    QuoteId(#[from] QuoteIdError),

    #[error("PublicKey Error: {0}")]
    /// PublicKey Error
    PublicKey(#[from] crate::nuts::nut01::Error),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::nuts::nut00::CurrencyUnit;
    use crate::nuts::nut01::PublicKey;
    use crate::nuts::MeltQuoteState;
    use crate::Amount;

    #[test]
    fn notification_payload_onchain_mint_roundtrip() {
        let resp: MintQuoteOnchainResponse<String> = MintQuoteOnchainResponse {
            quote: "abc".to_string(),
            request: "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh".to_string(),
            unit: CurrencyUnit::Sat,
            expiry: Some(1701704757),
            pubkey: PublicKey::from_hex(
                "03d56ce4e446a85bbdaa547b4ec2b073d40ff802831352b8272b7dd7a4de5a7cac",
            )
            .unwrap(),
            amount_paid: Amount::from(100_000),
            amount_issued: Amount::from(0),
        };
        let payload: NotificationPayload<String> =
            NotificationPayload::MintQuoteOnchainResponse(resp.clone());

        let encoded = serde_json::to_string(&payload).unwrap();
        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::OnchainMintQuote,
            serde_json::from_str(&encoded).unwrap(),
        )
        .unwrap();

        match decoded {
            NotificationPayload::MintQuoteOnchainResponse(r) => {
                assert_eq!(r, resp);
            }
            other => panic!("expected MintQuoteOnchainResponse, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_bolt12_mint_roundtrip() {
        // Ensure a Bolt12 payload (with `amount`) still decodes as Bolt12 and
        // is not swallowed by the field-name overlap with the Onchain variant.
        let resp: MintQuoteBolt12Response<String> = MintQuoteBolt12Response {
            quote: "abc".to_string(),
            request: "lno1...".to_string(),
            amount: Some(Amount::from(100_000)),
            unit: CurrencyUnit::Sat,
            expiry: Some(1701704757),
            pubkey: PublicKey::from_hex(
                "03d56ce4e446a85bbdaa547b4ec2b073d40ff802831352b8272b7dd7a4de5a7cac",
            )
            .unwrap(),
            amount_paid: Amount::from(0),
            amount_issued: Amount::from(0),
        };
        let payload: NotificationPayload<String> =
            NotificationPayload::MintQuoteBolt12Response(resp.clone());

        let encoded = serde_json::to_string(&payload).unwrap();
        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Bolt12MintQuote,
            serde_json::from_str(&encoded).unwrap(),
        )
        .unwrap();

        match decoded {
            NotificationPayload::MintQuoteBolt12Response(r) => {
                assert_eq!(r, resp);
            }
            other => panic!("expected MintQuoteBolt12Response, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_onchain_melt_roundtrip() {
        let resp: MeltQuoteOnchainResponse<String> = MeltQuoteOnchainResponse {
            quote: "abc".to_string(),
            amount: Amount::from(100_000),
            unit: CurrencyUnit::Sat,
            state: MeltQuoteState::Pending,
            expiry: 1701704757,
            request: "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh".to_string(),
            fee_options: vec![crate::nut30::MeltQuoteOnchainFeeOption {
                fee_index: 0,
                fee_reserve: Amount::from(5_000),
                estimated_blocks: 1,
            }],
            selected_fee_index: Some(0),
            outpoint: Some("3b7f3b85:2".to_string()),
            change: None,
        };
        let payload: NotificationPayload<String> =
            NotificationPayload::MeltQuoteOnchainResponse(resp.clone());

        let encoded = serde_json::to_string(&payload).unwrap();
        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::OnchainMeltQuote,
            serde_json::from_str(&encoded).unwrap(),
        )
        .unwrap();

        match decoded {
            NotificationPayload::MeltQuoteOnchainResponse(r) => {
                assert_eq!(r, resp);
            }
            other => panic!("expected MeltQuoteOnchainResponse, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_bolt12_melt_roundtrip() {
        let resp: MeltQuoteBolt12Response<String> = MeltQuoteBolt12Response {
            quote: "abc".to_string(),
            amount: Amount::from(100_000),
            fee_reserve: Amount::from(10),
            state: MeltQuoteState::Pending,
            expiry: 1701704757,
            payment_preimage: None,
            change: None,
            request: Some("lno1...".to_string()),
            unit: Some(CurrencyUnit::Sat),
        };
        let payload: NotificationPayload<String> =
            NotificationPayload::MeltQuoteBolt12Response(resp.clone());

        let encoded = serde_json::to_string(&payload).unwrap();
        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Bolt12MeltQuote,
            serde_json::from_str(&encoded).unwrap(),
        )
        .unwrap();

        match decoded {
            NotificationPayload::MeltQuoteBolt12Response(r) => {
                assert_eq!(r, resp);
            }
            other => panic!("expected MeltQuoteBolt12Response, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_custom_arrays_require_method_and_object_response() {
        let custom_mint = r#"[
            "paypal",
            {
                "quote": "abc",
                "request": "pay://abc",
                "amount": 10,
                "amount_paid": 0,
                "amount_issued": 0,
                "unit": "sat",
                "expiry": 1701704757
            }
        ]"#;
        let mint_kind = Kind::Custom("paypal_mint_quote".to_string());
        let payload = deserialize_payload_for_kind::<String, serde_json::Error>(
            &mint_kind,
            serde_json::from_str(custom_mint).unwrap(),
        )
        .unwrap();
        match payload {
            NotificationPayload::CustomMintQuoteResponse(method, response) => {
                assert_eq!(method, "paypal");
                assert_eq!(response.quote, "abc");
            }
            other => panic!("expected CustomMintQuoteResponse, got {:?}", other),
        }

        let custom_melt = r#"[
            "paypal",
            {
                "quote": "abc",
                "amount": 10,
                "fee_reserve": 1,
                "state": "PENDING",
                "expiry": 1701704757,
                "request": "pay://abc",
                "unit": "sat"
            }
        ]"#;
        let melt_kind = Kind::Custom("paypal_melt_quote".to_string());
        let payload = deserialize_payload_for_kind::<String, serde_json::Error>(
            &melt_kind,
            serde_json::from_str(custom_melt).unwrap(),
        )
        .unwrap();
        match payload {
            NotificationPayload::CustomMeltQuoteResponse(method, response) => {
                assert_eq!(method, "paypal");
                assert_eq!(response.quote, "abc");
            }
            other => panic!("expected CustomMeltQuoteResponse, got {:?}", other),
        }

        assert!(deserialize_payload_for_kind::<String, serde_json::Error>(
            &mint_kind,
            serde_json::json!(["paypal"]),
        )
        .is_err());
        assert!(deserialize_payload_for_kind::<String, serde_json::Error>(
            &mint_kind,
            serde_json::json!(["paypal", "not an object"]),
        )
        .is_err());
        assert!(deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Custom("paypal".to_string()),
            serde_json::json!({}),
        )
        .is_err());
    }

    #[test]
    fn notification_payload_onchain_mint_tolerates_unknown_fields() {
        // A newer mint may extend the onchain mint quote response with
        // additional fields; classification and decoding must still succeed.
        let encoded = r#"{
            "quote": "abc",
            "request": "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh",
            "unit": "sat",
            "expiry": 1701704757,
            "pubkey": "03d56ce4e446a85bbdaa547b4ec2b073d40ff802831352b8272b7dd7a4de5a7cac",
            "amount_paid": 0,
            "amount_issued": 0,
            "some_future_extension": {"nested": true}
        }"#;

        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::OnchainMintQuote,
            serde_json::from_str(encoded).unwrap(),
        )
        .unwrap();

        match decoded {
            NotificationPayload::MintQuoteOnchainResponse(r) => {
                assert_eq!(r.quote, "abc");
            }
            other => panic!("expected MintQuoteOnchainResponse, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_onchain_mint_future_state_field_uses_kind() {
        let encoded = r#"{
            "quote": "abc",
            "request": "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh",
            "unit": "sat",
            "expiry": 1701704757,
            "pubkey": "03d56ce4e446a85bbdaa547b4ec2b073d40ff802831352b8272b7dd7a4de5a7cac",
            "amount_paid": 0,
            "amount_issued": 0,
            "state": "future-extension"
        }"#;

        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::OnchainMintQuote,
            serde_json::from_str(encoded).unwrap(),
        )
        .unwrap();

        match decoded {
            NotificationPayload::MintQuoteOnchainResponse(r) => {
                assert_eq!(r.quote, "abc");
            }
            other => panic!("expected MintQuoteOnchainResponse, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_custom_tuple_uses_kind() {
        let encoded = serde_json::json!([
            "foo",
            {
                "quote": "abc",
                "request": "custom-request",
                "amount": 100,
                "amount_paid": 0,
                "amount_issued": 0,
                "unit": "sat",
                "expiry": 1701704757
            }
        ]);

        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Custom("foo_mint_quote".to_string()),
            encoded,
        )
        .unwrap();

        match decoded {
            NotificationPayload::CustomMintQuoteResponse(method, r) => {
                assert_eq!(method, "foo");
                assert_eq!(r.quote, "abc");
            }
            other => panic!("expected CustomMintQuoteResponse, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_kind_decoder_accepts_case_insensitive_method() {
        let payload = serde_json::json!({
            "quote": "abc",
            "request": "lnbc1...",
            "amount": 100,
            "unit": "sat",
            "method": "BOLT11",
            "state": "UNPAID",
            "expiry": 1701704757
        });

        let decoded = deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Bolt11MintQuote,
            payload,
        )
        .unwrap();

        match decoded {
            NotificationPayload::MintQuoteBolt11Response(response) => {
                assert_eq!(response.quote, "abc");
            }
            other => panic!("expected MintQuoteBolt11Response, got {:?}", other),
        }
    }

    #[test]
    fn notification_payload_rejects_method_that_does_not_match_kind() {
        let bolt12_payload = serde_json::json!({
            "quote": "melt-quote",
            "amount": 21,
            "fee_reserve": 1,
            "method": "bolt11",
            "state": "PAID",
            "expiry": 1234
        });

        assert!(deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Bolt12MeltQuote,
            bolt12_payload,
        )
        .is_err());

        let custom_payload = serde_json::json!({
            "quote": "custom-quote",
            "request": "custom-request",
            "method": "bar",
            "amount": 100,
            "amount_paid": 0,
            "amount_issued": 0,
            "unit": "sat",
            "expiry": 1701704757
        });

        assert!(deserialize_payload_for_kind::<String, serde_json::Error>(
            &Kind::Custom("foo_mint_quote".to_string()),
            custom_payload,
        )
        .is_err());
    }
}