uniffi_lipalightninglib/
lib.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
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
//! # lipa-lightning-lib (aka 3L)
//!
//! This crate implements all the main business logic of the lipa wallet.
//!
//! Most functionality can be accessed by creating an instance of [`LightningNode`] and using its methods.

#![allow(clippy::let_unit_value)]
// clippy::empty_line_after_doc_comments is allowed due to the uniffi rust scaffolding code triggering this lint
// TODO: check if Uniffi generated code starts to conform to this lint and remove the following statement if so
#![allow(clippy::empty_line_after_doc_comments)]
#![allow(deprecated)]

extern crate core;

mod actions_required;
mod activities;
mod activity;
mod amount;
mod analytics;
mod async_runtime;
mod auth;
mod backup;
mod callbacks;
mod config;
mod data_store;
mod errors;
mod event;
mod exchange_rate_provider;
mod fiat_topup;
mod invoice_details;
mod key_derivation;
mod lightning;
mod lightning_address;
mod limits;
mod locker;
mod logger;
mod migrations;
mod node_config;
mod notification_handling;
mod offer;
mod onchain;
mod payment;
mod phone_number;
mod random;
mod recovery;
mod reverse_swap;
mod sanitize_input;
mod secret;
mod support;
mod swap;
mod symmetric_encryption;
mod task_manager;
mod util;

pub use crate::activity::{Activity, ChannelCloseInfo, ChannelCloseState, ListActivitiesResponse};
pub use crate::amount::{Amount, FiatValue};
use crate::amount::{AsSats, Msats, Permyriad, ToAmount};
use crate::analytics::{derive_analytics_keys, AnalyticsInterceptor};
pub use crate::analytics::{AnalyticsConfig, InvoiceCreationMetadata, PaymentMetadata};
use crate::async_runtime::AsyncRuntime;
use crate::auth::{build_async_auth, build_auth};
use crate::backup::BackupManager;
pub use crate::callbacks::EventsCallback;
pub use crate::errors::{
    DecodeDataError, Error as LnError, LnUrlPayError, LnUrlPayErrorCode, LnUrlPayResult,
    MnemonicError, NotificationHandlingError, NotificationHandlingErrorCode, ParseError,
    ParsePhoneNumberError, ParsePhoneNumberPrefixError, PayError, PayErrorCode, PayResult, Result,
    RuntimeErrorCode, SimpleError, UnsupportedDataType,
};
use crate::errors::{LnUrlWithdrawError, LnUrlWithdrawErrorCode, LnUrlWithdrawResult};
use crate::event::LipaEventListener;
pub use crate::exchange_rate_provider::ExchangeRate;
use crate::exchange_rate_provider::ExchangeRateProviderImpl;
pub use crate::invoice_details::InvoiceDetails;
use crate::key_derivation::derive_persistence_encryption_key;
pub use crate::lightning::bolt11::Bolt11;
pub use crate::lightning::lnurl::{LnUrlPayDetails, LnUrlWithdrawDetails, Lnurl};
pub use crate::lightning::receive_limits::{LiquidityLimit, ReceiveAmountLimits};
pub use crate::limits::PaymentAmountLimits;
use crate::locker::Locker;
pub use crate::node_config::{
    BreezSdkConfig, LightningNodeConfig, MaxRoutingFeeConfig, ReceiveLimitsConfig,
    RemoteServicesConfig, TzConfig, TzTime,
};
pub use crate::notification_handling::{handle_notification, Notification, NotificationToggles};
pub use crate::offer::{OfferInfo, OfferKind, OfferStatus};
pub use crate::payment::{
    IncomingPaymentInfo, OutgoingPaymentInfo, PaymentInfo, PaymentState, Recipient,
};
use crate::phone_number::PhoneNumberPrefixParser;
pub use crate::phone_number::{PhoneNumber, PhoneNumberRecipient};
pub use crate::recovery::recover_lightning_node;
pub use crate::reverse_swap::ReverseSwapInfo;
pub use crate::secret::{generate_secret, mnemonic_to_secret, words_by_prefix, Secret};
pub use crate::swap::{
    FailedSwapInfo, ResolveFailedSwapInfo, SwapAddressInfo, SwapInfo, SwapToLightningFees,
};
use crate::symmetric_encryption::deterministic_encrypt;
use crate::task_manager::TaskManager;
use crate::util::unix_timestamp_to_system_time;
pub use crate::util::Util;

pub use crow::FiatTopupSetupInfo;

pub use crate::actions_required::ActionsRequired;
pub use crate::activities::Activities;
pub use crate::config::Config;
pub use crate::fiat_topup::FiatTopup;
pub use crate::lightning::{Lightning, PaymentAffordability};
pub use crate::lightning_address::LightningAddress;
pub use crate::onchain::channel_closes::{ChannelClose, SweepChannelCloseInfo};
pub use crate::onchain::reverse_swap::ReverseSwap;
pub use crate::onchain::swap::{Swap, SweepFailedSwapInfo};
pub use crate::onchain::Onchain;
use crate::support::Support;
pub use breez_sdk_core::error::ReceiveOnchainError as SwapError;
pub use breez_sdk_core::error::RedeemOnchainError as SweepError;
use breez_sdk_core::error::{ReceiveOnchainError, RedeemOnchainError};
pub use breez_sdk_core::HealthCheckStatus as BreezHealthCheckStatus;
pub use breez_sdk_core::ReverseSwapStatus;
use breez_sdk_core::{
    BitcoinAddressData, BreezServices, ConnectRequest, EnvironmentType, EventListener,
    GreenlightCredentials, GreenlightNodeConfig, ListPaymentsRequest, LnUrlPayRequestData,
    LnUrlWithdrawRequestData, Network, NodeConfig, OpeningFeeParams, PaymentDetails,
    PaymentTypeFilter, PrepareOnchainPaymentResponse,
};
use crow::{OfferManager, TopupError};
pub use crow::{PermanentFailureCode, TemporaryFailureCode};
use data_store::DataStore;
use hex::FromHex;
use honeybadger::Auth;
pub use honeybadger::{TermsAndConditions, TermsAndConditionsStatus};
use log::{debug, error, info, Level};
use logger::init_logger_once;
use num_enum::TryFromPrimitive;
use parrot::AnalyticsClient;
pub use parrot::PaymentSource;
use perro::{
    ensure, invalid_input, permanent_failure, runtime_error, MapToError, OptionToError, ResultTrait,
};
use squirrel::RemoteBackupClient;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::{env, fs};

const LOGS_DIR: &str = "logs";

const CLN_DUST_LIMIT_SAT: u64 = 546;

pub(crate) const DB_FILENAME: &str = "db2.db3";

/// Represent the result of comparision of a value with a given range.
pub enum RangeHit {
    /// The value is below the left side of the range.
    Below { min: Amount },
    /// The value is whithin the range.
    In,
    /// The value is above the right side of the range.
    Above { max: Amount },
}

/// The fee charged by the Lightning Service Provider (LSP) for opening a channel with the node.
/// This fee is being charged at the time of the channel creation.
/// The LSP simply subtracts this fee from an incoming payment (if this incoming payment leads to a channel creation).
pub struct LspFee {
    pub channel_minimum_fee: Amount,
    /// Parts per myriad (aka basis points) -> 100 is 1%
    pub channel_fee_permyriad: u64,
}

/// The type returned by [`LightningNode::calculate_lsp_fee`].
///
#[deprecated = "CalculateLspFeeResponseV2 should be used instead"]
pub struct CalculateLspFeeResponse {
    /// Indicates the amount that will be charged.
    pub lsp_fee: Amount,
    /// An internal struct is not supposed to be inspected, but only passed to [`LightningNode::create_invoice`].
    pub lsp_fee_params: Option<OpeningFeeParams>,
}

/// The type returned by [`Swap::calculate_lsp_fee_for_amount`] and [`Lightning::calculate_lsp_fee_for_amount`] .
pub struct CalculateLspFeeResponseV2 {
    /// Indicates the amount that will be charged.
    pub lsp_fee: Amount,
    /// An internal struct is not supposed to be inspected, but only passed to [`LightningNode::create_invoice`].
    pub lsp_fee_params: OpeningFeeParams,
}

