@@ -181,16 +181,19 @@ fn construct_metadata_bytes(min_value_msat: Option<u64>, payment_type: Method,
181181
182182 if min_final_cltv_expiry_delta. is_some ( ) {
183183 if {
184- // `min_value_msat` should fit in 61 bits as an unsigned integer.
185- min_value_msat. is_some ( ) && min_value_msat. unwrap ( ) > ( ( 1 << 61 ) - 1 ) ||
186- // `expiry_timestamp` should fit in 48 bits as an unsigned integer.
187- expiry_timestamp > ( ( 1 << 48 ) - 1 )
184+ // `min_value_msat` should fit in (64 bits - 3 payment type bits =) 61 bits as an unsigned integer.
185+ // This should leave us with a maximum value greater than the 21M BTC supply cap anyway.
186+ min_value_msat. is_some ( ) && min_value_msat. unwrap ( ) > ( ( 1u64 << 61 ) - 1 ) ||
187+ // `expiry_timestamp` should fit in (64 bits - 2 delta bytes =) 48 bits as an unsigned integer.
188+ // Bitcoin's block header timestamps are actually `u32`s, so we're technically already limited to
189+ // the much smaller maximum timestamp of `u32::MAX` for now, but we check the u64 `expiry_timestamp`
190+ // for future-proofing.
191+ expiry_timestamp > ( ( 1u64 << 48 ) - 1 )
188192 } { return Err ( ( ) ) ; }
189193 }
190194
191- // Pack the 16 `min_final_cltv_expiry_delta` bits into the first two bytes of invoice expiry
192195 if let Some ( min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
193- let bytes = ( min_final_cltv_expiry_delta as u16 ) . to_be_bytes ( ) ;
196+ let bytes = min_final_cltv_expiry_delta. to_be_bytes ( ) ;
194197 expiry_bytes[ 0 ] |= bytes[ 0 ] ;
195198 expiry_bytes[ 1 ] |= bytes[ 1 ] ;
196199 }
@@ -294,7 +297,6 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
294297 }
295298 }
296299
297- // Match again to check for custom `min_final_cltv_expiry_delta`.
298300 match payment_type_res {
299301 Ok ( Method :: UserPaymentHashCustomFinalCltv ) | Ok ( Method :: LdkPaymentHashCustomFinalCltv ) => {
300302 min_final_cltv_expiry_delta = Some ( min_final_cltv_expiry_delta_from_metadata ( metadata_bytes) ) ;
0 commit comments