From d47763af57ebeabfe163e2158b4d66574c0612b0 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Tue, 23 Aug 2022 17:31:46 -0500 Subject: [PATCH] Refund parsing from bech32 strings 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 | 35 ++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/lightning/src/offers/refund.rs b/lightning/src/offers/refund.rs index 0a6c7e23a..82ecfd887 100644 --- a/lightning/src/offers/refund.rs +++ b/lightning/src/offers/refund.rs @@ -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(&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::Err> { + Refund::from_bech32_str(s) + } +} + impl TryFrom> for Refund { type Error = ParseError; @@ -239,3 +264,9 @@ impl TryFrom 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) + } +} -- 2.39.5