1 // This file is Copyright its original authors, visible in version control
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
10 use bitcoin::secp256k1::{Keypair, Parity, PublicKey, Secp256k1, SecretKey, self};
11 use crate::utils::test_logger;
12 use core::convert::TryFrom;
13 use lightning::blinded_path::BlindedPath;
14 use lightning::blinded_path::message::ForwardNode;
15 use lightning::sign::EntropySource;
16 use lightning::ln::PaymentHash;
17 use lightning::ln::features::BlindedHopFeatures;
18 use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
19 use lightning::offers::invoice_request::InvoiceRequest;
20 use lightning::offers::parse::Bolt12SemanticError;
21 use lightning::util::ser::Writeable;
24 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
25 if let Ok(invoice_request) = InvoiceRequest::try_from(data.to_vec()) {
26 let mut bytes = Vec::with_capacity(data.len());
27 invoice_request.write(&mut bytes).unwrap();
28 assert_eq!(data, bytes);
30 let secp_ctx = Secp256k1::new();
31 let keys = Keypair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
32 let mut buffer = Vec::new();
34 if let Ok(unsigned_invoice) = build_response(&invoice_request, &secp_ctx) {
35 let signing_pubkey = unsigned_invoice.signing_pubkey();
36 let (x_only_pubkey, _) = keys.x_only_public_key();
37 let odd_pubkey = x_only_pubkey.public_key(Parity::Odd);
38 let even_pubkey = x_only_pubkey.public_key(Parity::Even);
39 if signing_pubkey == odd_pubkey || signing_pubkey == even_pubkey {
41 .sign(|message: &UnsignedBolt12Invoice|
42 Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
49 .sign(|message: &UnsignedBolt12Invoice|
50 Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
60 impl EntropySource for Randomness {
61 fn get_secure_random_bytes(&self) -> [u8; 32] { [42; 32] }
64 fn pubkey(byte: u8) -> PublicKey {
65 let secp_ctx = Secp256k1::new();
66 PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
69 fn privkey(byte: u8) -> SecretKey {
70 SecretKey::from_slice(&[byte; 32]).unwrap()
73 fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
74 invoice_request: &InvoiceRequest, secp_ctx: &Secp256k1<T>
75 ) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
76 let entropy_source = Randomness {};
77 let intermediate_nodes = [
79 ForwardNode { node_id: pubkey(43), short_channel_id: None },
80 ForwardNode { node_id: pubkey(44), short_channel_id: None },
83 ForwardNode { node_id: pubkey(45), short_channel_id: None },
84 ForwardNode { node_id: pubkey(46), short_channel_id: None },
88 BlindedPath::new_for_message(&intermediate_nodes[0], pubkey(42), &entropy_source, secp_ctx).unwrap(),
89 BlindedPath::new_for_message(&intermediate_nodes[1], pubkey(42), &entropy_source, secp_ctx).unwrap(),
95 fee_proportional_millionths: 1_000,
96 cltv_expiry_delta: 42,
97 htlc_minimum_msat: 100,
98 htlc_maximum_msat: 1_000_000_000_000,
99 features: BlindedHopFeatures::empty(),
103 fee_proportional_millionths: 1_000,
104 cltv_expiry_delta: 42,
105 htlc_minimum_msat: 100,
106 htlc_maximum_msat: 1_000_000_000_000,
107 features: BlindedHopFeatures::empty(),
111 let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
112 let payment_hash = PaymentHash([42; 32]);
113 invoice_request.respond_with(payment_paths, payment_hash)?.build()
116 pub fn invoice_request_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
121 pub extern "C" fn invoice_request_deser_run(data: *const u8, datalen: usize) {
122 do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});