breez_sdk_liquid/
model.rs

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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
use std::path::PathBuf;

use anyhow::{anyhow, Result};

use boltz_client::boltz::ChainPair;
use boltz_client::{
    bitcoin::ScriptBuf,
    network::Chain,
    swaps::boltz::{
        CreateChainResponse, CreateReverseResponse, CreateSubmarineResponse, Leaf, Side, SwapTree,
    },
    ToHex,
};
use boltz_client::{BtcSwapScript, Keypair, LBtcSwapScript};
use lwk_wollet::{bitcoin::bip32, ElementsNetwork};
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use rusqlite::ToSql;
use sdk_common::prelude::*;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};

use crate::error::{PaymentError, SdkError, SdkResult};
use crate::prelude::DEFAULT_EXTERNAL_INPUT_PARSERS;
use crate::receive_swap::{
    DEFAULT_ZERO_CONF_MAX_SAT, DEFAULT_ZERO_CONF_MIN_FEE_RATE_MAINNET,
    DEFAULT_ZERO_CONF_MIN_FEE_RATE_TESTNET,
};
use crate::utils;

// Both use f64 for the maximum precision when converting between units
pub const STANDARD_FEE_RATE_SAT_PER_VBYTE: f64 = 0.1;
pub const LOWBALL_FEE_RATE_SAT_PER_VBYTE: f64 = 0.01;

/// Configuration for the Liquid SDK
#[derive(Clone, Debug, Serialize)]
pub struct Config {
    pub liquid_electrum_url: String,
    pub bitcoin_electrum_url: String,
    /// The mempool.space API URL, has to be in the format: `https://mempool.space/api`
    pub mempoolspace_url: String,
    /// Directory in which the DB and log files are stored.
    ///
    /// Prefix can be a relative or absolute path to this directory.
    pub working_dir: String,
    /// Directory in which the Liquid wallet cache is stored. Defaults to `working_dir`
    pub cache_dir: Option<String>,
    pub network: LiquidNetwork,
    /// Send payment timeout. See [LiquidSdk::send_payment](crate::sdk::LiquidSdk::send_payment)
    pub payment_timeout_sec: u64,
    /// Zero-conf minimum accepted fee-rate in millisatoshis per vbyte
    pub zero_conf_min_fee_rate_msat: u32,
    /// Maximum amount in satoshi to accept zero-conf payments with
    /// Defaults to [DEFAULT_ZERO_CONF_MAX_SAT]
    pub zero_conf_max_amount_sat: Option<u64>,
    /// The Breez API key used for making requests to their mempool service
    pub breez_api_key: Option<String>,
    /// A set of external input parsers that are used by [LiquidSdk::parse](crate::sdk::LiquidSdk::parse) when the input
    /// is not recognized. See [ExternalInputParser] for more details on how to configure
    /// external parsing.
    pub external_input_parsers: Option<Vec<ExternalInputParser>>,
    /// The SDK includes some default external input parsers
    /// ([DEFAULT_EXTERNAL_INPUT_PARSERS](crate::sdk::DEFAULT_EXTERNAL_INPUT_PARSERS)).
    /// Set this to false in order to prevent their use.
    pub use_default_external_input_parsers: bool,
}

impl Config {
    pub fn mainnet(breez_api_key: String) -> Self {
        Config {
            liquid_electrum_url: "elements-mainnet.breez.technology:50002".to_string(),
            bitcoin_electrum_url: "bitcoin-mainnet.blockstream.info:50002".to_string(),
            mempoolspace_url: "https://mempool.space/api".to_string(),
            working_dir: ".".to_string(),
            cache_dir: None,
            network: LiquidNetwork::Mainnet,
            payment_timeout_sec: 15,
            zero_conf_min_fee_rate_msat: DEFAULT_ZERO_CONF_MIN_FEE_RATE_MAINNET,
            zero_conf_max_amount_sat: None,
            breez_api_key: Some(breez_api_key),
            external_input_parsers: None,
            use_default_external_input_parsers: true,
        }
    }

    pub fn testnet(breez_api_key: Option<String>) -> Self {
        Config {
            liquid_electrum_url: "elements-testnet.blockstream.info:50002".to_string(),
            bitcoin_electrum_url: "bitcoin-testnet.blockstream.info:50002".to_string(),
            mempoolspace_url: "https://mempool.space/testnet/api".to_string(),
            working_dir: ".".to_string(),
            cache_dir: None,
            network: LiquidNetwork::Testnet,
            payment_timeout_sec: 15,
            zero_conf_min_fee_rate_msat: DEFAULT_ZERO_CONF_MIN_FEE_RATE_TESTNET,
            zero_conf_max_amount_sat: None,
            breez_api_key,
            external_input_parsers: None,
            use_default_external_input_parsers: true,
        }
    }

    pub(crate) fn get_wallet_dir(
        &self,
        base_dir: &str,
        fingerprint_hex: &str,
    ) -> anyhow::Result<String> {
        Ok(PathBuf::from(base_dir)
            .join(match self.network {
                LiquidNetwork::Mainnet => "mainnet",
                LiquidNetwork::Testnet => "testnet",
            })
            .join(fingerprint_hex)
            .to_str()
            .ok_or(anyhow::anyhow!(
                "Could not get retrieve current wallet directory"
            ))?
            .to_string())
    }

    pub fn zero_conf_max_amount_sat(&self) -> u64 {
        self.zero_conf_max_amount_sat
            .unwrap_or(DEFAULT_ZERO_CONF_MAX_SAT)
    }

    pub(crate) fn lowball_fee_rate_msat_per_vbyte(&self) -> Option<f32> {
        match self.network {
            LiquidNetwork::Mainnet => Some((LOWBALL_FEE_RATE_SAT_PER_VBYTE * 1000.0) as f32),
            LiquidNetwork::Testnet => None,
        }
    }

    pub(crate) fn get_all_external_input_parsers(&self) -> Vec<ExternalInputParser> {
        let mut external_input_parsers = Vec::new();
        if self.use_default_external_input_parsers {
            let default_parsers = DEFAULT_EXTERNAL_INPUT_PARSERS
                .iter()
                .map(|(id, regex, url)| ExternalInputParser {
                    provider_id: id.to_string(),
                    input_regex: regex.to_string(),
                    parser_url: url.to_string(),
                })
                .collect::<Vec<_>>();
            external_input_parsers.extend(default_parsers);
        }
        external_input_parsers.extend(self.external_input_parsers.clone().unwrap_or_default());

        external_input_parsers
    }
}

/// Network chosen for this Liquid SDK instance. Note that it represents both the Liquid and the
/// Bitcoin network used.
#[derive(Debug, Copy, Clone, PartialEq, Serialize)]
pub enum LiquidNetwork {
    /// Mainnet Bitcoin and Liquid chains
    Mainnet,
    /// Testnet Bitcoin and Liquid chains
    Testnet,
}
impl LiquidNetwork {
    pub fn as_bitcoin_chain(&self) -> Chain {
        match self {
            LiquidNetwork::Mainnet => Chain::Bitcoin,
            LiquidNetwork::Testnet => Chain::BitcoinTestnet,
        }
    }
}

impl From<LiquidNetwork> for ElementsNetwork {
    fn from(value: LiquidNetwork) -> Self {
        match value {
            LiquidNetwork::Mainnet => ElementsNetwork::Liquid,
            LiquidNetwork::Testnet => ElementsNetwork::LiquidTestnet,
        }
    }
}

