2023-01-20 13:01:47 -06:00
|
|
|
// This file is Copyright its original authors, visible in version control
|
|
|
|
// history.
|
|
|
|
//
|
|
|
|
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
|
|
|
|
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
|
|
|
|
// You may not use this file except in accordance with one or both of these
|
|
|
|
// licenses.
|
|
|
|
|
|
|
|
use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey, self};
|
|
|
|
use crate::utils::test_logger;
|
|
|
|
use core::convert::{Infallible, TryFrom};
|
2023-03-15 21:56:57 -04:00
|
|
|
use lightning::blinded_path::BlindedPath;
|
2023-04-28 14:11:37 -05:00
|
|
|
use lightning::sign::EntropySource;
|
2023-01-20 13:01:47 -06:00
|
|
|
use lightning::ln::PaymentHash;
|
|
|
|
use lightning::ln::features::BlindedHopFeatures;
|
2023-07-13 13:47:09 -05:00
|
|
|
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
|
2023-07-13 16:28:55 -05:00
|
|
|
use lightning::offers::parse::Bolt12SemanticError;
|
2023-01-20 13:01:47 -06:00
|
|
|
use lightning::offers::refund::Refund;
|
|
|
|
use lightning::util::ser::Writeable;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
|
|
|
|
if let Ok(refund) = Refund::try_from(data.to_vec()) {
|
|
|
|
let mut bytes = Vec::with_capacity(data.len());
|
|
|
|
refund.write(&mut bytes).unwrap();
|
|
|
|
assert_eq!(data, bytes);
|
|
|
|
|
|
|
|
let secp_ctx = Secp256k1::new();
|
|
|
|
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
|
|
|
|
let pubkey = PublicKey::from(keys);
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
|
|
|
if let Ok(invoice) = build_response(&refund, pubkey, &secp_ctx) {
|
|
|
|
invoice
|
|
|
|
.sign::<_, Infallible>(
|
2023-02-27 14:23:05 -06:00
|
|
|
|message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
|
2023-01-20 13:01:47 -06:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.write(&mut buffer)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Randomness;
|
|
|
|
|
|
|
|
impl EntropySource for Randomness {
|
|
|
|
fn get_secure_random_bytes(&self) -> [u8; 32] { [42; 32] }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pubkey(byte: u8) -> PublicKey {
|
|
|
|
let secp_ctx = Secp256k1::new();
|
|
|
|
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn privkey(byte: u8) -> SecretKey {
|
|
|
|
SecretKey::from_slice(&[byte; 32]).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:05 -06:00
|
|
|
fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
|
|
|
|
refund: &Refund, signing_pubkey: PublicKey, secp_ctx: &Secp256k1<T>
|
|
|
|
) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
|
2023-01-20 13:01:47 -06:00
|
|
|
let entropy_source = Randomness {};
|
|
|
|
let paths = vec![
|
2023-03-15 22:30:41 -04:00
|
|
|
BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
|
|
|
|
BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
|
2023-01-20 13:01:47 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
let payinfo = vec![
|
|
|
|
BlindedPayInfo {
|
|
|
|
fee_base_msat: 1,
|
|
|
|
fee_proportional_millionths: 1_000,
|
|
|
|
cltv_expiry_delta: 42,
|
|
|
|
htlc_minimum_msat: 100,
|
|
|
|
htlc_maximum_msat: 1_000_000_000_000,
|
|
|
|
features: BlindedHopFeatures::empty(),
|
|
|
|
},
|
|
|
|
BlindedPayInfo {
|
|
|
|
fee_base_msat: 1,
|
|
|
|
fee_proportional_millionths: 1_000,
|
|
|
|
cltv_expiry_delta: 42,
|
|
|
|
htlc_minimum_msat: 100,
|
|
|
|
htlc_maximum_msat: 1_000_000_000_000,
|
|
|
|
features: BlindedHopFeatures::empty(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-06-16 09:45:04 -04:00
|
|
|
let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
|
2023-01-20 13:01:47 -06:00
|
|
|
let payment_hash = PaymentHash([42; 32]);
|
|
|
|
refund.respond_with(payment_paths, payment_hash, signing_pubkey)?.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn refund_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
|
|
|
|
do_test(data, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn refund_deser_run(data: *const u8, datalen: usize) {
|
|
|
|
do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
|
|
|
|
}
|