Static invoice encoding and parsing
[rust-lightning] / lightning / src / offers / invoice_request.rs
index 90d6d5bf3084f4fafabcb55935bc83250abb9033..cdf94a2e80d5bee168deea35fdb03c817718cdc7 100644 (file)
@@ -23,8 +23,8 @@
 //! extern crate bitcoin;
 //! extern crate lightning;
 //!
-//! use bitcoin::network::constants::Network;
-//! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
+//! use bitcoin::network::Network;
+//! use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, SecretKey};
 //! use lightning::ln::features::OfferFeatures;
 //! use lightning::offers::invoice_request::UnsignedInvoiceRequest;
 //! use lightning::offers::offer::Offer;
@@ -32,7 +32,7 @@
 //!
 //! # fn parse() -> Result<(), lightning::offers::parse::Bolt12ParseError> {
 //! let secp_ctx = Secp256k1::new();
-//! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
+//! let keys = Keypair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
 //! let pubkey = PublicKey::from(keys);
 //! let mut buffer = Vec::new();
 //!
 //! ```
 
 use bitcoin::blockdata::constants::ChainHash;
-use bitcoin::network::constants::Network;
-use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, self};
+use bitcoin::network::Network;
+use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, self};
 use bitcoin::secp256k1::schnorr::Signature;
 use core::ops::Deref;
 use crate::sign::EntropySource;
 use crate::io;
 use crate::blinded_path::BlindedPath;
-use crate::ln::PaymentHash;
+use crate::ln::types::PaymentHash;
 use crate::ln::channelmanager::PaymentId;
 use crate::ln::features::InvoiceRequestFeatures;
 use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
