Skip to content

Commit 8b397ce

Browse files
committed
f - build and serialize response
1 parent 7331f27 commit 8b397ce

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

fuzz/src/invoice_request_deser.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10+
use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey, self};
1011
use crate::utils::test_logger;
12+
use core::convert::{Infallible, TryFrom};
13+
use lightning::chain::keysinterface::EntropySource;
14+
use lightning::ln::PaymentHash;
15+
use lightning::ln::features::BlindedHopFeatures;
16+
use lightning::offers::invoice::{BlindedPayInfo, UnsignedInvoice};
1117
use lightning::offers::invoice_request::InvoiceRequest;
18+
use lightning::offers::parse::SemanticError;
19+
use lightning::onion_message::BlindedPath;
1220
use lightning::util::ser::Writeable;
13-
use std::convert::TryFrom;
1421

1522
#[inline]
1623
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
@@ -20,9 +27,71 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
2027
let mut bytes = Vec::with_capacity(data.len());
2128
invoice_request.write(&mut bytes).unwrap();
2229
assert_eq!(data, bytes);
30+
31+
let secp_ctx = Secp256k1::new();
32+
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
33+
let mut buffer = Vec::new();
34+
35+
if let Ok(invoice) = build_response(&invoice_request, &secp_ctx) {
36+
invoice
37+
.sign::<_, Infallible>(
38+
|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
39+
)
40+
.unwrap()
41+
.write(&mut buffer)
42+
.unwrap();
43+
}
2344
}
2445
}
2546

47+
struct Randomness;
48+
49+
impl EntropySource for Randomness {
50+
fn get_secure_random_bytes(&self) -> [u8; 32] { [42; 32] }
51+
}
52+
53+
fn pubkey(byte: u8) -> PublicKey {
54+
let secp_ctx = Secp256k1::new();
55+
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
56+
}
57+
58+
fn privkey(byte: u8) -> SecretKey {
59+
SecretKey::from_slice(&[byte; 32]).unwrap()
60+
}
61+
62+
fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
63+
invoice_request: &'a InvoiceRequest, secp_ctx: &Secp256k1<T>
64+
) -> Result<UnsignedInvoice<'a>, SemanticError> {
65+
let entropy_source = Randomness {};
66+
let paths = vec![
67+
BlindedPath::new(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
68+
BlindedPath::new(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
69+
];
70+
71+
let payinfo = vec![
72+
BlindedPayInfo {
73+
fee_base_msat: 1,
74+
fee_proportional_millionths: 1_000,
75+
cltv_expiry_delta: 42,
76+
htlc_minimum_msat: 100,
77+
htlc_maximum_msat: 1_000_000_000_000,
78+
features: BlindedHopFeatures::empty(),
79+
},
80+
BlindedPayInfo {
81+
fee_base_msat: 1,
82+
fee_proportional_millionths: 1_000,
83+
cltv_expiry_delta: 42,
84+
htlc_minimum_msat: 100,
85+
htlc_maximum_msat: 1_000_000_000_000,
86+
features: BlindedHopFeatures::empty(),
87+
},
88+
];
89+
90+
let payment_paths = paths.into_iter().zip(payinfo.into_iter()).collect();
91+
let payment_hash = PaymentHash([42; 32]);
92+
invoice_request.respond_with(payment_paths, payment_hash)?.build()
93+
}
94+
2695
pub fn invoice_request_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
2796
do_test(data, out);
2897
}

0 commit comments

Comments
 (0)