Skip to content

Commit be6e8c2

Browse files
committed
Implement and yield PaymentClaimable events
1 parent 018a5b6 commit be6e8c2

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ interface Event {
230230
PaymentSuccessful(PaymentId? payment_id, PaymentHash payment_hash, u64? fee_paid_msat);
231231
PaymentFailed(PaymentId? payment_id, PaymentHash payment_hash, PaymentFailureReason? reason);
232232
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat);
233+
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline);
233234
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
234235
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
235236
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);

src/event.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ pub enum Event {
8484
/// The value, in thousandths of a satoshi, that has been received.
8585
amount_msat: u64,
8686
},
87+
/// A payment for a previously-registered payment hash has been received.
88+
///
89+
/// This needs to be manually claimed by supplying the correct preimage to [`claim_for_hash`].
90+
///
91+
/// If the the provided parameters don't match the expectations or the preimage can't be
92+
/// retrieved in time, should be failed-back via [`fail_for_hash`].
93+
///
94+
/// Note claiming will necessarily fail after the `claim_deadline` has been reached.
95+
///
96+
/// [`claim_for_hash`]: crate::payment::Bolt11Payment::claim_for_hash
97+
/// [`fail_for_hash`]: crate::payment::Bolt11Payment::fail_for_hash
98+
PaymentClaimable {
99+
/// A local identifier used to track the payment.
100+
payment_id: PaymentId,
101+
/// The hash of the payment.
102+
payment_hash: PaymentHash,
103+
/// The value, in thousandths of a satoshi, that is claimable.
104+
claimable_amount_msat: u64,
105+
/// The block height at which this payment will be failed back and will no longer be
106+
/// eligible for claiming.
107+
claim_deadline: Option<u32>,
108+
},
87109
/// A channel has been created and is pending confirmation on-chain.
88110
ChannelPending {
89111
/// The `channel_id` of the channel.
@@ -156,6 +178,12 @@ impl_writeable_tlv_based_enum!(Event,
156178
(1, counterparty_node_id, option),
157179
(2, user_channel_id, required),
158180
(3, reason, upgradable_option),
181+
},
182+
(6, PaymentClaimable) => {
183+
(0, payment_hash, required),
184+
(2, payment_id, required),
185+
(4, claimable_amount_msat, required),
186+
(6, claim_deadline, option),
159187
};
160188
);
161189

@@ -434,7 +462,7 @@ where
434462
receiver_node_id: _,
435463
via_channel_id: _,
436464
via_user_channel_id: _,
437-
claim_deadline: _,
465+
claim_deadline,
438466
onion_fields: _,
439467
counterparty_skimmed_fee_msat,
440468
} => {
@@ -500,6 +528,38 @@ where
500528
});
501529
return;
502530
}
531+
532+
// If this is known by the store but ChannelManager doesn't know the preimage,
533+
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
534+
// user interaction.
535+
match info.kind {
536+
PaymentKind::Bolt11 { preimage, .. } => {
537+
if purpose.preimage().is_none() {
538+
debug_assert!(
539+
preimage.is_none(),
540+
"We would have registered the preimage if we knew"
541+
);
542+
543+
self.event_queue
544+
.add_event(Event::PaymentClaimable {
545+
payment_id,
546+
payment_hash,
547+
claimable_amount_msat: amount_msat,
548+
claim_deadline,
549+
})
550+
.unwrap_or_else(|e| {
551+
log_error!(
552+
self.logger,
553+
"Failed to push to event queue: {}",
554+
e
555+
);
556+
panic!("Failed to push to event queue");
557+
});
558+
return;
559+
}
560+
},
561+
_ => {},
562+
}
503563
}
504564

505565
log_info!(

0 commit comments

Comments
 (0)