@@ -303,7 +303,7 @@ macro_rules! invoice_request_builder_methods { (
        }
 
        fn build_with_checks($($self_mut)* $self: $self_type) -> Result<
-               (UnsignedInvoiceRequest, Option<KeyPair>, Option<&'b Secp256k1<$secp_context>>),
+               (UnsignedInvoiceRequest, Option<Keypair>, Option<&'b Secp256k1<$secp_context>>),
                Bolt12SemanticError
        > {
                #[cfg(feature = "std")] {
@@ -334,7 +334,7 @@ macro_rules! invoice_request_builder_methods { (
        }
 
        fn build_without_checks($($self_mut)* $self: $self_type) ->
-               (UnsignedInvoiceRequest, Option<KeyPair>, Option<&'b Secp256k1<$secp_context>>)
+               (UnsignedInvoiceRequest, Option<Keypair>, Option<&'b Secp256k1<$secp_context>>)
        {
                // Create the metadata for stateless verification of a Bolt12Invoice.
                let mut keys = None;
@@ -487,6 +487,7 @@ for InvoiceRequestBuilder<'a, 'b, DerivedPayerId, secp256k1::All> {
 ///
 /// This is serialized as a TLV stream, which includes TLV records from the originating message. As
 /// such, it may include unknown, odd TLV records.
+#[derive(Clone)]
 pub struct UnsignedInvoiceRequest {
        bytes: Vec<u8>,
        contents: InvoiceRequestContents,
@@ -621,7 +622,7 @@ pub struct VerifiedInvoiceRequest {
        /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
        /// [`respond_using_derived_keys`]: Self::respond_using_derived_keys
        /// [`respond_with`]: Self::respond_with
-       pub keys: Option<KeyPair>,
+       pub keys: Option<Keypair>,
 }
 
 /// The contents of an [`InvoiceRequest`], which may be shared with an [`Bolt12Invoice`].
@@ -902,14 +903,12 @@ impl VerifiedInvoiceRequest {
                let InvoiceRequestContents {
                        payer_id,
                        inner: InvoiceRequestContentsWithoutPayerId {
-                               payer: _, offer: _, chain: _, amount_msats, features, quantity, payer_note
+                               payer: _, offer: _, chain: _, amount_msats: _, features: _, quantity, payer_note
                        },
                } = &self.inner.contents;
 
                InvoiceRequestFields {
                        payer_id: *payer_id,
-                       amount_msats: *amount_msats,
-                       features: features.clone(),
                        quantity: *quantity,
                        payer_note_truncated: payer_note.clone()
                                .map(|mut s| { s.truncate(PAYER_NOTE_LIMIT); UntrustedString(s) }),
@@ -1162,15 +1161,6 @@ pub struct InvoiceRequestFields {
        /// A possibly transient pubkey used to sign the invoice request.
        pub payer_id: PublicKey,
 
-       /// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
-       /// must be greater than or equal to [`Offer::amount`], converted if necessary.
-       ///
-       /// [`chain`]: InvoiceRequest::chain
-       pub amount_msats: Option<u64>,
-
-       /// Features pertaining to requesting an invoice.
-       pub features: InvoiceRequestFeatures,
-
        /// The quantity of the offer's item conforming to [`Offer::is_valid_quantity`].
        pub quantity: Option<u64>,
 
@@ -1186,10 +1176,8 @@ impl Writeable for InvoiceRequestFields {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
                write_tlv_fields!(writer, {
                        (0, self.payer_id, required),
-                       (2, self.amount_msats.map(|v| HighZeroBytesDroppedBigSize(v)), option),
-                       (4, WithoutLength(&self.features), required),
-                       (6, self.quantity.map(|v| HighZeroBytesDroppedBigSize(v)), option),
-                       (8, self.payer_note_truncated.as_ref().map(|s| WithoutLength(&s.0)), option),
+                       (2, self.quantity.map(|v| HighZeroBytesDroppedBigSize(v)), option),
+                       (4, self.payer_note_truncated.as_ref().map(|s| WithoutLength(&s.0)), option),
                });
                Ok(())
        }
@@ -1199,15 +1187,13 @@ impl Readable for InvoiceRequestFields {
        fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
                _init_and_read_len_prefixed_tlv_fields!(reader, {
                        (0, payer_id, required),
-                       (2, amount_msats, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
-                       (4, features, (option, encoding: (InvoiceRequestFeatures, WithoutLength))),
-                       (6, quantity, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
-                       (8, payer_note_truncated, (option, encoding: (String, WithoutLength))),
+                       (2, quantity, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                       (4, payer_note_truncated, (option, encoding: (String, WithoutLength))),
                });
-               let features = features.unwrap_or(InvoiceRequestFeatures::empty());
 
                Ok(InvoiceRequestFields {
-                       payer_id: payer_id.0.unwrap(), amount_msats, features, quantity,
+                       payer_id: payer_id.0.unwrap(),
+                       quantity,
                        payer_note_truncated: payer_note_truncated.map(|s| UntrustedString(s)),
                })
        }
@@ -1218,8 +1204,8 @@ mod tests {
        use super::{InvoiceRequest, InvoiceRequestFields, InvoiceRequestTlvStreamRef, PAYER_NOTE_LIMIT, SIGNATURE_TAG, UnsignedInvoiceRequest};
 
        use bitcoin::blockdata::constants::ChainHash;
-       use bitcoin::network::constants::Network;
-       use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey, self};
+       use bitcoin::network::Network;
+       use bitcoin::secp256k1::{Keypair, Secp256k1, SecretKey, self};
        use core::num::NonZeroU64;
        #[cfg(feature = "std")]
        use core::time::Duration;
@@ -1262,8 +1248,8 @@ mod tests {
                assert_eq!(unsigned_invoice_request.payer_metadata(), &[1; 32]);
                assert_eq!(unsigned_invoice_request.chains(), vec![ChainHash::using_genesis_block(Network::Bitcoin)]);
                assert_eq!(unsigned_invoice_request.metadata(), None);
-               assert_eq!(unsigned_invoice_request.amount(), Some(&Amount::Bitcoin { amount_msats: 1000 }));
-               assert_eq!(unsigned_invoice_request.description(), PrintableString(""));
+               assert_eq!(unsigned_invoice_request.amount(), Some(Amount::Bitcoin { amount_msats: 1000 }));
+               assert_eq!(unsigned_invoice_request.description(), Some(PrintableString("")));
                assert_eq!(unsigned_invoice_request.offer_features(), &OfferFeatures::empty());
                assert_eq!(unsigned_invoice_request.absolute_expiry(), None);
                assert_eq!(unsigned_invoice_request.paths(), &[]);
@@ -1294,8 +1280,8 @@ mod tests {
                assert_eq!(invoice_request.payer_metadata(), &[1; 32]);
                assert_eq!(invoice_request.chains(), vec![ChainHash::using_genesis_block(Network::Bitcoin)]);
                assert_eq!(invoice_request.metadata(), None);
-               assert_eq!(invoice_request.amount(), Some(&Amount::Bitcoin { amount_msats: 1000 }));
-               assert_eq!(invoice_request.description(), PrintableString(""));
+               assert_eq!(invoice_request.amount(), Some(Amount::Bitcoin { amount_msats: 1000 }));
+               assert_eq!(invoice_request.description(), Some(PrintableString("")));
                assert_eq!(invoice_request.offer_features(), &OfferFeatures::empty());
                assert_eq!(invoice_request.absolute_expiry(), None);
                assert_eq!(invoice_request.paths(), &[]);
@@ -1996,6 +1982,7 @@ mod tests {
                }
 
                let invoice_request = OfferBuilder::new(recipient_pubkey())
+                       .description("foo".to_string())
                        .amount(Amount::Currency { iso4217_code: *b"USD", amount: 1000 })
                        .build_unchecked()
                        .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
@@ -2258,7 +2245,7 @@ mod tests {
        #[test]
        fn fails_parsing_invoice_request_with_extra_tlv_records() {
                let secp_ctx = Secp256k1::new();
-               let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
+               let keys = Keypair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let invoice_request = OfferBuilder::new(keys.public_key())
                        .amount_msats(1000)
                        .build().unwrap()
@@ -2300,7 +2287,6 @@ mod tests {
 
                let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
                        .chain(Network::Testnet).unwrap()
-                       .amount_msats(1001).unwrap()
                        .quantity(1).unwrap()
                        .payer_note("0".repeat(PAYER_NOTE_LIMIT * 2))
                        .build().unwrap()
@@ -2313,8 +2299,6 @@ mod tests {
                                        fields,
                                        InvoiceRequestFields {
                                                payer_id: payer_pubkey(),
-                                               amount_msats: Some(1001),
-                                               features: InvoiceRequestFeatures::empty(),
                                                quantity: Some(1),
                                                payer_note_truncated: Some(UntrustedString("0".repeat(PAYER_NOTE_LIMIT))),
                                        }