]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Support reading the new `payment_metadata` field in invoices
authorMatt Corallo <git@bluematt.me>
Tue, 21 Dec 2021 05:25:18 +0000 (05:25 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 19 Apr 2023 02:57:19 +0000 (02:57 +0000)
This adds support for reading the new `PaymentMetadata` BOLT11
invoice field, giving us access to the `Vec<u8>` storing arbitrary
bytes we have to send to the recipient.

lightning-invoice/src/de.rs
lightning-invoice/src/lib.rs
lightning-invoice/src/ser.rs

index 925d7265c553526d993f94b29354adcac1db9ac1..01adf67d1af0816bcc78ddeeacd6473f680b71bf 100644 (file)
@@ -460,6 +460,8 @@ impl FromBase32 for TaggedField {
                                Ok(TaggedField::PrivateRoute(PrivateRoute::from_base32(field_data)?)),
                        constants::TAG_PAYMENT_SECRET =>
                                Ok(TaggedField::PaymentSecret(PaymentSecret::from_base32(field_data)?)),
+                       constants::TAG_PAYMENT_METADATA =>
+                               Ok(TaggedField::PaymentMetadata(Vec::<u8>::from_base32(field_data)?)),
                        constants::TAG_FEATURES =>
                                Ok(TaggedField::Features(InvoiceFeatures::from_base32(field_data)?)),
                        _ => {
index 097bdc458ce5391090f7c87a1dde32e1782728a5..e0ba4dda398732d80995e416a40654e9ab89f531 100644 (file)
@@ -419,6 +419,7 @@ pub enum TaggedField {
        Fallback(Fallback),
        PrivateRoute(PrivateRoute),
        PaymentSecret(PaymentSecret),
+       PaymentMetadata(Vec<u8>),
        Features(InvoiceFeatures),
 }
 
@@ -483,6 +484,7 @@ pub mod constants {
        pub const TAG_FALLBACK: u8 = 9;
        pub const TAG_PRIVATE_ROUTE: u8 = 3;
        pub const TAG_PAYMENT_SECRET: u8 = 16;
+       pub const TAG_PAYMENT_METADATA: u8 = 27;
        pub const TAG_FEATURES: u8 = 5;
 }
 
@@ -954,6 +956,10 @@ impl RawInvoice {
                find_extract!(self.known_tagged_fields(), TaggedField::PaymentSecret(ref x), x)
        }
 
+       pub fn payment_metadata(&self) -> Option<&Vec<u8>> {
+               find_extract!(self.known_tagged_fields(), TaggedField::PaymentMetadata(ref x), x)
+       }
+
        pub fn features(&self) -> Option<&InvoiceFeatures> {
                find_extract!(self.known_tagged_fields(), TaggedField::Features(ref x), x)
        }
@@ -1225,6 +1231,11 @@ impl Invoice {
                self.signed_invoice.payment_secret().expect("was checked by constructor")
        }
 
+       /// Get the payment metadata blob if one was included in the invoice
+       pub fn payment_metadata(&self) -> Option<&Vec<u8>> {
+               self.signed_invoice.payment_metadata()
+       }
+
        /// Get the invoice features if they were included in the invoice
        pub fn features(&self) -> Option<&InvoiceFeatures> {
                self.signed_invoice.features()
@@ -1374,6 +1385,7 @@ impl TaggedField {
                        TaggedField::Fallback(_) => constants::TAG_FALLBACK,
                        TaggedField::PrivateRoute(_) => constants::TAG_PRIVATE_ROUTE,
                        TaggedField::PaymentSecret(_) => constants::TAG_PAYMENT_SECRET,
+                       TaggedField::PaymentMetadata(_) => constants::TAG_PAYMENT_METADATA,
                        TaggedField::Features(_) => constants::TAG_FEATURES,
                };
 
index 29a2ca074edf0185c7d3ac97d74baa087dd1af29..0dca180cab370a79e9358b89d7f1e1d2e51a8c95 100644 (file)
@@ -446,6 +446,9 @@ impl ToBase32 for TaggedField {
                        TaggedField::PaymentSecret(ref payment_secret) => {
                                  write_tagged_field(writer, constants::TAG_PAYMENT_SECRET, payment_secret)
                        },
+                       TaggedField::PaymentMetadata(ref payment_metadata) => {
+                                 write_tagged_field(writer, constants::TAG_PAYMENT_METADATA, payment_metadata)
+                       },
                        TaggedField::Features(ref features) => {
                                write_tagged_field(writer, constants::TAG_FEATURES, features)
                        },