1#![deny(clippy::pedantic)]
2#![allow(clippy::cast_possible_truncation)]
3#![allow(clippy::missing_errors_doc)]
4#![allow(clippy::missing_panics_doc)]
5#![allow(clippy::module_name_repetitions)]
6#![allow(clippy::must_use_candidate)]
7#![allow(clippy::too_many_lines)]
8
9pub use fedimint_ln_common as common;
10
11pub mod api;
12#[cfg(feature = "cli")]
13pub mod cli;
14pub mod db;
15pub mod incoming;
16pub mod pay;
17pub mod receive;
18pub mod recurring;
20
21use std::collections::{BTreeMap, BTreeSet};
22use std::iter::once;
23use std::str::FromStr;
24use std::sync::Arc;
25use std::time::Duration;
26
27use anyhow::{Context, anyhow, bail, ensure, format_err};
28use api::LnFederationApi;
29use async_stream::{stream, try_stream};
30use bitcoin::Network;
31use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine, sha256};
32use db::{
33 DbKeyPrefix, LightningGatewayKey, LightningGatewayKeyPrefix, PaymentResult, PaymentResultKey,
34 RecurringPaymentCodeKeyPrefix,
35};
36use fedimint_api_client::api::{DynModuleApi, ServerError};
37use fedimint_client_module::db::{ClientModuleMigrationFn, migrate_state};
38use fedimint_client_module::module::init::{ClientModuleInit, ClientModuleInitArgs};
39use fedimint_client_module::module::recovery::NoModuleBackup;
40use fedimint_client_module::module::{ClientContext, ClientModule, IClientModule, OutPointRange};
41use fedimint_client_module::oplog::UpdateStreamOrOutcome;
42use fedimint_client_module::sm::{DynState, ModuleNotifier, State, StateTransition};
43use fedimint_client_module::transaction::{
44 ClientInput, ClientInputBundle, ClientOutput, ClientOutputBundle, ClientOutputSM,
45 TransactionBuilder,
46};
47use fedimint_client_module::{DynGlobalClientContext, sm_enum_variant_translation};
48use fedimint_core::config::FederationId;
49use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId};
50use fedimint_core::db::{DatabaseTransaction, DatabaseVersion, IDatabaseTransactionOpsCoreTyped};
51use fedimint_core::encoding::{Decodable, Encodable};
52use fedimint_core::module::{
53 Amounts, ApiVersion, CommonModuleInit, ModuleCommon, ModuleInit, MultiApiVersion,
54};
55use fedimint_core::secp256k1::{
56 All, Keypair, PublicKey, Scalar, Secp256k1, SecretKey, Signing, Verification,
57};
58use fedimint_core::task::{MaybeSend, MaybeSync, timeout};
59use fedimint_core::util::update_merge::UpdateMerge;
60use fedimint_core::util::{BoxStream, FmtCompactAnyhow as _, backoff_util, retry};
61use fedimint_core::{
62 Amount, OutPoint, apply, async_trait_maybe_send, push_db_pair_items, runtime, secp256k1,
63};
64use fedimint_derive_secret::{ChildId, DerivableSecret};
65use fedimint_ln_common::client::GatewayApi;
66use fedimint_ln_common::config::{FeeToAmount, LightningClientConfig};
67use fedimint_ln_common::contracts::incoming::{IncomingContract, IncomingContractOffer};
68use fedimint_ln_common::contracts::outgoing::{
69 OutgoingContract, OutgoingContractAccount, OutgoingContractData,
70};
71use fedimint_ln_common::contracts::{
72 Contract, ContractId, DecryptedPreimage, EncryptedPreimage, IdentifiableContract, Preimage,
73 PreimageKey,
74};
75use fedimint_ln_common::gateway_endpoint_constants::{
76 GET_GATEWAY_ID_ENDPOINT, PAY_INVOICE_ENDPOINT,
77};
78use fedimint_ln_common::{
79 ContractOutput, KIND, LightningCommonInit, LightningGateway, LightningGatewayAnnouncement,
80 LightningGatewayRegistration, LightningInput, LightningModuleTypes, LightningOutput,
81 LightningOutputV0,
82};
83use fedimint_logging::LOG_CLIENT_MODULE_LN;
84use futures::{Future, StreamExt};
85use incoming::IncomingSmError;
86use itertools::Itertools;
87use lightning_invoice::{
88 Bolt11Invoice, Currency, InvoiceBuilder, PaymentSecret, RouteHint, RouteHintHop, RoutingFees,
89};
90use pay::PayInvoicePayload;
91use rand::rngs::OsRng;
92use rand::seq::IteratorRandom as _;
93use rand::{CryptoRng, Rng, RngCore};
94use reqwest::Method;
95use serde::{Deserialize, Serialize};
96use strum::IntoEnumIterator;
97use tokio::sync::Notify;
98use tracing::{debug, error, info};
99
100use crate::db::PaymentResultPrefix;
101use crate::incoming::{
102 FundingOfferState, IncomingSmCommon, IncomingSmStates, IncomingStateMachine,
103};
104use crate::pay::lightningpay::LightningPayStates;
105use crate::pay::{
106 GatewayPayError, LightningPayCommon, LightningPayCreatedOutgoingLnContract,
107 LightningPayStateMachine,
108};
109use crate::receive::{
110 LightningReceiveError, LightningReceiveStateMachine, LightningReceiveStates,
111 LightningReceiveSubmittedOffer, get_incoming_contract,
112};
113use crate::recurring::RecurringPaymentCodeEntry;
114
115const OUTGOING_LN_CONTRACT_TIMELOCK: u64 = 500;
118
119const DEFAULT_INVOICE_EXPIRY_TIME: Duration = Duration::from_hours(24);
122
123#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Encodable, Decodable)]
124#[serde(rename_all = "snake_case")]
125pub enum PayType {
126 Internal(OperationId),
128 Lightning(OperationId),
130}
131
132impl PayType {
133 pub fn operation_id(&self) -> OperationId {
134 match self {
135 PayType::Internal(operation_id) | PayType::Lightning(operation_id) => *operation_id,
136 }
137 }
138
139 pub fn payment_type(&self) -> String {
140 match self {
141 PayType::Internal(_) => "internal",
142 PayType::Lightning(_) => "lightning",
143 }
144 .into()
145 }
146}
147
148#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
150pub enum ReceivingKey {
151 Personal(Keypair),
154 External(PublicKey),
157}
158
159impl ReceivingKey {
160 pub fn public_key(&self) -> PublicKey {
162 match self {
163 ReceivingKey::Personal(keypair) => keypair.public_key(),
164 ReceivingKey::External(public_key) => *public_key,
165 }
166 }
167}
168
169#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
170pub enum LightningPaymentOutcome {
171 Success { preimage: String },
172 Failure { error_message: String },
173}
174
175#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
178#[serde(rename_all = "snake_case")]
179pub enum InternalPayState {
180 Funding,
181 Preimage(Preimage),
182 RefundSuccess {
183 out_points: Vec<OutPoint>,
184 error: IncomingSmError,
185 },
186 RefundError {
187 error_message: String,
188 error: IncomingSmError,
189 },
190 FundingFailed {
191 error: IncomingSmError,
192 },
193 UnexpectedError(String),
194}
195
196#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
199#[serde(rename_all = "snake_case")]
200pub enum LnPayState {
201 Created,
202 Canceled,
203 Funded { block_height: u32 },
204 WaitingForRefund { error_reason: String },
205 AwaitingChange,
206 Success { preimage: String },
207 Refunded { gateway_error: GatewayPayError },
208 UnexpectedError { error_message: String },
209}
210
211#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
214#[serde(rename_all = "snake_case")]
215pub enum LnReceiveState {
216 Created,
217 WaitingForPayment { invoice: String, timeout: Duration },
218 Canceled { reason: LightningReceiveError },
219 Funded,
220 AwaitingFunds,
221 Claimed,
222}
223
224fn invoice_has_internal_payment_markers(
225 invoice: &Bolt11Invoice,
226 markers: (fedimint_core::secp256k1::PublicKey, u64),
227) -> bool {
228 invoice
231 .route_hints()
232 .first()
233 .and_then(|rh| rh.0.last())
234 .map(|hop| (hop.src_node_id, hop.short_channel_id))
235 == Some(markers)
236}
237
238fn invoice_routes_back_to_federation(
239 invoice: &Bolt11Invoice,
240 gateways: Vec<LightningGateway>,
241) -> bool {
242 gateways.into_iter().any(|gateway| {
243 invoice
244 .route_hints()
245 .first()
246 .and_then(|rh| rh.0.last())
247 .map(|hop| (hop.src_node_id, hop.short_channel_id))
248 == Some((gateway.node_pub_key, gateway.federation_index))
249 })
250}
251
252#[derive(Debug, Clone, Serialize, Deserialize)]
253#[serde(rename_all = "snake_case")]
254pub struct LightningOperationMetaPay {
255 pub out_point: OutPoint,
256 pub invoice: Bolt11Invoice,
257 pub fee: Amount,
258 pub change: Vec<OutPoint>,
259 pub is_internal_payment: bool,
260 pub contract_id: ContractId,
261 pub gateway_id: Option<secp256k1::PublicKey>,
262}
263
264#[derive(Debug, Clone, Serialize, Deserialize)]
265pub struct LightningOperationMeta {
266 pub variant: LightningOperationMetaVariant,
267 pub extra_meta: serde_json::Value,
268}
269
270pub use deprecated_variant_hack::LightningOperationMetaVariant;
271
272#[allow(deprecated)]
277mod deprecated_variant_hack {
278 use super::{
279 Bolt11Invoice, Deserialize, LightningOperationMetaPay, OutPoint, Serialize, secp256k1,
280 };
281 use crate::recurring::ReurringPaymentReceiveMeta;
282
283 #[derive(Debug, Clone, Serialize, Deserialize)]
284 #[serde(rename_all = "snake_case")]
285 pub enum LightningOperationMetaVariant {
286 Pay(LightningOperationMetaPay),
287 Receive {
288 out_point: OutPoint,
289 invoice: Bolt11Invoice,
290 gateway_id: Option<secp256k1::PublicKey>,
291 },
292 #[deprecated(
293 since = "0.7.0",
294 note = "Use recurring payment functionality instead instead"
295 )]
296 Claim {
297 out_points: Vec<OutPoint>,
298 },
299 RecurringPaymentReceive(ReurringPaymentReceiveMeta),
300 }
301}
302
303#[derive(Debug, Clone, Default)]
304pub struct LightningClientInit {
305 pub gateway_conn: Option<Arc<dyn GatewayConnection + Send + Sync>>,
306}
307
308impl ModuleInit for LightningClientInit {
309 type Common = LightningCommonInit;
310
311 async fn dump_database(
312 &self,
313 dbtx: &mut DatabaseTransaction<'_>,
314 prefix_names: Vec<String>,
315 ) -> Box<dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_> {
316 let mut ln_client_items: BTreeMap<String, Box<dyn erased_serde::Serialize + Send>> =
317 BTreeMap::new();
318 let filtered_prefixes = DbKeyPrefix::iter().filter(|f| {
319 prefix_names.is_empty() || prefix_names.contains(&f.to_string().to_lowercase())
320 });
321
322 for table in filtered_prefixes {
323 #[allow(clippy::match_same_arms)]
324 match table {
325 DbKeyPrefix::ActiveGateway | DbKeyPrefix::MetaOverridesDeprecated => {
326 }
328 DbKeyPrefix::PaymentResult => {
329 push_db_pair_items!(
330 dbtx,
331 PaymentResultPrefix,
332 PaymentResultKey,
333 PaymentResult,
334 ln_client_items,
335 "Payment Result"
336 );
337 }
338 DbKeyPrefix::LightningGateway => {
339 push_db_pair_items!(
340 dbtx,
341 LightningGatewayKeyPrefix,
342 LightningGatewayKey,
343 LightningGatewayRegistration,
344 ln_client_items,
345 "Lightning Gateways"
346 );
347 }
348 DbKeyPrefix::RecurringPaymentKey => {
349 push_db_pair_items!(
350 dbtx,
351 RecurringPaymentCodeKeyPrefix,
352 RecurringPaymentCodeKey,
353 RecurringPaymentCodeEntry,
354 ln_client_items,
355 "Recurring Payment Code"
356 );
357 }
358 DbKeyPrefix::ExternalReservedStart
359 | DbKeyPrefix::CoreInternalReservedStart
360 | DbKeyPrefix::CoreInternalReservedEnd => {}
361 }
362 }
363
364 Box::new(ln_client_items.into_iter())
365 }
366}
367
368#[derive(Debug)]
369#[repr(u64)]
370pub enum LightningChildKeys {
371 RedeemKey = 0,
372 PreimageAuthentication = 1,
373 RecurringPaymentCodeSecret = 2,
374}
375
376#[apply(async_trait_maybe_send!)]
377impl ClientModuleInit for LightningClientInit {
378 type Module = LightningClientModule;
379
380 fn supported_api_versions(&self) -> MultiApiVersion {
381 MultiApiVersion::try_from_iter([ApiVersion { major: 0, minor: 0 }])
382 .expect("no version conflicts")
383 }
384
385 async fn init(&self, args: &ClientModuleInitArgs<Self>) -> anyhow::Result<Self::Module> {
386 let gateway_conn = if let Some(gateway_conn) = self.gateway_conn.clone() {
387 gateway_conn
388 } else {
389 let api = GatewayApi::new(None, args.connector_registry.clone());
390 Arc::new(RealGatewayConnection { api })
391 };
392 Ok(LightningClientModule::new(args, gateway_conn))
393 }
394
395 fn get_database_migrations(&self) -> BTreeMap<DatabaseVersion, ClientModuleMigrationFn> {
396 let mut migrations: BTreeMap<DatabaseVersion, ClientModuleMigrationFn> = BTreeMap::new();
397 migrations.insert(DatabaseVersion(0), |dbtx, _, _| {
398 Box::pin(async {
399 dbtx.remove_entry(&crate::db::ActiveGatewayKey).await;
400 Ok(None)
401 })
402 });
403
404 migrations.insert(DatabaseVersion(1), |_, active_states, inactive_states| {
405 Box::pin(async {
406 migrate_state(active_states, inactive_states, db::get_v1_migrated_state)
407 })
408 });
409
410 migrations.insert(DatabaseVersion(2), |_, active_states, inactive_states| {
411 Box::pin(async {
412 migrate_state(active_states, inactive_states, db::get_v2_migrated_state)
413 })
414 });
415
416 migrations.insert(DatabaseVersion(3), |_, active_states, inactive_states| {
417 Box::pin(async {
418 migrate_state(active_states, inactive_states, db::get_v3_migrated_state)
419 })
420 });
421
422 migrations
423 }
424
425 fn used_db_prefixes(&self) -> Option<BTreeSet<u8>> {
426 Some(
427 DbKeyPrefix::iter()
428 .map(|p| p as u8)
429 .chain(
430 DbKeyPrefix::ExternalReservedStart as u8
431 ..=DbKeyPrefix::CoreInternalReservedEnd as u8,
432 )
433 .collect(),
434 )
435 }
436}
437
438#[derive(Debug)]
443pub struct LightningClientModule {
444 pub cfg: LightningClientConfig,
445 notifier: ModuleNotifier<LightningClientStateMachines>,
446 redeem_key: Keypair,
447 recurring_payment_code_secret: DerivableSecret,
448 secp: Secp256k1<All>,
449 module_api: DynModuleApi,
450 preimage_auth: Keypair,
451 client_ctx: ClientContext<Self>,
452 update_gateway_cache_merge: UpdateMerge,
453 gateway_conn: Arc<dyn GatewayConnection + Send + Sync>,
454 new_recurring_payment_code: Arc<Notify>,
455}
456
457#[apply(async_trait_maybe_send!)]
458impl ClientModule for LightningClientModule {
459 type Init = LightningClientInit;
460 type Common = LightningModuleTypes;
461 type Backup = NoModuleBackup;
462 type ModuleStateMachineContext = LightningClientContext;
463 type States = LightningClientStateMachines;
464
465 fn context(&self) -> Self::ModuleStateMachineContext {
466 LightningClientContext {
467 ln_decoder: self.decoder(),
468 redeem_key: self.redeem_key,
469 gateway_conn: self.gateway_conn.clone(),
470 }
471 }
472
473 fn input_fee(
474 &self,
475 _amount: &Amounts,
476 _input: &<Self::Common as ModuleCommon>::Input,
477 ) -> Option<Amounts> {
478 Some(Amounts::new_bitcoin(self.cfg.fee_consensus.contract_input))
479 }
480
481 fn output_fee(
482 &self,
483 _amount: &Amounts,
484 output: &<Self::Common as ModuleCommon>::Output,
485 ) -> Option<Amounts> {
486 match output.maybe_v0_ref()? {
487 LightningOutputV0::Contract(_) => {
488 Some(Amounts::new_bitcoin(self.cfg.fee_consensus.contract_output))
489 }
490 LightningOutputV0::Offer(_) | LightningOutputV0::CancelOutgoing { .. } => {
491 Some(Amounts::ZERO)
492 }
493 }
494 }
495
496 #[cfg(feature = "cli")]
497 async fn handle_cli_command(
498 &self,
499 args: &[std::ffi::OsString],
500 ) -> anyhow::Result<serde_json::Value> {
501 cli::handle_cli_command(self, args).await
502 }
503
504 async fn handle_rpc(
505 &self,
506 method: String,
507 payload: serde_json::Value,
508 ) -> BoxStream<'_, anyhow::Result<serde_json::Value>> {
509 Box::pin(try_stream! {
510 match method.as_str() {
511 "create_bolt11_invoice" => {
512 let req: CreateBolt11InvoiceRequest = serde_json::from_value(payload)?;
513 let (op, invoice, _) = self
514 .create_bolt11_invoice(
515 req.amount,
516 lightning_invoice::Bolt11InvoiceDescription::Direct(
517 lightning_invoice::Description::new(req.description)?,
518 ),
519 req.expiry_time,
520 req.extra_meta,
521 req.gateway,
522 )
523 .await?;
524 yield serde_json::json!({
525 "operation_id": op,
526 "invoice": invoice,
527 });
528 }
529 "pay_bolt11_invoice" => {
530 let req: PayBolt11InvoiceRequest = serde_json::from_value(payload)?;
531 let outgoing_payment = self
532 .pay_bolt11_invoice(req.maybe_gateway, req.invoice, req.extra_meta)
533 .await?;
534 yield serde_json::to_value(outgoing_payment)?;
535 }
536 "select_available_gateway" => {
537 let req: SelectAvailableGatewayRequest = serde_json::from_value(payload)?;
538 let gateway = self.select_available_gateway(req.maybe_gateway,req.maybe_invoice).await?;
539 yield serde_json::to_value(gateway)?;
540 }
541 "subscribe_ln_pay" => {
542 let req: SubscribeLnPayRequest = serde_json::from_value(payload)?;
543 for await state in self.subscribe_ln_pay(req.operation_id).await?.into_stream() {
544 yield serde_json::to_value(state)?;
545 }
546 }
547 "subscribe_internal_pay" => {
548 let req: SubscribeInternalPayRequest = serde_json::from_value(payload)?;
549 for await state in self.subscribe_internal_pay(req.operation_id).await?.into_stream() {
550 yield serde_json::to_value(state)?;
551 }
552 }
553 "subscribe_ln_receive" => {
554 let req: SubscribeLnReceiveRequest = serde_json::from_value(payload)?;
555 for await state in self.subscribe_ln_receive(req.operation_id).await?.into_stream()
556 {
557 yield serde_json::to_value(state)?;
558 }
559 }
560 "create_bolt11_invoice_for_user_tweaked" => {
561 let req: CreateBolt11InvoiceForUserTweakedRequest = serde_json::from_value(payload)?;
562 let (op, invoice, _) = self
563 .create_bolt11_invoice_for_user_tweaked(
564 req.amount,
565 lightning_invoice::Bolt11InvoiceDescription::Direct(
566 lightning_invoice::Description::new(req.description)?,
567 ),
568 req.expiry_time,
569 req.user_key,
570 req.index,
571 req.extra_meta,
572 req.gateway,
573 )
574 .await?;
575 yield serde_json::json!({
576 "operation_id": op,
577 "invoice": invoice,
578 });
579 }
580 #[allow(deprecated)]
581 "scan_receive_for_user_tweaked" => {
582 let req: ScanReceiveForUserTweakedRequest = serde_json::from_value(payload)?;
583 let keypair = Keypair::from_secret_key(&self.secp, &req.user_key);
584 let operation_ids = self.scan_receive_for_user_tweaked(keypair, req.indices, req.extra_meta).await;
585 yield serde_json::to_value(operation_ids)?;
586 }
587 #[allow(deprecated)]
588 "subscribe_ln_claim" => {
589 let req: SubscribeLnClaimRequest = serde_json::from_value(payload)?;
590 for await state in self.subscribe_ln_claim(req.operation_id).await?.into_stream() {
591 yield serde_json::to_value(state)?;
592 }
593 }
594 "get_gateway" => {
595 let req: GetGatewayRequest = serde_json::from_value(payload)?;
596 let gateway = self.get_gateway(req.gateway_id, req.force_internal).await?;
597 yield serde_json::to_value(gateway)?;
598 }
599 "list_gateways" => {
600 let gateways = self.list_gateways().await;
601 yield serde_json::to_value(gateways)?;
602 }
603 "update_gateway_cache" => {
604 self.update_gateway_cache().await?;
605 yield serde_json::Value::Null;
606 }
607 _ => {
608 Err(anyhow::format_err!("Unknown method: {}", method))?;
609 unreachable!()
610 },
611 }
612 })
613 }
614}
615
616#[derive(Deserialize)]
617struct CreateBolt11InvoiceRequest {
618 amount: Amount,
619 description: String,
620 expiry_time: Option<u64>,
621 extra_meta: serde_json::Value,
622 gateway: Option<LightningGateway>,
623}
624
625#[derive(Deserialize)]
626struct PayBolt11InvoiceRequest {
627 maybe_gateway: Option<LightningGateway>,
628 invoice: Bolt11Invoice,
629 extra_meta: Option<serde_json::Value>,
630}
631
632#[derive(Deserialize)]
633struct SubscribeLnPayRequest {
634 operation_id: OperationId,
635}
636
637#[derive(Deserialize)]
638struct SubscribeInternalPayRequest {
639 operation_id: OperationId,
640}
641
642#[derive(Deserialize)]
643struct SubscribeLnReceiveRequest {
644 operation_id: OperationId,
645}
646
647#[derive(Debug, Serialize, Deserialize)]
648pub struct SelectAvailableGatewayRequest {
649 maybe_gateway: Option<LightningGateway>,
650 maybe_invoice: Option<Bolt11Invoice>,
651}
652
653#[derive(Deserialize)]
654struct CreateBolt11InvoiceForUserTweakedRequest {
655 amount: Amount,
656 description: String,
657 expiry_time: Option<u64>,
658 user_key: PublicKey,
659 index: u64,
660 extra_meta: serde_json::Value,
661 gateway: Option<LightningGateway>,
662}
663
664#[derive(Deserialize)]
665struct ScanReceiveForUserTweakedRequest {
666 user_key: SecretKey,
667 indices: Vec<u64>,
668 extra_meta: serde_json::Value,
669}
670
671#[derive(Deserialize)]
672struct SubscribeLnClaimRequest {
673 operation_id: OperationId,
674}
675
676#[derive(Deserialize)]
677struct GetGatewayRequest {
678 gateway_id: Option<secp256k1::PublicKey>,
679 force_internal: bool,
680}
681
682#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
683pub enum GatewayStatus {
684 OnlineVetted,
685 OnlineNonVetted,
686}
687
688#[derive(thiserror::Error, Debug, Clone)]
689pub enum PayBolt11InvoiceError {
690 #[error("Previous payment attempt({}) still in progress", .operation_id.fmt_full())]
691 PreviousPaymentAttemptStillInProgress { operation_id: OperationId },
692 #[error("No LN gateway available")]
693 NoLnGatewayAvailable,
694 #[error("Funded contract already exists: {}", .contract_id)]
695 FundedContractAlreadyExists { contract_id: ContractId },
696}
697
698impl LightningClientModule {
699 fn new(
700 args: &ClientModuleInitArgs<LightningClientInit>,
701 gateway_conn: Arc<dyn GatewayConnection + Send + Sync>,
702 ) -> Self {
703 let secp = Secp256k1::new();
704
705 let new_recurring_payment_code = Arc::new(Notify::new());
706 args.task_group().spawn_cancellable(
707 "Recurring payment sync",
708 Self::scan_recurring_payment_code_invoices(
709 args.context(),
710 new_recurring_payment_code.clone(),
711 ),
712 );
713
714 Self {
715 cfg: args.cfg().clone(),
716 notifier: args.notifier().clone(),
717 redeem_key: args
718 .module_root_secret()
719 .child_key(ChildId(LightningChildKeys::RedeemKey as u64))
720 .to_secp_key(&secp),
721 recurring_payment_code_secret: args.module_root_secret().child_key(ChildId(
722 LightningChildKeys::RecurringPaymentCodeSecret as u64,
723 )),
724 module_api: args.module_api().clone(),
725 preimage_auth: args
726 .module_root_secret()
727 .child_key(ChildId(LightningChildKeys::PreimageAuthentication as u64))
728 .to_secp_key(&secp),
729 secp,
730 client_ctx: args.context(),
731 update_gateway_cache_merge: UpdateMerge::default(),
732 gateway_conn,
733 new_recurring_payment_code,
734 }
735 }
736
737 pub async fn get_prev_payment_result(
738 &self,
739 payment_hash: &sha256::Hash,
740 dbtx: &mut DatabaseTransaction<'_>,
741 ) -> PaymentResult {
742 let prev_result = dbtx
743 .get_value(&PaymentResultKey {
744 payment_hash: *payment_hash,
745 })
746 .await;
747 prev_result.unwrap_or(PaymentResult {
748 index: 0,
749 completed_payment: None,
750 })
751 }
752
753 fn get_payment_operation_id(payment_hash: &sha256::Hash, index: u16) -> OperationId {
754 let mut bytes = [0; 34];
757 bytes[0..32].copy_from_slice(&payment_hash.to_byte_array());
758 bytes[32..34].copy_from_slice(&index.to_le_bytes());
759 let hash: sha256::Hash = Hash::hash(&bytes);
760 OperationId(hash.to_byte_array())
761 }
762
763 fn get_preimage_authentication(&self, payment_hash: &sha256::Hash) -> sha256::Hash {
768 let mut bytes = [0; 64];
769 bytes[0..32].copy_from_slice(&payment_hash.to_byte_array());
770 bytes[32..64].copy_from_slice(&self.preimage_auth.secret_bytes());
771 Hash::hash(&bytes)
772 }
773
774 async fn create_outgoing_output<'a, 'b>(
778 &'a self,
779 operation_id: OperationId,
780 invoice: Bolt11Invoice,
781 gateway: LightningGateway,
782 fed_id: FederationId,
783 mut rng: impl RngCore + CryptoRng + 'a,
784 ) -> anyhow::Result<(
785 ClientOutput<LightningOutputV0>,
786 ClientOutputSM<LightningClientStateMachines>,
787 ContractId,
788 )> {
789 let federation_currency: Currency = self.cfg.network.0.into();
790 let invoice_currency = invoice.currency();
791 ensure!(
792 federation_currency == invoice_currency,
793 "Invalid invoice currency: expected={:?}, got={:?}",
794 federation_currency,
795 invoice_currency
796 );
797
798 self.gateway_conn
801 .verify_gateway_availability(&gateway)
802 .await?;
803
804 let consensus_count = self
805 .module_api
806 .fetch_consensus_block_count()
807 .await?
808 .ok_or(format_err!("Cannot get consensus block count"))?;
809
810 let min_final_cltv = invoice.min_final_cltv_expiry_delta();
813 let absolute_timelock =
814 consensus_count + min_final_cltv + OUTGOING_LN_CONTRACT_TIMELOCK - 1;
815
816 let invoice_amount = Amount::from_msats(
818 invoice
819 .amount_milli_satoshis()
820 .context("MissingInvoiceAmount")?,
821 );
822
823 let gateway_fee = gateway.fees.to_amount(&invoice_amount);
824 let contract_amount = invoice_amount + gateway_fee;
825
826 let user_sk = Keypair::new(&self.secp, &mut rng);
827
828 let payment_hash = *invoice.payment_hash();
829 let preimage_auth = self.get_preimage_authentication(&payment_hash);
830 let contract = OutgoingContract {
831 hash: payment_hash,
832 gateway_key: gateway.gateway_redeem_key,
833 timelock: absolute_timelock as u32,
834 user_key: user_sk.public_key(),
835 cancelled: false,
836 };
837
838 let outgoing_payment = OutgoingContractData {
839 recovery_key: user_sk,
840 contract_account: OutgoingContractAccount {
841 amount: contract_amount,
842 contract: contract.clone(),
843 },
844 };
845
846 let contract_id = contract.contract_id();
847 let sm_gen = Arc::new(move |out_point_range: OutPointRange| {
848 vec![LightningClientStateMachines::LightningPay(
849 LightningPayStateMachine {
850 common: LightningPayCommon {
851 operation_id,
852 federation_id: fed_id,
853 contract: outgoing_payment.clone(),
854 gateway_fee,
855 preimage_auth,
856 invoice: invoice.clone(),
857 },
858 state: LightningPayStates::CreatedOutgoingLnContract(
859 LightningPayCreatedOutgoingLnContract {
860 funding_txid: out_point_range.txid(),
861 contract_id,
862 gateway: gateway.clone(),
863 },
864 ),
865 },
866 )]
867 });
868
869 let ln_output = LightningOutputV0::Contract(ContractOutput {
870 amount: contract_amount,
871 contract: Contract::Outgoing(contract),
872 });
873
874 Ok((
875 ClientOutput {
876 output: ln_output,
877 amounts: Amounts::new_bitcoin(contract_amount),
878 },
879 ClientOutputSM {
880 state_machines: sm_gen,
881 },
882 contract_id,
883 ))
884 }
885
886 async fn create_incoming_output(
890 &self,
891 operation_id: OperationId,
892 invoice: Bolt11Invoice,
893 ) -> anyhow::Result<(
894 ClientOutput<LightningOutputV0>,
895 ClientOutputSM<LightningClientStateMachines>,
896 ContractId,
897 )> {
898 let payment_hash = *invoice.payment_hash();
899 let invoice_amount = Amount {
900 msats: invoice
901 .amount_milli_satoshis()
902 .ok_or(IncomingSmError::AmountError {
903 invoice: invoice.clone(),
904 })?,
905 };
906
907 let (incoming_output, amount, contract_id) = create_incoming_contract_output(
908 &self.module_api,
909 payment_hash,
910 invoice_amount,
911 &self.redeem_key,
912 )
913 .await?;
914
915 let client_output = ClientOutput::<LightningOutputV0> {
916 output: incoming_output,
917 amounts: Amounts::new_bitcoin(amount),
918 };
919
920 let client_output_sm = ClientOutputSM::<LightningClientStateMachines> {
921 state_machines: Arc::new(move |out_point_range| {
922 vec![LightningClientStateMachines::InternalPay(
923 IncomingStateMachine {
924 common: IncomingSmCommon {
925 operation_id,
926 contract_id,
927 payment_hash,
928 },
929 state: IncomingSmStates::FundingOffer(FundingOfferState {
930 txid: out_point_range.txid(),
931 }),
932 },
933 )]
934 }),
935 };
936
937 Ok((client_output, client_output_sm, contract_id))
938 }
939
940 async fn await_receive_success(
941 &self,
942 operation_id: OperationId,
943 ) -> Result<(), LightningReceiveError> {
944 let mut stream = self.notifier.subscribe(operation_id).await;
945 loop {
946 if let Some(LightningClientStateMachines::Receive(state)) = stream.next().await {
947 match state.state {
948 LightningReceiveStates::Success(_) => return Ok(()),
949 LightningReceiveStates::Canceled(e) => {
950 return Err(e);
951 }
952 _ => {}
953 }
954 }
955 }
956 }
957
958 async fn await_claim_acceptance(
959 &self,
960 operation_id: OperationId,
961 ) -> Result<Vec<OutPoint>, LightningReceiveError> {
962 let mut stream = self.notifier.subscribe(operation_id).await;
963 loop {
964 if let Some(LightningClientStateMachines::Receive(state)) = stream.next().await {
965 match state.state {
966 LightningReceiveStates::Success(out_points) => return Ok(out_points),
967 LightningReceiveStates::Canceled(e) => {
968 return Err(e);
969 }
970 _ => {}
971 }
972 }
973 }
974 }
975
976 #[allow(clippy::too_many_arguments)]
977 #[allow(clippy::type_complexity)]
978 fn create_lightning_receive_output<'a>(
979 &'a self,
980 amount: Amount,
981 description: lightning_invoice::Bolt11InvoiceDescription,
982 receiving_key: ReceivingKey,
983 mut rng: impl RngCore + CryptoRng + 'a,
984 expiry_time: Option<u64>,
985 src_node_id: secp256k1::PublicKey,
986 short_channel_id: u64,
987 route_hints: &[fedimint_ln_common::route_hints::RouteHint],
988 network: Network,
989 ) -> anyhow::Result<(
990 OperationId,
991 Bolt11Invoice,
992 ClientOutputBundle<LightningOutput, LightningClientStateMachines>,
993 [u8; 32],
994 )> {
995 let preimage_key: [u8; 33] = receiving_key.public_key().serialize();
996 let preimage = sha256::Hash::hash(&preimage_key);
997 let payment_hash = sha256::Hash::hash(&preimage.to_byte_array());
998
999 let (node_secret_key, node_public_key) = self.secp.generate_keypair(&mut rng);
1001
1002 let route_hint_last_hop = RouteHintHop {
1004 src_node_id,
1005 short_channel_id,
1006 fees: RoutingFees {
1007 base_msat: 0,
1008 proportional_millionths: 0,
1009 },
1010 cltv_expiry_delta: 30,
1011 htlc_minimum_msat: None,
1012 htlc_maximum_msat: None,
1013 };
1014 let mut final_route_hints = vec![RouteHint(vec![route_hint_last_hop.clone()])];
1015 if !route_hints.is_empty() {
1016 let mut two_hop_route_hints: Vec<RouteHint> = route_hints
1017 .iter()
1018 .map(|rh| {
1019 RouteHint(
1020 rh.to_ldk_route_hint()
1021 .0
1022 .iter()
1023 .cloned()
1024 .chain(once(route_hint_last_hop.clone()))
1025 .collect(),
1026 )
1027 })
1028 .collect();
1029 final_route_hints.append(&mut two_hop_route_hints);
1030 }
1031
1032 let duration_since_epoch = fedimint_core::time::duration_since_epoch();
1033
1034 let mut invoice_builder = InvoiceBuilder::new(network.into())
1035 .amount_milli_satoshis(amount.msats)
1036 .invoice_description(description)
1037 .payment_hash(payment_hash)
1038 .payment_secret(PaymentSecret(rng.r#gen()))
1039 .duration_since_epoch(duration_since_epoch)
1040 .min_final_cltv_expiry_delta(18)
1041 .payee_pub_key(node_public_key)
1042 .expiry_time(Duration::from_secs(
1043 expiry_time.unwrap_or(DEFAULT_INVOICE_EXPIRY_TIME.as_secs()),
1044 ));
1045
1046 for rh in final_route_hints {
1047 invoice_builder = invoice_builder.private_route(rh);
1048 }
1049
1050 let invoice = invoice_builder
1051 .build_signed(|msg| self.secp.sign_ecdsa_recoverable(msg, &node_secret_key))?;
1052
1053 let operation_id = OperationId(*invoice.payment_hash().as_ref());
1054
1055 let sm_invoice = invoice.clone();
1056 let sm_gen = Arc::new(move |out_point_range: OutPointRange| {
1057 vec![LightningClientStateMachines::Receive(
1058 LightningReceiveStateMachine {
1059 operation_id,
1060 state: LightningReceiveStates::SubmittedOffer(LightningReceiveSubmittedOffer {
1061 offer_txid: out_point_range.txid(),
1062 invoice: sm_invoice.clone(),
1063 receiving_key,
1064 }),
1065 },
1066 )]
1067 });
1068
1069 let ln_output = LightningOutput::new_v0_offer(IncomingContractOffer {
1070 amount,
1071 hash: payment_hash,
1072 encrypted_preimage: EncryptedPreimage::new(
1073 &PreimageKey(preimage_key),
1074 &self.cfg.threshold_pub_key,
1075 ),
1076 expiry_time,
1077 });
1078
1079 Ok((
1080 operation_id,
1081 invoice,
1082 ClientOutputBundle::new(
1083 vec![ClientOutput {
1084 output: ln_output,
1085 amounts: Amounts::ZERO,
1086 }],
1087 vec![ClientOutputSM {
1088 state_machines: sm_gen,
1089 }],
1090 ),
1091 *preimage.as_ref(),
1092 ))
1093 }
1094
1095 pub async fn select_available_gateway(
1096 &self,
1097 maybe_gateway: Option<LightningGateway>,
1098 maybe_invoice: Option<Bolt11Invoice>,
1099 ) -> anyhow::Result<LightningGateway> {
1100 if let Some(gw) = maybe_gateway {
1101 let gw_id = gw.gateway_id;
1102 if self
1103 .gateway_conn
1104 .verify_gateway_availability(&gw)
1105 .await
1106 .is_ok()
1107 {
1108 return Ok(gw);
1109 }
1110 return Err(anyhow::anyhow!("Specified gateway is offline: {}", gw_id));
1111 }
1112
1113 let gateways: Vec<LightningGatewayAnnouncement> = self.list_gateways().await;
1114 if gateways.is_empty() {
1115 return Err(anyhow::anyhow!("No gateways available"));
1116 }
1117
1118 let gateways_with_status =
1119 futures::future::join_all(gateways.into_iter().map(|gw| async {
1120 let online = self
1121 .gateway_conn
1122 .verify_gateway_availability(&gw.info)
1123 .await
1124 .is_ok();
1125 (gw, online)
1126 }))
1127 .await;
1128
1129 let sorted_gateways: Vec<(LightningGatewayAnnouncement, GatewayStatus)> =
1130 gateways_with_status
1131 .into_iter()
1132 .filter_map(|(ann, online)| {
1133 if online {
1134 let status = if ann.vetted {
1135 GatewayStatus::OnlineVetted
1136 } else {
1137 GatewayStatus::OnlineNonVetted
1138 };
1139 Some((ann, status))
1140 } else {
1141 None
1142 }
1143 })
1144 .collect();
1145
1146 if sorted_gateways.is_empty() {
1147 return Err(anyhow::anyhow!("No Lightning Gateway was reachable"));
1148 }
1149
1150 let amount_msat = maybe_invoice.and_then(|inv| inv.amount_milli_satoshis());
1151 let sorted_gateways = sorted_gateways
1152 .into_iter()
1153 .sorted_by_key(|(ann, status)| {
1154 let total_fee_msat: u64 =
1155 amount_msat.map_or(u64::from(ann.info.fees.base_msat), |amt| {
1156 u64::from(ann.info.fees.base_msat)
1157 + ((u128::from(amt)
1158 * u128::from(ann.info.fees.proportional_millionths))
1159 / 1_000_000) as u64
1160 });
1161 (status.clone(), total_fee_msat)
1162 })
1163 .collect::<Vec<_>>();
1164
1165 Ok(sorted_gateways[0].0.info.clone())
1166 }
1167
1168 pub async fn select_gateway(
1171 &self,
1172 gateway_id: &secp256k1::PublicKey,
1173 ) -> Option<LightningGateway> {
1174 let mut dbtx = self.client_ctx.module_db().begin_transaction_nc().await;
1175 let gateways = dbtx
1176 .find_by_prefix(&LightningGatewayKeyPrefix)
1177 .await
1178 .map(|(_, gw)| gw.info)
1179 .collect::<Vec<_>>()
1180 .await;
1181 gateways.into_iter().find(|g| &g.gateway_id == gateway_id)
1182 }
1183
1184 pub async fn update_gateway_cache(&self) -> anyhow::Result<()> {
1189 self.update_gateway_cache_merge
1190 .merge(async {
1191 let gateways = self.module_api.fetch_gateways().await?;
1192 let mut dbtx = self.client_ctx.module_db().begin_transaction().await;
1193
1194 dbtx.remove_by_prefix(&LightningGatewayKeyPrefix).await;
1196
1197 for gw in &gateways {
1198 dbtx.insert_entry(
1199 &LightningGatewayKey(gw.info.gateway_id),
1200 &gw.clone().anchor(),
1201 )
1202 .await;
1203 }
1204
1205 dbtx.commit_tx().await;
1206
1207 Ok(())
1208 })
1209 .await
1210 }
1211
1212 pub async fn update_gateway_cache_continuously<Fut>(
1217 &self,
1218 gateways_filter: impl Fn(Vec<LightningGatewayAnnouncement>) -> Fut,
1219 ) -> !
1220 where
1221 Fut: Future<Output = Vec<LightningGatewayAnnouncement>>,
1222 {
1223 const ABOUT_TO_EXPIRE: Duration = Duration::from_secs(30);
1224 const EMPTY_GATEWAY_SLEEP: Duration = Duration::from_mins(10);
1225
1226 let mut first_time = true;
1227
1228 loop {
1229 let gateways = self.list_gateways().await;
1230 let sleep_time = gateways_filter(gateways)
1231 .await
1232 .into_iter()
1233 .map(|x| x.ttl.saturating_sub(ABOUT_TO_EXPIRE))
1234 .min()
1235 .unwrap_or(if first_time {
1236 Duration::ZERO
1238 } else {
1239 EMPTY_GATEWAY_SLEEP
1240 });
1241 runtime::sleep(sleep_time).await;
1242
1243 let _ = retry(
1245 "update_gateway_cache",
1246 backoff_util::background_backoff(),
1247 || self.update_gateway_cache(),
1248 )
1249 .await;
1250 first_time = false;
1251 }
1252 }
1253
1254 pub async fn list_gateways(&self) -> Vec<LightningGatewayAnnouncement> {
1256 let mut dbtx = self.client_ctx.module_db().begin_transaction_nc().await;
1257 dbtx.find_by_prefix(&LightningGatewayKeyPrefix)
1258 .await
1259 .map(|(_, gw)| gw.unanchor())
1260 .collect::<Vec<_>>()
1261 .await
1262 }
1263
1264 pub async fn pay_bolt11_invoice<M: Serialize + MaybeSend + MaybeSync>(
1273 &self,
1274 maybe_gateway: Option<LightningGateway>,
1275 invoice: Bolt11Invoice,
1276 extra_meta: M,
1277 ) -> anyhow::Result<OutgoingLightningPayment> {
1278 let mut dbtx = self.client_ctx.module_db().begin_transaction().await;
1279 let maybe_gateway_id = maybe_gateway.as_ref().map(|g| g.gateway_id);
1280 let prev_payment_result = self
1281 .get_prev_payment_result(invoice.payment_hash(), &mut dbtx.to_ref_nc())
1282 .await;
1283
1284 if let Some(completed_payment) = prev_payment_result.completed_payment {
1285 return Ok(completed_payment);
1286 }
1287
1288 let prev_operation_id = LightningClientModule::get_payment_operation_id(
1290 invoice.payment_hash(),
1291 prev_payment_result.index,
1292 );
1293 if self.client_ctx.has_active_states(prev_operation_id).await {
1294 bail!(
1295 PayBolt11InvoiceError::PreviousPaymentAttemptStillInProgress {
1296 operation_id: prev_operation_id
1297 }
1298 )
1299 }
1300
1301 let next_index = prev_payment_result.index + 1;
1302 let operation_id =
1303 LightningClientModule::get_payment_operation_id(invoice.payment_hash(), next_index);
1304
1305 let new_payment_result = PaymentResult {
1306 index: next_index,
1307 completed_payment: None,
1308 };
1309
1310 dbtx.insert_entry(
1311 &PaymentResultKey {
1312 payment_hash: *invoice.payment_hash(),
1313 },
1314 &new_payment_result,
1315 )
1316 .await;
1317
1318 let markers = self.client_ctx.get_internal_payment_markers()?;
1319
1320 let mut is_internal_payment = invoice_has_internal_payment_markers(&invoice, markers);
1321 if !is_internal_payment {
1322 let gateways = dbtx
1323 .find_by_prefix(&LightningGatewayKeyPrefix)
1324 .await
1325 .map(|(_, gw)| gw.info)
1326 .collect::<Vec<_>>()
1327 .await;
1328 is_internal_payment = invoice_routes_back_to_federation(&invoice, gateways);
1329 }
1330
1331 let (pay_type, client_output, client_output_sm, contract_id) = if is_internal_payment {
1332 let (output, output_sm, contract_id) = self
1333 .create_incoming_output(operation_id, invoice.clone())
1334 .await?;
1335 (
1336 PayType::Internal(operation_id),
1337 output,
1338 output_sm,
1339 contract_id,
1340 )
1341 } else {
1342 let gateway = maybe_gateway.context(PayBolt11InvoiceError::NoLnGatewayAvailable)?;
1343 let (output, output_sm, contract_id) = self
1344 .create_outgoing_output(
1345 operation_id,
1346 invoice.clone(),
1347 gateway,
1348 self.client_ctx
1349 .get_config()
1350 .await
1351 .global
1352 .calculate_federation_id(),
1353 rand::rngs::OsRng,
1354 )
1355 .await?;
1356 (
1357 PayType::Lightning(operation_id),
1358 output,
1359 output_sm,
1360 contract_id,
1361 )
1362 };
1363
1364 if let Ok(Some(contract)) = self.module_api.fetch_contract(contract_id).await
1366 && contract.amount.msats != 0
1367 {
1368 bail!(PayBolt11InvoiceError::FundedContractAlreadyExists { contract_id });
1369 }
1370
1371 let fee = match &client_output.output {
1374 LightningOutputV0::Contract(contract) => {
1375 let fee_msat = contract
1376 .amount
1377 .msats
1378 .checked_sub(
1379 invoice
1380 .amount_milli_satoshis()
1381 .ok_or(anyhow!("MissingInvoiceAmount"))?,
1382 )
1383 .expect("Contract amount should be greater or equal than invoice amount");
1384 Amount::from_msats(fee_msat)
1385 }
1386 _ => unreachable!("User client will only create contract outputs on spend"),
1387 };
1388
1389 let output = self.client_ctx.make_client_outputs(ClientOutputBundle::new(
1390 vec![ClientOutput {
1391 output: LightningOutput::V0(client_output.output),
1392 amounts: client_output.amounts,
1393 }],
1394 vec![client_output_sm],
1395 ));
1396
1397 let tx = TransactionBuilder::new().with_outputs(output);
1398 let extra_meta =
1399 serde_json::to_value(extra_meta).context("Failed to serialize extra meta")?;
1400 let operation_meta_gen = move |change_range: OutPointRange| LightningOperationMeta {
1401 variant: LightningOperationMetaVariant::Pay(LightningOperationMetaPay {
1402 out_point: OutPoint {
1403 txid: change_range.txid(),
1404 out_idx: 0,
1405 },
1406 invoice: invoice.clone(),
1407 fee,
1408 change: change_range.into_iter().collect(),
1409 is_internal_payment,
1410 contract_id,
1411 gateway_id: maybe_gateway_id,
1412 }),
1413 extra_meta: extra_meta.clone(),
1414 };
1415
1416 dbtx.commit_tx_result().await?;
1419
1420 self.client_ctx
1421 .finalize_and_submit_transaction(
1422 operation_id,
1423 LightningCommonInit::KIND.as_str(),
1424 operation_meta_gen,
1425 tx,
1426 )
1427 .await?;
1428
1429 Ok(OutgoingLightningPayment {
1430 payment_type: pay_type,
1431 contract_id,
1432 fee,
1433 })
1434 }
1435
1436 pub async fn get_ln_pay_details_for(
1437 &self,
1438 operation_id: OperationId,
1439 ) -> anyhow::Result<LightningOperationMetaPay> {
1440 let operation = self.client_ctx.get_operation(operation_id).await?;
1441 let LightningOperationMetaVariant::Pay(pay) =
1442 operation.meta::<LightningOperationMeta>().variant
1443 else {
1444 anyhow::bail!("Operation is not a lightning payment")
1445 };
1446 Ok(pay)
1447 }
1448
1449 pub async fn subscribe_internal_pay(
1450 &self,
1451 operation_id: OperationId,
1452 ) -> anyhow::Result<UpdateStreamOrOutcome<InternalPayState>> {
1453 let operation = self.client_ctx.get_operation(operation_id).await?;
1454
1455 let LightningOperationMetaVariant::Pay(LightningOperationMetaPay {
1456 out_point: _,
1457 invoice: _,
1458 change: _, is_internal_payment,
1460 ..
1461 }) = operation.meta::<LightningOperationMeta>().variant
1462 else {
1463 bail!("Operation is not a lightning payment")
1464 };
1465
1466 ensure!(
1467 is_internal_payment,
1468 "Subscribing to an external LN payment, expected internal LN payment"
1469 );
1470
1471 let mut stream = self.notifier.subscribe(operation_id).await;
1472 let client_ctx = self.client_ctx.clone();
1473
1474 Ok(self.client_ctx.outcome_or_updates(operation, operation_id, move || {
1475 stream! {
1476 yield InternalPayState::Funding;
1477
1478 let state = loop {
1479 match stream.next().await { Some(LightningClientStateMachines::InternalPay(state)) => {
1480 match state.state {
1481 IncomingSmStates::Preimage(preimage) => break InternalPayState::Preimage(preimage),
1482 IncomingSmStates::RefundSubmitted{ out_points, error } => {
1483 match client_ctx.await_primary_module_outputs(operation_id, out_points.clone()).await {
1484 Ok(()) => break InternalPayState::RefundSuccess { out_points, error },
1485 Err(e) => break InternalPayState::RefundError{ error_message: e.to_string(), error },
1486 }
1487 },
1488 IncomingSmStates::FundingFailed { error } => break InternalPayState::FundingFailed{ error },
1489 _ => {}
1490 }
1491 } _ => {
1492 break InternalPayState::UnexpectedError("Unexpected State! Expected an InternalPay state".to_string())
1493 }}
1494 };
1495 yield state;
1496 }
1497 }))
1498 }
1499
1500 pub async fn subscribe_ln_pay(
1503 &self,
1504 operation_id: OperationId,
1505 ) -> anyhow::Result<UpdateStreamOrOutcome<LnPayState>> {
1506 async fn get_next_pay_state(
1507 stream: &mut BoxStream<'_, LightningClientStateMachines>,
1508 ) -> Option<LightningPayStates> {
1509 match stream.next().await {
1510 Some(LightningClientStateMachines::LightningPay(state)) => Some(state.state),
1511 Some(event) => {
1512 error!(event = ?event, "Operation is not a lightning payment");
1514 debug_assert!(false, "Operation is not a lightning payment: {event:?}");
1515 None
1516 }
1517 None => None,
1518 }
1519 }
1520
1521 let operation = self.client_ctx.get_operation(operation_id).await?;
1522 let LightningOperationMetaVariant::Pay(LightningOperationMetaPay {
1523 out_point: _,
1524 invoice: _,
1525 change,
1526 is_internal_payment,
1527 ..
1528 }) = operation.meta::<LightningOperationMeta>().variant
1529 else {
1530 bail!("Operation is not a lightning payment")
1531 };
1532
1533 ensure!(
1534 !is_internal_payment,
1535 "Subscribing to an internal LN payment, expected external LN payment"
1536 );
1537
1538 let client_ctx = self.client_ctx.clone();
1539
1540 Ok(self.client_ctx.outcome_or_updates(operation, operation_id, move || {
1541 stream! {
1542 let self_ref = client_ctx.self_ref();
1543
1544 let mut stream = self_ref.notifier.subscribe(operation_id).await;
1545 let state = get_next_pay_state(&mut stream).await;
1546 match state {
1547 Some(LightningPayStates::CreatedOutgoingLnContract(_)) => {
1548 yield LnPayState::Created;
1549 }
1550 Some(LightningPayStates::FundingRejected) => {
1551 yield LnPayState::Canceled;
1552 return;
1553 }
1554 Some(state) => {
1555 yield LnPayState::UnexpectedError { error_message: format!("Found unexpected state during lightning payment: {state:?}") };
1556 return;
1557 }
1558 None => {
1559 error!("Unexpected end of lightning pay state machine");
1560 return;
1561 }
1562 }
1563
1564 let state = get_next_pay_state(&mut stream).await;
1565 match state {
1566 Some(LightningPayStates::Funded(funded)) => {
1567 yield LnPayState::Funded { block_height: funded.timelock }
1568 }
1569 Some(state) => {
1570 yield LnPayState::UnexpectedError { error_message: format!("Found unexpected state during lightning payment: {state:?}") };
1571 return;
1572 }
1573 _ => {
1574 error!("Unexpected end of lightning pay state machine");
1575 return;
1576 }
1577 }
1578
1579 let state = get_next_pay_state(&mut stream).await;
1580 match state {
1581 Some(LightningPayStates::Success(preimage)) => {
1582 if change.is_empty() {
1583 yield LnPayState::Success { preimage };
1584 } else {
1585 yield LnPayState::AwaitingChange;
1586 match client_ctx.await_primary_module_outputs(operation_id, change.clone()).await {
1587 Ok(()) => {
1588 yield LnPayState::Success { preimage };
1589 }
1590 Err(e) => {
1591 yield LnPayState::UnexpectedError { error_message: format!("Error occurred while waiting for the change: {e:?}") };
1592 }
1593 }
1594 }
1595 }
1596 Some(LightningPayStates::Refund(refund)) => {
1597 yield LnPayState::WaitingForRefund {
1598 error_reason: refund.error_reason.clone(),
1599 };
1600
1601 match client_ctx.await_primary_module_outputs(operation_id, refund.out_points).await {
1602 Ok(()) => {
1603 let gateway_error = GatewayPayError::GatewayInternalError { error_code: Some(500), error_message: refund.error_reason };
1604 yield LnPayState::Refunded { gateway_error };
1605 }
1606 Err(e) => {
1607 yield LnPayState::UnexpectedError {
1608 error_message: format!("Error occurred trying to get refund. Refund was not successful: {e:?}"),
1609 };
1610 }
1611 }
1612 }
1613 Some(state) => {
1614 yield LnPayState::UnexpectedError { error_message: format!("Found unexpected state during lightning payment: {state:?}") };
1615 }
1616 None => {
1617 error!("Unexpected end of lightning pay state machine");
1618 yield LnPayState::UnexpectedError { error_message: "Unexpected end of lightning pay state machine".to_string() };
1619 }
1620 }
1621 }
1622 }))
1623 }
1624
1625 #[deprecated(since = "0.7.0", note = "Use recurring payment functionality instead")]
1628 #[allow(deprecated)]
1629 pub async fn scan_receive_for_user_tweaked<M: Serialize + Send + Sync + Clone>(
1630 &self,
1631 key_pair: Keypair,
1632 indices: Vec<u64>,
1633 extra_meta: M,
1634 ) -> Vec<OperationId> {
1635 let mut claims = Vec::new();
1636 for i in indices {
1637 let key_pair_tweaked = tweak_user_secret_key(&self.secp, key_pair, i);
1638 match self
1639 .scan_receive_for_user(key_pair_tweaked, extra_meta.clone())
1640 .await
1641 {
1642 Ok(operation_id) => claims.push(operation_id),
1643 Err(err) => {
1644 error!(err = %err.fmt_compact_anyhow(), %i, "Failed to scan tweaked key at index i");
1645 }
1646 }
1647 }
1648
1649 claims
1650 }
1651
1652 #[deprecated(since = "0.7.0", note = "Use recurring payment functionality instead")]
1655 #[allow(deprecated)]
1656 pub async fn scan_receive_for_user<M: Serialize + Send + Sync>(
1657 &self,
1658 key_pair: Keypair,
1659 extra_meta: M,
1660 ) -> anyhow::Result<OperationId> {
1661 let preimage_key: [u8; 33] = key_pair.public_key().serialize();
1662 let preimage = sha256::Hash::hash(&preimage_key);
1663 let contract_id = ContractId::from_raw_hash(sha256::Hash::hash(&preimage.to_byte_array()));
1664 self.claim_funded_incoming_contract(key_pair, contract_id, extra_meta)
1665 .await
1666 }
1667
1668 #[deprecated(since = "0.7.0", note = "Use recurring payment functionality instead")]
1671 #[allow(deprecated)]
1672 pub async fn claim_funded_incoming_contract<M: Serialize + Send + Sync>(
1673 &self,
1674 key_pair: Keypair,
1675 contract_id: ContractId,
1676 extra_meta: M,
1677 ) -> anyhow::Result<OperationId> {
1678 let incoming_contract_account = get_incoming_contract(self.module_api.clone(), contract_id)
1679 .await?
1680 .ok_or(anyhow!("No contract account found"))
1681 .with_context(|| format!("No contract found for {contract_id:?}"))?;
1682
1683 let input = incoming_contract_account.claim();
1684 let client_input = ClientInput::<LightningInput> {
1685 input,
1686 amounts: Amounts::new_bitcoin(incoming_contract_account.amount),
1687 keys: vec![key_pair],
1688 };
1689
1690 let tx = TransactionBuilder::new().with_inputs(
1691 self.client_ctx
1692 .make_client_inputs(ClientInputBundle::new_no_sm(vec![client_input])),
1693 );
1694 let extra_meta = serde_json::to_value(extra_meta).expect("extra_meta is serializable");
1695 let operation_meta_gen = move |change_range: OutPointRange| LightningOperationMeta {
1696 variant: LightningOperationMetaVariant::Claim {
1697 out_points: change_range.into_iter().collect(),
1698 },
1699 extra_meta: extra_meta.clone(),
1700 };
1701 let operation_id = OperationId::new_random();
1702 self.client_ctx
1703 .finalize_and_submit_transaction(
1704 operation_id,
1705 LightningCommonInit::KIND.as_str(),
1706 operation_meta_gen,
1707 tx,
1708 )
1709 .await?;
1710 Ok(operation_id)
1711 }
1712
1713 pub async fn create_bolt11_invoice<M: Serialize + Send + Sync>(
1715 &self,
1716 amount: Amount,
1717 description: lightning_invoice::Bolt11InvoiceDescription,
1718 expiry_time: Option<u64>,
1719 extra_meta: M,
1720 gateway: Option<LightningGateway>,
1721 ) -> anyhow::Result<(OperationId, Bolt11Invoice, [u8; 32])> {
1722 let receiving_key =
1723 ReceivingKey::Personal(Keypair::new(&self.secp, &mut rand::rngs::OsRng));
1724 self.create_bolt11_invoice_internal(
1725 amount,
1726 description,
1727 expiry_time,
1728 receiving_key,
1729 extra_meta,
1730 gateway,
1731 )
1732 .await
1733 }
1734
1735 #[allow(clippy::too_many_arguments)]
1738 pub async fn create_bolt11_invoice_for_user_tweaked<M: Serialize + Send + Sync>(
1739 &self,
1740 amount: Amount,
1741 description: lightning_invoice::Bolt11InvoiceDescription,
1742 expiry_time: Option<u64>,
1743 user_key: PublicKey,
1744 index: u64,
1745 extra_meta: M,
1746 gateway: Option<LightningGateway>,
1747 ) -> anyhow::Result<(OperationId, Bolt11Invoice, [u8; 32])> {
1748 let tweaked_key = tweak_user_key(&self.secp, user_key, index);
1749 self.create_bolt11_invoice_for_user(
1750 amount,
1751 description,
1752 expiry_time,
1753 tweaked_key,
1754 extra_meta,
1755 gateway,
1756 )
1757 .await
1758 }
1759
1760 pub async fn create_bolt11_invoice_for_user<M: Serialize + Send + Sync>(
1762 &self,
1763 amount: Amount,
1764 description: lightning_invoice::Bolt11InvoiceDescription,
1765 expiry_time: Option<u64>,
1766 user_key: PublicKey,
1767 extra_meta: M,
1768 gateway: Option<LightningGateway>,
1769 ) -> anyhow::Result<(OperationId, Bolt11Invoice, [u8; 32])> {
1770 let receiving_key = ReceivingKey::External(user_key);
1771 self.create_bolt11_invoice_internal(
1772 amount,
1773 description,
1774 expiry_time,
1775 receiving_key,
1776 extra_meta,
1777 gateway,
1778 )
1779 .await
1780 }
1781
1782 async fn create_bolt11_invoice_internal<M: Serialize + Send + Sync>(
1784 &self,
1785 amount: Amount,
1786 description: lightning_invoice::Bolt11InvoiceDescription,
1787 expiry_time: Option<u64>,
1788 receiving_key: ReceivingKey,
1789 extra_meta: M,
1790 gateway: Option<LightningGateway>,
1791 ) -> anyhow::Result<(OperationId, Bolt11Invoice, [u8; 32])> {
1792 let gateway_id = gateway.as_ref().map(|g| g.gateway_id);
1793 let (src_node_id, short_channel_id, route_hints) = if let Some(current_gateway) = gateway {
1794 (
1795 current_gateway.node_pub_key,
1796 current_gateway.federation_index,
1797 current_gateway.route_hints,
1798 )
1799 } else {
1800 let markers = self.client_ctx.get_internal_payment_markers()?;
1802 (markers.0, markers.1, vec![])
1803 };
1804
1805 debug!(target: LOG_CLIENT_MODULE_LN, ?gateway_id, %amount, "Selected LN gateway for invoice generation");
1806
1807 let (operation_id, invoice, output, preimage) = self.create_lightning_receive_output(
1808 amount,
1809 description,
1810 receiving_key,
1811 rand::rngs::OsRng,
1812 expiry_time,
1813 src_node_id,
1814 short_channel_id,
1815 &route_hints,
1816 self.cfg.network.0,
1817 )?;
1818
1819 let tx =
1820 TransactionBuilder::new().with_outputs(self.client_ctx.make_client_outputs(output));
1821 let extra_meta = serde_json::to_value(extra_meta).expect("extra_meta is serializable");
1822 let operation_meta_gen = {
1823 let invoice = invoice.clone();
1824 move |change_range: OutPointRange| LightningOperationMeta {
1825 variant: LightningOperationMetaVariant::Receive {
1826 out_point: OutPoint {
1827 txid: change_range.txid(),
1828 out_idx: 0,
1829 },
1830 invoice: invoice.clone(),
1831 gateway_id,
1832 },
1833 extra_meta: extra_meta.clone(),
1834 }
1835 };
1836 let change_range = self
1837 .client_ctx
1838 .finalize_and_submit_transaction(
1839 operation_id,
1840 LightningCommonInit::KIND.as_str(),
1841 operation_meta_gen,
1842 tx,
1843 )
1844 .await?;
1845
1846 debug!(target: LOG_CLIENT_MODULE_LN, txid = ?change_range.txid(), ?operation_id, "Waiting for LN invoice to be confirmed");
1847
1848 self.client_ctx
1851 .transaction_updates(operation_id)
1852 .await
1853 .await_tx_accepted(change_range.txid())
1854 .await
1855 .map_err(|e| anyhow!("Offer transaction was not accepted: {e:?}"))?;
1856
1857 debug!(target: LOG_CLIENT_MODULE_LN, %invoice, "Invoice confirmed");
1858
1859 Ok((operation_id, invoice, preimage))
1860 }
1861
1862 #[deprecated(since = "0.7.0", note = "Use recurring payment functionality instead")]
1863 #[allow(deprecated)]
1864 pub async fn subscribe_ln_claim(
1865 &self,
1866 operation_id: OperationId,
1867 ) -> anyhow::Result<UpdateStreamOrOutcome<LnReceiveState>> {
1868 let operation = self.client_ctx.get_operation(operation_id).await?;
1869 let LightningOperationMetaVariant::Claim { out_points } =
1870 operation.meta::<LightningOperationMeta>().variant
1871 else {
1872 bail!("Operation is not a lightning claim")
1873 };
1874
1875 let client_ctx = self.client_ctx.clone();
1876
1877 Ok(self.client_ctx.outcome_or_updates(operation, operation_id, move || {
1878 stream! {
1879 yield LnReceiveState::AwaitingFunds;
1880
1881 if client_ctx.await_primary_module_outputs(operation_id, out_points).await.is_ok() {
1882 yield LnReceiveState::Claimed;
1883 } else {
1884 yield LnReceiveState::Canceled { reason: LightningReceiveError::ClaimRejected }
1885 }
1886 }
1887 }))
1888 }
1889
1890 pub async fn subscribe_ln_receive(
1891 &self,
1892 operation_id: OperationId,
1893 ) -> anyhow::Result<UpdateStreamOrOutcome<LnReceiveState>> {
1894 let operation = self.client_ctx.get_operation(operation_id).await?;
1895 let LightningOperationMetaVariant::Receive {
1896 out_point, invoice, ..
1897 } = operation.meta::<LightningOperationMeta>().variant
1898 else {
1899 bail!("Operation is not a lightning payment")
1900 };
1901
1902 let tx_accepted_future = self
1903 .client_ctx
1904 .transaction_updates(operation_id)
1905 .await
1906 .await_tx_accepted(out_point.txid);
1907
1908 let client_ctx = self.client_ctx.clone();
1909
1910 Ok(self.client_ctx.outcome_or_updates(operation, operation_id, move || {
1911 stream! {
1912
1913 let self_ref = client_ctx.self_ref();
1914
1915 yield LnReceiveState::Created;
1916
1917 if tx_accepted_future.await.is_err() {
1918 yield LnReceiveState::Canceled { reason: LightningReceiveError::Rejected };
1919 return;
1920 }
1921 yield LnReceiveState::WaitingForPayment { invoice: invoice.to_string(), timeout: invoice.expiry_time() };
1922
1923 match self_ref.await_receive_success(operation_id).await {
1924 Ok(()) => {
1925
1926 yield LnReceiveState::Funded;
1927
1928 if let Ok(out_points) = self_ref.await_claim_acceptance(operation_id).await {
1929 yield LnReceiveState::AwaitingFunds;
1930
1931 if client_ctx.await_primary_module_outputs(operation_id, out_points).await.is_ok() {
1932 yield LnReceiveState::Claimed;
1933 return;
1934 }
1935 }
1936
1937 yield LnReceiveState::Canceled { reason: LightningReceiveError::Rejected };
1938 }
1939 Err(e) => {
1940 yield LnReceiveState::Canceled { reason: e };
1941 }
1942 }
1943 }
1944 }))
1945 }
1946
1947 pub async fn get_gateway(
1951 &self,
1952 gateway_id: Option<secp256k1::PublicKey>,
1953 force_internal: bool,
1954 ) -> anyhow::Result<Option<LightningGateway>> {
1955 match gateway_id {
1956 Some(gateway_id) => {
1957 if let Some(gw) = self.select_gateway(&gateway_id).await {
1958 Ok(Some(gw))
1959 } else {
1960 self.update_gateway_cache().await?;
1963 Ok(self.select_gateway(&gateway_id).await)
1964 }
1965 }
1966 None if !force_internal => {
1967 self.update_gateway_cache().await?;
1969 let gateways = self.list_gateways().await;
1970 let gw = gateways.into_iter().choose(&mut OsRng).map(|gw| gw.info);
1971 if let Some(gw) = gw {
1972 let gw_id = gw.gateway_id;
1973 info!(%gw_id, "Using random gateway");
1974 Ok(Some(gw))
1975 } else {
1976 Err(anyhow!(
1977 "No gateways exist in gateway cache and `force_internal` is false"
1978 ))
1979 }
1980 }
1981 None => Ok(None),
1982 }
1983 }
1984
1985 pub async fn await_outgoing_payment(
1989 &self,
1990 operation_id: OperationId,
1991 ) -> anyhow::Result<LightningPaymentOutcome> {
1992 let operation = self.client_ctx.get_operation(operation_id).await?;
1993 let variant = operation.meta::<LightningOperationMeta>().variant;
1994 let LightningOperationMetaVariant::Pay(LightningOperationMetaPay {
1995 is_internal_payment,
1996 ..
1997 }) = variant
1998 else {
1999 bail!("Operation is not a lightning payment")
2000 };
2001
2002 let mut final_state = None;
2003
2004 if is_internal_payment {
2006 let updates = self.subscribe_internal_pay(operation_id).await?;
2007 let mut stream = updates.into_stream();
2008 while let Some(update) = stream.next().await {
2009 match update {
2010 InternalPayState::Preimage(preimage) => {
2011 final_state = Some(LightningPaymentOutcome::Success {
2012 preimage: preimage.0.consensus_encode_to_hex(),
2013 });
2014 }
2015 InternalPayState::RefundSuccess {
2016 out_points: _,
2017 error,
2018 } => {
2019 final_state = Some(LightningPaymentOutcome::Failure {
2020 error_message: format!("LNv1 internal payment was refunded: {error:?}"),
2021 });
2022 }
2023 InternalPayState::FundingFailed { error } => {
2024 final_state = Some(LightningPaymentOutcome::Failure {
2025 error_message: format!(
2026 "LNv1 internal payment funding failed: {error:?}"
2027 ),
2028 });
2029 }
2030 InternalPayState::RefundError {
2031 error_message,
2032 error,
2033 } => {
2034 final_state = Some(LightningPaymentOutcome::Failure {
2035 error_message: format!(
2036 "LNv1 refund failed: {error_message}: {error:?}"
2037 ),
2038 });
2039 }
2040 InternalPayState::UnexpectedError(error) => {
2041 final_state = Some(LightningPaymentOutcome::Failure {
2042 error_message: error,
2043 });
2044 }
2045 InternalPayState::Funding => {}
2046 }
2047 }
2048 } else {
2049 let updates = self.subscribe_ln_pay(operation_id).await?;
2050 let mut stream = updates.into_stream();
2051 while let Some(update) = stream.next().await {
2052 match update {
2053 LnPayState::Success { preimage } => {
2054 final_state = Some(LightningPaymentOutcome::Success { preimage });
2055 }
2056 LnPayState::Refunded { gateway_error } => {
2057 final_state = Some(LightningPaymentOutcome::Failure {
2058 error_message: format!(
2059 "LNv1 external payment was refunded: {gateway_error:?}"
2060 ),
2061 });
2062 }
2063 LnPayState::UnexpectedError { error_message } => {
2064 final_state = Some(LightningPaymentOutcome::Failure { error_message });
2065 }
2066 _ => {}
2067 }
2068 }
2069 }
2070
2071 final_state.ok_or(anyhow!(
2072 "Internal or external outgoing lightning payment did not reach a final state"
2073 ))
2074 }
2075}
2076
2077#[derive(Debug, Clone, Serialize, Deserialize)]
2080#[serde(rename_all = "snake_case")]
2081pub struct PayInvoiceResponse {
2082 operation_id: OperationId,
2083 contract_id: ContractId,
2084 preimage: String,
2085}
2086
2087#[allow(clippy::large_enum_variant)]
2088#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
2089pub enum LightningClientStateMachines {
2090 InternalPay(IncomingStateMachine),
2091 LightningPay(LightningPayStateMachine),
2092 Receive(LightningReceiveStateMachine),
2093}
2094
2095impl IntoDynInstance for LightningClientStateMachines {
2096 type DynType = DynState;
2097
2098 fn into_dyn(self, instance_id: ModuleInstanceId) -> Self::DynType {
2099 DynState::from_typed(instance_id, self)
2100 }
2101}
2102
2103impl State for LightningClientStateMachines {
2104 type ModuleContext = LightningClientContext;
2105
2106 fn transitions(
2107 &self,
2108 context: &Self::ModuleContext,
2109 global_context: &DynGlobalClientContext,
2110 ) -> Vec<StateTransition<Self>> {
2111 match self {
2112 LightningClientStateMachines::InternalPay(internal_pay_state) => {
2113 sm_enum_variant_translation!(
2114 internal_pay_state.transitions(context, global_context),
2115 LightningClientStateMachines::InternalPay
2116 )
2117 }
2118 LightningClientStateMachines::LightningPay(lightning_pay_state) => {
2119 sm_enum_variant_translation!(
2120 lightning_pay_state.transitions(context, global_context),
2121 LightningClientStateMachines::LightningPay
2122 )
2123 }
2124 LightningClientStateMachines::Receive(receive_state) => {
2125 sm_enum_variant_translation!(
2126 receive_state.transitions(context, global_context),
2127 LightningClientStateMachines::Receive
2128 )
2129 }
2130 }
2131 }
2132
2133 fn operation_id(&self) -> OperationId {
2134 match self {
2135 LightningClientStateMachines::InternalPay(internal_pay_state) => {
2136 internal_pay_state.operation_id()
2137 }
2138 LightningClientStateMachines::LightningPay(lightning_pay_state) => {
2139 lightning_pay_state.operation_id()
2140 }
2141 LightningClientStateMachines::Receive(receive_state) => receive_state.operation_id(),
2142 }
2143 }
2144}
2145
2146async fn fetch_and_validate_offer(
2147 module_api: &DynModuleApi,
2148 payment_hash: sha256::Hash,
2149 amount_msat: Amount,
2150) -> anyhow::Result<IncomingContractOffer, IncomingSmError> {
2151 let offer = timeout(Duration::from_secs(5), module_api.fetch_offer(payment_hash))
2152 .await
2153 .map_err(|_| IncomingSmError::TimeoutFetchingOffer { payment_hash })?
2154 .map_err(|e| IncomingSmError::FetchContractError {
2155 payment_hash,
2156 error_message: e.to_string(),
2157 })?;
2158
2159 if offer.amount > amount_msat {
2160 return Err(IncomingSmError::ViolatedFeePolicy {
2161 offer_amount: offer.amount,
2162 payment_amount: amount_msat,
2163 });
2164 }
2165 if offer.hash != payment_hash {
2166 return Err(IncomingSmError::InvalidOffer {
2167 offer_hash: offer.hash,
2168 payment_hash,
2169 });
2170 }
2171 Ok(offer)
2172}
2173
2174pub async fn create_incoming_contract_output(
2175 module_api: &DynModuleApi,
2176 payment_hash: sha256::Hash,
2177 amount_msat: Amount,
2178 redeem_key: &Keypair,
2179) -> Result<(LightningOutputV0, Amount, ContractId), IncomingSmError> {
2180 let offer = fetch_and_validate_offer(module_api, payment_hash, amount_msat).await?;
2181 let our_pub_key = secp256k1::PublicKey::from_keypair(redeem_key);
2182 let contract = IncomingContract {
2183 hash: offer.hash,
2184 encrypted_preimage: offer.encrypted_preimage.clone(),
2185 decrypted_preimage: DecryptedPreimage::Pending,
2186 gateway_key: our_pub_key,
2187 };
2188 let contract_id = contract.contract_id();
2189 let incoming_output = LightningOutputV0::Contract(ContractOutput {
2190 amount: offer.amount,
2191 contract: Contract::Incoming(contract),
2192 });
2193
2194 Ok((incoming_output, offer.amount, contract_id))
2195}
2196
2197#[derive(Debug, Encodable, Decodable, Serialize)]
2198pub struct OutgoingLightningPayment {
2199 pub payment_type: PayType,
2200 pub contract_id: ContractId,
2201 pub fee: Amount,
2202}
2203
2204async fn set_payment_result(
2205 dbtx: &mut DatabaseTransaction<'_>,
2206 payment_hash: sha256::Hash,
2207 payment_type: PayType,
2208 contract_id: ContractId,
2209 fee: Amount,
2210) {
2211 if let Some(mut payment_result) = dbtx.get_value(&PaymentResultKey { payment_hash }).await {
2212 payment_result.completed_payment = Some(OutgoingLightningPayment {
2213 payment_type,
2214 contract_id,
2215 fee,
2216 });
2217 dbtx.insert_entry(&PaymentResultKey { payment_hash }, &payment_result)
2218 .await;
2219 }
2220}
2221
2222pub fn tweak_user_key<Ctx: Verification + Signing>(
2225 secp: &Secp256k1<Ctx>,
2226 user_key: PublicKey,
2227 index: u64,
2228) -> PublicKey {
2229 let mut hasher = HmacEngine::<sha256::Hash>::new(&user_key.serialize()[..]);
2230 hasher.input(&index.to_be_bytes());
2231 let tweak = Hmac::from_engine(hasher).to_byte_array();
2232
2233 user_key
2234 .add_exp_tweak(secp, &Scalar::from_be_bytes(tweak).expect("can't fail"))
2235 .expect("tweak is always 32 bytes, other failure modes are negligible")
2236}
2237
2238fn tweak_user_secret_key<Ctx: Verification + Signing>(
2241 secp: &Secp256k1<Ctx>,
2242 key_pair: Keypair,
2243 index: u64,
2244) -> Keypair {
2245 let public_key = key_pair.public_key();
2246 let mut hasher = HmacEngine::<sha256::Hash>::new(&public_key.serialize()[..]);
2247 hasher.input(&index.to_be_bytes());
2248 let tweak = Hmac::from_engine(hasher).to_byte_array();
2249
2250 let secret_key = key_pair.secret_key();
2251 let sk_tweaked = secret_key
2252 .add_tweak(&Scalar::from_be_bytes(tweak).expect("Cant fail"))
2253 .expect("Cant fail");
2254 Keypair::from_secret_key(secp, &sk_tweaked)
2255}
2256
2257pub async fn get_invoice(
2259 info: &str,
2260 amount: Option<Amount>,
2261 lnurl_comment: Option<String>,
2262) -> anyhow::Result<Bolt11Invoice> {
2263 let info = info.trim();
2264 match lightning_invoice::Bolt11Invoice::from_str(info) {
2265 Ok(invoice) => {
2266 debug!("Parsed parameter as bolt11 invoice: {invoice}");
2267 match (invoice.amount_milli_satoshis(), amount) {
2268 (Some(_), Some(_)) => {
2269 bail!("Amount specified in both invoice and command line")
2270 }
2271 (None, _) => {
2272 bail!("We don't support invoices without an amount")
2273 }
2274 _ => {}
2275 }
2276 Ok(invoice)
2277 }
2278 Err(e) => {
2279 let lnurl = if info.to_lowercase().starts_with("lnurl") {
2280 lnurl::lnurl::LnUrl::from_str(info)?
2281 } else if info.contains('@') {
2282 lnurl::lightning_address::LightningAddress::from_str(info)?.lnurl()
2283 } else {
2284 bail!("Invalid invoice or lnurl: {e:?}");
2285 };
2286 debug!("Parsed parameter as lnurl: {lnurl:?}");
2287 let amount = amount.context("When using a lnurl, an amount must be specified")?;
2288 let async_client = lnurl::AsyncClient::from_client(reqwest::Client::new());
2289 let response = async_client.make_request(&lnurl.url).await?;
2290 match response {
2291 lnurl::LnUrlResponse::LnUrlPayResponse(response) => {
2292 let invoice = async_client
2293 .get_invoice(&response, amount.msats, None, lnurl_comment.as_deref())
2294 .await?;
2295 let invoice = Bolt11Invoice::from_str(invoice.invoice())?;
2296 let invoice_amount = invoice.amount_milli_satoshis();
2297 ensure!(
2298 invoice_amount == Some(amount.msats),
2299 "the amount generated by the lnurl ({invoice_amount:?}) is different from the requested amount ({amount}), try again using a different amount"
2300 );
2301 Ok(invoice)
2302 }
2303 other => {
2304 bail!("Unexpected response from lnurl: {other:?}");
2305 }
2306 }
2307 }
2308 }
2309}
2310
2311#[derive(Debug, Clone)]
2312pub struct LightningClientContext {
2313 pub ln_decoder: Decoder,
2314 pub redeem_key: Keypair,
2315 pub gateway_conn: Arc<dyn GatewayConnection + Send + Sync>,
2316}
2317
2318impl fedimint_client_module::sm::Context for LightningClientContext {
2319 const KIND: Option<ModuleKind> = Some(KIND);
2320}
2321
2322#[apply(async_trait_maybe_send!)]
2323pub trait GatewayConnection: std::fmt::Debug {
2324 async fn verify_gateway_availability(
2327 &self,
2328 gateway: &LightningGateway,
2329 ) -> Result<(), ServerError>;
2330
2331 async fn pay_invoice(
2333 &self,
2334 gateway: LightningGateway,
2335 payload: PayInvoicePayload,
2336 ) -> Result<String, GatewayPayError>;
2337}
2338
2339#[derive(Debug)]
2340pub struct RealGatewayConnection {
2341 pub api: GatewayApi,
2342}
2343
2344#[apply(async_trait_maybe_send!)]
2345impl GatewayConnection for RealGatewayConnection {
2346 async fn verify_gateway_availability(
2347 &self,
2348 gateway: &LightningGateway,
2349 ) -> Result<(), ServerError> {
2350 self.api
2351 .request::<PublicKey, serde_json::Value>(
2352 &gateway.api,
2353 Method::GET,
2354 GET_GATEWAY_ID_ENDPOINT,
2355 None,
2356 )
2357 .await?;
2358 Ok(())
2359 }
2360
2361 async fn pay_invoice(
2362 &self,
2363 gateway: LightningGateway,
2364 payload: PayInvoicePayload,
2365 ) -> Result<String, GatewayPayError> {
2366 let preimage: String = self
2367 .api
2368 .request(
2369 &gateway.api,
2370 Method::POST,
2371 PAY_INVOICE_ENDPOINT,
2372 Some(payload),
2373 )
2374 .await
2375 .map_err(|e| GatewayPayError::GatewayInternalError {
2376 error_code: None,
2377 error_message: e.to_string(),
2378 })?;
2379 let length = preimage.len();
2380 Ok(preimage[1..length - 1].to_string())
2381 }
2382}
2383
2384#[derive(Debug)]
2385pub struct MockGatewayConnection;
2386
2387#[apply(async_trait_maybe_send!)]
2388impl GatewayConnection for MockGatewayConnection {
2389 async fn verify_gateway_availability(
2390 &self,
2391 _gateway: &LightningGateway,
2392 ) -> Result<(), ServerError> {
2393 Ok(())
2394 }
2395
2396 async fn pay_invoice(
2397 &self,
2398 _gateway: LightningGateway,
2399 _payload: PayInvoicePayload,
2400 ) -> Result<String, GatewayPayError> {
2401 Ok("00000000".to_string())
2403 }
2404}