|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! Data structures and encoding for `invoice_request` messages. |
| 11 | +
|
| 12 | +use bitcoin::hash_types::BlockHash; |
| 13 | +use bitcoin::secp256k1::PublicKey; |
| 14 | +use bitcoin::secp256k1::schnorr::Signature; |
| 15 | +use core::convert::TryFrom; |
| 16 | +use core::str::FromStr; |
| 17 | +use ln::features::OfferFeatures; |
| 18 | +use offers::{Offer, PayerTlvStream}; |
| 19 | +use offers::merkle::SignatureTlvStream; |
| 20 | +use offers::offer::{OfferTlvStream, ParsedOffer}; |
| 21 | +use offers::parse::{Bech32Encode, ParseError, SemanticError}; |
| 22 | + |
| 23 | +/// |
| 24 | +pub struct InvoiceRequest { |
| 25 | + bytes: Vec<u8>, |
| 26 | + offer: Offer, |
| 27 | + chain: Option<BlockHash>, |
| 28 | + amount_msat: Option<u64>, |
| 29 | + features: Option<OfferFeatures>, |
| 30 | + quantity: Option<u64>, |
| 31 | + payer_id: Option<PublicKey>, |
| 32 | + payer_note: Option<String>, |
| 33 | + payer_info: Option<Vec<u8>>, |
| 34 | + signature: Option<Signature>, |
| 35 | +} |
| 36 | + |
| 37 | +impl AsRef<[u8]> for InvoiceRequest { |
| 38 | + fn as_ref(&self) -> &[u8] { |
| 39 | + &self.bytes |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +tlv_stream!(struct InvoiceRequestTlvStream { |
| 44 | + (80, chain: BlockHash), |
| 45 | + (82, amount: u64), |
| 46 | + (84, features: OfferFeatures), |
| 47 | + (86, quantity: u64), |
| 48 | + (88, payer_id: PublicKey), |
| 49 | + (89, payer_note: String), |
| 50 | +}); |
| 51 | + |
| 52 | +impl Bech32Encode for InvoiceRequest { |
| 53 | + type TlvStream = FullInvoiceRequestTlvStream; |
| 54 | + |
| 55 | + const BECH32_HRP: &'static str = "lnr"; |
| 56 | +} |
| 57 | + |
| 58 | +type FullInvoiceRequestTlvStream = |
| 59 | + (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream); |
| 60 | + |
| 61 | +impl FromStr for InvoiceRequest { |
| 62 | + type Err = ParseError; |
| 63 | + |
| 64 | + fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> { |
| 65 | + let (tlv_stream, bytes) = InvoiceRequest::from_bech32_str(s)?; |
| 66 | + Ok(InvoiceRequest::try_from((tlv_stream, bytes))?) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +type ParsedInvoiceRequest = (FullInvoiceRequestTlvStream, Vec<u8>); |
| 71 | + |
| 72 | +impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest { |
| 73 | + type Error = SemanticError; |
| 74 | + |
| 75 | + fn try_from(invoice_request: ParsedInvoiceRequest) -> Result<Self, Self::Error> { |
| 76 | + let (( |
| 77 | + PayerTlvStream { payer_info }, |
| 78 | + offer_tlv_stream, |
| 79 | + InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note }, |
| 80 | + SignatureTlvStream { signature }, |
| 81 | + ), bytes) = invoice_request; |
| 82 | + |
| 83 | + let offer = Offer::try_from(ParsedOffer(offer_tlv_stream, vec![]))?; |
| 84 | + |
| 85 | + let chain = match chain { |
| 86 | + None => None, |
| 87 | + Some(chain) if chain == offer.chain() => Some(chain), |
| 88 | + Some(_) => return Err(SemanticError::UnsupportedChain), |
| 89 | + }; |
| 90 | + |
| 91 | + // TODO: Check remaining fields against the reflected offer |
| 92 | + let amount_msat = amount.map(Into::into); |
| 93 | + |
| 94 | + let quantity = quantity.map(Into::into); |
| 95 | + |
| 96 | + let payer_note = payer_note.map(Into::into); |
| 97 | + |
| 98 | + let payer_info = payer_info.map(Into::into); |
| 99 | + |
| 100 | + Ok(InvoiceRequest { |
| 101 | + bytes, offer, chain, amount_msat, features, quantity, payer_id, payer_note, payer_info, |
| 102 | + signature, |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl core::fmt::Display for InvoiceRequest { |
| 108 | + fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { |
| 109 | + self.fmt_bech32_str(f) |
| 110 | + } |
| 111 | +} |
0 commit comments