impl From<LiquidNetwork> for Chain {
    fn from(value: LiquidNetwork) -> Self {
        match value {
            LiquidNetwork::Mainnet => Chain::Liquid,
            LiquidNetwork::Testnet => Chain::LiquidTestnet,
        }
    }
}

impl TryFrom<&str> for LiquidNetwork {
    type Error = anyhow::Error;

    fn try_from(value: &str) -> Result<LiquidNetwork, anyhow::Error> {
        match value.to_lowercase().as_str() {
            "mainnet" => Ok(LiquidNetwork::Mainnet),
            "testnet" => Ok(LiquidNetwork::Testnet),
            _ => Err(anyhow!("Invalid network")),
        }
    }
}

impl From<LiquidNetwork> for sdk_common::prelude::Network {
    fn from(value: LiquidNetwork) -> Self {
        match value {
            LiquidNetwork::Mainnet => Self::Bitcoin,
            LiquidNetwork::Testnet => Self::Testnet,
        }
    }
}

impl From<LiquidNetwork> for sdk_common::bitcoin::Network {
    fn from(value: LiquidNetwork) -> Self {
        match value {
            LiquidNetwork::Mainnet => Self::Bitcoin,
            LiquidNetwork::Testnet => Self::Testnet,
        }
    }
}

impl From<LiquidNetwork> for boltz_client::bitcoin::Network {
    fn from(value: LiquidNetwork) -> Self {
        match value {
            LiquidNetwork::Mainnet => Self::Bitcoin,
            LiquidNetwork::Testnet => Self::Testnet,
        }
    }
}

/// Trait that can be used to react to various [SdkEvent]s emitted by the SDK.
pub trait EventListener: Send + Sync {
    fn on_event(&self, e: SdkEvent);
}

/// Event emitted by the SDK. Add an [EventListener] by calling [crate::sdk::LiquidSdk::add_event_listener]
/// to listen for emitted events.
#[derive(Clone, Debug, PartialEq)]
pub enum SdkEvent {
    PaymentFailed { details: Payment },
    PaymentPending { details: Payment },
    PaymentRefunded { details: Payment },
    PaymentRefundPending { details: Payment },
    PaymentSucceeded { details: Payment },
    PaymentWaitingConfirmation { details: Payment },
    Synced,
}

#[derive(thiserror::Error, Debug)]
pub enum SignerError {
    #[error("Signer error: {err}")]
    Generic { err: String },
}

impl From<anyhow::Error> for SignerError {
    fn from(err: anyhow::Error) -> Self {
        SignerError::Generic {
            err: err.to_string(),
        }
    }
}

impl From<bip32::Error> for SignerError {
    fn from(err: bip32::Error) -> Self {
        SignerError::Generic {
            err: err.to_string(),
        }
    }
}

/// A trait that can be used to sign messages and verify signatures.
/// The sdk user can implement this trait to use their own signer.
pub trait Signer: Send + Sync {
    /// The master xpub encoded as 78 bytes length as defined in bip32 specification.
    /// For reference: <https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#user-content-Serialization_format>
    fn xpub(&self) -> Result<Vec<u8>, SignerError>;

    /// The derived xpub encoded as 78 bytes length as defined in bip32 specification.
    /// The derivation path is a string represents the shorter notation of the key tree to derive. For example:
    /// m/49'/1'/0'/0/0
    /// m/48'/1'/0'/0/0
    /// For reference: <https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#user-content-The_key_tree>
    fn derive_xpub(&self, derivation_path: String) -> Result<Vec<u8>, SignerError>;

    /// Sign an ECDSA message using the private key derived from the given derivation path
    fn sign_ecdsa(&self, msg: Vec<u8>, derivation_path: String) -> Result<Vec<u8>, SignerError>;

    /// Sign an ECDSA message using the private key derived from the master key
    fn sign_ecdsa_recoverable(&self, msg: Vec<u8>) -> Result<Vec<u8>, SignerError>;

    /// Return the master blinding key for SLIP77: <https://github.com/satoshilabs/slips/blob/master/slip-0077.md>
    fn slip77_master_blinding_key(&self) -> Result<Vec<u8>, SignerError>;

    /// HMAC-SHA256 using the private key derived from the given derivation path
    /// This is used to calculate the linking key of lnurl-auth specification: <https://github.com/lnurl/luds/blob/luds/05.md>
    fn hmac_sha256(&self, msg: Vec<u8>, derivation_path: String) -> Result<Vec<u8>, SignerError>;
}

/// An argument when calling [crate::sdk::LiquidSdk::connect].
pub struct ConnectRequest {
    pub config: Config,
    pub mnemonic: String,
}

pub struct ConnectWithSignerRequest {
    pub config: Config,
}

/// A reserved address. Once an address is reserved, it can only be
/// reallocated to another payment after the block height expiration.
#[derive(Clone, Debug)]
pub(crate) struct ReservedAddress {
    /// The address that is reserved
    pub(crate) address: String,
    /// The block height that the address is reserved until
    pub(crate) expiry_block_height: u32,
}

/// The send/receive methods supported by the SDK
#[derive(Clone, Debug, EnumString, Serialize, Eq, PartialEq)]
pub enum PaymentMethod {
    #[strum(serialize = "lightning")]
    Lightning,
    #[strum(serialize = "bitcoin")]
    BitcoinAddress,
    #[strum(serialize = "liquid")]
    LiquidAddress,
}

/// An argument when calling [crate::sdk::LiquidSdk::prepare_receive_payment].
#[derive(Debug, Serialize)]
pub struct PrepareReceiveRequest {
    pub payer_amount_sat: Option<u64>,
    pub payment_method: PaymentMethod,
}

/// Returned when calling [crate::sdk::LiquidSdk::prepare_receive_payment].
#[derive(Debug, Serialize)]
pub struct PrepareReceiveResponse {
    pub payment_method: PaymentMethod,
    pub payer_amount_sat: Option<u64>,

    /// Generally represents the total fees that would be paid to send or receive this payment.
    ///
    /// In case of Zero-Amount Receive Chain swaps, the swapper service fee (`swapper_feerate` times
    /// the amount) is paid in addition to `fees_sat`. The swapper service feerate is already known
    /// in the beginning, but the exact swapper service fee will only be known when the
    /// `payer_amount_sat` is known.
    ///
    /// In all other types of swaps, the swapper service fee is included in `fees_sat`.
    pub fees_sat: u64,

    /// The minimum amount the payer can send for this swap to succeed.
    ///
    /// When the method is [PaymentMethod::LiquidAddress], this is empty.
    pub min_payer_amount_sat: Option<u64>,

    /// The maximum amount the payer can send for this swap to succeed.
    ///
    /// When the method is [PaymentMethod::LiquidAddress], this is empty.
    pub max_payer_amount_sat: Option<u64>,

    /// The percentage of the sent amount that will count towards the service fee.
    ///
    /// When the method is [PaymentMethod::LiquidAddress], this is empty.
    pub swapper_feerate: Option<f64>,
}

/// An argument when calling [crate::sdk::LiquidSdk::receive_payment].
#[derive(Debug, Serialize)]
pub struct ReceivePaymentRequest {
    pub prepare_response: PrepareReceiveResponse,
    /// The description for this payment request.
    pub description: Option<String>,
    /// If set to true, then the hash of the description will be used.
    pub use_description_hash: Option<bool>,
}

