]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/src/refund_deser.rs
Persist scorer upon update based on event handling
[rust-lightning] / fuzz / src / refund_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, 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::chain::keysinterface::EntropySource;
15 use lightning::ln::PaymentHash;
16 use lightning::ln::features::BlindedHopFeatures;
17 use lightning::offers::invoice::{BlindedPayInfo, UnsignedInvoice};
18 use lightning::offers::parse::SemanticError;
19 use lightning::offers::refund::Refund;
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(refund) = Refund::try_from(data.to_vec()) {
25                 let mut bytes = Vec::with_capacity(data.len());
26                 refund.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 pubkey = PublicKey::from(keys);
32                 let mut buffer = Vec::new();
33
34                 if let Ok(invoice) = build_response(&refund, pubkey, &secp_ctx) {
35                         invoice
36                                 .sign::<_, Infallible>(
37                                         |digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
38                                 )
39                                 .unwrap()
40                                 .write(&mut buffer)
41                                 .unwrap();
42                 }
43         }
44 }
45
46 struct Randomness;
47
48 impl EntropySource for Randomness {
49         fn get_secure_random_bytes(&self) -> [u8; 32] { [42; 32] }
50 }
51
52 fn pubkey(byte: u8) -> PublicKey {
53         let secp_ctx = Secp256k1::new();
54         PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
55 }
56
57 fn privkey(byte: u8) -> SecretKey {
58         SecretKey::from_slice(&[byte; 32]).unwrap()
59 }
60
61 fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
62         refund: &'a Refund, signing_pubkey: PublicKey, secp_ctx: &Secp256k1<T>
63 ) -> Result<UnsignedInvoice<'a>, SemanticError> {
64         let entropy_source = Randomness {};
65         let paths = vec![
66                 BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
67                 BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
68         ];
69
70         let payinfo = vec![
71                 BlindedPayInfo {
72                         fee_base_msat: 1,
73                         fee_proportional_millionths: 1_000,
74                         cltv_expiry_delta: 42,
75                         htlc_minimum_msat: 100,
76                         htlc_maximum_msat: 1_000_000_000_000,
77                         features: BlindedHopFeatures::empty(),
78                 },
79                 BlindedPayInfo {
80                         fee_base_msat: 1,
81                         fee_proportional_millionths: 1_000,
82                         cltv_expiry_delta: 42,
83                         htlc_minimum_msat: 100,
84                         htlc_maximum_msat: 1_000_000_000_000,
85                         features: BlindedHopFeatures::empty(),
86                 },
87         ];
88
89         let payment_paths = paths.into_iter().zip(payinfo.into_iter()).collect();
90         let payment_hash = PaymentHash([42; 32]);
91         refund.respond_with(payment_paths, payment_hash, signing_pubkey)?.build()
92 }
93
94 pub fn refund_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
95         do_test(data, out);
96 }
97
98 #[no_mangle]
99 pub extern "C" fn refund_deser_run(data: *const u8, datalen: usize) {
100         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
101 }