Skip to content

Commit 7d0f1cb

Browse files
committed
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.
1 parent 0075f98 commit 7d0f1cb

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

lightning/src/offers/refund.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ use bitcoin::blockdata::constants::ChainHash;
2121
use bitcoin::network::constants::Network;
2222
use bitcoin::secp256k1::PublicKey;
2323
use core::convert::TryFrom;
24+
use core::str::FromStr;
2425
use core::time::Duration;
2526
use crate::io;
2627
use crate::ln::features::InvoiceRequestFeatures;
2728
use crate::ln::msgs::DecodeError;
2829
use crate::offers::invoice_request::InvoiceRequestTlvStream;
2930
use crate::offers::offer::OfferTlvStream;
30-
use crate::offers::parse::{ParseError, ParsedMessage, SemanticError};
31+
use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError};
3132
use crate::offers::payer::{PayerContents, PayerTlvStream};
3233
use crate::onion_message::BlindedPath;
33-
use crate::util::ser::SeekReadable;
34+
use crate::util::ser::{SeekReadable, WithoutLength, Writeable, Writer};
3435
use crate::util::string::PrintableString;
3536

3637
use crate::prelude::*;
@@ -143,6 +144,18 @@ impl Refund {
143144
}
144145
}
145146

147+
impl AsRef<[u8]> for Refund {
148+
fn as_ref(&self) -> &[u8] {
149+
&self.bytes
150+
}
151+
}
152+
153+
impl Writeable for Refund {
154+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
155+
WithoutLength(&self.bytes).write(writer)
156+
}
157+
}
158+
146159
type RefundTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream);
147160

148161
impl SeekReadable for RefundTlvStream {
@@ -155,6 +168,18 @@ impl SeekReadable for RefundTlvStream {
155168
}
156169
}
157170

171+
impl Bech32Encode for Refund {
172+
const BECH32_HRP: &'static str = "lnr";
173+
}
174+
175+
impl FromStr for Refund {
176+
type Err = ParseError;
177+
178+
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
179+
Refund::from_bech32_str(s)
180+
}
181+
}
182+
158183
impl TryFrom<Vec<u8>> for Refund {
159184
type Error = ParseError;
160185

@@ -236,3 +261,9 @@ impl TryFrom<RefundTlvStream> for RefundContents {
236261
})
237262
}
238263
}
264+
265+
impl core::fmt::Display for Refund {
266+
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
267+
self.fmt_bech32_str(f)
268+
}
269+
}

0 commit comments

Comments
 (0)