/// Returned when calling [crate::sdk::LiquidSdk::receive_payment].
#[derive(Debug, Serialize)]
pub struct ReceivePaymentResponse {
    /// Either a BIP21 URI (Liquid or Bitcoin), a Liquid address
    /// or an invoice, depending on the [PrepareReceiveResponse] parameters
    pub destination: String,
}

/// The minimum and maximum in satoshis of a Lightning or onchain payment.
#[derive(Debug, Serialize)]
pub struct Limits {
    pub min_sat: u64,
    pub max_sat: u64,
    pub max_zero_conf_sat: u64,
}

/// Returned when calling [crate::sdk::LiquidSdk::fetch_lightning_limits].
#[derive(Debug, Serialize)]
pub struct LightningPaymentLimitsResponse {
    /// Amount limits for a Send Payment to be valid
    pub send: Limits,
    /// Amount limits for a Receive Payment to be valid
    pub receive: Limits,
}

/// Returned when calling [crate::sdk::LiquidSdk::fetch_onchain_limits].
#[derive(Debug, Serialize)]
pub struct OnchainPaymentLimitsResponse {
    /// Amount limits for a Send Onchain Payment to be valid
    pub send: Limits,
    /// Amount limits for a Receive Onchain Payment to be valid
    pub receive: Limits,
}

/// An argument when calling [crate::sdk::LiquidSdk::prepare_send_payment].
#[derive(Debug, Serialize, Clone)]
pub struct PrepareSendRequest {
    /// The destination we intend to pay to.
    /// Supports BIP21 URIs, BOLT11 invoices, BOLT12 offers and Liquid addresses
    pub destination: String,

    /// Should only be set when paying directly onchain or to a BIP21 URI
    /// where no amount is specified, or when the caller wishes to drain
    pub amount: Option<PayAmount>,
}

/// Specifies the supported destinations which can be payed by the SDK
#[derive(Clone, Debug, Serialize)]
pub enum SendDestination {
    LiquidAddress {
        address_data: liquid::LiquidAddressData,
    },
    Bolt11 {
        invoice: LNInvoice,
    },
    Bolt12 {
        offer: LNOffer,
        receiver_amount_sat: u64,
    },
}

/// Returned when calling [crate::sdk::LiquidSdk::prepare_send_payment].
#[derive(Debug, Serialize, Clone)]
pub struct PrepareSendResponse {
    pub destination: SendDestination,
    pub fees_sat: u64,
}

/// An argument when calling [crate::sdk::LiquidSdk::send_payment].
#[derive(Debug, Serialize)]
pub struct SendPaymentRequest {
    pub prepare_response: PrepareSendResponse,
}

/// Returned when calling [crate::sdk::LiquidSdk::send_payment].
#[derive(Debug, Serialize)]
pub struct SendPaymentResponse {
    pub payment: Payment,
}

#[derive(Debug, Serialize, Clone)]
pub enum PayAmount {
    /// The amount in satoshi that will be received
    Receiver { amount_sat: u64 },
    /// Indicates that all available funds should be sent
    Drain,
}

/// An argument when calling [crate::sdk::LiquidSdk::prepare_pay_onchain].
#[derive(Debug, Serialize, Clone)]
pub struct PreparePayOnchainRequest {
    pub amount: PayAmount,
    /// The optional fee rate of the Bitcoin claim transaction in sat/vB. Defaults to the swapper estimated claim fee.
    pub fee_rate_sat_per_vbyte: Option<u32>,
}

/// Returned when calling [crate::sdk::LiquidSdk::prepare_pay_onchain].
#[derive(Debug, Serialize, Clone)]
pub struct PreparePayOnchainResponse {
    pub receiver_amount_sat: u64,
    pub claim_fees_sat: u64,
    pub total_fees_sat: u64,
}

/// An argument when calling [crate::sdk::LiquidSdk::pay_onchain].
#[derive(Debug, Serialize)]
pub struct PayOnchainRequest {
    pub address: String,
    pub prepare_response: PreparePayOnchainResponse,
}

/// An argument when calling [crate::sdk::LiquidSdk::prepare_refund].
#[derive(Debug, Serialize)]
pub struct PrepareRefundRequest {
    /// The address where the swap funds are locked up
    pub swap_address: String,
    /// The address to refund the swap funds to
    pub refund_address: String,
    /// The fee rate in sat/vB for the refund transaction
    pub fee_rate_sat_per_vbyte: u32,
}

/// Returned when calling [crate::sdk::LiquidSdk::prepare_refund].
#[derive(Debug, Serialize)]
pub struct PrepareRefundResponse {
    pub tx_vsize: u32,
    pub tx_fee_sat: u64,
    pub refund_tx_id: Option<String>,
}

/// An argument when calling [crate::sdk::LiquidSdk::refund].
#[derive(Debug, Serialize)]
pub struct RefundRequest {
    /// The address where the swap funds are locked up
    pub swap_address: String,
    /// The address to refund the swap funds to
    pub refund_address: String,
    /// The fee rate in sat/vB for the refund transaction
    pub fee_rate_sat_per_vbyte: u32,
}

/// Returned when calling [crate::sdk::LiquidSdk::refund].
#[derive(Debug, Serialize)]
pub struct RefundResponse {
    pub refund_tx_id: String,
}

/// Returned when calling [crate::sdk::LiquidSdk::get_info].
#[derive(Debug, Serialize)]
pub struct GetInfoResponse {
    /// Usable balance. This is the confirmed onchain balance minus `pending_send_sat`.
    pub balance_sat: u64,
    /// Amount that is being used for ongoing Send swaps
    pub pending_send_sat: u64,
    /// Incoming amount that is pending from ongoing Receive swaps
    pub pending_receive_sat: u64,
    /// The wallet's fingerprint. It is used to build the working directory in [Config::get_wallet_dir].
    pub fingerprint: String,
    /// The wallet's pubkey. Used to verify signed messages.
    pub pubkey: String,
}

/// An argument when calling [crate::sdk::LiquidSdk::sign_message].
#[derive(Clone, Debug, PartialEq)]
pub struct SignMessageRequest {
    pub message: String,
}

/// Returned when calling [crate::sdk::LiquidSdk::sign_message].
#[derive(Clone, Debug, PartialEq)]
pub struct SignMessageResponse {
    pub signature: String,
}

/// An argument when calling [crate::sdk::LiquidSdk::check_message].
#[derive(Clone, Debug, PartialEq)]
pub struct CheckMessageRequest {
    /// The message that was signed.
    pub message: String,
    /// The public key of the node that signed the message.
    pub pubkey: String,
    /// The zbase encoded signature to verify.
    pub signature: String,
}

/// Returned when calling [crate::sdk::LiquidSdk::check_message].
#[derive(Clone, Debug, PartialEq)]
pub struct CheckMessageResponse {
    /// Boolean value indicating whether the signature covers the message and
    /// was signed by the given pubkey.
    pub is_valid: bool,
}

/// An argument when calling [crate::sdk::LiquidSdk::backup].
#[derive(Debug, Serialize)]
pub struct BackupRequest {
    /// Path to the backup.
    ///
    /// If not set, it defaults to `backup.sql` for mainnet and `backup-testnet.sql` for testnet.
    /// The file will be saved in [ConnectRequest]'s `data_dir`.
    pub backup_path: Option<String>,
}

/// An argument when calling [crate::sdk::LiquidSdk::restore].
#[derive(Debug, Serialize)]
pub struct RestoreRequest {
    pub backup_path: Option<String>,
}