/// Information about the Lightning node running in the background
pub struct NodeInfo {
    /// Lightning network public key of the node (also known as node id)
    pub node_pubkey: String,
    /// List of node ids of all the peers the node is connected to
    pub peers: Vec<String>,
    /// Amount of on-chain balance the node has
    pub onchain_balance: Amount,
    /// Information about the channels of the node
    pub channels_info: ChannelsInfo,
}

/// Information about the channels of the node
pub struct ChannelsInfo {
    /// The balance of the local node
    pub local_balance: Amount,
    /// The max amount that can be received in a single payment.
    /// Can be lower than `total_inbound_capacity` because MPP isn't allowed.
    pub max_receivable_single_payment: Amount,
    /// Capacity the node can actually receive.
    /// It excludes non usable channels, pending HTLCs, channels reserves, etc.
    pub total_inbound_capacity: Amount,
    /// Capacity the node can actually send.
    /// It excludes non usable channels, pending HTLCs, channels reserves, etc.
    pub outbound_capacity: Amount,
}

/// Indicates the max routing fee mode used to restrict fees of a payment of a given size
pub enum MaxRoutingFeeMode {
    /// `max_fee_permyriad` Parts per myriad (aka basis points) -> 100 is 1%
    Relative {
        max_fee_permyriad: u16,
    },
    Absolute {
        max_fee_amount: Amount,
    },
}

/// An error associated with a specific PocketOffer. Can be temporary, indicating there was an issue
/// with a previous withdrawal attempt and it can be retried, or it can be permanent.
///
/// More information on each specific error can be found on
/// [Pocket's Documentation Page](<https://pocketbitcoin.com/developers/docs/rest/v1/webhooks>).
pub type PocketOfferError = TopupError;

#[derive(Clone)]
pub struct SweepInfo {
    pub address: String,
    pub onchain_fee_rate: u32,
    pub onchain_fee_amount: Amount,
    pub amount: Amount,
}

#[derive(Clone, PartialEq, Debug)]
pub(crate) struct UserPreferences {
    fiat_currency: String,
    timezone_config: TzConfig,
}

/// Decoded data that can be obtained using [`LightningNode::decode_data`].
pub enum DecodedData {
    Bolt11Invoice {
        invoice_details: InvoiceDetails,
    },
    LnUrlPay {
        lnurl_pay_details: LnUrlPayDetails,
    },
    LnUrlWithdraw {
        lnurl_withdraw_details: LnUrlWithdrawDetails,
    },
    OnchainAddress {
        onchain_address_details: BitcoinAddressData,
    },
}

/// Invoice affordability returned by [`LightningNode::get_invoice_affordability`].
#[derive(Debug)]
pub enum InvoiceAffordability {
    /// Not enough funds available to pay the requested amount.
    NotEnoughFunds,
    /// Not enough funds available to pay the requested amount and the max routing fees.
    /// There might be a route that is affordable enough but it is unknown until tried.
    UnaffordableFees,
    /// Enough funds for the invoice and routing fees are available.
    Affordable,
}

impl From<PaymentAffordability> for InvoiceAffordability {
    fn from(value: PaymentAffordability) -> Self {
        match value {
            PaymentAffordability::NotEnoughFunds => InvoiceAffordability::NotEnoughFunds,
            PaymentAffordability::UnaffordableFees => InvoiceAffordability::UnaffordableFees,
            PaymentAffordability::Affordable => InvoiceAffordability::Affordable,
        }
    }
}

/// Information about a wallet clearance operation as returned by
/// [`LightningNode::prepare_clear_wallet`].
pub struct ClearWalletInfo {
    /// The total amount available to be cleared. The amount sent will be smaller due to fees.
    pub clear_amount: Amount,
    /// Total fee estimate. Can differ from that fees that are charged when clearing the wallet.
    pub total_estimated_fees: Amount,
    /// Estimate for the total that will be paid in on-chain fees (lockup + claim txs).
    pub onchain_fee: Amount,
    /// Estimate for the fee paid to the swap service.
    pub swap_fee: Amount,
    prepare_response: PrepareOnchainPaymentResponse,
}

#[derive(PartialEq, Eq, Debug, TryFromPrimitive, Clone, Copy)]
#[repr(u8)]
pub(crate) enum EnableStatus {
    Enabled,
    FeatureDisabled,
}

pub enum FeatureFlag {
    LightningAddress,
    PhoneNumber,
}

/// The main class/struct of this library. Constructing an instance will initiate the Lightning node and
/// run it in the background. As long as an instance of `LightningNode` is held, the node will continue to run
/// in the background. Dropping the instance will start a deinit process.  
pub struct LightningNode {
    sdk: Arc<BreezServices>,
    auth: Arc<Auth>,
    offer_manager: Arc<OfferManager>,
    rt: Arc<AsyncRuntime>,
    node_config: LightningNodeConfig,
    activities: Arc<Activities>,
    lightning: Arc<Lightning>,
    config: Arc<Config>,
    fiat_topup: Arc<FiatTopup>,
    actions_required: Arc<ActionsRequired>,
    onchain: Arc<Onchain>,
    lightning_address: Arc<LightningAddress>,
    phone_number: Arc<PhoneNumber>,
    util: Arc<Util>,
}

/// Contains the fee information for the options to resolve funds that have moved on-chain.
///
/// This can occur due to channel closes, or swaps that failed to resolve in the available period.
pub struct OnchainResolvingFees {
    /// Fees to swap the funds back to lightning using [`LightningNode::swap_channel_close_funds_to_lightning`]
    /// or [`LightningNode::swap_failed_swap_funds_to_lightning`].
    /// Only available if enough funds are there to swap.
    pub swap_fees: Option<SwapToLightningFees>,
    /// Estimate of the fees for sending the funds on-chain using [`LightningNode::sweep_funds_from_channel_closes`]
    /// or [`LightningNode::resolve_failed_swap`].
    /// The exact fees will be known when calling [`LightningNode::prepare_sweep_funds_from_channel_closes`]
    /// or [`LightningNode::prepare_resolve_failed_swap`].
    pub sweep_onchain_fee_estimate: Amount,
    /// The fee rate used to compute `swaps_fees` and `sweep_onchain_fee_estimate`.
    /// It should be provided when swapping funds back to lightning or when sweeping funds
    /// to on-chain to ensure the same fee rate is used.
    pub sats_per_vbyte: u32,
}

#[allow(clippy::large_enum_variant)]
pub enum ActionRequiredItem {
    UncompletedOffer { offer: OfferInfo },
    UnresolvedFailedSwap { failed_swap: FailedSwapInfo },
    ChannelClosesFundsAvailable { available_funds: Amount },
}

impl From<OfferInfo> for ActionRequiredItem {
    fn from(value: OfferInfo) -> Self {
        ActionRequiredItem::UncompletedOffer { offer: value }
    }
}

impl From<FailedSwapInfo> for ActionRequiredItem {
    fn from(value: FailedSwapInfo) -> Self {
        ActionRequiredItem::UnresolvedFailedSwap { failed_swap: value }
    }
}

