-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathoffer_deser.rs
More file actions
77 lines (64 loc) · 2.46 KB
/
offer_deser.rs
File metadata and controls
77 lines (64 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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 crate::utils::test_logger;
use bitcoin::secp256k1::Secp256k1;
use core::convert::TryFrom;
use lightning::ln::channelmanager::PaymentId;
use lightning::ln::inbound_payment::ExpandedKey;
use lightning::offers::currency::DefaultCurrencyConversion;
use lightning::offers::invoice_request::InvoiceRequest;
use lightning::offers::nonce::Nonce;
use lightning::offers::offer::{Offer, Quantity};
use lightning::offers::parse::Bolt12SemanticError;
use lightning::sign::EntropySource;
use lightning::util::ser::Writeable;
#[inline]
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
if let Ok(offer) = Offer::try_from(data.to_vec()) {
let mut bytes = Vec::with_capacity(data.len());
offer.write(&mut bytes).unwrap();
assert_eq!(data, bytes);
let mut buffer = Vec::new();
if let Ok(invoice_request) = build_request(&offer) {
invoice_request.write(&mut buffer).unwrap();
}
}
}
struct FixedEntropy;
impl EntropySource for FixedEntropy {
fn get_secure_random_bytes(&self) -> [u8; 32] {
[42; 32]
}
}
fn build_request(offer: &Offer) -> Result<InvoiceRequest, Bolt12SemanticError> {
let expanded_key = ExpandedKey::new([42; 32]);
let entropy = FixedEntropy {};
let nonce = Nonce::from_entropy_source(&entropy);
let secp_ctx = Secp256k1::new();
let payment_id = PaymentId([1; 32]);
let conversion = DefaultCurrencyConversion;
let mut builder = offer.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id, &conversion)?;
builder = match offer.amount() {
None => builder.amount_msats(1000).unwrap(),
Some(amount) => builder.amount_msats(amount.into_msats(&conversion)?)?,
};
builder = match offer.supported_quantity() {
Quantity::Bounded(n) => builder.quantity(n.get()).unwrap(),
Quantity::Unbounded => builder.quantity(10).unwrap(),
Quantity::One => builder,
};
builder.build_and_sign()
}
pub fn offer_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
do_test(data, out);
}
#[no_mangle]
pub extern "C" fn offer_deser_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
}