This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathfederation.rs
More file actions
1421 lines (1255 loc) · 50.5 KB
/
federation.rs
File metadata and controls
1421 lines (1255 loc) · 50.5 KB
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
use crate::utils::{convert_from_fedimint_invoice, convert_to_fedimint_invoice, spawn};
use crate::{
error::{MutinyError, MutinyStorageError},
event::PaymentInfo,
key::{create_root_child_key, ChildKey},
logging::MutinyLogger,
onchain::coin_type_from_network,
storage::{
get_payment_info, list_payment_info, persist_payment_info, MutinyStorage, VersionedValue,
},
utils::sleep,
HTLCStatus, MutinyInvoice, DEFAULT_PAYMENT_TIMEOUT,
};
use async_trait::async_trait;
use bip39::Mnemonic;
use bitcoin::secp256k1::ThirtyTwoByteHash;
use bitcoin::{
bip32::{ChildNumber, DerivationPath, ExtendedPrivKey},
hashes::sha256,
secp256k1::Secp256k1,
Network,
};
use core::fmt;
use fedimint_bip39::Bip39RootSecretStrategy;
use fedimint_client::{
db::ChronologicalOperationLogKey,
derivable_secret::DerivableSecret,
get_config_from_db,
oplog::{OperationLogEntry, UpdateStreamOrOutcome},
secret::{get_default_client_secret, RootSecretStrategy},
ClientArc, FederationInfo,
};
use fedimint_core::{
api::InviteCode,
config::FederationId,
core::OperationId,
module::CommonModuleInit,
task::{MaybeSend, MaybeSync},
Amount,
};
use fedimint_core::{
db::{
mem_impl::{MemDatabase, MemTransaction},
IDatabaseTransactionOps, IDatabaseTransactionOpsCore, IRawDatabase,
IRawDatabaseTransaction, PrefixStream,
},
BitcoinHash,
};
use fedimint_ln_client::{
InternalPayState, LightningClientInit, LightningClientModule, LightningOperationMeta,
LightningOperationMetaVariant, LnPayState, LnReceiveState,
};
use fedimint_ln_common::lightning_invoice::RoutingFees;
use fedimint_ln_common::LightningCommonInit;
use fedimint_mint_client::{MintClientInit, MintClientModule, OOBNotes, ReissueExternalNotesState};
use fedimint_wallet_client::{WalletClientInit, WalletClientModule};
use futures::future::{self};
use futures_util::{pin_mut, StreamExt};
use hex_conservative::{DisplayHex, FromHex};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::{
ln::PaymentHash, log_debug, log_error, log_info, log_trace, log_warn, util::logger::Logger,
};
use lightning_invoice::Bolt11Invoice;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use std::{
str::FromStr,
sync::atomic::{AtomicU32, Ordering},
};
// The amount of time in milliseconds to wait for
// checking the status of a fedimint OOB re-issuance. This
// is to work around their stream status checking
// when wanting just the current status.
const FEDIMINT_OOB_REISSUE_TIMEOUT_CHECK_MS: u64 = 30;
// The amount of time in milliseconds to wait for
// checking the status of a fedimint payment. This
// is to work around their stream status checking
// when wanting just the current status.
const FEDIMINT_STATUS_TIMEOUT_CHECK_MS: u64 = 30;
// The maximum amount of operations we try to pull
// from fedimint when we need to search through
// their internal list.
const FEDIMINT_OPERATIONS_LIST_MAX: usize = 100;
pub const FEDIMINTS_PREFIX_KEY: &str = "fedimints/";
// Default signet/mainnet federation gateway info
const SIGNET_GATEWAY: &str = "0256f5ef1d986e9abf559651b7167de28bfd954683cd0f14703be12d1421aedc55";
const MAINNET_GATEWAY: &str = "025b9f090d3daab012346701f27d1c220d6d290f6b498255cddc492c255532a09d";
const SIGNET_FEDERATION: &str = "c8d423964c7ad944d30f57359b6e5b260e211dcfdb945140e28d4df51fd572d2";
const MAINNET_FEDERATION: &str = "c36038cce5a97e3467f03336fa8e7e3410960b81d1865cda2a609f70a8f51efb";
impl From<LnReceiveState> for HTLCStatus {
fn from(state: LnReceiveState) -> Self {
match state {
LnReceiveState::Created => HTLCStatus::Pending,
LnReceiveState::Claimed => HTLCStatus::Succeeded,
LnReceiveState::WaitingForPayment { .. } => HTLCStatus::Pending,
LnReceiveState::Canceled { .. } => HTLCStatus::Failed,
LnReceiveState::Funded => HTLCStatus::InFlight,
LnReceiveState::AwaitingFunds => HTLCStatus::InFlight,
}
}
}
impl From<InternalPayState> for HTLCStatus {
fn from(state: InternalPayState) -> Self {
match state {
InternalPayState::Funding => HTLCStatus::InFlight,
InternalPayState::Preimage(_) => HTLCStatus::Succeeded,
InternalPayState::RefundSuccess { .. } => HTLCStatus::Failed,
InternalPayState::RefundError { .. } => HTLCStatus::Failed,
InternalPayState::FundingFailed { .. } => HTLCStatus::Failed,
InternalPayState::UnexpectedError(_) => HTLCStatus::Failed,
}
}
}
impl From<LnPayState> for HTLCStatus {
fn from(state: LnPayState) -> Self {
match state {
LnPayState::Created => HTLCStatus::Pending,
LnPayState::Canceled => HTLCStatus::Failed,
LnPayState::Funded => HTLCStatus::InFlight,
LnPayState::WaitingForRefund { .. } => HTLCStatus::InFlight,
LnPayState::AwaitingChange => HTLCStatus::InFlight,
LnPayState::Success { .. } => HTLCStatus::Succeeded,
LnPayState::Refunded { .. } => HTLCStatus::Failed,
LnPayState::UnexpectedError { .. } => HTLCStatus::Failed,
}
}
}
// This is the FederationStorage object saved to the DB
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct FederationStorage {
pub federations: HashMap<String, FederationIndex>,
pub version: u32,
}
// This is the FederationIdentity that refer to a specific federation
// Used for public facing identification.
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct FederationIdentity {
pub uuid: String,
pub federation_id: FederationId,
// https://github.com/fedimint/fedimint/tree/master/docs/meta_fields
pub federation_name: Option<String>,
pub federation_expiry_timestamp: Option<String>,
pub welcome_message: Option<String>,
pub gateway_fees: Option<GatewayFees>,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct GatewayFees {
pub base_msat: u32,
pub proportional_millionths: u32,
}
impl From<RoutingFees> for GatewayFees {
fn from(val: RoutingFees) -> Self {
GatewayFees {
base_msat: val.base_msat,
proportional_millionths: val.proportional_millionths,
}
}
}
// This is the FederationIndex reference that is saved to the DB
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct FederationIndex {
pub federation_code: InviteCode,
}
pub struct FedimintBalance {
pub amount: u64,
}
pub(crate) struct FederationClient<S: MutinyStorage> {
pub(crate) uuid: String,
pub(crate) fedimint_client: ClientArc,
storage: S,
#[allow(dead_code)]
fedimint_storage: FedimintStorage<S>,
pub(crate) logger: Arc<MutinyLogger>,
}
impl<S: MutinyStorage> FederationClient<S> {
#[allow(clippy::too_many_arguments)]
pub(crate) async fn new(
uuid: String,
federation_code: InviteCode,
xprivkey: ExtendedPrivKey,
storage: &S,
network: Network,
logger: Arc<MutinyLogger>,
) -> Result<Self, MutinyError> {
log_info!(logger, "initializing a new federation client: {uuid}");
let federation_id = federation_code.federation_id();
let mut client_builder = fedimint_client::Client::builder();
client_builder.with_module(WalletClientInit(None));
client_builder.with_module(MintClientInit);
client_builder.with_module(LightningClientInit);
log_trace!(logger, "Building fedimint client db");
let fedimint_storage =
FedimintStorage::new(storage.clone(), federation_id.to_string(), logger.clone())
.await?;
let db = fedimint_storage.clone().into();
if get_config_from_db(&db).await.is_none() {
let download = Instant::now();
let federation_info = FederationInfo::from_invite_code(federation_code)
.await
.map_err(|e| {
log_error!(logger, "Could not parse invite code: {}", e);
e
})?;
log_trace!(
logger,
"Downloaded federation info in: {}ms",
download.elapsed().as_millis()
);
log_debug!(
logger,
"parsed federation invite code: {:?}",
federation_info.invite_code()
);
client_builder.with_federation_info(federation_info);
}
client_builder.with_database(db);
client_builder.with_primary_module(1);
log_trace!(logger, "Building fedimint client db");
let secret = create_federation_secret(xprivkey, network)?;
let fedimint_client = client_builder
.build(get_default_client_secret(&secret, &federation_id))
.await?;
log_trace!(logger, "Retrieving fedimint wallet client module");
// check federation is on expected network
let wallet_client = fedimint_client.get_first_module::<WalletClientModule>();
// compare magic bytes because different versions of rust-bitcoin
if network.magic().to_bytes() != wallet_client.get_network().magic().to_le_bytes() {
log_error!(
logger,
"Fedimint on different network {}, expected: {network}",
wallet_client.get_network()
);
// try to delete the storage for this federation
if let Err(e) = fedimint_storage.delete_store().await {
log_error!(logger, "Could not delete fedimint storage: {e}");
}
return Err(MutinyError::NetworkMismatch);
}
// Set active gateway preference in background
let client_clone = fedimint_client.clone();
let logger_clone = logger.clone();
spawn(async move {
let start = Instant::now();
let lightning_module = client_clone.get_first_module::<LightningClientModule>();
let gateways = lightning_module
.fetch_registered_gateways()
.await
.map_err(|e| {
log_warn!(
logger_clone,
"Could not fetch gateways from federation {federation_id}: {e}",
)
});
if let Ok(gateways) = gateways {
if let Some(a) = get_gateway_preference(gateways, federation_id) {
log_info!(
logger_clone,
"Setting active gateway for federation {federation_id}: {a}"
);
let _ = lightning_module.set_active_gateway(&a).await.map_err(|e| {
log_warn!(
logger_clone,
"Could not set gateway for federation {federation_id}: {e}",
)
});
}
}
log_trace!(
logger_clone,
"Setting active gateway took: {}ms",
start.elapsed().as_millis()
);
});
log_debug!(logger, "Built fedimint client");
Ok(FederationClient {
uuid,
fedimint_client,
fedimint_storage,
storage: storage.clone(),
logger,
})
}
pub(crate) async fn gateway_fee(&self) -> Result<GatewayFees, MutinyError> {
let lightning_module = self
.fedimint_client
.get_first_module::<LightningClientModule>();
let gw = lightning_module.select_active_gateway().await?;
Ok(gw.fees.into())
}
pub(crate) async fn get_invoice(
&self,
amount: u64,
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyError> {
let inbound = true;
let lightning_module = self
.fedimint_client
.get_first_module::<LightningClientModule>();
let (_id, invoice) = lightning_module
.create_bolt11_invoice(Amount::from_sats(amount), String::new(), None, ())
.await?;
let invoice = convert_from_fedimint_invoice(&invoice);
// persist the invoice
let mut stored_payment: MutinyInvoice = invoice.clone().into();
stored_payment.inbound = inbound;
stored_payment.labels = labels;
log_trace!(self.logger, "Persiting payment");
let hash = stored_payment.payment_hash.into_32();
let payment_info = PaymentInfo::from(stored_payment);
persist_payment_info(&self.storage, &hash, &payment_info, inbound)?;
log_trace!(self.logger, "Persisted payment");
Ok(invoice.into())
}
/// Get the balance of this federation client in sats
pub(crate) async fn get_balance(&self) -> Result<u64, MutinyError> {
Ok(self.fedimint_client.get_balance().await.msats / 1_000)
}
pub async fn check_activity(&self) -> Result<(), MutinyError> {
log_trace!(self.logger, "Getting activity");
let mut pending_invoices = Vec::new();
// inbound
pending_invoices.extend(
list_payment_info(&self.storage, true)?
.into_iter()
.filter_map(|(h, i)| {
let mutiny_invoice = MutinyInvoice::from(i.clone(), h, true, vec![]).ok();
// filter out finalized invoices
mutiny_invoice.filter(|invoice| {
matches!(invoice.status, HTLCStatus::InFlight | HTLCStatus::Pending)
})
}),
);
// outbound
pending_invoices.extend(
list_payment_info(&self.storage, false)?
.into_iter()
.filter_map(|(h, i)| {
let mutiny_invoice = MutinyInvoice::from(i.clone(), h, false, vec![]).ok();
// filter out finalized invoices
mutiny_invoice.filter(|invoice| {
matches!(invoice.status, HTLCStatus::InFlight | HTLCStatus::Pending)
})
}),
);
let operations = if !pending_invoices.is_empty() {
log_trace!(self.logger, "pending invoices, going to list operations");
self.fedimint_client
.operation_log()
.list_operations(FEDIMINT_OPERATIONS_LIST_MAX, None)
.await
} else {
vec![]
};
let lightning_module = Arc::new(
self.fedimint_client
.get_first_module::<LightningClientModule>(),
);
let mut operation_map: HashMap<
[u8; 32],
(ChronologicalOperationLogKey, OperationLogEntry),
> = HashMap::with_capacity(operations.len());
log_trace!(
self.logger,
"About to go through {} operations",
operations.len()
);
for (key, entry) in operations {
if entry.operation_module_kind() == LightningCommonInit::KIND.as_str() {
let lightning_meta: LightningOperationMeta = entry.meta();
match lightning_meta.variant {
LightningOperationMetaVariant::Pay(pay_meta) => {
let hash = pay_meta.invoice.payment_hash().into_inner();
operation_map.insert(hash, (key, entry));
}
LightningOperationMetaVariant::Receive { invoice, .. } => {
let hash = invoice.payment_hash().into_inner();
operation_map.insert(hash, (key, entry));
}
}
}
}
log_trace!(
self.logger,
"Going through {} pending invoices to extract status",
pending_invoices.len()
);
for invoice in pending_invoices {
let hash = invoice.payment_hash.into_32();
if let Some((key, entry)) = operation_map.get(&hash) {
if let Some(updated_invoice) = extract_invoice_from_entry(
self.logger.clone(),
entry,
hash,
key.operation_id,
&lightning_module,
)
.await
{
self.maybe_update_after_checking_fedimint(updated_invoice.clone())
.await?;
}
}
}
Ok(())
}
async fn maybe_update_after_checking_fedimint(
&self,
updated_invoice: MutinyInvoice,
) -> Result<(), MutinyError> {
if matches!(
updated_invoice.status,
HTLCStatus::Succeeded | HTLCStatus::Failed
) {
log_debug!(self.logger, "Saving updated payment");
let hash = updated_invoice.payment_hash.into_32();
let inbound = updated_invoice.inbound;
let payment_info = PaymentInfo::from(updated_invoice);
persist_payment_info(&self.storage, &hash, &payment_info, inbound)?;
}
Ok(())
}
pub async fn get_invoice_by_hash(
&self,
hash: &sha256::Hash,
) -> Result<MutinyInvoice, MutinyError> {
log_trace!(self.logger, "get_invoice_by_hash");
// Try to get the invoice from storage first
let (invoice, inbound) = match get_payment_info(&self.storage, hash, &self.logger) {
Ok(i) => i,
Err(e) => {
log_error!(self.logger, "could not get invoice by hash: {e}");
return Err(e);
}
};
log_trace!(self.logger, "retrieved invoice by hash");
if matches!(invoice.status, HTLCStatus::InFlight | HTLCStatus::Pending) {
log_trace!(self.logger, "invoice still in flight, getting operations");
// If the invoice is InFlight or Pending, check the operation log for updates
let lightning_module = self
.fedimint_client
.get_first_module::<LightningClientModule>();
let operations = self
.fedimint_client
.operation_log()
.list_operations(FEDIMINT_OPERATIONS_LIST_MAX, None)
.await;
log_trace!(
self.logger,
"going to go through {} operations",
operations.len()
);
for (key, entry) in operations {
if entry.operation_module_kind() == LightningCommonInit::KIND.as_str() {
if let Some(updated_invoice) = extract_invoice_from_entry(
self.logger.clone(),
&entry,
hash.into_32(),
key.operation_id,
&lightning_module,
)
.await
{
self.maybe_update_after_checking_fedimint(updated_invoice.clone())
.await?;
return Ok(updated_invoice);
}
} else {
log_warn!(
self.logger,
"Unsupported module: {}",
entry.operation_module_kind()
);
}
}
} else {
// If the invoice is not InFlight or Pending, return it directly
log_trace!(self.logger, "returning final invoice");
// TODO labels
return MutinyInvoice::from(invoice, PaymentHash(hash.into_32()), inbound, vec![]);
}
log_debug!(self.logger, "could not find invoice");
Err(MutinyError::NotFound)
}
pub(crate) async fn pay_invoice(
&self,
invoice: Bolt11Invoice,
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyError> {
let inbound = false;
let lightning_module = self
.fedimint_client
.get_first_module::<LightningClientModule>();
let fedimint_invoice = convert_to_fedimint_invoice(&invoice);
let outgoing_payment = lightning_module
.pay_bolt11_invoice(fedimint_invoice, ())
.await?;
// Save after payment was initiated successfully
let mut stored_payment: MutinyInvoice = invoice.clone().into();
stored_payment.inbound = inbound;
stored_payment.labels = labels;
let hash = stored_payment.payment_hash.into_32();
let payment_info = PaymentInfo::from(stored_payment);
persist_payment_info(&self.storage, &hash, &payment_info, inbound)?;
// Subscribe and process outcome based on payment type
let mut inv = match outgoing_payment.payment_type {
fedimint_ln_client::PayType::Internal(pay_id) => {
match lightning_module.subscribe_internal_pay(pay_id).await {
Ok(o) => {
process_outcome(
o,
process_pay_state_internal,
invoice.clone(),
inbound,
DEFAULT_PAYMENT_TIMEOUT * 1_000,
Arc::clone(&self.logger),
)
.await
}
Err(_) => invoice.clone().into(),
}
}
fedimint_ln_client::PayType::Lightning(pay_id) => {
match lightning_module.subscribe_ln_pay(pay_id).await {
Ok(o) => {
process_outcome(
o,
process_pay_state_ln,
invoice.clone(),
inbound,
DEFAULT_PAYMENT_TIMEOUT * 1_000,
Arc::clone(&self.logger),
)
.await
}
Err(_) => invoice.clone().into(),
}
}
};
inv.fees_paid = Some(sats_round_up(&outgoing_payment.fee));
self.maybe_update_after_checking_fedimint(inv.clone())
.await?;
match inv.status {
HTLCStatus::Succeeded => Ok(inv),
HTLCStatus::Failed => Err(MutinyError::RoutingFailed),
HTLCStatus::Pending => Err(MutinyError::PaymentTimeout),
HTLCStatus::InFlight => Err(MutinyError::PaymentTimeout),
}
}
pub(crate) async fn reissue(&self, oob_notes: OOBNotes) -> Result<(), MutinyError> {
// Get the `MintClientModule`
let mint_module = self.fedimint_client.get_first_module::<MintClientModule>();
// Reissue `OOBNotes`
let operation_id = mint_module.reissue_external_notes(oob_notes, ()).await?;
match process_reissue_outcome(
&mint_module,
operation_id,
FEDIMINT_OOB_REISSUE_TIMEOUT_CHECK_MS,
self.logger.clone(),
)
.await?
{
ReissueExternalNotesState::Done => {
log_trace!(self.logger, "re-issuance of OOBNotes was successful!");
Ok(())
}
ReissueExternalNotesState::Failed(reason) => {
log_trace!(
self.logger,
"re-issuance of OOBNotes failed explicitly, reason: {reason}"
);
Err(MutinyError::FedimintReissueFailed)
}
_ => {
log_trace!(
self.logger,
"re-issuance of OOBNotes explicitly failed, due to outcome with a stale state!"
);
Err(MutinyError::FedimintReissueStaleState)
}
}
}
pub async fn get_mutiny_federation_identity(&self) -> FederationIdentity {
let gateway_fees = self.gateway_fee().await.ok();
FederationIdentity {
uuid: self.uuid.clone(),
federation_id: self.fedimint_client.federation_id(),
federation_name: self.fedimint_client.get_meta("federation_name"),
federation_expiry_timestamp: self
.fedimint_client
.get_meta("federation_expiry_timestamp"),
welcome_message: self.fedimint_client.get_meta("welcome_message"),
gateway_fees,
}
}
// delete_fedimint_storage is not suggested at the moment due to the lack of easy restores
#[allow(dead_code)]
pub async fn delete_fedimint_storage(&self) -> Result<(), MutinyError> {
self.fedimint_storage.delete_store().await
}
pub async fn recover_backup(&self) -> Result<(), MutinyError> {
self.fedimint_client
.restore_from_backup(None)
.await
.map_err(|e| {
log_error!(self.logger, "Could not restore from backup: {}", e);
e
})?;
Ok(())
}
}
fn sats_round_up(amount: &Amount) -> u64 {
Amount::from_msats(amount.msats + 999).sats_round_down()
}
// Get a preferred gateway from a federation
fn get_gateway_preference(
gateways: Vec<fedimint_ln_common::LightningGatewayAnnouncement>,
federation_id: FederationId,
) -> Option<fedimint_ln_common::bitcoin::secp256k1::PublicKey> {
let mut active_choice: Option<fedimint_ln_common::bitcoin::secp256k1::PublicKey> = None;
let signet_gateway_id =
fedimint_ln_common::bitcoin::secp256k1::PublicKey::from_str(SIGNET_GATEWAY)
.expect("should be valid pubkey");
let mainnet_gateway_id =
fedimint_ln_common::bitcoin::secp256k1::PublicKey::from_str(MAINNET_GATEWAY)
.expect("should be valid pubkey");
let signet_federation_id =
FederationId::from_str(SIGNET_FEDERATION).expect("should be a valid federation id");
let mainnet_federation_id =
FederationId::from_str(MAINNET_FEDERATION).expect("should be a valid federation id");
for g in gateways {
let g_id = g.info.gateway_id;
// if the gateway node ID matches what we expect for our signet/mainnet
// these take the highest priority
if (g_id == signet_gateway_id && federation_id == signet_federation_id)
|| (g_id == mainnet_gateway_id && federation_id == mainnet_federation_id)
{
return Some(g_id);
}
// if vetted, set up as current active choice
if g.vetted {
active_choice = Some(g_id);
continue;
}
// if not vetted, make sure fee is high enough
if active_choice.is_none() {
let fees = g.info.fees;
if fees.base_msat >= 1_000 && fees.proportional_millionths >= 100 {
active_choice = Some(g_id);
continue;
}
}
}
active_choice
}
// A federation private key will be derived from
// `m/1'/N'` where `N` is the network type.
//
// Federation will derive further keys from there.
fn create_federation_secret(
xprivkey: ExtendedPrivKey,
network: Network,
) -> Result<DerivableSecret, MutinyError> {
let context = Secp256k1::new();
let shared_key = create_root_child_key(&context, xprivkey, ChildKey::FederationChildKey)?;
let xpriv = shared_key.derive_priv(
&context,
&DerivationPath::from(vec![ChildNumber::from_hardened_idx(
coin_type_from_network(network),
)?]),
)?;
// now that we have a private key for our federation secret, turn that into a mnemonic so we
// can derive it just like fedimint does in case we ever want to expose the mnemonic for
// fedimint cross compatibility.
let mnemonic = mnemonic_from_xpriv(xpriv)?;
Ok(Bip39RootSecretStrategy::<12>::to_root_secret(&mnemonic))
}
pub(crate) fn mnemonic_from_xpriv(xpriv: ExtendedPrivKey) -> Result<Mnemonic, MutinyError> {
let mnemonic = Mnemonic::from_entropy(&xpriv.private_key.secret_bytes())?;
Ok(mnemonic)
}
async fn extract_invoice_from_entry(
logger: Arc<MutinyLogger>,
entry: &OperationLogEntry,
hash: [u8; 32],
operation_id: OperationId,
lightning_module: &LightningClientModule,
) -> Option<MutinyInvoice> {
let lightning_meta: LightningOperationMeta = entry.meta();
match lightning_meta.variant {
LightningOperationMetaVariant::Pay(pay_meta) => {
let invoice = convert_from_fedimint_invoice(&pay_meta.invoice);
if invoice.payment_hash().into_32() == hash {
match lightning_module.subscribe_ln_pay(operation_id).await {
Ok(o) => Some(
process_outcome(
o,
process_pay_state_ln,
invoice,
false,
FEDIMINT_STATUS_TIMEOUT_CHECK_MS,
logger,
)
.await,
),
Err(_) => Some(invoice.into()),
}
} else {
None
}
}
LightningOperationMetaVariant::Receive { invoice, .. } => {
let invoice = convert_from_fedimint_invoice(&invoice);
if invoice.payment_hash().into_32() == hash {
match lightning_module.subscribe_ln_receive(operation_id).await {
Ok(o) => Some(
process_outcome(
o,
process_receive_state,
invoice,
true,
FEDIMINT_STATUS_TIMEOUT_CHECK_MS,
logger,
)
.await,
),
Err(_) => Some(invoice.into()),
}
} else {
None
}
}
}
}
fn process_pay_state_internal(pay_state: InternalPayState, invoice: &mut MutinyInvoice) {
invoice.preimage = if let InternalPayState::Preimage(ref preimage) = pay_state {
Some(preimage.0.to_lower_hex_string())
} else {
None
};
invoice.status = pay_state.into();
}
fn process_pay_state_ln(pay_state: LnPayState, invoice: &mut MutinyInvoice) {
invoice.preimage = if let LnPayState::Success { ref preimage } = pay_state {
Some(preimage.to_string())
} else {
None
};
invoice.status = pay_state.into();
}
fn process_receive_state(receive_state: LnReceiveState, invoice: &mut MutinyInvoice) {
invoice.status = receive_state.into();
}
async fn process_outcome<U, F>(
stream_or_outcome: UpdateStreamOrOutcome<U>,
process_fn: F,
invoice: Bolt11Invoice,
inbound: bool,
timeout: u64,
logger: Arc<MutinyLogger>,
) -> MutinyInvoice
where
U: Into<HTLCStatus>
+ Clone
+ Serialize
+ DeserializeOwned
+ Debug
+ MaybeSend
+ MaybeSync
+ 'static,
F: Fn(U, &mut MutinyInvoice),
{
let mut invoice: MutinyInvoice = invoice.into();
invoice.inbound = inbound;
match stream_or_outcome {
UpdateStreamOrOutcome::Outcome(outcome) => {
invoice.status = outcome.into();
log_trace!(logger, "Outcome received: {}", invoice.status);
}
UpdateStreamOrOutcome::UpdateStream(mut s) => {
let timeout_future = sleep(timeout as i32);
pin_mut!(timeout_future);
log_trace!(logger, "start timeout stream futures");
while let future::Either::Left((outcome_option, _)) =
future::select(s.next(), &mut timeout_future).await
{
if let Some(outcome) = outcome_option {
log_trace!(logger, "Streamed Outcome received: {:?}", outcome);
process_fn(outcome, &mut invoice);
if matches!(invoice.status, HTLCStatus::Succeeded | HTLCStatus::Failed) {
log_trace!(logger, "Streamed Outcome final, returning");
break;
}
} else {
log_debug!(
logger,
"Timeout reached, exiting loop for payment {}",
invoice.payment_hash
);
break;
}
}
log_trace!(
logger,
"Done with stream outcome, status: {}",
invoice.status
);
}
}
invoice
}
async fn process_reissue_outcome(
mint_module: &MintClientModule,
operation_id: OperationId,
timeout: u64,
logger: Arc<MutinyLogger>,
) -> Result<ReissueExternalNotesState, MutinyError> {
// Subscribe to the outcome based on `ReissueExternalNotesState`
let stream_or_outcome = mint_module
.subscribe_reissue_external_notes(operation_id)
.await
.map_err(MutinyError::Other)?;
// Process the outcome based on `ReissueExternalNotesState`
match stream_or_outcome {
UpdateStreamOrOutcome::Outcome(outcome) => {
log_trace!(logger, "outcome received {:?}", outcome);
Ok(outcome)
}
UpdateStreamOrOutcome::UpdateStream(mut stream) => {
let timeout_fut = sleep(timeout as i32);
pin_mut!(timeout_fut);
log_trace!(logger, "started timeout future {:?}", timeout);
while let future::Either::Left((outcome_opt, _)) =
future::select(stream.next(), &mut timeout_fut).await
{
if let Some(outcome) = outcome_opt {
log_trace!(logger, "streamed outcome received {:?}", outcome);
match outcome {
ReissueExternalNotesState::Failed(_) | ReissueExternalNotesState::Done => {
log_trace!(
logger,
"streamed outcome received is final {:?}, returning",
outcome
);
return Ok(outcome);
}
_ => {
log_trace!(
logger,
"streamed outcome received is not final {:?}, skipping",
outcome
);
}
}
};
}
Err(MutinyError::FedimintReissueFailed)
}
}
}
#[derive(Clone)]
pub struct FedimintStorage<S: MutinyStorage> {
pub(crate) storage: S,
fedimint_memory: Arc<MemDatabase>,
federation_id: String,
federation_version: Arc<AtomicU32>,
}
impl<S: MutinyStorage> fmt::Debug for FedimintStorage<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FedimintDB").finish()
}
}
impl<S: MutinyStorage> FedimintStorage<S> {
pub async fn new(
storage: S,
federation_id: String,
logger: Arc<MutinyLogger>,
) -> Result<Self, MutinyError> {
log_debug!(logger, "initializing fedimint storage");
let fedimint_memory = MemDatabase::new();
let key = key_id(&federation_id);
let federation_version = match storage.get_data::<VersionedValue>(&key) {
Ok(Some(versioned_value)) => {
// get the value/version and load it into fedimint memory
let hex: String = serde_json::from_value(versioned_value.value.clone())?;
if !hex.is_empty() {
let bytes: Vec<u8> =
FromHex::from_hex(&hex).map_err(|e| MutinyError::ReadError {
source: MutinyStorageError::Other(anyhow::Error::new(e)),
})?;
let key_value_pairs: Vec<(Vec<u8>, Vec<u8>)> = bincode::deserialize(&bytes)