|
7 | 7 | // You may not use this file except in accordance with one or both of these
|
8 | 8 | // licenses.
|
9 | 9 |
|
| 10 | +use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey, self}; |
10 | 11 | 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; |
11 | 18 | use lightning::offers::refund::Refund;
|
| 19 | +use lightning::onion_message::BlindedPath; |
| 20 | +use lightning::util::ser::Writeable; |
12 | 21 |
|
13 | 22 | #[inline]
|
14 | 23 | pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
|
15 | 24 | if let Ok(bech32_encoded) = std::str::from_utf8(data) {
|
16 | 25 | if let Ok(refund) = bech32_encoded.parse::<Refund>() {
|
17 | 26 | let bech32_encoded = refund.to_string();
|
18 | 27 | 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 | + } |
19 | 44 | }
|
20 | 45 | }
|
21 | 46 | }
|
22 | 47 |
|
| 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 | + |
23 | 96 | pub fn refund_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
|
24 | 97 | do_test(data, out);
|
25 | 98 | }
|
|
0 commit comments