Expose send_payment_for_bolt12_invoice
[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 crate::utils::test_logger;
11 use bitcoin::secp256k1::{self, Keypair, PublicKey, Secp256k1, SecretKey};
12 use core::convert::TryFrom;
13 use lightning::blinded_path::message::ForwardNode;
14 use lightning::blinded_path::BlindedPath;
15 use lightning::ln::features::BlindedHopFeatures;
16 use lightning::ln::PaymentHash;
17 use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
18 use lightning::offers::parse::Bolt12SemanticError;
19 use lightning::offers::refund::Refund;
20 use lightning::sign::EntropySource;
21 use lightning::util::ser::Writeable;
22
23 #[inline]
24 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
25         if let Ok(refund) = Refund::try_from(data.to_vec()) {
26                 let mut bytes = Vec::with_capacity(data.len());
27                 refund.write(&mut bytes).unwrap();
28                 assert_eq!(data, bytes);
29
30                 let secp_ctx = Secp256k1::new();
31                 let keys = Keypair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
32                 let pubkey = PublicKey::from(keys);
33                 let mut buffer = Vec::new();
34
35                 if let Ok(invoice) = build_response(&refund, pubkey, &secp_ctx) {
36                         invoice
37                                 .sign(|message: &UnsignedBolt12Invoice| {
38                                         Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
39                                 })
40                                 .unwrap()
41                                 .write(&mut buffer)
42                                 .unwrap();
43                 }
44         }
45 }
46
47 struct Randomness;
48
49 impl EntropySource for Randomness {
50         fn get_secure_random_bytes(&self) -> [u8; 32] {
51                 [42; 32]
52         }
53 }
54
55 fn pubkey(byte: u8) -> PublicKey {
56         let secp_ctx = Secp256k1::new();
57         PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
58 }
59
60 fn privkey(byte: u8) -> SecretKey {
61         SecretKey::from_slice(&[byte; 32]).unwrap()
62 }
63
64 fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
65         refund: &Refund, signing_pubkey: PublicKey, secp_ctx: &Secp256k1<T>,
66 ) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
67         let entropy_source = Randomness {};
68         let intermediate_nodes = [
69                 [
70                         ForwardNode { node_id: pubkey(43), short_channel_id: None },
71                         ForwardNode { node_id: pubkey(44), short_channel_id: None },
72                 ],
73                 [
74                         ForwardNode { node_id: pubkey(45), short_channel_id: None },
75                         ForwardNode { node_id: pubkey(46), short_channel_id: None },
76                 ],
77         ];
78         let paths = vec![
79                 BlindedPath::new_for_message(&intermediate_nodes[0], pubkey(42), &entropy_source, secp_ctx)
80                         .unwrap(),
81                 BlindedPath::new_for_message(&intermediate_nodes[1], pubkey(42), &entropy_source, secp_ctx)
82                         .unwrap(),
83         ];
84
85         let payinfo = vec![
86                 BlindedPayInfo {
87                         fee_base_msat: 1,
88                         fee_proportional_millionths: 1_000,
89                         cltv_expiry_delta: 42,
90                         htlc_minimum_msat: 100,
91                         htlc_maximum_msat: 1_000_000_000_000,
92                         features: BlindedHopFeatures::empty(),
93                 },
94                 BlindedPayInfo {
95                         fee_base_msat: 1,
96                         fee_proportional_millionths: 1_000,
97                         cltv_expiry_delta: 42,
98                         htlc_minimum_msat: 100,
99                         htlc_maximum_msat: 1_000_000_000_000,
100                         features: BlindedHopFeatures::empty(),
101                 },
102         ];
103
104         let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
105         let payment_hash = PaymentHash([42; 32]);
106         refund.respond_with(payment_paths, payment_hash, signing_pubkey)?.build()
107 }
108
109 pub fn refund_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
110         do_test(data, out);
111 }
112
113 #[no_mangle]
114 pub extern "C" fn refund_deser_run(data: *const u8, datalen: usize) {
115         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
116 }