impl LightningNode {
    /// Create a new instance of [`LightningNode`].
    ///
    /// Parameters:
    /// * `config` - configuration parameters
    /// * `events_callback` - a callbacks interface for the consumer of this library to be notified
    ///   of certain events.
    ///
    /// Requires network: **yes**
    pub fn new(
        node_config: LightningNodeConfig,
        events_callback: Box<dyn EventsCallback>,
    ) -> Result<Self> {
        enable_backtrace();
        fs::create_dir_all(&node_config.local_persistence_path).map_to_permanent_failure(
            format!(
                "Failed to create directory: {}",
                &node_config.local_persistence_path,
            ),
        )?;
        if let Some(level) = node_config.file_logging_level {
            init_logger_once(
                level,
                &Path::new(&node_config.local_persistence_path).join(LOGS_DIR),
            )?;
        }
        info!("3L version: {}", env!("GITHUB_REF"));

        let rt = Arc::new(AsyncRuntime::new()?);

        let strong_typed_seed = sanitize_input::strong_type_seed(&node_config.seed)?;
        let auth = Arc::new(build_auth(
            &strong_typed_seed,
            &node_config.remote_services_config.backend_url,
        )?);
        let async_auth = Arc::new(build_async_auth(
            &strong_typed_seed,
            &node_config.remote_services_config.backend_url,
        )?);

        let db_path = format!("{}/{DB_FILENAME}", node_config.local_persistence_path);
        let mut data_store = DataStore::new(&db_path)?;

        let fiat_currency = match data_store.retrieve_last_set_fiat_currency()? {
            None => {
                data_store.store_selected_fiat_currency(&node_config.default_fiat_currency)?;
                node_config.default_fiat_currency.clone()
            }
            Some(c) => c,
        };

        let data_store = Arc::new(Mutex::new(data_store));

        let user_preferences = Arc::new(Mutex::new(UserPreferences {
            fiat_currency,
            timezone_config: node_config.timezone_config.clone(),
        }));

        let analytics_client = AnalyticsClient::new(
            node_config.remote_services_config.backend_url.clone(),
            derive_analytics_keys(&strong_typed_seed)?,
            Arc::clone(&async_auth),
        );

        let analytics_config = data_store.lock_unwrap().retrieve_analytics_config()?;
        let analytics_interceptor = Arc::new(AnalyticsInterceptor::new(
            analytics_client,
            Arc::clone(&user_preferences),
            rt.handle(),
            analytics_config,
        ));

        let events_callback = Arc::new(events_callback);
        let event_listener = Box::new(LipaEventListener::new(
            Arc::clone(&events_callback),
            Arc::clone(&analytics_interceptor),
        ));

        let sdk = rt.handle().block_on(async {
            let sdk = start_sdk(&node_config, event_listener).await?;
            if sdk
                .lsp_id()
                .await
                .map_to_runtime_error(
                    RuntimeErrorCode::NodeUnavailable,
                    "Failed to get current lsp id",
                )?
                .is_none()
            {
                let lsps = sdk.list_lsps().await.map_to_runtime_error(
                    RuntimeErrorCode::NodeUnavailable,
                    "Failed to list lsps",
                )?;
                let lsp = lsps
                    .into_iter()
                    .next()
                    .ok_or_runtime_error(RuntimeErrorCode::NodeUnavailable, "No lsp available")?;
                sdk.connect_lsp(lsp.id).await.map_to_runtime_error(
                    RuntimeErrorCode::NodeUnavailable,
                    "Failed to connect to lsp",
                )?;
            }
            Ok(sdk)
        })?;

        let exchange_rate_provider = Box::new(ExchangeRateProviderImpl::new(
            node_config.remote_services_config.backend_url.clone(),
            Arc::clone(&auth),
        ));

        let offer_manager = Arc::new(OfferManager::new(
            node_config.remote_services_config.backend_url.clone(),
            Arc::clone(&auth),
        ));

        let persistence_encryption_key = derive_persistence_encryption_key(&strong_typed_seed)?;
        let backup_client = RemoteBackupClient::new(
            node_config.remote_services_config.backend_url.clone(),
            Arc::clone(&async_auth),
        );
        let backup_manager = BackupManager::new(backup_client, db_path, persistence_encryption_key);

        let task_manager = Arc::new(Mutex::new(TaskManager::new(
            rt.handle(),
            exchange_rate_provider,
            Arc::clone(&data_store),
            Arc::clone(&sdk),
            backup_manager,
            events_callback,
            node_config.breez_sdk_config.breez_sdk_api_key.clone(),
        )?));
        task_manager.lock_unwrap().foreground();

        register_webhook_url(&rt, &sdk, &auth, &node_config)?;

        let phone_number_prefix_parser = PhoneNumberPrefixParser::new(
            &node_config.phone_number_allowed_countries_iso_3166_1_alpha_2,
        );

        let support = Arc::new(Support {
            user_preferences: Arc::clone(&user_preferences),
            sdk: Arc::clone(&sdk),
            auth: Arc::clone(&auth),
            async_auth: Arc::clone(&async_auth),
            offer_manager: Arc::clone(&offer_manager),
            rt: Arc::clone(&rt),
            data_store: Arc::clone(&data_store),
            task_manager: Arc::clone(&task_manager),
            allowed_countries_country_iso_3166_1_alpha_2: node_config
                .phone_number_allowed_countries_iso_3166_1_alpha_2
                .clone(),
            phone_number_prefix_parser: phone_number_prefix_parser.clone(),
            persistence_encryption_key,
            node_config: node_config.clone(),
            analytics_interceptor,
        });

        let activities = Arc::new(Activities::new(Arc::clone(&support)));

        let lightning = Arc::new(Lightning::new(Arc::clone(&support)));

        let config = Arc::new(Config::new(Arc::clone(&support)));

        let fiat_topup = Arc::new(FiatTopup::new(
            Arc::clone(&support),
            Arc::clone(&activities),
        ));

        let onchain = Arc::new(Onchain::new(Arc::clone(&support)));

        let actions_required = Arc::new(ActionsRequired::new(
            Arc::clone(&support),
            Arc::clone(&fiat_topup),
            Arc::clone(&onchain),
        ));

        let lightning_address = Arc::new(LightningAddress::new(Arc::clone(&support)));

        let phone_number = Arc::new(PhoneNumber::new(Arc::clone(&support)));

        let util = Arc::new(Util::new(Arc::clone(&support)));

        Ok(LightningNode {
            sdk,
            auth,
            offer_manager,
            rt,
            node_config,
            activities,
            lightning,
            config,
            fiat_topup,
            actions_required,
            onchain,
            lightning_address,
            phone_number,
            util,
        })
    }

    pub fn activities(&self) -> Arc<Activities> {
        Arc::clone(&self.activities)
    }

    pub fn lightning(&self) -> Arc<Lightning> {
        Arc::clone(&self.lightning)
    }

    pub fn config(&self) -> Arc<Config> {
        Arc::clone(&self.config)
    }

    pub fn fiat_topup(&self) -> Arc<FiatTopup> {
        Arc::clone(&self.fiat_topup)
    }

    pub fn actions_required(&self) -> Arc<ActionsRequired> {
        Arc::clone(&self.actions_required)
    }

    pub fn onchain(&self) -> Arc<Onchain> {
        Arc::clone(&self.onchain)
    }

    pub fn lightning_address(&self) -> Arc<LightningAddress> {
        Arc::clone(&self.lightning_address)
    }

    pub fn phone_number(&self) -> Arc<PhoneNumber> {
        Arc::clone(&self.phone_number)
    }

    pub fn util(&self) -> Arc<Util> {
        Arc::clone(&self.util)
    }

    /// Request some basic info about the node
    ///
    /// Requires network: **no**
    #[deprecated = "util().get_node_info() should be used instead"]
    pub fn get_node_info(&self) -> Result<NodeInfo> {
        self.util.get_node_info()
    }

    /// When *receiving* payments, a new channel MAY be required. A fee will be charged to the user.
    /// This does NOT impact *sending* payments.
    /// Get information about the fee charged by the LSP for opening new channels
    ///
    /// Requires network: **no**
    #[deprecated = "lightning().get_lsp_fee() or swap().get_lsp_fee() should be used instead"]
    pub fn query_lsp_fee(&self) -> Result<LspFee> {
        self.lightning.get_lsp_fee()
    }

    /// Calculate the actual LSP fee for the given amount of an incoming payment.
    /// If the already existing inbound capacity is enough, no new channel is required.
    ///
    /// Parameters:
    /// * `amount_sat` - amount in sats to compute LSP fee for
    ///
    /// For the returned fees to be guaranteed to be accurate, the returned `lsp_fee_params` must be
    /// provided to [`LightningNode::create_invoice`]
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning().calculate_lsp_fee_for_amount() should be used instead"]
    pub fn calculate_lsp_fee(&self, amount_sat: u64) -> Result<CalculateLspFeeResponse> {
        let response = self.lightning.calculate_lsp_fee_for_amount(amount_sat)?;

        Ok(CalculateLspFeeResponse {
            lsp_fee: response.lsp_fee,
            lsp_fee_params: Some(response.lsp_fee_params),
        })
    }

    /// Get the current limits for the amount that can be transferred in a single payment.
    /// Currently there are only limits for receiving payments.
    /// The limits (partly) depend on the channel situation of the node, so it should be called
    /// again every time the user is about to receive a payment.
    /// The limits stay the same regardless of what amount wants to receive (= no changes while
    /// he's typing the amount)
    ///
    /// Requires network: **no**
    #[deprecated = "lightning().determine_receive_amount_limits() should be used instead"]
    pub fn get_payment_amount_limits(&self) -> Result<PaymentAmountLimits> {
        self.lightning
            .determine_receive_amount_limits()
            .map(PaymentAmountLimits::from)
    }