/// An argument when calling [crate::sdk::LiquidSdk::list_payments].
#[derive(Default)]
pub struct ListPaymentsRequest {
    pub filters: Option<Vec<PaymentType>>,
    /// Epoch time, in seconds
    pub from_timestamp: Option<i64>,
    /// Epoch time, in seconds
    pub to_timestamp: Option<i64>,
    pub offset: Option<u32>,
    pub limit: Option<u32>,
    pub details: Option<ListPaymentDetails>,
}

/// An argument of [ListPaymentsRequest] when calling [crate::sdk::LiquidSdk::list_payments].
#[derive(Debug, Serialize)]
pub enum ListPaymentDetails {
    /// The Liquid BIP21 URI or address of the payment
    Liquid { destination: String },

    /// The Bitcoin address of the payment
    Bitcoin { address: String },
}

/// An argument when calling [crate::sdk::LiquidSdk::get_payment].
#[derive(Debug, Serialize)]
pub enum GetPaymentRequest {
    /// The Lightning payment hash of the payment
    Lightning { payment_hash: String },
}

// A swap enum variant
#[derive(Clone, Debug)]
pub(crate) enum Swap {
    Chain(ChainSwap),
    Send(SendSwap),
    Receive(ReceiveSwap),
}
impl Swap {
    pub(crate) fn id(&self) -> String {
        match &self {
            Swap::Chain(ChainSwap { id, .. })
            | Swap::Send(SendSwap { id, .. })
            | Swap::Receive(ReceiveSwap { id, .. }) => id.clone(),
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) enum SwapScriptV2 {
    Bitcoin(BtcSwapScript),
    Liquid(LBtcSwapScript),
}
impl SwapScriptV2 {
    pub(crate) fn as_bitcoin_script(&self) -> Result<BtcSwapScript> {
        match self {
            SwapScriptV2::Bitcoin(script) => Ok(script.clone()),
            _ => Err(anyhow!("Invalid chain")),
        }
    }

    pub(crate) fn as_liquid_script(&self) -> Result<LBtcSwapScript> {
        match self {
            SwapScriptV2::Liquid(script) => Ok(script.clone()),
            _ => Err(anyhow!("Invalid chain")),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Serialize)]
pub enum Direction {
    Incoming = 0,
    Outgoing = 1,
}
impl ToSql for Direction {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        Ok(rusqlite::types::ToSqlOutput::from(*self as i8))
    }
}
impl FromSql for Direction {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
        match value {
            ValueRef::Integer(i) => match i as u8 {
                0 => Ok(Direction::Incoming),
                1 => Ok(Direction::Outgoing),
                _ => Err(FromSqlError::OutOfRange(i)),
            },
            _ => Err(FromSqlError::InvalidType),
        }
    }
}

/// A chain swap
///
/// See <https://docs.boltz.exchange/v/api/lifecycle#chain-swaps>
#[derive(Clone, Debug)]
pub(crate) struct ChainSwap {
    pub(crate) id: String,
    pub(crate) direction: Direction,
    /// The Bitcoin claim address is only set for Outgoing Chain Swaps
    pub(crate) claim_address: Option<String>,
    pub(crate) lockup_address: String,
    pub(crate) timeout_block_height: u32,
    pub(crate) preimage: String,
    pub(crate) description: Option<String>,
    pub(crate) payer_amount_sat: u64,
    pub(crate) receiver_amount_sat: u64,
    pub(crate) claim_fees_sat: u64,
    /// The [ChainPair] chosen on swap creation
    pub(crate) pair_fees_json: String,
    pub(crate) accept_zero_conf: bool,
    /// JSON representation of [crate::persist::chain::InternalCreateChainResponse]
    pub(crate) create_response_json: String,
    /// Persisted only when the server lockup tx is successfully broadcast
    pub(crate) server_lockup_tx_id: Option<String>,
    /// Persisted only when the user lockup tx is successfully broadcast
    pub(crate) user_lockup_tx_id: Option<String>,
    /// Persisted as soon as a claim tx is broadcast
    pub(crate) claim_tx_id: Option<String>,
    /// Persisted as soon as a refund tx is broadcast
    pub(crate) refund_tx_id: Option<String>,
    pub(crate) created_at: u32,
    pub(crate) state: PaymentState,
    pub(crate) claim_private_key: String,
    pub(crate) refund_private_key: String,
}
impl ChainSwap {
    pub(crate) fn get_claim_keypair(&self) -> SdkResult<Keypair> {
        utils::decode_keypair(&self.claim_private_key).map_err(Into::into)
    }

    pub(crate) fn get_refund_keypair(&self) -> SdkResult<Keypair> {
        utils::decode_keypair(&self.refund_private_key).map_err(Into::into)
    }

    pub(crate) fn get_boltz_create_response(&self) -> Result<CreateChainResponse> {
        let internal_create_response: crate::persist::chain::InternalCreateChainResponse =
            serde_json::from_str(&self.create_response_json).map_err(|e| {
                anyhow!("Failed to deserialize InternalCreateSubmarineResponse: {e:?}")
            })?;

        Ok(CreateChainResponse {
            id: self.id.clone(),
            claim_details: internal_create_response.claim_details,
            lockup_details: internal_create_response.lockup_details,
        })
    }

    pub(crate) fn get_boltz_pair(&self) -> Result<ChainPair> {
        let pair: ChainPair = serde_json::from_str(&self.pair_fees_json)
            .map_err(|e| anyhow!("Failed to deserialize ChainPair: {e:?}"))?;

        Ok(pair)
    }

    pub(crate) fn get_claim_swap_script(&self) -> SdkResult<SwapScriptV2> {
        let chain_swap_details = self.get_boltz_create_response()?.claim_details;
        let our_pubkey = self.get_claim_keypair()?.public_key();
        let swap_script = match self.direction {
            Direction::Incoming => SwapScriptV2::Liquid(LBtcSwapScript::chain_from_swap_resp(
                Side::Claim,
                chain_swap_details,
                our_pubkey.into(),
            )?),
            Direction::Outgoing => SwapScriptV2::Bitcoin(BtcSwapScript::chain_from_swap_resp(
                Side::Claim,
                chain_swap_details,
                our_pubkey.into(),
            )?),
        };
        Ok(swap_script)
    }

    pub(crate) fn get_lockup_swap_script(&self) -> SdkResult<SwapScriptV2> {
        let chain_swap_details = self.get_boltz_create_response()?.lockup_details;
        let our_pubkey = self.get_refund_keypair()?.public_key();
        let swap_script = match self.direction {
            Direction::Incoming => SwapScriptV2::Bitcoin(BtcSwapScript::chain_from_swap_resp(
                Side::Lockup,
                chain_swap_details,
                our_pubkey.into(),
            )?),
            Direction::Outgoing => SwapScriptV2::Liquid(LBtcSwapScript::chain_from_swap_resp(
                Side::Lockup,
                chain_swap_details,
                our_pubkey.into(),
            )?),
        };
        Ok(swap_script)
    }

    /// Returns the lockup script pubkey for Receive Chain Swaps
    pub(crate) fn get_receive_lockup_swap_script_pubkey(
        &self,
        network: LiquidNetwork,
    ) -> SdkResult<ScriptBuf> {
        let swap_script = self.get_lockup_swap_script()?.as_bitcoin_script()?;
        let script_pubkey = swap_script
            .to_address(network.as_bitcoin_chain())
            .map_err(|e| SdkError::generic(format!("Error getting script address: {e:?}")))?
            .script_pubkey();
        Ok(script_pubkey)
    }

