11use anyhow:: { anyhow, bail, Context } ;
2+ use bitcoin:: hashes:: { hex:: FromHex , sha256, Hash } ;
23use chrono:: { Duration , Utc } ;
34use cln_lsps:: jsonrpc:: client:: JsonRpcClient ;
45use cln_lsps:: lsps0:: primitives:: Msat ;
@@ -8,7 +9,8 @@ use cln_lsps::lsps0::{
89} ;
910use cln_lsps:: lsps2:: cln:: tlv:: encode_tu64;
1011use cln_lsps:: lsps2:: cln:: {
11- HtlcAcceptedRequest , HtlcAcceptedResponse , TLV_FORWARD_AMT , TLV_PAYMENT_SECRET ,
12+ HtlcAcceptedRequest , HtlcAcceptedResponse , InvoicePaymentRequest , TLV_FORWARD_AMT ,
13+ TLV_PAYMENT_SECRET ,
1214} ;
1315use cln_lsps:: lsps2:: model:: {
1416 compute_opening_fee, Lsps2BuyRequest , Lsps2BuyResponse , Lsps2GetInfoRequest ,
@@ -80,6 +82,7 @@ async fn main() -> Result<(), anyhow::Error> {
8082 "Requests a new jit channel from LSP and returns the matching invoice" ,
8183 on_lsps_jitchannel,
8284 )
85+ . hook ( "invoice_payment" , on_invoice_payment)
8386 . hook ( "htlc_accepted" , on_htlc_accepted)
8487 . hook ( "openchannel" , on_openchannel)
8588 . configure ( )
@@ -251,7 +254,7 @@ async fn on_lsps_lsps2_approve(
251254) -> Result < serde_json:: Value , anyhow:: Error > {
252255 let req: ClnRpcLsps2Approve = serde_json:: from_value ( v) ?;
253256 let ds_rec = DatastoreRecord {
254- jit_channel_scid : req. jit_channel_scid ,
257+ jit_channel_scid : req. jit_channel_scid . clone ( ) ,
255258 client_trusts_lsp : req. client_trusts_lsp . unwrap_or_default ( ) ,
256259 } ;
257260 let ds_rec_json = serde_json:: to_string ( & ds_rec) ?;
@@ -489,6 +492,29 @@ async fn on_lsps_jitchannel(
489492 Ok ( serde_json:: to_value ( out) ?)
490493}
491494
495+ async fn on_invoice_payment (
496+ p : cln_plugin:: Plugin < State > ,
497+ v : serde_json:: Value ,
498+ ) -> Result < serde_json:: Value , anyhow:: Error > {
499+ let req: InvoicePaymentRequest = serde_json:: from_value ( v) . context ( "invalid hook request" ) ?;
500+ let preimage = <[ u8 ; 32 ] >:: from_hex ( & req. payment . preimage ) . context ( "invalid preimage hex" ) ?;
501+ let hash = payment_hash ( & preimage) ;
502+
503+ // Delete DS-entries.
504+ let dir = p. configuration ( ) . lightning_dir ;
505+ let rpc_path = Path :: new ( & dir) . join ( & p. configuration ( ) . rpc_file ) ;
506+ let mut cln_client = cln_rpc:: ClnRpc :: new ( rpc_path. clone ( ) ) . await ?;
507+ cln_client
508+ . call_typed ( & DeldatastoreRequest {
509+ key : vec ! [ "lsps" . to_string( ) , "invoice" . to_string( ) , hash. to_string( ) ] ,
510+ generation : None ,
511+ } )
512+ . await
513+ . ok ( ) ;
514+
515+ Ok ( serde_json:: json!( { "result" : "continue" } ) )
516+ }
517+
492518async fn on_htlc_accepted (
493519 p : cln_plugin:: Plugin < State > ,
494520 v : serde_json:: Value ,
@@ -761,6 +787,10 @@ pub fn gen_rand_preimage_hex<R: Rng + CryptoRng>(rng: &mut R) -> String {
761787 hex:: encode ( & pre)
762788}
763789
790+ pub fn payment_hash ( preimage : & [ u8 ] ) -> sha256:: Hash {
791+ sha256:: Hash :: hash ( preimage)
792+ }
793+
764794#[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
765795struct LspsBuyJitChannelResponse {
766796 bolt11 : String ,
0 commit comments