    /// Create an invoice to receive a payment with.
    ///
    /// Parameters:
    /// * `amount_sat` - the smallest amount of sats required for the node to accept the incoming
    ///   payment (sender will have to pay fees on top of that amount)
    /// * `lsp_fee_params` - the params that will be used to determine the lsp fee.
    ///    Can be obtained from [`LightningNode::calculate_lsp_fee`] to guarantee predicted fees
    ///    are the ones charged.
    /// * `description` - a description to be embedded into the created invoice
    /// * `metadata` - additional data about the invoice creation used for analytics purposes,
    ///    used to improve the user experience
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning().bolt11().create() should be used instead"]
    pub fn create_invoice(
        &self,
        amount_sat: u64,
        lsp_fee_params: Option<OpeningFeeParams>,
        description: String,
        metadata: InvoiceCreationMetadata,
    ) -> Result<InvoiceDetails> {
        self.lightning
            .bolt11()
            .create(amount_sat, lsp_fee_params, description, metadata)
    }

    /// Parse a phone number prefix, check against the list of allowed countries
    /// (set in [`LightningNodeConfig::phone_number_allowed_countries_iso_3166_1_alpha_2`]).
    /// The parser is not strict, it parses some invalid prefixes as valid.
    ///
    /// Requires network: **no**
    #[deprecated = "phone_number().parse_prefix() should be used instead"]
    pub fn parse_phone_number_prefix(
        &self,
        phone_number_prefix: String,
    ) -> std::result::Result<(), ParsePhoneNumberPrefixError> {
        self.phone_number.parse_prefix(phone_number_prefix)
    }

    /// Parse a phone number, check against the list of allowed countries
    /// (set in [`LightningNodeConfig::phone_number_allowed_countries_iso_3166_1_alpha_2`]).
    ///
    /// Returns a possible lightning address, which can be checked for existence
    /// with [`LightningNode::decode_data`].
    ///
    /// Requires network: **no**
    #[deprecated = "phone_number().parse_to_lightning_address() should be used instead"]
    pub fn parse_phone_number_to_lightning_address(
        &self,
        phone_number: String,
    ) -> std::result::Result<String, ParsePhoneNumberError> {
        self.phone_number.parse_to_lightning_address(phone_number)
    }

    /// Decode a user-provided string (usually obtained from QR-code or pasted).
    ///
    /// Requires network: **yes**
    #[deprecated = "util().decode_data() should be used instead"]
    pub fn decode_data(&self, data: String) -> std::result::Result<DecodedData, DecodeDataError> {
        self.util.decode_data(data)
    }

    /// Get the max routing fee mode that will be employed to restrict the fees for paying a given amount in sats
    ///
    /// Requires network: **no**
    #[deprecated = "lightning().determine_max_routing_fee_mode() should be used instead"]
    pub fn get_payment_max_routing_fee_mode(&self, amount_sat: u64) -> MaxRoutingFeeMode {
        self.lightning.determine_max_routing_fee_mode(amount_sat)
    }

    /// Checks if the given amount could be spent on an invoice.
    ///
    /// Parameters:
    /// * `amount` - The to be spent amount.
    ///
    /// Requires network: **no**
    #[deprecated = "lightning().determine_payment_affordability() should be used instead"]
    pub fn get_invoice_affordability(&self, amount_sat: u64) -> Result<InvoiceAffordability> {
        self.lightning
            .determine_payment_affordability(amount_sat)
            .map(InvoiceAffordability::from)
    }

    /// Start an attempt to pay an invoice. Can immediately fail, meaning that the payment couldn't be started.
    /// If successful, it doesn't mean that the payment itself was successful (funds received by the payee).
    /// After this method returns, the consumer of this library will learn about a successful/failed payment through the
    /// callbacks [`EventsCallback::payment_sent`] and [`EventsCallback::payment_failed`].
    ///
    /// Parameters:
    /// * `invoice_details` - details of an invoice decode by [`LightningNode::decode_data`]
    /// * `metadata` - additional meta information about the payment, used by analytics to improve the user experience.
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning().bolt11().pay() should be used instead"]
    pub fn pay_invoice(
        &self,
        invoice_details: InvoiceDetails,
        metadata: PaymentMetadata,
    ) -> PayResult<()> {
        self.lightning.bolt11().pay(invoice_details, metadata)
    }

    /// Similar to [`LightningNode::pay_invoice`] with the difference that the passed in invoice
    /// does not have any payment amount specified, and allows the caller of the method to
    /// specify an amount instead.
    ///
    /// Additional Parameters:
    /// * `amount_sat` - amount in sats to be paid
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning().bolt11().pay_open_amount() should be used instead"]
    pub fn pay_open_invoice(
        &self,
        invoice_details: InvoiceDetails,
        amount_sat: u64,
        metadata: PaymentMetadata,
    ) -> PayResult<()> {
        self.lightning
            .bolt11()
            .pay_open_amount(invoice_details, amount_sat, metadata)
    }

    /// Pay an LNURL-pay the provided amount.
    ///
    /// Parameters:
    /// * `lnurl_pay_request_data` - LNURL-pay request data as obtained from [`LightningNode::decode_data`]
    /// * `amount_sat` - amount to be paid
    /// * `comment` - optional comment to be sent to payee (`max_comment_length` in
    ///   [`LnUrlPayDetails`] must be respected)
    ///
    /// Returns the payment hash of the payment.
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning().lnurl().pay() should be used instead"]
    pub fn pay_lnurlp(
        &self,
        lnurl_pay_request_data: LnUrlPayRequestData,
        amount_sat: u64,
        comment: Option<String>,
    ) -> LnUrlPayResult<String> {
        self.lightning
            .lnurl()
            .pay(lnurl_pay_request_data, amount_sat, comment)
    }

    /// List recipients from the most recent used.
    ///
    /// Returns a list of recipients (lightning addresses or phone numbers for now).
    ///
    /// Requires network: **no**
    pub fn list_recipients(&self) -> Result<Vec<Recipient>> {
        let list_payments_request = ListPaymentsRequest {
            filters: Some(vec![PaymentTypeFilter::Sent]),
            metadata_filters: None,
            from_timestamp: None,
            to_timestamp: None,
            include_failures: Some(true),
            limit: None,
            offset: None,
        };
        let to_lightning_address = |p: breez_sdk_core::Payment| match p.details {
            PaymentDetails::Ln { data } => match data.ln_address {
                Some(lightning_address) => Some((lightning_address, -p.payment_time)),
                None => None,
            },
            _ => None,
        };
        let mut lightning_addresses = self
            .rt
            .handle()
            .block_on(self.sdk.list_payments(list_payments_request))
            .map_to_runtime_error(RuntimeErrorCode::NodeUnavailable, "Failed to list payments")?
            .into_iter()
            .flat_map(to_lightning_address)
            .collect::<Vec<_>>();
        lightning_addresses.sort();
        lightning_addresses.dedup_by_key(|p| p.0.clone());
        lightning_addresses.sort_by_key(|p| p.1);

        let recipients = lightning_addresses
            .into_iter()
            .map(|p| {
                Recipient::from_lightning_address(
                    &p.0,
                    &self
                        .node_config
                        .remote_services_config
                        .lipa_lightning_domain,
                )
            })
            .collect();
        Ok(recipients)
    }

    /// Withdraw an LNURL-withdraw the provided amount.
    ///
    /// A successful return means the LNURL-withdraw service has started a payment.
    /// Only after the event [`EventsCallback::payment_received`] can the payment be considered
    /// received.
    ///
    /// Parameters:
    /// * `lnurl_withdraw_request_data` - LNURL-withdraw request data as obtained from [`LightningNode::decode_data`]
    /// * `amount_sat` - amount to be withdraw
    ///
    /// Returns the payment hash of the payment.
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning().lnurl().withdraw() should be used instead"]
    pub fn withdraw_lnurlw(
        &self,
        lnurl_withdraw_request_data: LnUrlWithdrawRequestData,
        amount_sat: u64,
    ) -> LnUrlWithdrawResult<String> {
        self.lightning
            .lnurl()
            .withdraw(lnurl_withdraw_request_data, amount_sat)
    }