    pub(crate) fn to_refundable(&self, refundable_amount_sat: u64) -> RefundableSwap {
        RefundableSwap {
            swap_address: self.lockup_address.clone(),
            timestamp: self.created_at,
            amount_sat: refundable_amount_sat,
        }
    }

    pub(crate) fn from_boltz_struct_to_json(
        create_response: &CreateChainResponse,
        expected_swap_id: &str,
    ) -> Result<String, PaymentError> {
        let internal_create_response =
            crate::persist::chain::InternalCreateChainResponse::try_convert_from_boltz(
                create_response,
                expected_swap_id,
            )?;

        let create_response_json =
            serde_json::to_string(&internal_create_response).map_err(|e| {
                PaymentError::Generic {
                    err: format!("Failed to serialize InternalCreateChainResponse: {e:?}"),
                }
            })?;

        Ok(create_response_json)
    }
}

/// A submarine swap, used for Send
#[derive(Clone, Debug)]
pub(crate) struct SendSwap {
    pub(crate) id: String,
    /// Bolt11 or Bolt12 invoice. This is determined by whether `bolt12_offer` is set or not.
    pub(crate) invoice: String,
    /// The bolt12 offer, if this swap sends to a Bolt12 offer
    pub(crate) bolt12_offer: Option<String>,
    pub(crate) payment_hash: Option<String>,
    pub(crate) description: Option<String>,
    pub(crate) preimage: Option<String>,
    pub(crate) payer_amount_sat: u64,
    pub(crate) receiver_amount_sat: u64,
    /// The [SubmarinePair] chosen on swap creation
    pub(crate) pair_fees_json: String,
    /// JSON representation of [crate::persist::send::InternalCreateSubmarineResponse]
    pub(crate) create_response_json: String,
    /// Persisted only when the lockup tx is successfully broadcast
    pub(crate) lockup_tx_id: Option<String>,
    /// Persisted as soon as a refund tx is broadcast
    pub(crate) refund_tx_id: Option<String>,
    pub(crate) created_at: u32,
    pub(crate) state: PaymentState,
    pub(crate) refund_private_key: String,
}
impl SendSwap {
    pub(crate) fn get_refund_keypair(&self) -> Result<Keypair, SdkError> {
        utils::decode_keypair(&self.refund_private_key).map_err(Into::into)
    }

    pub(crate) fn get_boltz_create_response(&self) -> Result<CreateSubmarineResponse> {
        let internal_create_response: crate::persist::send::InternalCreateSubmarineResponse =
            serde_json::from_str(&self.create_response_json).map_err(|e| {
                anyhow!("Failed to deserialize InternalCreateSubmarineResponse: {e:?}")
            })?;

        let res = CreateSubmarineResponse {
            id: self.id.clone(),
            accept_zero_conf: internal_create_response.accept_zero_conf,
            address: internal_create_response.address.clone(),
            bip21: internal_create_response.bip21.clone(),
            claim_public_key: crate::utils::json_to_pubkey(
                &internal_create_response.claim_public_key,
            )?,
            expected_amount: internal_create_response.expected_amount,
            referral_id: internal_create_response.referral_id,
            swap_tree: internal_create_response.swap_tree.clone().into(),
            timeout_block_height: internal_create_response.timeout_block_height,
            blinding_key: internal_create_response.blinding_key.clone(),
        };
        Ok(res)
    }

    pub(crate) fn get_swap_script(&self) -> Result<LBtcSwapScript, SdkError> {
        LBtcSwapScript::submarine_from_swap_resp(
            &self.get_boltz_create_response()?,
            self.get_refund_keypair()?.public_key().into(),
        )
        .map_err(|e| {
            SdkError::generic(format!(
                "Failed to create swap script for Send Swap {}: {e:?}",
                self.id
            ))
        })
    }

    pub(crate) fn from_boltz_struct_to_json(
        create_response: &CreateSubmarineResponse,
        expected_swap_id: &str,
    ) -> Result<String, PaymentError> {
        let internal_create_response =
            crate::persist::send::InternalCreateSubmarineResponse::try_convert_from_boltz(
                create_response,
                expected_swap_id,
            )?;

        let create_response_json =
            serde_json::to_string(&internal_create_response).map_err(|e| {
                PaymentError::Generic {
                    err: format!("Failed to serialize InternalCreateSubmarineResponse: {e:?}"),
                }
            })?;

        Ok(create_response_json)
    }
}

/// A reverse swap, used for Receive
#[derive(Clone, Debug)]
pub(crate) struct ReceiveSwap {
    pub(crate) id: String,
    pub(crate) preimage: String,
    /// JSON representation of [crate::persist::receive::InternalCreateReverseResponse]
    pub(crate) create_response_json: String,
    pub(crate) claim_private_key: String,
    pub(crate) invoice: String,
    pub(crate) payment_hash: Option<String>,
    pub(crate) description: Option<String>,
    /// The amount of the invoice
    pub(crate) payer_amount_sat: u64,
    pub(crate) receiver_amount_sat: u64,
    /// The [ReversePair] chosen on swap creation
    pub(crate) pair_fees_json: String,
    pub(crate) claim_fees_sat: u64,
    /// Persisted as soon as a claim tx is broadcast
    pub(crate) claim_tx_id: Option<String>,
    /// Persisted only when the lockup tx is broadcast
    pub(crate) lockup_tx_id: Option<String>,
    /// The address reserved for a magic routing hint payment
    pub(crate) mrh_address: String,
    /// The script pubkey for a magic routing hint payment
    pub(crate) mrh_script_pubkey: String,
    /// Persisted only if a transaction is sent to the `mrh_address`
    pub(crate) mrh_tx_id: Option<String>,
    /// Until the lockup tx is seen in the mempool, it contains the swap creation time.
    /// Afterwards, it shows the lockup tx creation time.
    pub(crate) created_at: u32,
    pub(crate) state: PaymentState,
}
impl ReceiveSwap {
    pub(crate) fn get_claim_keypair(&self) -> Result<Keypair, PaymentError> {
        utils::decode_keypair(&self.claim_private_key).map_err(Into::into)
    }

    pub(crate) fn get_boltz_create_response(&self) -> Result<CreateReverseResponse, PaymentError> {
        let internal_create_response: crate::persist::receive::InternalCreateReverseResponse =
            serde_json::from_str(&self.create_response_json).map_err(|e| {
                PaymentError::Generic {
                    err: format!("Failed to deserialize InternalCreateReverseResponse: {e:?}"),
                }
            })?;

        let res = CreateReverseResponse {
            id: self.id.clone(),
            invoice: self.invoice.clone(),
            swap_tree: internal_create_response.swap_tree.clone().into(),
            lockup_address: internal_create_response.lockup_address.clone(),
            refund_public_key: crate::utils::json_to_pubkey(
                &internal_create_response.refund_public_key,
            )?,
            timeout_block_height: internal_create_response.timeout_block_height,
            onchain_amount: internal_create_response.onchain_amount,
            blinding_key: internal_create_response.blinding_key.clone(),
        };
        Ok(res)
    }

