Skip to content

Commit e2cf8fe

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 98ba8ad commit e2cf8fe

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
@@ -196,7 +196,7 @@ impl<'a> InvoiceRequestBuilder<'a> {
196196
}
197197

198198
#[cfg(test)]
199-
fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
199+
pub(super) fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
200200
let InvoiceRequestBuilder { offer, invoice_request } = self;
201201
UnsignedInvoiceRequest { offer, invoice_request }
202202
}
@@ -248,7 +248,7 @@ impl<'a> UnsignedInvoiceRequest<'a> {
248248
/// [`Offer`]: crate::offers::offer::Offer
249249
#[derive(Clone, Debug)]
250250
pub struct InvoiceRequest {
251-
bytes: Vec<u8>,
251+
pub(super) bytes: Vec<u8>,
252252
contents: InvoiceRequestContents,
253253
signature: Option<Signature>,
254254
}

lightning/src/offers/merkle.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ impl<'a> Iterator for TlvStream<'a> {
188188
#[cfg(test)]
189189
mod tests {
190190
use bitcoin::hashes::{Hash, sha256};
191+
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
192+
use crate::offers::offer::{Amount, OfferBuilder};
193+
use crate::offers::invoice_request::InvoiceRequest;
194+
use crate::offers::parse::Bech32Encode;
191195

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

0 commit comments

Comments
 (0)