    /// Get a list of the latest activities
    ///
    /// Parameters:
    /// * `number_of_completed_activities` - the maximum number of completed activities that will be returned
    ///
    /// Requires network: **no**
    #[deprecated = "activities().list() should be used instead"]
    pub fn get_latest_activities(
        &self,
        number_of_completed_activities: u32,
    ) -> Result<ListActivitiesResponse> {
        self.activities.list(number_of_completed_activities)
    }

    /// Get an incoming payment by its payment hash.
    ///
    /// Parameters:
    /// * `hash` - hex representation of payment hash
    ///
    /// Requires network: **no**
    #[deprecated = "activities().get_incoming_payment() should be used instead"]
    pub fn get_incoming_payment(&self, hash: String) -> Result<IncomingPaymentInfo> {
        self.activities.get_incoming_payment(hash)
    }

    /// Get an outgoing payment by its payment hash.
    ///
    /// Parameters:
    /// * `hash` - hex representation of payment hash
    ///
    /// Requires network: **no**
    #[deprecated = "activities().get_outgoing_payment() should be used instead"]
    pub fn get_outgoing_payment(&self, hash: String) -> Result<OutgoingPaymentInfo> {
        self.activities.get_outgoing_payment(hash)
    }

    /// Get an activity by its payment hash.
    ///
    /// Parameters:
    /// * `hash` - hex representation of payment hash
    ///
    /// Requires network: **no**
    #[deprecated = "activities().get() should be used instead"]
    pub fn get_activity(&self, hash: String) -> Result<Activity> {
        self.activities.get(hash)
    }

    /// Set a personal note on a specific payment.
    ///
    /// Parameters:
    /// * `payment_hash` - The hash of the payment for which a personal note will be set.
    /// * `note` - The personal note.
    ///
    /// Requires network: **no**
    #[deprecated = "activities().set_personal_note() should be used instead"]
    pub fn set_payment_personal_note(&self, payment_hash: String, note: String) -> Result<()> {
        self.activities.set_personal_note(payment_hash, note)
    }

    /// Call the method when the app goes to foreground, such that the user can interact with it.
    /// The library starts running the background tasks more frequently to improve user experience.
    ///
    /// Requires network: **no**
    #[deprecated = "config().foreground() should be used instead"]
    pub fn foreground(&self) {
        self.config.foreground()
    }

    /// Call the method when the app goes to background, such that the user can not interact with it.
    /// The library stops running some unnecessary tasks and runs necessary tasks less frequently.
    /// It should save battery and internet traffic.
    ///
    /// Requires network: **no**
    #[deprecated = "config().background() should be used instead"]
    pub fn background(&self) {
        self.config.background()
    }

    /// List codes of supported fiat currencies.
    /// Please keep in mind that this method doesn't make any network calls. It simply retrieves
    /// previously fetched values that are frequently updated by a background task.
    ///
    /// The fetched list will be persisted across restarts to alleviate the consequences of a
    /// slow or unresponsive exchange rate service.
    /// The method will return an empty list if there is nothing persisted yet and
    /// the values are not yet fetched from the service.
    ///
    /// Requires network: **no**
    #[deprecated = "config().list_currencies() should be used instead"]
    pub fn list_currency_codes(&self) -> Vec<String> {
        self.config.list_currencies()
    }

    /// Get exchange rate on the BTC/default currency pair
    /// Please keep in mind that this method doesn't make any network calls. It simply retrieves
    /// previously fetched values that are frequently updated by a background task.
    ///
    /// The fetched exchange rates will be persisted across restarts to alleviate the consequences of a
    /// slow or unresponsive exchange rate service.
    ///
    /// The return value is an optional to deal with the possibility
    /// of no exchange rate values being known.
    ///
    /// Requires network: **no**
    #[deprecated = "util().get_exchange_rate() should be used instead"]
    pub fn get_exchange_rate(&self) -> Option<ExchangeRate> {
        self.util.get_exchange_rate()
    }

    /// Change the fiat currency (ISO 4217 currency code) - not all are supported
    /// The method [`LightningNode::list_currency_codes`] can used to list supported codes.
    ///
    /// Requires network: **no**
    #[deprecated = "config().set_fiat_currency() should be used instead"]
    pub fn change_fiat_currency(&self, fiat_currency: String) -> Result<()> {
        self.config.set_fiat_currency(fiat_currency)
    }

    /// Change the timezone config.
    ///
    /// Parameters:
    /// * `timezone_config` - the user's current timezone
    ///
    /// Requires network: **no**
    #[deprecated = "config().set_timezone_config() should be used instead"]
    pub fn change_timezone_config(&self, timezone_config: TzConfig) {
        self.config.set_timezone_config(timezone_config)
    }

    /// Accepts Pocket's T&C.
    ///
    /// Parameters:
    /// * `version` - the version number being accepted.
    /// * `fingerprint` - the fingerprint of the version being accepted.
    ///
    /// Requires network: **yes**
    #[deprecated = "fiat_topup().accept_tc() should be used instead"]
    pub fn accept_pocket_terms_and_conditions(
        &self,
        version: i64,
        fingerprint: String,
    ) -> Result<()> {
        self.fiat_topup.accept_tc(version, fingerprint)
    }

    /// Similar to [`get_terms_and_conditions_status`] with the difference that this method is pre-filling
    /// the environment and seed based on the node configuration.
    ///
    /// Requires network: **yes**
    #[deprecated = "fiat_topup().query_tc_status() should be used instead"]
    pub fn get_terms_and_conditions_status(
        &self,
        terms_and_conditions: TermsAndConditions,
    ) -> Result<TermsAndConditionsStatus> {
        self.auth
            .get_terms_and_conditions_status(terms_and_conditions)
            .map_runtime_error_to(RuntimeErrorCode::AuthServiceUnavailable)
    }

    /// Resets a previous fiat topups registration.
    ///
    /// Requires network: **no**
    #[deprecated = "fiat_topup().reset() should be used instead"]
    pub fn reset_fiat_topup(&self) -> Result<()> {
        self.fiat_topup.reset()
    }

    /// Hides the topup with the given id. Can be called on expired topups so that they stop being returned
    /// by [`LightningNode::query_uncompleted_offers`].
    ///
    /// Topup id can be obtained from [`OfferKind::Pocket`].
    ///
    /// Requires network: **yes**
    #[deprecated = "fiat_topup().dismiss_topup() should be used instead"]
    pub fn hide_topup(&self, id: String) -> Result<()> {
        self.offer_manager
            .hide_topup(id)
            .map_runtime_error_to(RuntimeErrorCode::OfferServiceUnavailable)
    }

    /// List action required items.
    ///
    /// Returns a list of actionable items. They can be:
    /// * Uncompleted offers (either available for collection or failed).
    /// * Unresolved failed swaps.
    /// * Available funds resulting from channel closes.
    ///
    /// Requires network: **yes**
    #[deprecated = "actions_required().list() should be used instead"]
    pub fn list_action_required_items(&self) -> Result<Vec<ActionRequiredItem>> {
        self.actions_required.list()
    }

    /// Hides the channel close action required item in case the amount cannot be recovered due
    /// to it being too small. The item will reappear once the amount of funds changes or
    /// onchain-fees go down enough to make the amount recoverable.
    ///
    /// Requires network: **no**
    #[deprecated = "actions_required().hide_unrecoverable_channel_close_funds_item() should be used instead"]
    pub fn hide_channel_closes_funds_available_action_required_item(&self) -> Result<()> {
        self.actions_required()
            .hide_unrecoverable_channel_close_funds_item()
    }

    /// Hides the unresolved failed swap action required item in case the amount cannot be
    /// recovered due to it being too small. The item will reappear once the onchain-fees go
    /// down enough to make the amount recoverable.
    ///
    /// Requires network: **no**
    #[deprecated = "actions_required().hide_unrecoverable_failed_swap_item() should be used instead"]
    pub fn hide_unresolved_failed_swap_action_required_item(
        &self,
        failed_swap_info: FailedSwapInfo,
    ) -> Result<()> {
        self.actions_required()
            .hide_unrecoverable_failed_swap_item(failed_swap_info)
    }

