@@ -705,18 +705,20 @@ impl<'a, 'b, L: Deref> WithChannelDetails<'a, 'b, L> where L::Target: Logger {
705705#[ cfg( test) ]
706706mod test {
707707 use super :: * ;
708+ use std:: time:: SystemTime ;
708709 use core:: time:: Duration ;
709710 use lightning_invoice:: { Currency , Description , Bolt11InvoiceDescriptionRef , SignOrCreationError , CreationError } ;
711+ use bitcoin:: key:: Secp256k1 ;
710712 use bitcoin:: hashes:: { Hash , sha256} ;
711713 use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
712714 use bitcoin:: network:: Network ;
713715 use crate :: sign:: PhantomKeysManager ;
714- use crate :: events:: { MessageSendEvent , MessageSendEventsProvider } ;
716+ use crate :: events:: { Event , MessageSendEvent , MessageSendEventsProvider } ;
715717 use crate :: types:: payment:: { PaymentHash , PaymentPreimage } ;
716718 use crate :: ln:: channelmanager:: { Bolt11InvoiceParameters , PhantomRouteHints , MIN_FINAL_CLTV_EXPIRY_DELTA , PaymentId , RecipientOnionFields , Retry } ;
717719 use crate :: ln:: functional_test_utils:: * ;
718720 use crate :: ln:: msgs:: ChannelMessageHandler ;
719- use crate :: routing:: router:: { PaymentParameters , RouteParameters } ;
721+ use crate :: routing:: router:: { PaymentParameters , RouteParameters , RouteParametersConfig } ;
720722 use crate :: util:: test_utils;
721723 use crate :: util:: config:: UserConfig ;
722724 use std:: collections:: HashSet ;
@@ -750,7 +752,7 @@ mod test {
750752
751753
752754 #[ test]
753- fn test_from_channelmanager ( ) {
755+ fn create_and_pay_for_bolt11_invoice ( ) {
754756 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
755757 let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
756758 let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
@@ -784,17 +786,10 @@ mod test {
784786 assert_eq ! ( invoice. route_hints( ) [ 0 ] . 0 [ 0 ] . htlc_minimum_msat, chan. inbound_htlc_minimum_msat) ;
785787 assert_eq ! ( invoice. route_hints( ) [ 0 ] . 0 [ 0 ] . htlc_maximum_msat, chan. inbound_htlc_maximum_msat) ;
786788
787- let payment_params = PaymentParameters :: from_node_id ( invoice. recover_payee_pub_key ( ) ,
788- invoice. min_final_cltv_expiry_delta ( ) as u32 )
789- . with_bolt11_features ( invoice. features ( ) . unwrap ( ) . clone ( ) ) . unwrap ( )
790- . with_route_hints ( invoice. route_hints ( ) ) . unwrap ( ) ;
791- let route_params = RouteParameters :: from_payment_params_and_value (
792- payment_params, invoice. amount_milli_satoshis ( ) . unwrap ( ) ) ;
793789 let payment_event = {
794- let payment_hash = PaymentHash ( invoice. payment_hash ( ) . to_byte_array ( ) ) ;
795- nodes[ 0 ] . node . send_payment ( payment_hash,
796- RecipientOnionFields :: secret_only ( * invoice. payment_secret ( ) ) ,
797- PaymentId ( payment_hash. 0 ) , route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
790+ nodes[ 0 ] . node . pay_for_bolt11_invoice (
791+ & invoice, PaymentId ( [ 42 ; 32 ] ) , None , RouteParametersConfig :: default ( ) ,
792+ Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
798793 check_added_monitors ( & nodes[ 0 ] , 1 ) ;
799794
800795 let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -808,6 +803,111 @@ mod test {
808803 assert_eq ! ( events. len( ) , 2 ) ;
809804 }
810805
806+ #[ test]
807+ fn payment_metadata_end_to_end_for_invoice_with_amount ( ) {
808+ // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
809+ // the way out through the `PaymentClaimable` event.
810+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
811+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
812+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
813+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
814+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
815+
816+ let payment_metadata = vec ! [ 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 42 ] ;
817+
818+ let ( payment_hash, payment_secret) =
819+ nodes[ 1 ] . node . create_inbound_payment ( None , 7200 , None ) . unwrap ( ) ;
820+
821+ let secp_ctx = Secp256k1 :: new ( ) ;
822+ let node_secret = nodes[ 1 ] . keys_manager . backing . get_node_secret_key ( ) ;
823+ let timestamp = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
824+ let invoice = InvoiceBuilder :: new ( Currency :: Bitcoin )
825+ . description ( "test" . into ( ) )
826+ . payment_hash ( Sha256 :: from_slice ( & payment_hash. 0 ) . unwrap ( ) )
827+ . payment_secret ( payment_secret)
828+ . duration_since_epoch ( timestamp)
829+ . min_final_cltv_expiry_delta ( 144 )
830+ . amount_milli_satoshis ( 50_000 )
831+ . payment_metadata ( payment_metadata. clone ( ) )
832+ . build_signed ( |hash| secp_ctx. sign_ecdsa_recoverable ( hash, & node_secret) )
833+ . unwrap ( ) ;
834+
835+ assert ! ( nodes[ 0 ] . node. pay_for_bolt11_invoice( & invoice, PaymentId ( payment_hash. 0 ) , Some ( 100 ) , RouteParametersConfig :: default ( ) , Retry :: Attempts ( 0 ) ) . is_err( ) ) ;
836+
837+ nodes[ 0 ] . node . pay_for_bolt11_invoice (
838+ & invoice, PaymentId ( payment_hash. 0 ) , None ,
839+ RouteParametersConfig :: default ( ) , Retry :: Attempts ( 0 )
840+ ) . unwrap ( ) ;
841+
842+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
843+ let send_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
844+ nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & send_event. msgs [ 0 ] ) ;
845+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , & send_event. commitment_msg, false ) ;
846+
847+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
848+
849+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
850+ assert_eq ! ( events. len( ) , 1 ) ;
851+ match events. pop ( ) . unwrap ( ) {
852+ Event :: PaymentClaimable { onion_fields, .. } => {
853+ assert_eq ! ( Some ( payment_metadata) , onion_fields. unwrap( ) . payment_metadata) ;
854+ } ,
855+ _ => panic ! ( "Unexpected event" ) ,
856+ }
857+ }
858+
859+ #[ test]
860+ fn payment_metadata_end_to_end_for_invoice_with_no_amount ( ) {
861+ // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
862+ // the way out through the `PaymentClaimable` event.
863+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
864+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
865+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
866+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
867+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
868+
869+ let payment_metadata = vec ! [ 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 42 ] ;
870+
871+ let ( payment_hash, payment_secret) =
872+ nodes[ 1 ] . node . create_inbound_payment ( None , 7200 , None ) . unwrap ( ) ;
873+
874+ let secp_ctx = Secp256k1 :: new ( ) ;
875+ let node_secret = nodes[ 1 ] . keys_manager . backing . get_node_secret_key ( ) ;
876+ let timestamp = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
877+ let invoice = InvoiceBuilder :: new ( Currency :: Bitcoin )
878+ . description ( "test" . into ( ) )
879+ . payment_hash ( Sha256 :: from_slice ( & payment_hash. 0 ) . unwrap ( ) )
880+ . payment_secret ( payment_secret)
881+ . duration_since_epoch ( timestamp)
882+ . min_final_cltv_expiry_delta ( 144 )
883+ . payment_metadata ( payment_metadata. clone ( ) )
884+ . build_signed ( |hash| secp_ctx. sign_ecdsa_recoverable ( hash, & node_secret) )
885+ . unwrap ( ) ;
886+
887+ assert ! ( nodes[ 0 ] . node. pay_for_bolt11_invoice( & invoice, PaymentId ( payment_hash. 0 ) , None , RouteParametersConfig :: default ( ) , Retry :: Attempts ( 0 ) ) . is_err( ) ) ;
888+
889+ nodes[ 0 ] . node . pay_for_bolt11_invoice (
890+ & invoice, PaymentId ( payment_hash. 0 ) , Some ( 50_000 ) ,
891+ RouteParametersConfig :: default ( ) , Retry :: Attempts ( 0 )
892+ ) . unwrap ( ) ;
893+
894+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
895+ let send_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
896+ nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & send_event. msgs [ 0 ] ) ;
897+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , & send_event. commitment_msg, false ) ;
898+
899+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
900+
901+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
902+ assert_eq ! ( events. len( ) , 1 ) ;
903+ match events. pop ( ) . unwrap ( ) {
904+ Event :: PaymentClaimable { onion_fields, .. } => {
905+ assert_eq ! ( Some ( payment_metadata) , onion_fields. unwrap( ) . payment_metadata) ;
906+ } ,
907+ _ => panic ! ( "Unexpected event" ) ,
908+ }
909+ }
910+
811911 fn do_create_invoice_min_final_cltv_delta ( with_custom_delta : bool ) {
812912 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
813913 let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
0 commit comments