Reverse (BlindedPath, BlindedPayInfo) tuple order in offers invoice.
[rust-lightning] / fuzz / src / invoice_request_deser.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 use bitcoin::secp256k1::{KeyPair, Parity, PublicKey, Secp256k1, SecretKey, self};
11 use crate::utils::test_logger;
12 use core::convert::{Infallible, TryFrom};
13 use lightning::blinded_path::BlindedPath;
14 use lightning::sign::EntropySource;
15 use lightning::ln::PaymentHash;
16 use lightning::ln::features::BlindedHopFeatures;
17 use lightning::offers::invoice::{BlindedPayInfo, UnsignedInvoice};
18 use lightning::offers::invoice_request::InvoiceRequest;
19 use lightning::offers::parse::SemanticError;
20 use lightning::util::ser::Writeable;
21
22 #[inline]
23 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
24         if let Ok(invoice_request) = InvoiceRequest::try_from(data.to_vec()) {
25                 let mut bytes = Vec::with_capacity(data.len());
26                 invoice_request.write(&mut bytes).unwrap();
27                 assert_eq!(data, bytes);
28
29                 let secp_ctx = Secp256k1::new();
30                 let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
31                 let mut buffer = Vec::new();
32
33                 if let Ok(unsigned_invoice) = build_response(&invoice_request, &secp_ctx) {
34                         let signing_pubkey = unsigned_invoice.signing_pubkey();
35                         let (x_only_pubkey, _) = keys.x_only_public_key();
36                         let odd_pubkey = x_only_pubkey.public_key(Parity::Odd);
37                         let even_pubkey = x_only_pubkey.public_key(Parity::Even);
38                         if signing_pubkey == odd_pubkey || signing_pubkey == even_pubkey {
39                                 unsigned_invoice
40                                         .sign::<_, Infallible>(
41                                                 |digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
42                                         )
43                                         .unwrap()
44                                         .write(&mut buffer)
45                                         .unwrap();
46                         } else {
47                                 unsigned_invoice
48                                         .sign::<_, Infallible>(
49                                                 |digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
50                                         )
51                                         .unwrap_err();
52                         }
53                 }
54         }
55 }
56
57 struct Randomness;
58
59 impl EntropySource for Randomness {
60         fn get_secure_random_bytes(&self) -> [u8; 32] { [42; 32] }
61 }
62
63 fn pubkey(byte: u8) -> PublicKey {
64         let secp_ctx = Secp256k1::new();
65         PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
66 }
67
68 fn privkey(byte: u8) -> SecretKey {
69         SecretKey::from_slice(&[byte; 32]).unwrap()
70 }
71
72 fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
73         invoice_request: &'a InvoiceRequest, secp_ctx: &Secp256k1<T>
74 ) -> Result<UnsignedInvoice<'a>, SemanticError> {
75         let entropy_source = Randomness {};
76         let paths = vec![
77                 BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
78                 BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
79         ];
80
81         let payinfo = vec![
82                 BlindedPayInfo {
83                         fee_base_msat: 1,
84                         fee_proportional_millionths: 1_000,
85                         cltv_expiry_delta: 42,
86                         htlc_minimum_msat: 100,
87                         htlc_maximum_msat: 1_000_000_000_000,
88                         features: BlindedHopFeatures::empty(),
89                 },
90                 BlindedPayInfo {
91                         fee_base_msat: 1,
92                         fee_proportional_millionths: 1_000,
93                         cltv_expiry_delta: 42,
94                         htlc_minimum_msat: 100,
95                         htlc_maximum_msat: 1_000_000_000_000,
96                         features: BlindedHopFeatures::empty(),
97                 },
98         ];
99
100         let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
101         let payment_hash = PaymentHash([42; 32]);
102         invoice_request.respond_with(payment_paths, payment_hash)?.build()
103 }
104
105 pub fn invoice_request_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
106         do_test(data, out);
107 }
108
109 #[no_mangle]
110 pub extern "C" fn invoice_request_deser_run(data: *const u8, datalen: usize) {
111         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
112 }