]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add BOLT 12 merkle root test for `invoice_request`
authorJeffrey Czyz <jkczyz@gmail.com>
Mon, 28 Nov 2022 16:20:07 +0000 (11:20 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 9 Dec 2022 19:28:53 +0000 (13:28 -0600)
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.

lightning/src/offers/invoice_request.rs
lightning/src/offers/merkle.rs

index fd3251deacdf36305acc770001116189c10c37d3..90f6c183c0ee23ba1b1b6cf5a0e3b45b10d5f6af 100644 (file)
@@ -194,7 +194,7 @@ impl<'a> InvoiceRequestBuilder<'a> {
                self
        }
 
-       fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
+       pub(super) fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
                let InvoiceRequestBuilder { offer, invoice_request } = self;
                UnsignedInvoiceRequest { offer, invoice_request }
        }
@@ -247,7 +247,7 @@ impl<'a> UnsignedInvoiceRequest<'a> {
 /// [`Offer`]: crate::offers::offer::Offer
 #[derive(Clone, Debug)]
 pub struct InvoiceRequest {
-       bytes: Vec<u8>,
+       pub(super) bytes: Vec<u8>,
        contents: InvoiceRequestContents,
        signature: Option<Signature>,
 }
index 5a388be971734935a15a44c43358a99c7d8fb728..57e7fe6833c2fd7eac8f4ec6125a0e0b2bc58039 100644 (file)
@@ -198,6 +198,11 @@ impl<'a> Iterator for TlvStream<'a> {
 #[cfg(test)]
 mod tests {
        use bitcoin::hashes::{Hash, sha256};
+       use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
+       use core::convert::Infallible;
+       use crate::offers::offer::{Amount, OfferBuilder};
+       use crate::offers::invoice_request::InvoiceRequest;
+       use crate::offers::parse::Bech32Encode;
 
        #[test]
        fn calculates_merkle_root_hash() {
@@ -218,4 +223,50 @@ mod tests {
                        sha256::Hash::from_slice(&hex::decode("ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d").unwrap()).unwrap(),
                );
        }
+
+       #[test]
+       fn calculates_merkle_root_hash_from_invoice_request() {
+               let secp_ctx = Secp256k1::new();
+               let recipient_pubkey = {
+                       let secret_key = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap();
+                       KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key()
+               };
+               let payer_keys = {
+                       let secret_key = SecretKey::from_slice(&hex::decode("4242424242424242424242424242424242424242424242424242424242424242").unwrap()).unwrap();
+                       KeyPair::from_secret_key(&secp_ctx, &secret_key)
+               };
+
+               // BOLT 12 test vectors
+               let invoice_request = OfferBuilder::new("A Mathematical Treatise".into(), recipient_pubkey)
+                       .amount(Amount::Currency { iso4217_code: *b"USD", amount: 100 })
+                       .build_unchecked()
+                       .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
+                       .build_unchecked()
+                       .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &payer_keys)))
+                       .unwrap();
+               assert_eq!(
+                       invoice_request.to_string(),
+                       "lnr1qqyqqqqqqqqqqqqqqcp4256ypqqkgzshgysy6ct5dpjk6ct5d93kzmpq23ex2ct5d9ek293pqthvwfzadd7jejes8q9lhc4rvjxd022zv5l44g6qah82ru5rdpnpjkppqvjx204vgdzgsqpvcp4mldl3plscny0rt707gvpdh6ndydfacz43euzqhrurageg3n7kafgsek6gz3e9w52parv8gs2hlxzk95tzeswywffxlkeyhml0hh46kndmwf4m6xma3tkq2lu04qz3slje2rfthc89vss",
+               );
+               assert_eq!(
+                       super::root_hash(&invoice_request.bytes[..]),
+                       sha256::Hash::from_slice(&hex::decode("608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624").unwrap()).unwrap(),
+               );
+       }
+
+       impl AsRef<[u8]> for InvoiceRequest {
+               fn as_ref(&self) -> &[u8] {
+                       &self.bytes
+               }
+       }
+
+       impl Bech32Encode for InvoiceRequest {
+               const BECH32_HRP: &'static str = "lnr";
+       }
+
+       impl core::fmt::Display for InvoiceRequest {
+               fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
+                       self.fmt_bech32_str(f)
+               }
+       }
 }