    pub(crate) fn get_swap_script(&self) -> Result<LBtcSwapScript, PaymentError> {
        let keypair = self.get_claim_keypair()?;
        let create_response =
            self.get_boltz_create_response()
                .map_err(|e| PaymentError::Generic {
                    err: format!(
                        "Failed to create swap script for Receive Swap {}: {e:?}",
                        self.id
                    ),
                })?;
        LBtcSwapScript::reverse_from_swap_resp(&create_response, keypair.public_key().into())
            .map_err(|e| PaymentError::Generic {
                err: format!(
                    "Failed to create swap script for Receive Swap {}: {e:?}",
                    self.id
                ),
            })
    }

    pub(crate) fn from_boltz_struct_to_json(
        create_response: &CreateReverseResponse,
        expected_swap_id: &str,
        expected_invoice: &str,
    ) -> Result<String, PaymentError> {
        let internal_create_response =
            crate::persist::receive::InternalCreateReverseResponse::try_convert_from_boltz(
                create_response,
                expected_swap_id,
                expected_invoice,
            )?;

        let create_response_json =
            serde_json::to_string(&internal_create_response).map_err(|e| {
                PaymentError::Generic {
                    err: format!("Failed to serialize InternalCreateReverseResponse: {e:?}"),
                }
            })?;

        Ok(create_response_json)
    }
}

/// Returned when calling [crate::sdk::LiquidSdk::list_refundables].
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct RefundableSwap {
    pub swap_address: String,
    pub timestamp: u32,
    /// Amount that is refundable, from all UTXOs
    pub amount_sat: u64,
}

/// The payment state of an individual payment.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Hash)]
pub enum PaymentState {
    Created = 0,

    /// ## Receive Swaps
    ///
    /// Covers the cases when
    /// - the lockup tx is seen in the mempool or
    /// - our claim tx is broadcast
    ///
    /// When the claim tx is broadcast, `claim_tx_id` is set in the swap.
    ///
    /// ## Send Swaps
    ///
    /// This is the status when our lockup tx was broadcast
    ///
    /// ## Chain Swaps
    ///
    /// This is the status when the user lockup tx was broadcast
    ///
    /// ## No swap data available
    ///
    /// If no associated swap is found, this indicates the underlying tx is not confirmed yet.
    Pending = 1,

    /// ## Receive Swaps
    ///
    /// Covers the case when the claim tx is confirmed.
    ///
    /// ## Send and Chain Swaps
    ///
    /// This is the status when the claim tx is broadcast and we see it in the mempool.
    ///
    /// ## No swap data available
    ///
    /// If no associated swap is found, this indicates the underlying tx is confirmed.
    Complete = 2,

    /// ## Receive Swaps
    ///
    /// This is the status when the swap failed for any reason and the Receive could not complete.
    ///
    /// ## Send and Chain Swaps
    ///
    /// This is the status when a swap refund was initiated and the refund tx is confirmed.
    Failed = 3,

    /// ## Send and Outgoing Chain Swaps
    ///
    /// This covers the case when the swap state is still Created and the swap fails to reach the
    /// Pending state in time. The TimedOut state indicates the lockup tx should never be broadcast.
    TimedOut = 4,

    /// ## Incoming Chain Swaps
    ///
    /// This covers the case when the swap failed for any reason and there is a user lockup tx.
    /// The swap in this case has to be manually refunded with a provided Bitcoin address
    Refundable = 5,

