]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Refund parsing from bech32 strings
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 23 Aug 2022 22:31:46 +0000 (17:31 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 14 Dec 2022 22:20:53 +0000 (16:20 -0600)
Implement Bech32Encode for Refund, which supports creating and parsing
QR codes for the merchant-pays-user (i.e., offer for money) flow.

lightning/src/offers/refund.rs

index 0a6c7e23a0c2f6b0b2c2eae878fa0c452751e6b8..82ecfd88719050bd4a5b87ba6c0db8a2772de1f8 100644 (file)
@@ -21,16 +21,17 @@ use bitcoin::blockdata::constants::ChainHash;
 use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::PublicKey;
 use core::convert::TryFrom;
+use core::str::FromStr;
 use core::time::Duration;
 use crate::io;
 use crate::ln::features::InvoiceRequestFeatures;
 use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
 use crate::offers::invoice_request::InvoiceRequestTlvStream;
 use crate::offers::offer::OfferTlvStream;
-use crate::offers::parse::{ParseError, ParsedMessage, SemanticError};
+use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError};
 use crate::offers::payer::{PayerContents, PayerTlvStream};
 use crate::onion_message::BlindedPath;
-use crate::util::ser::SeekReadable;
+use crate::util::ser::{SeekReadable, WithoutLength, Writeable, Writer};
 use crate::util::string::PrintableString;
 
 use crate::prelude::*;
@@ -143,6 +144,18 @@ impl Refund {
        }
 }
 
+impl AsRef<[u8]> for Refund {
+       fn as_ref(&self) -> &[u8] {
+               &self.bytes
+       }
+}
+
+impl Writeable for Refund {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               WithoutLength(&self.bytes).write(writer)
+       }
+}
+
 type RefundTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream);
 
 impl SeekReadable for RefundTlvStream {
@@ -155,6 +168,18 @@ impl SeekReadable for RefundTlvStream {
        }
 }
 
+impl Bech32Encode for Refund {
+       const BECH32_HRP: &'static str = "lnr";
+}
+
+impl FromStr for Refund {
+       type Err = ParseError;
+
+       fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
+               Refund::from_bech32_str(s)
+       }
+}
+
 impl TryFrom<Vec<u8>> for Refund {
        type Error = ParseError;
 
@@ -239,3 +264,9 @@ impl TryFrom<RefundTlvStream> for RefundContents {
                })
        }
 }
+
+impl core::fmt::Display for Refund {
+       fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
+               self.fmt_bech32_str(f)
+       }
+}