]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Encoding for TLV stream without signature records
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 22 Dec 2022 15:33:41 +0000 (09:33 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 18 Jan 2023 21:31:48 +0000 (15:31 -0600)
When using bytes from an InvoiceRequest to constructing bytes for an
Invoice, any signature TLV records in the bytes must be excluded. Define
a wrapper for encoding such pre-serialized bytes in this manner. This
will allow the forthcoming InvoiceBuilder to construct bytes for an
Invoice properly.

lightning/src/offers/merkle.rs

index 1a505aae8f018e07ee0230b7da01b1ef3939e4ab..9782dc7d1e84131f1d6e659dce2d28ac7e9de6f0 100644 (file)
@@ -13,7 +13,7 @@ use bitcoin::hashes::{Hash, HashEngine, sha256};
 use bitcoin::secp256k1::{Message, PublicKey, Secp256k1, self};
 use bitcoin::secp256k1::schnorr::Signature;
 use crate::io;
-use crate::util::ser::{BigSize, Readable};
+use crate::util::ser::{BigSize, Readable, Writeable, Writer};
 
 use crate::prelude::*;
 
@@ -194,14 +194,33 @@ impl<'a> Iterator for TlvStream<'a> {
        }
 }
 
+/// Encoding for a pre-serialized TLV stream that excludes any signature TLV records.
+///
+/// Panics if the wrapped bytes are not a well-formed TLV stream.
+pub(super) struct WithoutSignatures<'a>(pub &'a Vec<u8>);
+
+impl<'a> Writeable for WithoutSignatures<'a> {
+       #[inline]
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let tlv_stream = TlvStream::new(&self.0[..]);
+               for record in tlv_stream.skip_signatures() {
+                       writer.write_all(record.record_bytes)?;
+               }
+               Ok(())
+       }
+}
+
 #[cfg(test)]
 mod tests {
+       use super::{TlvStream, WithoutSignatures};
+
        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;
+       use crate::util::ser::Writeable;
 
        #[test]
        fn calculates_merkle_root_hash() {
@@ -253,6 +272,36 @@ mod tests {
                );
        }
 
+       #[test]
+       fn skips_encoding_signature_tlv_records() {
+               let secp_ctx = Secp256k1::new();
+               let recipient_pubkey = {
+                       let secret_key = SecretKey::from_slice(&[41; 32]).unwrap();
+                       KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key()
+               };
+               let payer_keys = {
+                       let secret_key = SecretKey::from_slice(&[42; 32]).unwrap();
+                       KeyPair::from_secret_key(&secp_ctx, &secret_key)
+               };
+
+               let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey)
+                       .amount_msats(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();
+
+               let mut bytes_without_signature = Vec::new();
+               WithoutSignatures(&invoice_request.bytes).write(&mut bytes_without_signature).unwrap();
+
+               assert_ne!(bytes_without_signature, invoice_request.bytes);
+               assert_eq!(
+                       TlvStream::new(&bytes_without_signature).count(),
+                       TlvStream::new(&invoice_request.bytes).count() - 1,
+               );
+       }
+
        impl AsRef<[u8]> for InvoiceRequest {
                fn as_ref(&self) -> &[u8] {
                        &self.bytes