    /// ## Send and Chain Swaps
    ///
    /// This is the status when a refund was initiated and/or our refund tx was broadcast
    ///
    /// When the refund tx is broadcast, `refund_tx_id` is set in the swap.
    RefundPending = 6,
}
impl ToSql for PaymentState {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        Ok(rusqlite::types::ToSqlOutput::from(*self as i8))
    }
}
impl FromSql for PaymentState {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
        match value {
            ValueRef::Integer(i) => match i as u8 {
                0 => Ok(PaymentState::Created),
                1 => Ok(PaymentState::Pending),
                2 => Ok(PaymentState::Complete),
                3 => Ok(PaymentState::Failed),
                4 => Ok(PaymentState::TimedOut),
                5 => Ok(PaymentState::Refundable),
                6 => Ok(PaymentState::RefundPending),
                _ => Err(FromSqlError::OutOfRange(i)),
            },
            _ => Err(FromSqlError::InvalidType),
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, EnumString, Display, Hash, PartialEq, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum PaymentType {
    Receive = 0,
    Send = 1,
}
impl From<Direction> for PaymentType {
    fn from(value: Direction) -> Self {
        match value {
            Direction::Incoming => Self::Receive,
            Direction::Outgoing => Self::Send,
        }
    }
}
impl ToSql for PaymentType {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        Ok(rusqlite::types::ToSqlOutput::from(*self as i8))
    }
}
impl FromSql for PaymentType {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
        match value {
            ValueRef::Integer(i) => match i as u8 {
                0 => Ok(PaymentType::Receive),
                1 => Ok(PaymentType::Send),
                _ => Err(FromSqlError::OutOfRange(i)),
            },
            _ => Err(FromSqlError::InvalidType),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub enum PaymentStatus {
    Pending = 0,
    Complete = 1,
}
impl ToSql for PaymentStatus {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        Ok(rusqlite::types::ToSqlOutput::from(*self as i8))
    }
}
impl FromSql for PaymentStatus {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
        match value {
            ValueRef::Integer(i) => match i as u8 {
                0 => Ok(PaymentStatus::Pending),
                1 => Ok(PaymentStatus::Complete),
                _ => Err(FromSqlError::OutOfRange(i)),
            },
            _ => Err(FromSqlError::InvalidType),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct PaymentTxData {
    /// The tx ID of the transaction
    pub tx_id: String,

    /// The point in time when the underlying tx was included in a block.
    pub timestamp: Option<u32>,

    /// The onchain tx amount.
    ///
    /// In case of an outbound payment (Send), this is the payer amount. Otherwise it's the receiver amount.
    pub amount_sat: u64,

    /// The onchain fees of this tx
    pub fees_sat: u64,

    pub payment_type: PaymentType,

    /// Onchain tx status
    pub is_confirmed: bool,
}

#[derive(Debug, Clone, Serialize)]
pub enum PaymentSwapType {
    Receive,
    Send,
    Chain,
}

#[derive(Debug, Clone, Serialize)]
pub struct PaymentSwapData {
    pub swap_id: String,

    pub swap_type: PaymentSwapType,

    /// Swap creation timestamp
    pub created_at: u32,

    pub preimage: Option<String>,
    pub bolt11: Option<String>,
    pub bolt12_offer: Option<String>,
    pub payment_hash: Option<String>,
    pub description: String,

    /// Amount sent by the swap payer
    pub payer_amount_sat: u64,

    /// Amount received by the swap receiver
    pub receiver_amount_sat: u64,

    /// The swapper service fee
    pub swapper_fees_sat: u64,

    pub refund_tx_id: Option<String>,
    pub refund_tx_amount_sat: Option<u64>,

    /// Present only for chain swaps.
    /// In case of an outgoing chain swap, it's the Bitcoin address which will receive the funds
    /// In case of an incoming chain swap, it's the Liquid address which will receive the funds
    pub claim_address: Option<String>,

    /// Payment status derived from the swap status
    pub status: PaymentState,
}

/// The specific details of a payment, depending on its type
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum PaymentDetails {
    /// Swapping to or from Lightning
    Lightning {
        swap_id: String,

        /// Represents the invoice description
        description: String,

        /// In case of a Send swap, this is the preimage of the paid invoice (proof of payment).
        preimage: Option<String>,

        /// Represents the Bolt11 invoice associated with a payment
        /// In the case of a Send payment, this is the invoice paid by the swapper
        /// In the case of a Receive payment, this is the invoice paid by the user
        bolt11: Option<String>,

        bolt12_offer: Option<String>,

        /// The payment hash of the invoice
        payment_hash: Option<String>,

        /// For a Send swap which was refunded, this is the refund tx id
        refund_tx_id: Option<String>,

        /// For a Send swap which was refunded, this is the refund amount
        refund_tx_amount_sat: Option<u64>,
    },
    /// Direct onchain payment to a Liquid address
    Liquid {
        /// Represents either a Liquid BIP21 URI or pure address
        destination: String,

        /// Represents the BIP21 `message` field
        description: String,
    },
    /// Swapping to or from the Bitcoin chain
    Bitcoin {
        swap_id: String,

        /// Represents the invoice description
        description: String,

        /// For a Send swap which was refunded, this is the refund tx id
        refund_tx_id: Option<String>,

        /// For a Send swap which was refunded, this is the refund amount
        refund_tx_amount_sat: Option<u64>,
    },
}

impl PaymentDetails {
    pub(crate) fn get_swap_id(&self) -> Option<String> {
        match self {
            Self::Lightning { swap_id, .. } | Self::Bitcoin { swap_id, .. } => {
                Some(swap_id.clone())
            }
            Self::Liquid { .. } => None,
        }
    }

    pub(crate) fn get_refund_tx_amount_sat(&self) -> Option<u64> {
        match self {
            Self::Lightning {
                refund_tx_amount_sat,
                ..
            }
            | Self::Bitcoin {
                refund_tx_amount_sat,
                ..
            } => *refund_tx_amount_sat,
            Self::Liquid { .. } => None,
        }
    }
}

/// Represents an SDK payment.
///
/// By default, this is an onchain tx. It may represent a swap, if swap metadata is available.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Payment {
    /// The destination associated with the payment, if it was created via our SDK.
    /// Can be either a Liquid/Bitcoin address, a Liquid BIP21 URI or an invoice
    pub destination: Option<String>,

    pub tx_id: Option<String>,

    /// Composite timestamp that can be used for sorting or displaying the payment.
    ///
    /// If this payment has an associated swap, it is the swap creation time. Otherwise, the point
    /// in time when the underlying tx was included in a block. If there is no associated swap
    /// available and the underlying tx is not yet confirmed, the value is `now()`.
    pub timestamp: u32,

    /// The payment amount, which corresponds to the onchain tx amount.
    ///
    /// In case of an outbound payment (Send), this is the payer amount. Otherwise it's the receiver amount.
    pub amount_sat: u64,

    /// Represents the fees paid by this wallet for this payment.
    ///
    /// ### Swaps
    /// If there is an associated Send Swap, these fees represent the total fees paid by this wallet
    /// (the sender). It is the difference between the amount that was sent and the amount received.
    ///
    /// If there is an associated Receive Swap, these fees represent the total fees paid by this wallet
    /// (the receiver). It is also the difference between the amount that was sent and the amount received.
    ///
    /// ### Pure onchain txs
    /// If no swap is associated with this payment:
    /// - for Send payments, this is the onchain tx fee
    /// - for Receive payments, this is zero
    pub fees_sat: u64,

    /// Service fees paid to the swapper service. This is only set for swaps (i.e. doesn't apply to
    /// direct Liquid payments).
    pub swapper_fees_sat: Option<u64>,

    /// If it is a `Send` or `Receive` payment
    pub payment_type: PaymentType,

    /// Composite status representing the overall status of the payment.
    ///
    /// If the tx has no associated swap, this reflects the onchain tx status (confirmed or not).
    ///
    /// If the tx has an associated swap, this is determined by the swap status (pending or complete).
    pub status: PaymentState,

    /// The details of a payment, depending on its [destination](Payment::destination) and
    /// [type](Payment::payment_type)
    pub details: PaymentDetails,
}
impl Payment {
    pub(crate) fn from_pending_swap(swap: PaymentSwapData, payment_type: PaymentType) -> Payment {
        let amount_sat = match payment_type {
            PaymentType::Receive => swap.receiver_amount_sat,
            PaymentType::Send => swap.payer_amount_sat,
        };

        Payment {
            destination: swap.bolt11.clone(),
            tx_id: None,
            timestamp: swap.created_at,
            amount_sat,
            fees_sat: swap.payer_amount_sat - swap.receiver_amount_sat,
            swapper_fees_sat: Some(swap.swapper_fees_sat),
            payment_type,
            status: swap.status,
            details: PaymentDetails::Lightning {
                swap_id: swap.swap_id,
                preimage: swap.preimage,
                bolt11: swap.bolt11,
                bolt12_offer: swap.bolt12_offer,
                payment_hash: swap.payment_hash,
                description: swap.description,
                refund_tx_id: swap.refund_tx_id,
                refund_tx_amount_sat: swap.refund_tx_amount_sat,
            },
        }
    }

    pub(crate) fn from_tx_data(
        tx: PaymentTxData,
        swap: Option<PaymentSwapData>,
        details: PaymentDetails,
    ) -> Payment {
        Payment {
            tx_id: Some(tx.tx_id),
            // When the swap is present and of type send and receive, we retrieve the destination from the invoice.
            // If it's a chain swap instead, we use the `claim_address` field from the swap data (either pure Bitcoin or Liquid address).
            // Otherwise, we specify the Liquid address (BIP21 or pure), set in `payment_details.address`.
            destination: match &swap {
                Some(PaymentSwapData {
                    swap_type: PaymentSwapType::Receive,
                    bolt11,
                    ..
                }) => bolt11.clone(),
                Some(PaymentSwapData {
                    swap_type: PaymentSwapType::Send,
                    bolt11,
                    bolt12_offer,
                    ..
                }) => bolt11.clone().or(bolt12_offer.clone()),
                Some(PaymentSwapData {
                    swap_type: PaymentSwapType::Chain,
                    claim_address,
                    ..
                }) => claim_address.clone(),
                _ => match &details {
                    PaymentDetails::Liquid { destination, .. } => Some(destination.clone()),
                    _ => None,
                },
            },
            timestamp: tx
                .timestamp
                .or(swap.as_ref().map(|s| s.created_at))
                .unwrap_or(utils::now()),
            amount_sat: tx.amount_sat,
            fees_sat: match swap.as_ref() {
                Some(s) => s.payer_amount_sat - s.receiver_amount_sat,
                None => match tx.payment_type {
                    PaymentType::Receive => 0,
                    PaymentType::Send => tx.fees_sat,
                },
            },
            swapper_fees_sat: swap.as_ref().map(|s| s.swapper_fees_sat),
            payment_type: tx.payment_type,
            status: match &swap {
                Some(swap) => swap.status,
                None => match tx.is_confirmed {
                    true => PaymentState::Complete,
                    false => PaymentState::Pending,
                },
            },
            details,
        }
    }

