@@ -13,6 +13,7 @@ use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
13
13
use lightning:: ln:: channelmanager:: { ChannelDetails , ChannelManager , PaymentId , PaymentSendFailure , MIN_FINAL_CLTV_EXPIRY } ;
14
14
#[ cfg( feature = "std" ) ]
15
15
use lightning:: ln:: channelmanager:: { PhantomRouteHints , MIN_CLTV_EXPIRY_DELTA } ;
16
+ use lightning:: ln:: inbound_payment:: { create, create_from_hash, ExpandedKey } ;
16
17
use lightning:: ln:: msgs:: LightningError ;
17
18
use lightning:: routing:: scoring:: Score ;
18
19
use lightning:: routing:: network_graph:: { NetworkGraph , RoutingFees } ;
@@ -38,29 +39,28 @@ use sync::Mutex;
38
39
/// may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
39
40
/// down
40
41
///
41
- /// `payment_hash` and `payment_secret` can come from [`ChannelManager::create_inbound_payment`] or
42
+ /// `payment_hash` can come from [`ChannelManager::create_inbound_payment`] or
42
43
/// [`ChannelManager::create_inbound_payment_for_hash`]. These values can be retrieved from any
43
- /// participating node. Alternatively, [`inbound_payment::create`] or
44
- /// [`inbound_payment::create_from_hash`] may be used to retrieve these values without a
45
- /// `ChannelManager`.
44
+ /// participating node. If `None` is provided for `payment_hash`, then one will be created.
45
+ ///
46
+ /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
47
+ /// in excess of the current time.
46
48
///
47
49
/// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
48
50
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
49
51
/// requirement).
50
52
///
51
53
/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
52
54
/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
53
- /// [`inbound_payment::create`]: lightning::ln::inbound_payment::create
54
- /// [`inbound_payment::create_from_hash`]: lightning::ln::inbound_payment::create_from_hash
55
55
/// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
56
56
pub fn create_phantom_invoice < Signer : Sign , K : Deref > (
57
- amt_msat : Option < u64 > , description : String , payment_hash : PaymentHash , payment_secret : PaymentSecret ,
57
+ amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , description : String , invoice_expiry_delta_secs : u32 ,
58
58
phantom_route_hints : Vec < PhantomRouteHints > , keys_manager : K , network : Currency ,
59
59
) -> Result < Invoice , SignOrCreationError < ( ) > > where K :: Target : KeysInterface {
60
60
let description = Description :: new ( description) . map_err ( SignOrCreationError :: CreationError ) ?;
61
61
let description = InvoiceDescription :: Direct ( & description, ) ;
62
62
_create_phantom_invoice :: < Signer , K > (
63
- amt_msat, description , payment_hash , payment_secret , phantom_route_hints, keys_manager, network,
63
+ amt_msat, payment_hash , description , invoice_expiry_delta_secs , phantom_route_hints, keys_manager, network,
64
64
)
65
65
}
66
66
@@ -80,42 +80,39 @@ pub fn create_phantom_invoice<Signer: Sign, K: Deref>(
80
80
///
81
81
/// `description_hash` is a SHA-256 hash of the description text
82
82
///
83
- /// `payment_hash` and `payment_secret` can come from [`ChannelManager::create_inbound_payment`] or
83
+ /// `payment_hash` can come from [`ChannelManager::create_inbound_payment`] or
84
84
/// [`ChannelManager::create_inbound_payment_for_hash`]. These values can be retrieved from any
85
- /// participating node. Alternatively, [`inbound_payment::create`] or
86
- /// [`inbound_payment::create_from_hash`] may be used to retrieve these values without a
87
- /// `ChannelManager`.
85
+ /// participating node. If `None` is provided for `payment_hash`, then one will be created.
86
+ ///
87
+ /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
88
+ /// in excess of the current time.
88
89
///
89
90
/// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
90
91
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
91
92
/// requirement).
92
93
///
93
94
/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
94
95
/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
95
- /// [`inbound_payment::create`]: lightning::ln::inbound_payment::create
96
- /// [`inbound_payment::create_from_hash`]: lightning::ln::inbound_payment::create_from_hash
97
96
/// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
98
97
pub fn create_phantom_invoice_with_description_hash < Signer : Sign , K : Deref > (
99
- amt_msat : Option < u64 > , description_hash : Sha256 , payment_hash : PaymentHash ,
100
- payment_secret : PaymentSecret , phantom_route_hints : Vec < PhantomRouteHints > ,
101
- keys_manager : K , network : Currency ,
98
+ amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , invoice_expiry_delta_secs : u32 ,
99
+ description_hash : Sha256 , phantom_route_hints : Vec < PhantomRouteHints > , keys_manager : K , network : Currency ,
102
100
) -> Result < Invoice , SignOrCreationError < ( ) > > where K :: Target : KeysInterface
103
101
{
104
-
105
102
_create_phantom_invoice :: < Signer , K > (
106
- amt_msat,
107
- InvoiceDescription :: Hash ( & description_hash) ,
108
- payment_hash, payment_secret, phantom_route_hints, keys_manager, network,
103
+ amt_msat, payment_hash, InvoiceDescription :: Hash ( & description_hash) ,
104
+ invoice_expiry_delta_secs, phantom_route_hints, keys_manager, network,
109
105
)
110
106
}
111
107
112
108
#[ cfg( feature = "std" ) ]
113
109
fn _create_phantom_invoice < Signer : Sign , K : Deref > (
114
- amt_msat : Option < u64 > , description : InvoiceDescription , payment_hash : PaymentHash ,
115
- payment_secret : PaymentSecret , phantom_route_hints : Vec < PhantomRouteHints > ,
116
- keys_manager : K , network : Currency ,
110
+ amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , description : InvoiceDescription ,
111
+ invoice_expiry_delta_secs : u32 , phantom_route_hints : Vec < PhantomRouteHints > , keys_manager : K , network : Currency ,
117
112
) -> Result < Invoice , SignOrCreationError < ( ) > > where K :: Target : KeysInterface
118
113
{
114
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
115
+
119
116
if phantom_route_hints. len ( ) == 0 {
120
117
return Err ( SignOrCreationError :: CreationError (
121
118
CreationError :: MissingRouteHints ,
@@ -128,6 +125,34 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
128
125
InvoiceDescription :: Hash ( hash) => InvoiceBuilder :: new ( network) . description_hash ( hash. 0 ) ,
129
126
} ;
130
127
128
+ let keys = ExpandedKey :: new ( & keys_manager. get_inbound_payment_key_material ( ) ) ;
129
+ let ( payment_hash, payment_secret) = if let Some ( payment_hash) = payment_hash {
130
+ let payment_secret = create_from_hash (
131
+ & keys,
132
+ amt_msat,
133
+ payment_hash,
134
+ invoice_expiry_delta_secs,
135
+ SystemTime :: now ( )
136
+ . duration_since ( UNIX_EPOCH )
137
+ . expect ( "Time must be > 1970" )
138
+ . as_secs ( ) ,
139
+ )
140
+ . map_err ( |_| SignOrCreationError :: CreationError ( CreationError :: InvalidAmount ) ) ?;
141
+ ( payment_hash, payment_secret)
142
+ } else {
143
+ create (
144
+ & keys,
145
+ amt_msat,
146
+ invoice_expiry_delta_secs,
147
+ & keys_manager,
148
+ SystemTime :: now ( )
149
+ . duration_since ( UNIX_EPOCH )
150
+ . expect ( "Time must be > 1970" )
151
+ . as_secs ( ) ,
152
+ )
153
+ . map_err ( |_| SignOrCreationError :: CreationError ( CreationError :: InvalidAmount ) ) ?
154
+ } ;
155
+
131
156
let mut invoice = invoice
132
157
. current_timestamp ( )
133
158
. payment_hash ( Hash :: from_slice ( & payment_hash. 0 ) . unwrap ( ) )
@@ -755,23 +780,25 @@ mod test {
755
780
nodes[ 2 ] . node . handle_channel_update ( & nodes[ 0 ] . node . get_our_node_id ( ) , & chan_0_2. 0 ) ;
756
781
757
782
let payment_amt = 10_000 ;
758
- let ( payment_preimage, payment_hash, payment_secret) = {
759
- if user_generated_pmt_hash {
760
- let payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
761
- let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . into_inner ( ) ) ;
762
- let payment_secret = nodes[ 1 ] . node . create_inbound_payment_for_hash ( payment_hash, Some ( payment_amt) , 3600 ) . unwrap ( ) ;
763
- ( payment_preimage, payment_hash, payment_secret)
764
- } else {
765
- let ( payment_hash, payment_secret) = nodes[ 1 ] . node . create_inbound_payment ( Some ( payment_amt) , 3600 ) . unwrap ( ) ;
766
- let payment_preimage = nodes[ 1 ] . node . get_payment_preimage ( payment_hash, payment_secret) . unwrap ( ) ;
767
- ( payment_preimage, payment_hash, payment_secret)
768
- }
769
- } ;
770
783
let route_hints = vec ! [
771
784
nodes[ 1 ] . node. get_phantom_route_hints( ) ,
772
785
nodes[ 2 ] . node. get_phantom_route_hints( ) ,
773
786
] ;
774
- let invoice = :: utils:: create_phantom_invoice :: < EnforcingSigner , & test_utils:: TestKeysInterface > ( Some ( payment_amt) , "test" . to_string ( ) , payment_hash, payment_secret, route_hints, & nodes[ 1 ] . keys_manager , Currency :: BitcoinTestnet ) . unwrap ( ) ;
787
+
788
+ let user_payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
789
+ let payment_hash = if user_generated_pmt_hash {
790
+ Some ( PaymentHash ( Sha256 :: hash ( & user_payment_preimage. 0 [ ..] ) . into_inner ( ) ) )
791
+ } else {
792
+ None
793
+ } ;
794
+
795
+ let invoice = :: utils:: create_phantom_invoice :: < EnforcingSigner , & test_utils:: TestKeysInterface > ( Some ( payment_amt) , payment_hash, "test" . to_string ( ) , 3600 , route_hints, & nodes[ 1 ] . keys_manager , Currency :: BitcoinTestnet ) . unwrap ( ) ;
796
+ let ( payment_hash, payment_secret) = ( PaymentHash ( invoice. payment_hash ( ) . into_inner ( ) ) , * invoice. payment_secret ( ) ) ;
797
+ let payment_preimage = if user_generated_pmt_hash {
798
+ user_payment_preimage
799
+ } else {
800
+ nodes[ 1 ] . node . get_payment_preimage ( payment_hash, payment_secret) . unwrap ( )
801
+ } ;
775
802
776
803
assert_eq ! ( invoice. min_final_cltv_expiry( ) , MIN_FINAL_CLTV_EXPIRY as u64 ) ;
777
804
assert_eq ! ( invoice. description( ) , InvoiceDescription :: Direct ( & Description ( "test" . to_string( ) ) ) ) ;
@@ -821,6 +848,7 @@ mod test {
821
848
// Note that we have to "forward pending HTLCs" twice before we see the PaymentReceived as
822
849
// this "emulates" the payment taking two hops, providing some privacy to make phantom node
823
850
// payments "look real" by taking more time.
851
+
824
852
expect_pending_htlcs_forwardable_ignore ! ( nodes[ fwd_idx] ) ;
825
853
nodes[ fwd_idx] . node . process_pending_htlc_forwards ( ) ;
826
854
expect_pending_htlcs_forwardable_ignore ! ( nodes[ fwd_idx] ) ;
@@ -856,14 +884,13 @@ mod test {
856
884
let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
857
885
858
886
let payment_amt = 20_000 ;
859
- let ( payment_hash, payment_secret) = nodes[ 1 ] . node . create_inbound_payment ( Some ( payment_amt) , 3600 ) . unwrap ( ) ;
860
887
let route_hints = vec ! [
861
888
nodes[ 1 ] . node. get_phantom_route_hints( ) ,
862
889
nodes[ 2 ] . node. get_phantom_route_hints( ) ,
863
890
] ;
864
891
865
892
let description_hash = crate :: Sha256 ( Hash :: hash ( "Description hash phantom invoice" . as_bytes ( ) ) ) ;
866
- let invoice = :: utils:: create_phantom_invoice_with_description_hash :: < EnforcingSigner , & test_utils:: TestKeysInterface > ( Some ( payment_amt) , description_hash , payment_hash , payment_secret , route_hints, & nodes[ 1 ] . keys_manager , Currency :: BitcoinTestnet ) . unwrap ( ) ;
893
+ let invoice = :: utils:: create_phantom_invoice_with_description_hash :: < EnforcingSigner , & test_utils:: TestKeysInterface > ( Some ( payment_amt) , None , 3600 , description_hash , route_hints, & nodes[ 1 ] . keys_manager , Currency :: BitcoinTestnet ) . unwrap ( ) ;
867
894
868
895
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 200_000 ) ) ;
869
896
assert_eq ! ( invoice. min_final_cltv_expiry( ) , MIN_FINAL_CLTV_EXPIRY as u64 ) ;
@@ -1166,15 +1193,14 @@ mod test {
1166
1193
mut chan_ids_to_match : HashSet < u64 > ,
1167
1194
nodes_contains_public_channels : bool
1168
1195
) {
1169
- let ( payment_hash, payment_secret) = invoice_node. node . create_inbound_payment ( invoice_amt, 3600 ) . unwrap ( ) ;
1170
1196
let phantom_route_hints = network_multi_nodes. iter ( )
1171
1197
. map ( |node| node. node . get_phantom_route_hints ( ) )
1172
1198
. collect :: < Vec < PhantomRouteHints > > ( ) ;
1173
1199
let phantom_scids = phantom_route_hints. iter ( )
1174
1200
. map ( |route_hint| route_hint. phantom_scid )
1175
1201
. collect :: < HashSet < u64 > > ( ) ;
1176
1202
1177
- let invoice = :: utils:: create_phantom_invoice :: < EnforcingSigner , & test_utils:: TestKeysInterface > ( invoice_amt, "test" . to_string ( ) , payment_hash , payment_secret , phantom_route_hints, & invoice_node. keys_manager , Currency :: BitcoinTestnet ) . unwrap ( ) ;
1203
+ let invoice = :: utils:: create_phantom_invoice :: < EnforcingSigner , & test_utils:: TestKeysInterface > ( invoice_amt, None , "test" . to_string ( ) , 3600 , phantom_route_hints, & invoice_node. keys_manager , Currency :: BitcoinTestnet ) . unwrap ( ) ;
1178
1204
1179
1205
let invoice_hints = invoice. private_routes ( ) ;
1180
1206
0 commit comments