Skip to content

Commit e411844

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 9fde3d9 commit e411844

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ 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 ln::msgs::DecodeError;
6061
use offers::merkle::{SignatureTlvStream, SignatureTlvStreamRef, self};
6162
use offers::offer::{Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, SendInvoiceOfferContents};
62-
use offers::parse::{ParseError, SemanticError};
63+
use offers::parse::{Bech32Encode, ParseError, SemanticError};
6364
use offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
6465
use util::ser::{HighZeroBytesDroppedBigSize, SeekReadable, WithoutLength, Writeable, Writer};
6566

@@ -324,6 +325,12 @@ impl InvoiceRequestContents {
324325
}
325326
}
326327

328+
impl AsRef<[u8]> for InvoiceRequest {
329+
fn as_ref(&self) -> &[u8] {
330+
&self.bytes
331+
}
332+
}
333+
327334
impl Writeable for InvoiceRequest {
328335
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
329336
WithoutLength(&self.bytes).write(writer)
@@ -356,6 +363,10 @@ tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, 80..160, {
356363

357364
try_from_tlv_stream_bytes!(FullInvoiceRequestTlvStream, ParsedInvoiceRequest);
358365

366+
impl Bech32Encode for InvoiceRequest {
367+
const BECH32_HRP: &'static str = "lnr";
368+
}
369+
359370
struct ParsedInvoiceRequest(Vec<u8>, FullInvoiceRequestTlvStream);
360371

361372
type FullInvoiceRequestTlvStream =
@@ -380,6 +391,14 @@ type PartialInvoiceRequestTlvStreamRef<'a> = (
380391
InvoiceRequestTlvStreamRef<'a>,
381392
);
382393

394+
impl FromStr for InvoiceRequest {
395+
type Err = ParseError;
396+
397+
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
398+
InvoiceRequest::from_bech32_str(s)
399+
}
400+
}
401+
383402
impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest {
384403
type Error = ParseError;
385404

@@ -464,6 +483,12 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
464483
}
465484
}
466485

486+
impl core::fmt::Display for InvoiceRequest {
487+
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
488+
self.fmt_bech32_str(f)
489+
}
490+
}
491+
467492
#[cfg(test)]
468493
mod tests {
469494
use super::InvoiceRequest;

0 commit comments

Comments
 (0)