    pub(crate) fn get_refund_tx_id(&self) -> Option<String> {
        match self.details.clone() {
            PaymentDetails::Lightning { refund_tx_id, .. } => Some(refund_tx_id),
            PaymentDetails::Bitcoin { refund_tx_id, .. } => Some(refund_tx_id),
            PaymentDetails::Liquid { .. } => None,
        }
        .flatten()
    }
}

/// Returned when calling [crate::sdk::LiquidSdk::recommended_fees].
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RecommendedFees {
    pub fastest_fee: u64,
    pub half_hour_fee: u64,
    pub hour_fee: u64,
    pub economy_fee: u64,
    pub minimum_fee: u64,
}

/// An argument of [PrepareBuyBitcoinRequest] when calling [crate::sdk::LiquidSdk::prepare_buy_bitcoin].
#[derive(Debug, Clone, Copy, EnumString, PartialEq, Serialize)]
pub enum BuyBitcoinProvider {
    #[strum(serialize = "moonpay")]
    Moonpay,
}

/// An argument when calling [crate::sdk::LiquidSdk::prepare_buy_bitcoin].
#[derive(Debug, Serialize)]
pub struct PrepareBuyBitcoinRequest {
    pub provider: BuyBitcoinProvider,
    pub amount_sat: u64,
}

/// Returned when calling [crate::sdk::LiquidSdk::prepare_buy_bitcoin].
#[derive(Clone, Debug, Serialize)]
pub struct PrepareBuyBitcoinResponse {
    pub provider: BuyBitcoinProvider,
    pub amount_sat: u64,
    pub fees_sat: u64,
}

/// An argument when calling [crate::sdk::LiquidSdk::buy_bitcoin].
#[derive(Clone, Debug, Serialize)]
pub struct BuyBitcoinRequest {
    pub prepare_response: PrepareBuyBitcoinResponse,

    /// The optional URL to redirect to after completing the buy.
    ///
    /// For Moonpay, see <https://dev.moonpay.com/docs/on-ramp-configure-user-journey-params>
    pub redirect_url: Option<String>,
}

/// Internal SDK log entry used in the Uniffi and Dart bindings
#[derive(Clone, Debug)]
pub struct LogEntry {
    pub line: String,
    pub level: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct InternalLeaf {
    pub output: String,
    pub version: u8,
}
impl From<InternalLeaf> for Leaf {
    fn from(value: InternalLeaf) -> Self {
        Leaf {
            output: value.output,
            version: value.version,
        }
    }
}
impl From<Leaf> for InternalLeaf {
    fn from(value: Leaf) -> Self {
        InternalLeaf {
            output: value.output,
            version: value.version,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(super) struct InternalSwapTree {
    claim_leaf: InternalLeaf,
    refund_leaf: InternalLeaf,
}
impl From<InternalSwapTree> for SwapTree {
    fn from(value: InternalSwapTree) -> Self {
        SwapTree {
            claim_leaf: value.claim_leaf.into(),
            refund_leaf: value.refund_leaf.into(),
        }
    }
}
impl From<SwapTree> for InternalSwapTree {
    fn from(value: SwapTree) -> Self {
        InternalSwapTree {
            claim_leaf: value.claim_leaf.into(),
            refund_leaf: value.refund_leaf.into(),
        }
    }
}

/// An argument when calling [crate::sdk::LiquidSdk::prepare_lnurl_pay].
#[derive(Debug, Serialize)]
pub struct PrepareLnUrlPayRequest {
    /// The [LnUrlPayRequestData] returned by [crate::input_parser::parse]
    pub data: LnUrlPayRequestData,
    /// The amount in millisatoshis for this payment
    pub amount_msat: u64,
    /// An optional comment for this payment
    pub comment: Option<String>,
    /// Validates that, if there is a URL success action, the URL domain matches
    /// the LNURL callback domain. Defaults to `true`
    pub validate_success_action_url: Option<bool>,
}

/// Returned when calling [crate::sdk::LiquidSdk::prepare_lnurl_pay].
#[derive(Debug, Serialize)]
pub struct PrepareLnUrlPayResponse {
    /// The destination of the payment
    pub destination: SendDestination,
    /// The fees in satoshis to send the payment
    pub fees_sat: u64,
    /// The unprocessed LUD-09 success action. This will be processed and decrypted if
    /// needed after calling [crate::sdk::LiquidSdk::lnurl_pay]
    pub success_action: Option<SuccessAction>,
}

/// An argument when calling [crate::sdk::LiquidSdk::lnurl_pay].
#[derive(Debug, Serialize)]
pub struct LnUrlPayRequest {
    /// The response from calling [crate::sdk::LiquidSdk::prepare_lnurl_pay]
    pub prepare_response: PrepareLnUrlPayResponse,
}

/// Contains the result of the entire LNURL-pay interaction, as reported by the LNURL endpoint.
///
/// * `EndpointSuccess` indicates the payment is complete. The endpoint may return a `SuccessActionProcessed`,
///   in which case, the wallet has to present it to the user as described in
///   <https://github.com/lnurl/luds/blob/luds/09.md>
///
/// * `EndpointError` indicates a generic issue the LNURL endpoint encountered, including a freetext
///   field with the reason.
///
/// * `PayError` indicates that an error occurred while trying to pay the invoice from the LNURL endpoint.
///   This includes the payment hash of the failed invoice and the failure reason.
#[derive(Serialize)]
#[allow(clippy::large_enum_variant)]
pub enum LnUrlPayResult {
    EndpointSuccess { data: LnUrlPaySuccessData },
    EndpointError { data: LnUrlErrorData },
    PayError { data: LnUrlPayErrorData },
}

#[derive(Serialize)]
pub struct LnUrlPaySuccessData {
    pub payment: Payment,
    pub success_action: Option<SuccessActionProcessed>,
}

#[derive(Debug, Clone)]
pub enum Transaction {
    Liquid(boltz_client::elements::Transaction),
    Bitcoin(boltz_client::bitcoin::Transaction),
}

impl Transaction {
    pub(crate) fn txid(&self) -> String {
        match self {
            Transaction::Liquid(tx) => tx.txid().to_hex(),
            Transaction::Bitcoin(tx) => tx.txid().to_hex(),
        }
    }
}

#[derive(Debug, Clone)]
pub enum Utxo {
    Liquid(
        Box<(
            boltz_client::elements::OutPoint,
            boltz_client::elements::TxOut,
        )>,
    ),
    Bitcoin(
        (
            boltz_client::bitcoin::OutPoint,
            boltz_client::bitcoin::TxOut,
        ),
    ),
}

impl Utxo {
    pub(crate) fn as_bitcoin(
        &self,
    ) -> Option<&(
        boltz_client::bitcoin::OutPoint,
        boltz_client::bitcoin::TxOut,
    )> {
        match self {
            Utxo::Liquid(_) => None,
            Utxo::Bitcoin(utxo) => Some(utxo),
        }
    }

    pub(crate) fn as_liquid(
        &self,
    ) -> Option<
        Box<(
            boltz_client::elements::OutPoint,
            boltz_client::elements::TxOut,
        )>,
    > {
        match self {
            Utxo::Bitcoin(_) => None,
            Utxo::Liquid(utxo) => Some(utxo.clone()),
        }
    }
}

#[macro_export]
macro_rules! get_invoice_amount {
    ($invoice:expr) => {
        $invoice
            .parse::<Bolt11Invoice>()
            .expect("Expecting valid invoice")
            .amount_milli_satoshis()
            .expect("Expecting valid amount")
            / 1000
    };
}

#[macro_export]
macro_rules! get_invoice_description {
    ($invoice:expr) => {
        match $invoice
            .trim()
            .parse::<Bolt11Invoice>()
            .expect("Expecting valid invoice")
            .description()
        {
            Bolt11InvoiceDescription::Direct(msg) => Some(msg.to_string()),
            Bolt11InvoiceDescription::Hash(_) => None,
        }
    };
}