@@ -188,6 +188,24 @@ struct ClaimableHTLC {
188
188
total_msat : u64 ,
189
189
}
190
190
191
+ /// An identifier used to uniquely identify an intercepted htlc to LDK.
192
+ /// (C-not exported) as we just use [u8; 32] directly
193
+ #[ derive( Hash , Copy , Clone , PartialEq , Eq , Debug ) ]
194
+ pub struct InterceptId ( pub [ u8 ; 32 ] ) ;
195
+
196
+ impl Writeable for InterceptId {
197
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
198
+ self . 0 . write ( w)
199
+ }
200
+ }
201
+
202
+ impl Readable for InterceptId {
203
+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
204
+ let buf: [ u8 ; 32 ] = Readable :: read ( r) ?;
205
+ Ok ( InterceptId ( buf) )
206
+ }
207
+ }
208
+
191
209
/// A payment identifier used to uniquely identify a payment to LDK.
192
210
/// (C-not exported) as we just use [u8; 32] directly
193
211
#[ derive( Hash , Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -722,6 +740,10 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
722
740
/// Locked *after* channel_state.
723
741
pending_outbound_payments : Mutex < HashMap < PaymentId , PendingOutboundPayment > > ,
724
742
743
+ /// Storage for HTLCForwardInfo's that have been intercepted and bubbled up to the user.
744
+ /// We hold them here until the user tells us what we should to with them.
745
+ pending_intercepted_payments : Mutex < HashMap < InterceptId , HTLCForwardInfo > > ,
746
+
725
747
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
726
748
/// and some closed channels which reached a usable state prior to being closed. This is used
727
749
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1586,6 +1608,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1586
1608
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1587
1609
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1588
1610
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1611
+ pending_intercepted_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1589
1612
1590
1613
our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
1591
1614
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ) ,
@@ -3055,6 +3078,59 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3055
3078
Ok ( ( ) )
3056
3079
}
3057
3080
3081
+ /// Fails the intercepted payment indicated by intercept_id. This should really only be called in response
3082
+ /// to a PaymentIntercepted event
3083
+ pub fn fail_intercepted_payment ( & self , intercept_id : InterceptId ) {
3084
+ let pending_intercept = {
3085
+ let mut pending_intercepts = self . pending_intercepted_payments . lock ( ) . unwrap ( ) ;
3086
+ pending_intercepts. remove ( & intercept_id)
3087
+ } ;
3088
+
3089
+ if let Some ( _payment) = pending_intercept {
3090
+ // TODO: what's best way to fail this? in `process_pending_htlc_forwards` it uses that `fail_forward` macro
3091
+ // awkward way could be to just use the forward_intercepted_payment code and pick a random scid
3092
+ }
3093
+ }
3094
+
3095
+ /// Attempts to forward an intercepted payment over the provided scid and with the provided amt_to_forward.
3096
+ /// Should only really be called in response to a PaymentIntercepted event
3097
+ pub fn forward_intercepted_payment ( & self , intercept_id : InterceptId , scid : u64 , amt_to_forward : u64 ) -> Result < ( ) , APIError > {
3098
+ let pending_intercept = {
3099
+ let mut pending_intercepts = self . pending_intercepted_payments . lock ( ) . unwrap ( ) ;
3100
+ pending_intercepts. remove ( & intercept_id)
3101
+ } ;
3102
+
3103
+ match pending_intercept {
3104
+ None => Err ( APIError :: APIMisuseError { err : "Payment with that InterceptId not found" . to_string ( ) } ) ,
3105
+ Some ( payment) => {
3106
+ match payment {
3107
+ HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info, prev_funding_outpoint } => {
3108
+
3109
+ let routing = match forward_info. routing {
3110
+ PendingHTLCRouting :: Forward { onion_packet, .. } => {
3111
+ PendingHTLCRouting :: Forward { onion_packet, short_channel_id : scid }
3112
+ } ,
3113
+ _ => forward_info. routing
3114
+ } ;
3115
+
3116
+ let pending_htlc_info = PendingHTLCInfo {
3117
+ amt_to_forward,
3118
+ routing,
3119
+ ..forward_info
3120
+ } ;
3121
+
3122
+ let mut per_source_pending_forward = vec ! [ ( prev_short_channel_id, prev_funding_outpoint, vec![ ( pending_htlc_info, prev_htlc_id) ] ) ] ;
3123
+
3124
+ self . forward_htlcs ( & mut per_source_pending_forward) ;
3125
+
3126
+ Ok ( ( ) )
3127
+ } ,
3128
+ _ => Err ( APIError :: APIMisuseError { err : "impossible" . to_string ( ) } )
3129
+ }
3130
+ }
3131
+ }
3132
+ }
3133
+
3058
3134
/// Processes HTLCs which are pending waiting on random forward delay.
3059
3135
///
3060
3136
/// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -3078,7 +3154,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3078
3154
for forward_info in pending_forwards. drain ( ..) {
3079
3155
match forward_info {
3080
3156
HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
3081
- routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value } ,
3157
+ ref routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value } ,
3082
3158
prev_funding_outpoint } => {
3083
3159
macro_rules! fail_forward {
3084
3160
( $msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr) => {
@@ -3125,6 +3201,24 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3125
3201
} ,
3126
3202
_ => panic ! ( ) ,
3127
3203
}
3204
+ } else if fake_scid:: is_valid_intercept ( & self . fake_scid_rand_bytes , short_chan_id) {
3205
+ let intercept_id = InterceptId ( Sha256 :: hash ( & incoming_shared_secret) . into_inner ( ) ) ;
3206
+ let mut pending_intercepts = self . pending_intercepted_payments . lock ( ) . unwrap ( ) ;
3207
+ match pending_intercepts. entry ( intercept_id) {
3208
+ hash_map:: Entry :: Vacant ( entry) => {
3209
+ entry. insert ( forward_info) ;
3210
+ new_events. push ( events:: Event :: PaymentIntercepted {
3211
+ short_channel_id : short_chan_id,
3212
+ payment_hash,
3213
+ inbound_amount_msats : 0 ,
3214
+ expected_outbound_amount_msats : amt_to_forward,
3215
+ intercept_id
3216
+ } ) ;
3217
+ } ,
3218
+ hash_map:: Entry :: Occupied ( _) => {
3219
+ fail_forward ! ( format!( "Unknown short channel id {} for forward HTLC" , short_chan_id) , 0x4000 | 10 , Vec :: new( ) , None ) ;
3220
+ }
3221
+ }
3128
3222
} else {
3129
3223
fail_forward ! ( format!( "Unknown short channel id {} for forward HTLC" , short_chan_id) , 0x4000 | 10 , Vec :: new( ) , None ) ;
3130
3224
}
@@ -5494,6 +5588,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
5494
5588
inbound_payment:: get_payment_preimage ( payment_hash, payment_secret, & self . inbound_payment_key )
5495
5589
}
5496
5590
5591
+ /// Gets a fake short channel id for use in receiving [intercepted payments]. These fake scids
5592
+ /// are used when constructing the route hints for payments intended to be intercepted.
5593
+ pub fn get_intercept_scid ( & self ) -> u64 {
5594
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
5595
+ let best_block = self . best_block . read ( ) . unwrap ( ) ;
5596
+ loop {
5597
+ let scid_candidate = fake_scid:: Namespace :: Intercept . get_fake_scid ( best_block. height ( ) , & self . genesis_hash , & self . fake_scid_rand_bytes , & self . keys_manager ) ;
5598
+ // Ensure the generated scid doesn't conflict with a real channel.
5599
+ match channel_state. short_to_id . entry ( scid_candidate) {
5600
+ hash_map:: Entry :: Occupied ( _) => continue ,
5601
+ hash_map:: Entry :: Vacant ( _) => return scid_candidate
5602
+ }
5603
+ }
5604
+ }
5605
+
5497
5606
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
5498
5607
/// are used when constructing the phantom invoice's route hints.
5499
5608
///
@@ -7209,6 +7318,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7209
7318
inbound_payment_key : expanded_inbound_key,
7210
7319
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
7211
7320
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7321
+ pending_intercepted_payments : Mutex :: new ( HashMap :: new ( ) ) ,
7212
7322
7213
7323
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7214
7324
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
0 commit comments