Skip to content

Commit 930d153

Browse files
committed
Add BOLT 12 merkle root test for invoice_request
A BOLT 12 test vector uses an `invoice_request` message that has a currency, which aren't supported, so using OfferBuilder::build_unchecked is required to avoid a panic.
1 parent 701deb5 commit 930d153

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'a> InvoiceRequestBuilder<'a> {
203203
}
204204

205205
#[cfg(test)]
206-
fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
206+
pub(super) fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
207207
let InvoiceRequestBuilder { offer, invoice_request } = self;
208208
UnsignedInvoiceRequest { offer, invoice_request }
209209
}
@@ -256,7 +256,7 @@ impl<'a> UnsignedInvoiceRequest<'a> {
256256
/// [`Offer`]: crate::offers::offer::Offer
257257
#[derive(Clone, Debug)]
258258
pub struct InvoiceRequest {
259-
bytes: Vec<u8>,
259+
pub(super) bytes: Vec<u8>,
260260
contents: InvoiceRequestContents,
261261
signature: Option<Signature>,
262262
}

lightning/src/offers/merkle.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ impl<'a> Iterator for TlvStream<'a> {
198198
#[cfg(test)]
199199
mod tests {
200200
use bitcoin::hashes::{Hash, sha256};
201+
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
202+
use crate::offers::offer::{Amount, OfferBuilder};
203+
use crate::offers::invoice_request::InvoiceRequest;
204+
use crate::offers::parse::Bech32Encode;
201205

202206
#[test]
203207
fn calculates_merkle_root_hash() {
@@ -218,4 +222,51 @@ mod tests {
218222
sha256::Hash::from_slice(&hex::decode("ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d").unwrap()).unwrap(),
219223
);
220224
}
225+
226+
#[test]
227+
fn calculates_merkle_root_hash_from_invoice_request() {
228+
let secp_ctx = Secp256k1::new();
229+
let recipient_pubkey = {
230+
let secret_key = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap();
231+
KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key()
232+
};
233+
let payer_keys = {
234+
let secret_key = SecretKey::from_slice(&hex::decode("4242424242424242424242424242424242424242424242424242424242424242").unwrap()).unwrap();
235+
KeyPair::from_secret_key(&secp_ctx, &secret_key)
236+
};
237+
238+
// BOLT 12 test vectors
239+
let invoice_request = OfferBuilder::new("A Mathematical Treatise".into(), recipient_pubkey)
240+
.amount(Amount::Currency { iso4217_code: *b"USD", amount: 100 })
241+
.build_unchecked()
242+
.request_invoice(payer_keys.public_key())
243+
.metadata(vec![0; 8])
244+
.build_unchecked()
245+
.sign(|digest| secp_ctx.sign_schnorr_no_aux_rand(digest, &payer_keys))
246+
.unwrap();
247+
assert_eq!(
248+
invoice_request.to_string(),
249+
"lnr1qqyqqqqqqqqqqqqqqcp4256ypqqkgzshgysy6ct5dpjk6ct5d93kzmpq23ex2ct5d9ek293pqthvwfzadd7jejes8q9lhc4rvjxd022zv5l44g6qah82ru5rdpnpjkppqvjx204vgdzgsqpvcp4mldl3plscny0rt707gvpdh6ndydfacz43euzqhrurageg3n7kafgsek6gz3e9w52parv8gs2hlxzk95tzeswywffxlkeyhml0hh46kndmwf4m6xma3tkq2lu04qz3slje2rfthc89vss",
250+
);
251+
assert_eq!(
252+
super::root_hash(&invoice_request.bytes[..]),
253+
sha256::Hash::from_slice(&hex::decode("608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624").unwrap()).unwrap(),
254+
);
255+
}
256+
257+
impl AsRef<[u8]> for InvoiceRequest {
258+
fn as_ref(&self) -> &[u8] {
259+
&self.bytes
260+
}
261+
}
262+
263+
impl Bech32Encode for InvoiceRequest {
264+
const BECH32_HRP: &'static str = "lnr";
265+
}
266+
267+
impl core::fmt::Display for InvoiceRequest {
268+
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
269+
self.fmt_bech32_str(f)
270+
}
271+
}
221272
}

0 commit comments

Comments
 (0)