+ let expiry_timestamp = highest_seen_timestamp + invoice_expiry_delta_secs as u64 + 7200;
+ let mut expiry_bytes = expiry_timestamp.to_be_bytes();
+
+ // `min_value_msat` should fit in (64 bits - 3 payment type bits =) 61 bits as an unsigned integer.
+ // This should leave us with a maximum value greater than the 21M BTC supply cap anyway.
+ if min_value_msat.is_some() && min_value_msat.unwrap() > ((1u64 << 61) - 1) { return Err(()); }
+
+ // `expiry_timestamp` should fit in (64 bits - 2 delta bytes =) 48 bits as an unsigned integer.
+ // Bitcoin's block header timestamps are actually `u32`s, so we're technically already limited to
+ // the much smaller maximum timestamp of `u32::MAX` for now, but we check the u64 `expiry_timestamp`
+ // for future-proofing.
+ if min_final_cltv_expiry_delta.is_some() && expiry_timestamp > ((1u64 << 48) - 1) { return Err(()); }
+
+ if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
+ let bytes = min_final_cltv_expiry_delta.to_be_bytes();
+ expiry_bytes[0] |= bytes[0];
+ expiry_bytes[1] |= bytes[1];
+ }