    /// Get a list of unclaimed fund offers
    ///
    /// Requires network: **yes**
    #[deprecated = "actions_required().list() should be used instead"]
    pub fn query_uncompleted_offers(&self) -> Result<Vec<OfferInfo>> {
        self.fiat_topup.query_uncompleted_offers()
    }

    /// Calculates the lightning payout fee for an uncompleted offer.
    ///
    /// Parameters:
    /// * `offer` - An uncompleted offer for which the lightning payout fee should get calculated.
    ///
    /// Requires network: **yes**
    #[deprecated = "fiat_topup().calculate_payout_fee() should be used instead"]
    pub fn calculate_lightning_payout_fee(&self, offer: OfferInfo) -> Result<Amount> {
        self.fiat_topup().calculate_payout_fee(offer)
    }

    /// Request to collect the offer (e.g. a Pocket topup).
    /// A payment hash will be returned to track incoming payment.
    /// The offer collection might be considered successful once
    /// [`EventsCallback::payment_received`] is called,
    /// or the [`PaymentState`] of the respective payment becomes [`PaymentState::Succeeded`].
    ///
    /// Parameters:
    /// * `offer` - An offer that is still valid for collection. Must have its `lnurlw` field
    ///   filled in.
    ///
    /// Requires network: **yes**
    #[deprecated = "fiat_topup().request_collection() should be used instead"]
    pub fn request_offer_collection(&self, offer: OfferInfo) -> Result<String> {
        self.fiat_topup().request_collection(offer)
    }

    /// Registers a new notification token. If a token has already been registered, it will be updated.
    ///
    /// Requires network: **yes**
    #[deprecated = "config().register_notification_token() should be used instead"]
    pub fn register_notification_token(
        &self,
        notification_token: String,
        language_iso_639_1: String,
        country_iso_3166_1_alpha_2: String,
    ) -> Result<()> {
        self.config.register_notification_token(
            notification_token,
            language_iso_639_1,
            country_iso_3166_1_alpha_2,
        )
    }

    /// Get the wallet UUID v5 from the wallet pubkey
    ///
    /// If the auth flow has never succeeded in this Auth instance, this method will require network
    /// access.
    ///
    /// Requires network: **yes**
    #[deprecated = "util().query_wallet_pubkey_id() should be used instead"]
    pub fn get_wallet_pubkey_id(&self) -> Result<String> {
        self.util.query_wallet_pubkey_id()
    }

    /// Get the payment UUID v5 from the payment hash
    ///
    /// Returns a UUID v5 derived from the payment hash. This will always return the same output
    /// given the same input.
    ///
    /// Parameters:
    /// * `payment_hash` - a payment hash represented in hex
    ///
    /// Requires network: **no**
    #[deprecated = "util().derive_payment_uuid() should be used instead"]
    pub fn get_payment_uuid(&self, payment_hash: String) -> Result<String> {
        self.util.derive_payment_uuid(payment_hash)
    }

    /// Query the current recommended on-chain fee rate.
    ///
    /// This is useful to obtain a fee rate to be used for [`LightningNode::sweep_funds_from_channel_closes`].
    ///
    /// Requires network: **yes**
    #[deprecated = "New onchain interface automatically chooses fee rate"]
    pub fn query_onchain_fee_rate(&self) -> Result<u32> {
        let recommended_fees = self
            .rt
            .handle()
            .block_on(self.sdk.recommended_fees())
            .map_to_runtime_error(
                RuntimeErrorCode::NodeUnavailable,
                "Couldn't fetch recommended fees",
            )?;

        Ok(recommended_fees.half_hour_fee as u32)
    }

    /// Prepares a sweep of all available on-chain funds to the provided on-chain address.
    ///
    /// Parameters:
    /// * `address` - the funds will be sweeped to this address
    /// * `onchain_fee_rate` - ignored
    ///
    /// Returns information on the prepared sweep, including the exact fee that results from
    /// using the provided fee rate. The method [`LightningNode::sweep_funds_from_channel_closes`] can be used to broadcast
    /// the sweep transaction.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().channel_close().prepare_sweep() should be used instead"]
    pub fn prepare_sweep_funds_from_channel_closes(
        &self,
        address: String,
        _onchain_fee_rate: u32,
    ) -> std::result::Result<SweepInfo, RedeemOnchainError> {
        self.onchain
            .channel_close()
            .prepare_sweep(BitcoinAddressData {
                address,
                network: Network::Bitcoin,
                amount_sat: None,
                label: None,
                message: None,
            })
            .map(SweepInfo::from)
    }

    /// Sweeps all available on-chain funds to the specified on-chain address.
    ///
    /// Parameters:
    /// * `sweep_info` - a prepared sweep info that can be obtained using
    ///     [`LightningNode::prepare_sweep_funds_from_channel_closes`]
    ///
    /// Returns the txid of the sweep transaction.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().channel_close().sweep() should be used instead"]
    pub fn sweep_funds_from_channel_closes(&self, sweep_info: SweepInfo) -> Result<String> {
        self.onchain.channel_close().sweep(sweep_info.into())
    }

    /// Generates a Bitcoin on-chain address that can be used to topup the local LN wallet from an
    /// external on-chain wallet.
    ///
    /// Funds sent to this address should conform to the min and max values provided within
    /// [`SwapAddressInfo`].
    ///
    /// If a swap is in progress, this method will return an error.
    ///
    /// Parameters:
    /// * `lsp_fee_params` - the lsp fee parameters to be used if a new channel needs to
    ///   be opened. Can be obtained using [`LightningNode::calculate_lsp_fee`].
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().swaps().create() should be used instead"]
    pub fn generate_swap_address(
        &self,
        _lsp_fee_params: Option<OpeningFeeParams>,
    ) -> std::result::Result<SwapAddressInfo, ReceiveOnchainError> {
        self.onchain.swap().create()
    }

    /// Lists all unresolved failed swaps. Each individual failed swap can be recovered
    /// using [`LightningNode::resolve_failed_swap`].
    ///
    /// Requires network: **yes**
    #[deprecated = "actions_required().list() should be used instead"]
    pub fn get_unresolved_failed_swaps(&self) -> Result<Vec<FailedSwapInfo>> {
        Ok(self
            .rt
            .handle()
            .block_on(self.sdk.list_refundables())
            .map_to_runtime_error(
                RuntimeErrorCode::NodeUnavailable,
                "Failed to list refundable failed swaps",
            )?
            .into_iter()
            .filter(|s| s.refund_tx_ids.is_empty())
            .map(|s| FailedSwapInfo {
                address: s.bitcoin_address,
                amount: s
                    .confirmed_sats
                    .as_sats()
                    .to_amount_down(&self.get_exchange_rate()),
                created_at: unix_timestamp_to_system_time(s.created_at as u64),
            })
            .collect())
    }

    /// Returns the fees for resolving a failed swap if there are enough funds to pay for fees.
    ///
    /// Must only be called when the failed swap is unresolved.
    ///
    /// Returns the fee information for the available resolving options.
    ///
    /// Requires network: *yes*
    #[deprecated = "onchain().swaps().determine_resolving_fees() should be used instead"]
    pub fn get_failed_swap_resolving_fees(
        &self,
        failed_swap_info: FailedSwapInfo,
    ) -> Result<Option<OnchainResolvingFees>> {
        self.onchain
            .swap()
            .determine_resolving_fees(failed_swap_info)
    }

    /// Prepares the resolution of a failed swap in order to know how much will be recovered and how much
    /// will be paid in on-chain fees.
    ///
    /// Parameters:
    /// * `failed_swap_info` - the failed swap that will be prepared
    /// * `to_address` - the destination address to which funds will be sent
    /// * `onchain_fee_rate` - ignored
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().swaps().prepare_sweep() should be used instead"]
    pub fn prepare_resolve_failed_swap(
        &self,
        failed_swap_info: FailedSwapInfo,
        to_address: String,
        _onchain_fee_rate: u32,
    ) -> Result<ResolveFailedSwapInfo> {
        self.onchain
            .swap()
            .prepare_sweep(
                failed_swap_info,
                BitcoinAddressData {
                    address: to_address,
                    network: Network::Bitcoin,
                    amount_sat: None,
                    label: None,
                    message: None,
                },
            )
            .map(ResolveFailedSwapInfo::from)
    }

