Skip to content

Commit 0f5e8fc

Browse files
committed
Invoice request parsing from bech32 strings
Implement Bech32Encode for InvoiceRequest, which supports creating and parsing QR codes for the merchant-pays-user (e.g., refund, ATM) flow.
1 parent 8f00613 commit 0f5e8fc

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ use bitcoin::secp256k1::{Message, PublicKey, self};
5555
use bitcoin::secp256k1::schnorr::Signature;
5656
use core::convert::TryFrom;
5757
use io;
58+
use core::str::FromStr;
5859
use ln::features::OfferFeatures;
5960
use offers::merkle::{SignatureTlvStream, SignatureTlvStreamRef, self};
6061
use offers::offer::{Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, SendInvoiceOfferContents};
61-
use offers::parse::{ParseError, SemanticError};
62+
use offers::parse::{Bech32Encode, ParseError, SemanticError};
6263
use offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
6364
use util::ser::{Readable, WithoutLength, Writeable, Writer};
6465

@@ -325,6 +326,12 @@ impl InvoiceRequestContents {
325326
}
326327
}
327328

329+
impl AsRef<[u8]> for InvoiceRequest {
330+
fn as_ref(&self) -> &[u8] {
331+
&self.bytes
332+
}
333+
}
334+
328335
impl Writeable for InvoiceRequest {
329336
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
330337
WithoutLength(&self.bytes).write(writer)
@@ -355,6 +362,12 @@ tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, {
355362
(89, payer_note: String),
356363
});
357364

365+
impl Bech32Encode for InvoiceRequest {
366+
type TlvStream = FullInvoiceRequestTlvStream;
367+
368+
const BECH32_HRP: &'static str = "lnr";
369+
}
370+
358371
type ParsedInvoiceRequest = (Vec<u8>, FullInvoiceRequestTlvStream);
359372

360373
type FullInvoiceRequestTlvStream =
@@ -368,6 +381,15 @@ type PartialInvoiceRequestTlvStreamRef<'a> = (
368381
InvoiceRequestTlvStreamRef<'a>,
369382
);
370383

384+
impl FromStr for InvoiceRequest {
385+
type Err = ParseError;
386+
387+
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
388+
let (tlv_stream, bytes) = InvoiceRequest::from_bech32_str(s)?;
389+
InvoiceRequest::try_from((bytes, tlv_stream))
390+
}
391+
}
392+
371393
impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest {
372394
type Error = ParseError;
373395

@@ -451,3 +473,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
451473
})
452474
}
453475
}
476+
477+
impl core::fmt::Display for InvoiceRequest {
478+
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
479+
self.fmt_bech32_str(f)
480+
}
481+
}

0 commit comments

Comments
 (0)