Skip to content

Commit ef0223c

Browse files
committed
f - build and serialize response
1 parent 73716fa commit ef0223c

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

fuzz/src/refund_deser.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,92 @@
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;
13+
use lightning::chain::keysinterface::EntropySource;
14+
use lightning::ln::PaymentHash;
15+
use lightning::ln::features::BlindedHopFeatures;
16+
use lightning::offers::invoice::{BlindedPayInfo, UnsignedInvoice};
17+
use lightning::offers::parse::SemanticError;
1118
use lightning::offers::refund::Refund;
19+
use lightning::onion_message::BlindedPath;
20+
use lightning::util::ser::Writeable;
1221

1322
#[inline]
1423
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
1524
if let Ok(bech32_encoded) = std::str::from_utf8(data) {
1625
if let Ok(refund) = bech32_encoded.parse::<Refund>() {
1726
let bech32_encoded = refund.to_string();
1827
assert_eq!(refund, bech32_encoded.parse::<Refund>().unwrap());
28+
29+
let secp_ctx = Secp256k1::new();
30+
let keys =
31+
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::<_, Infallible>(
38+
|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
39+
)
40+
.unwrap()
41+
.write(&mut buffer)
42+
.unwrap();
43+
}
1944
}
2045
}
2146
}
2247

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

0 commit comments

Comments
 (0)