    /// Creates and broadcasts a resolving transaction to recover funds from a failed swap. Existing
    /// failed swaps can be listed using [`LightningNode::get_unresolved_failed_swaps`] and preparing
    /// the resolution of a failed swap can be done using [`LightningNode::prepare_resolve_failed_swap`].
    ///
    /// Parameters:
    /// * `resolve_failed_swap_info` - Information needed to resolve the failed swap. Can be obtained
    ///   using [`LightningNode::prepare_resolve_failed_swap`].
    ///
    /// Returns the txid of the resolving transaction.
    ///
    /// Paid on-chain fees can be known in advance using [`LightningNode::prepare_resolve_failed_swap`].
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().swaps().sweep() should be used instead"]
    pub fn resolve_failed_swap(
        &self,
        resolve_failed_swap_info: ResolveFailedSwapInfo,
    ) -> Result<String> {
        self.onchain.swap().sweep(resolve_failed_swap_info.into())
    }

    /// Automatically swaps failed swap funds back to lightning.
    ///
    /// If a swap is in progress, this method will return an error.
    ///
    /// If the current balance doesn't fulfill the limits, this method will return an error.
    /// Before using this method use [`LightningNode::get_failed_swap_resolving_fees`] to validate a swap is available.
    ///
    /// Parameters:
    /// * `sats_per_vbyte` - the fee rate to use for the on-chain transaction.
    ///   Can be obtained with [`LightningNode::get_failed_swap_resolving_fees`].
    /// * `lsp_fee_params` - the lsp fee params for opening a new channel if necessary.
    ///   Can be obtained with [`LightningNode::get_failed_swap_resolving_fees`].
    ///
    /// Returns the txid of the sweeping tx.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().swaps().swap() should be used instead"]
    pub fn swap_failed_swap_funds_to_lightning(
        &self,
        failed_swap_info: FailedSwapInfo,
        sats_per_vbyte: u32,
        _lsp_fee_param: Option<OpeningFeeParams>,
    ) -> Result<String> {
        self.onchain.swap().swap(failed_swap_info, sats_per_vbyte)
    }

    /// Returns the fees for resolving channel closes if there are enough funds to pay for fees.
    ///
    /// Must only be called when there are onchain funds to resolve.
    ///
    /// Returns the fee information for the available resolving options.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().channel_close().determine_resolving_fees() should be used instead"]
    pub fn get_channel_close_resolving_fees(&self) -> Result<Option<OnchainResolvingFees>> {
        self.onchain.channel_close().determine_resolving_fees()
    }

    /// Automatically swaps on-chain funds back to lightning.
    ///
    /// If a swap is in progress, this method will return an error.
    ///
    /// If the current balance doesn't fulfill the limits, this method will return an error.
    /// Before using this method use [`LightningNode::get_channel_close_resolving_fees`] to validate a swap is available.
    ///
    /// Parameters:
    /// * `sats_per_vbyte` - the fee rate to use for the on-chain transaction.
    ///   Can be obtained with [`LightningNode::get_channel_close_resolving_fees`].
    /// * `lsp_fee_params` - the lsp fee params for opening a new channel if necessary.
    ///   Can be obtained with [`LightningNode::get_channel_close_resolving_fees`].
    ///
    /// Returns the txid of the sweeping tx.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().channel_close().swap() should be used instead"]
    pub fn swap_channel_close_funds_to_lightning(
        &self,
        sats_per_vbyte: u32,
        _lsp_fee_params: Option<OpeningFeeParams>,
    ) -> std::result::Result<String, RedeemOnchainError> {
        self.onchain.channel_close().swap(sats_per_vbyte)
    }

    /// Prints additional debug information to the logs.
    ///
    /// Throws an error in case that the necessary information can't be retrieved.
    ///
    /// Requires network: **yes**
    #[deprecated = "util().log_debug_info() should be used instead"]
    pub fn log_debug_info(&self) -> Result<()> {
        self.util.log_debug_info()
    }

    /// Returns the latest [`FiatTopupSetupInfo`] if the user has registered for the fiat topup.
    ///
    /// Requires network: **no**
    #[deprecated = "fiat_topup().get_info() should be used instead"]
    pub fn retrieve_latest_fiat_topup_info(&self) -> Result<Option<FiatTopupSetupInfo>> {
        self.fiat_topup.get_info()
    }

    /// Returns the health check status of Breez and Greenlight services.
    ///
    /// Requires network: **yes**
    #[deprecated = "util().query_health_status() should be used instead"]
    pub fn get_health_status(&self) -> Result<BreezHealthCheckStatus> {
        self.util.query_health_status()
    }

    /// Check if clearing the wallet is feasible.
    ///
    /// Meaning that the balance is within the range of what can be reverse-swapped.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().reverse_swap().determine_clear_wallet_feasibility() should be used instead"]
    pub fn check_clear_wallet_feasibility(&self) -> Result<RangeHit> {
        self.onchain
            .reverse_swap()
            .determine_clear_wallet_feasibility()
    }

    /// Prepares a reverse swap that sends all funds in LN channels. This is possible because the
    /// route to the swap service is known, so fees can be known in advance.
    ///
    /// This can fail if the balance is either too low or too high for it to be reverse-swapped.
    /// The method [`LightningNode::check_clear_wallet_feasibility`] can be used to check if the balance
    /// is within the required range.
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().reverse_swap().prepare_clear_wallet() should be used instead"]
    pub fn prepare_clear_wallet(&self) -> Result<ClearWalletInfo> {
        self.onchain.reverse_swap().prepare_clear_wallet()
    }

    /// Starts a reverse swap that sends all funds in LN channels to the provided on-chain address.
    ///
    /// Parameters:
    /// * `clear_wallet_info` - An instance of [`ClearWalletInfo`] obtained using
    ///   [`LightningNode::prepare_clear_wallet`].
    /// * `destination_onchain_address_data` - An on-chain address data instance. Can be obtained
    ///   using [`LightningNode::decode_data`].
    ///
    /// Requires network: **yes**
    #[deprecated = "onchain().reverse_swap().clear_wallet() should be used instead"]
    pub fn clear_wallet(
        &self,
        clear_wallet_info: ClearWalletInfo,
        destination_onchain_address_data: BitcoinAddressData,
    ) -> Result<()> {
        self.onchain
            .reverse_swap()
            .clear_wallet(clear_wallet_info, destination_onchain_address_data)
    }

    /// Set the analytics configuration.
    ///
    /// This can be used to completely prevent any analytics data from being reported.
    ///
    /// Requires network: **no**
    #[deprecated = "config().set_analytics_config() should be used instead"]
    pub fn set_analytics_config(&self, config: AnalyticsConfig) -> Result<()> {
        self.config.set_analytics_config(config)
    }

    /// Get the currently configured analytics configuration.
    ///
    /// Requires network: **no**
    #[deprecated = "config().get_analytics_config() should be used instead"]
    pub fn get_analytics_config(&self) -> Result<AnalyticsConfig> {
        self.config.get_analytics_config()
    }

    /// Register a human-readable lightning address or return the previously
    /// registered one.
    ///
    /// Requires network: **yes**
    #[deprecated = "lightning_address().register() should be used instead"]
    pub fn register_lightning_address(&self) -> Result<String> {
        self.lightning_address.register()
    }

    /// Query the registered lightning address.
    ///
    /// Requires network: **no**
    #[deprecated = "lightning_address().get() should be used instead"]
    pub fn query_lightning_address(&self) -> Result<Option<String>> {
        self.lightning_address.get()
    }

    /// Query for a previously verified phone number.
    ///
    /// Requires network: **no**
    #[deprecated = "phone_number().get() should be used instead"]
    pub fn query_verified_phone_number(&self) -> Result<Option<String>> {
        self.phone_number.get()
    }

    /// Start the verification process for a new phone number. This will trigger an SMS containing
    /// an OTP to be sent to the provided `phone_number`. To conclude the verification process,
    /// the method [`LightningNode::verify_phone_number`] should be called next.
    ///
    /// Parameters:
    /// * `phone_number` - the phone number to be registered. Needs to be checked for validity using
    ///   [LightningNode::parse_phone_number_to_lightning_address].
    ///
    /// Requires network: **yes**
    #[deprecated = "phone_number().register() should be used instead"]
    pub fn request_phone_number_verification(&self, phone_number: String) -> Result<()> {
        self.phone_number.register(phone_number)
    }

    /// Finish the verification process for a new phone number.
    ///
    /// Parameters:
    /// * `phone_number` - the phone number to be verified.
    /// * `otp` - the OTP code sent as an SMS to the phone number.
    ///
    /// Requires network: **yes**
    #[deprecated = "phone_number().verify() should be used instead"]
    pub fn verify_phone_number(&self, phone_number: String, otp: String) -> Result<()> {
        self.phone_number.verify(phone_number, otp)
    }

    /// Set value of a feature flag.
    /// The method will report the change to the backend and update the local database.
    ///
    /// Parameters:
    /// * `feature` - feature flag to be set.
    /// * `enable` - enable or disable the feature.
    ///
    /// Requires network: **yes**
    #[deprecated = "config().set_feature_flag() should be used instead"]
    pub fn set_feature_flag(&self, feature: FeatureFlag, flag_enabled: bool) -> Result<()> {
        self.config.set_feature_flag(feature, flag_enabled)
    }

    // Only meant for example CLI use
    #[doc(hidden)]
    pub fn close_all_channels_with_current_lsp(&self) -> Result<()> {
        self.rt
            .handle()
            .block_on(self.sdk.close_lsp_channels())
            .map_to_runtime_error(
                RuntimeErrorCode::NodeUnavailable,
                "Failed to close channels",
            )?;
        Ok(())
    }
}

pub(crate) async fn start_sdk(
    config: &LightningNodeConfig,
    event_listener: Box<dyn EventListener>,
) -> Result<Arc<BreezServices>> {
    let developer_cert = config
        .breez_sdk_config
        .breez_sdk_partner_certificate
        .as_bytes()
        .to_vec();
    let developer_key = config
        .breez_sdk_config
        .breez_sdk_partner_key
        .as_bytes()
        .to_vec();
    let partner_credentials = GreenlightCredentials {
        developer_cert,
        developer_key,
    };

    let mut breez_config = BreezServices::default_config(
        EnvironmentType::Production,
        config.breez_sdk_config.breez_sdk_api_key.clone(),
        NodeConfig::Greenlight {
            config: GreenlightNodeConfig {
                partner_credentials: Some(partner_credentials),
                invite_code: None,
            },
        },
    );

    breez_config
        .working_dir
        .clone_from(&config.local_persistence_path);
    breez_config.exemptfee_msat = config
        .max_routing_fee_config
        .max_routing_fee_exempt_fee_sats
        .as_sats()
        .msats;
    breez_config.maxfee_percent =
        Permyriad(config.max_routing_fee_config.max_routing_fee_permyriad).to_percentage();
    let connect_request = ConnectRequest {
        config: breez_config,
        seed: config.seed.clone(),
        restore_only: None,
    };
    BreezServices::connect(connect_request, event_listener)
        .await
        .map_to_runtime_error(
            RuntimeErrorCode::NodeUnavailable,
            "Failed to initialize a breez sdk instance",
        )
}

/// Accept lipa's terms and conditions. Should be called before instantiating a [`LightningNode`]
/// for the first time.
///
/// Parameters:
/// * `backend_url`
/// * `seed` - the seed from the wallet for which the T&C will be accepted.
/// * `version` - the version number being accepted.
/// * `fingerprint` - the fingerprint of the version being accepted.
///
/// Requires network: **yes**
pub fn accept_terms_and_conditions(
    backend_url: String,
    seed: Vec<u8>,
    version: i64,
    fingerprint: String,
) -> Result<()> {
    enable_backtrace();
    let seed = sanitize_input::strong_type_seed(&seed)?;
    let auth = build_auth(&seed, &backend_url)?;
    auth.accept_terms_and_conditions(TermsAndConditions::Lipa, version, fingerprint)
        .map_runtime_error_to(RuntimeErrorCode::AuthServiceUnavailable)
}

/// Try to parse the provided string as a lightning address, return [`ParseError`]
/// precisely indicating why parsing failed.
///
/// Requires network: **no**
pub fn parse_lightning_address(address: &str) -> std::result::Result<(), ParseError> {
    parser::parse_lightning_address(address).map_err(ParseError::from)
}

/// Allows checking if certain terms and conditions have been accepted by the user.
///
/// Parameters:
/// * `environment` - Which environment should be used.
/// * `seed` - The seed of the wallet.
/// * `terms_and_conditions` - [`TermsAndConditions`] for which the status should be requested.
///
/// Returns the status of the requested [`TermsAndConditions`].
///
/// Requires network: **yes**
pub fn get_terms_and_conditions_status(
    backend_url: String,
    seed: Vec<u8>,
    terms_and_conditions: TermsAndConditions,
) -> Result<TermsAndConditionsStatus> {
    enable_backtrace();
    let seed = sanitize_input::strong_type_seed(&seed)?;
    let auth = build_auth(&seed, &backend_url)?;
    auth.get_terms_and_conditions_status(terms_and_conditions)
        .map_runtime_error_to(RuntimeErrorCode::AuthServiceUnavailable)
}

pub(crate) fn enable_backtrace() {
    env::set_var("RUST_BACKTRACE", "1");
}

fn fill_payout_fee(
    offer: OfferKind,
    requested_amount: Msats,
    rate: &Option<ExchangeRate>,
) -> OfferKind {
    match offer {
        OfferKind::Pocket {
            id,
            exchange_rate,
            topup_value_minor_units,
            topup_value_sats,
            exchange_fee_minor_units,
            exchange_fee_rate_permyriad,
            lightning_payout_fee: _,
            error,
        } => {
            let lightning_payout_fee = topup_value_sats.map(|v| {
                (v.as_sats().msats - requested_amount.msats)
                    .as_msats()
                    .to_amount_up(rate)
            });

            OfferKind::Pocket {
                id,
                exchange_rate,
                topup_value_minor_units,
                topup_value_sats,
                exchange_fee_minor_units,
                exchange_fee_rate_permyriad,
                lightning_payout_fee,
                error,
            }
        }
    }
}

// TODO provide corrupted acticity information partially instead of hiding it
fn filter_out_and_log_corrupted_activities(r: Result<Activity>) -> Option<Activity> {
    if r.is_ok() {
        r.ok()
    } else {
        error!(
            "Corrupted activity data, ignoring activity: {}",
            r.expect_err("Expected error, received ok")
        );
        None
    }
}

// TODO provide corrupted payment information partially instead of hiding it
fn filter_out_and_log_corrupted_payments(
    r: Result<IncomingPaymentInfo>,
) -> Option<IncomingPaymentInfo> {
    if r.is_ok() {
        r.ok()
    } else {
        error!(
            "Corrupted payment data, ignoring payment: {}",
            r.expect_err("Expected error, received ok")
        );
        None
    }
}

pub(crate) fn register_webhook_url(
    rt: &AsyncRuntime,
    sdk: &BreezServices,
    auth: &Auth,
    config: &LightningNodeConfig,
) -> Result<()> {
    let id = auth.get_wallet_pubkey_id().map_to_runtime_error(
        RuntimeErrorCode::AuthServiceUnavailable,
        "Failed to authenticate in order to get wallet pubkey id",
    )?;
    let encrypted_id = deterministic_encrypt(
        id.as_bytes(),
        &<[u8; 32]>::from_hex(
            &config
                .remote_services_config
                .notification_webhook_secret_hex,
        )
        .map_to_invalid_input("Invalid notification_webhook_secret_hex")?,
    )
    .map_to_permanent_failure("Failed to encrypt wallet pubkey id")?;
    let encrypted_id = hex::encode(encrypted_id);
    let webhook_url = config
        .remote_services_config
        .notification_webhook_base_url
        .replacen("{id}", &encrypted_id, 1);
    rt.handle()
        .block_on(sdk.register_webhook(webhook_url.clone()))
        .map_to_runtime_error(
            RuntimeErrorCode::NodeUnavailable,
            "Failed to register notification webhook",
        )?;
    debug!("Successfully registered notification webhook with Breez SDK. URL: {webhook_url}");
    Ok(())
}

fn with_status(status: EnableStatus) -> impl Fn((String, EnableStatus)) -> Option<String> {
    move |(v, s)| if s == status { Some(v) } else { None }
}

include!(concat!(env!("OUT_DIR"), "/lipalightninglib.uniffi.rs"));