|
| 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::blockdata::constants::ChainHash; |
| 13 | +use bitcoin::secp256k1::PublicKey; |
| 14 | +use bitcoin::secp256k1::schnorr::Signature; |
| 15 | +use core::convert::TryFrom; |
| 16 | +use core::str::FromStr; |
| 17 | +use io; |
| 18 | +use ln::features::OfferFeatures; |
| 19 | +use offers::merkle::{SignatureTlvStream, self}; |
| 20 | +use offers::offer::{Amount, OfferContents, OfferTlvStream, self}; |
| 21 | +use offers::parse::{Bech32Encode, ParseError, SemanticError}; |
| 22 | +use offers::payer::{PayerContents, PayerTlvStream, self}; |
| 23 | +use util::ser::{Readable, WithoutLength, Writeable, Writer}; |
| 24 | + |
| 25 | +use prelude::*; |
| 26 | + |
| 27 | +/// |
| 28 | +pub struct InvoiceRequest { |
| 29 | + bytes: Vec<u8>, |
| 30 | + contents: InvoiceRequestContents, |
| 31 | + signature: Option<Signature>, |
| 32 | +} |
| 33 | + |
| 34 | +/// |
| 35 | +pub(crate) struct InvoiceRequestContents { |
| 36 | + payer: PayerContents, |
| 37 | + offer: OfferContents, |
| 38 | + chain: Option<ChainHash>, |
| 39 | + amount_msats: Option<u64>, |
| 40 | + features: Option<OfferFeatures>, |
| 41 | + quantity: Option<u64>, |
| 42 | + payer_id: PublicKey, |
| 43 | + payer_note: Option<String>, |
| 44 | +} |
| 45 | + |
| 46 | +impl InvoiceRequest { |
| 47 | + /// |
| 48 | + pub fn payer_info(&self) -> Option<&Vec<u8>> { |
| 49 | + self.contents.payer.0.as_ref() |
| 50 | + } |
| 51 | + |
| 52 | + /// |
| 53 | + pub fn chain(&self) -> ChainHash { |
| 54 | + self.contents.chain.unwrap_or_else(|| self.contents.offer.chain()) |
| 55 | + } |
| 56 | + |
| 57 | + /// |
| 58 | + pub fn amount_msats(&self) -> Option<u64> { |
| 59 | + self.contents.amount_msats |
| 60 | + } |
| 61 | + |
| 62 | + /// |
| 63 | + pub fn features(&self) -> Option<&OfferFeatures> { |
| 64 | + self.contents.features.as_ref() |
| 65 | + } |
| 66 | + |
| 67 | + /// |
| 68 | + pub fn quantity(&self) -> Option<u64> { |
| 69 | + self.contents.quantity |
| 70 | + } |
| 71 | + |
| 72 | + /// |
| 73 | + pub fn payer_id(&self) -> PublicKey { |
| 74 | + self.contents.payer_id |
| 75 | + } |
| 76 | + |
| 77 | + /// |
| 78 | + pub fn payer_note(&self) -> Option<&String> { |
| 79 | + self.contents.payer_note.as_ref() |
| 80 | + } |
| 81 | + |
| 82 | + /// |
| 83 | + pub fn signature(&self) -> Option<Signature> { |
| 84 | + self.signature |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl AsRef<[u8]> for InvoiceRequest { |
| 89 | + fn as_ref(&self) -> &[u8] { |
| 90 | + &self.bytes |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl InvoiceRequestContents { |
| 95 | + pub(super) fn as_tlv_stream(&self) -> ReferencedPartialInvoiceRequestTlvStream { |
| 96 | + let payer = payer::reference::PayerTlvStream { |
| 97 | + payer_info: self.payer.0.as_ref().map(Into::into), |
| 98 | + }; |
| 99 | + |
| 100 | + let offer = self.offer.as_tlv_stream(); |
| 101 | + |
| 102 | + let invoice_request = reference::InvoiceRequestTlvStream { |
| 103 | + chain: self.chain.as_ref(), |
| 104 | + amount: self.amount_msats.map(Into::into), |
| 105 | + features: self.features.as_ref(), |
| 106 | + quantity: self.quantity.map(Into::into), |
| 107 | + payer_id: Some(&self.payer_id), |
| 108 | + payer_note: self.payer_note.as_ref().map(Into::into), |
| 109 | + }; |
| 110 | + |
| 111 | + (payer, offer, invoice_request) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl Writeable for InvoiceRequest { |
| 116 | + fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { |
| 117 | + WithoutLength(&self.bytes).write(writer) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Writeable for InvoiceRequestContents { |
| 122 | + fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { |
| 123 | + self.as_tlv_stream().write(writer) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl TryFrom<Vec<u8>> for InvoiceRequest { |
| 128 | + type Error = ParseError; |
| 129 | + |
| 130 | + fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { |
| 131 | + let tlv_stream: FullInvoiceRequestTlvStream = Readable::read(&mut &bytes[..])?; |
| 132 | + InvoiceRequest::try_from((bytes, tlv_stream)) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +tlv_stream!(struct InvoiceRequestTlvStream { |
| 137 | + (80, chain: ChainHash), |
| 138 | + (82, amount: u64), |
| 139 | + (84, features: OfferFeatures), |
| 140 | + (86, quantity: u64), |
| 141 | + (88, payer_id: PublicKey), |
| 142 | + (89, payer_note: String), |
| 143 | +}); |
| 144 | + |
| 145 | +impl Bech32Encode for InvoiceRequest { |
| 146 | + type TlvStream = FullInvoiceRequestTlvStream; |
| 147 | + |
| 148 | + const BECH32_HRP: &'static str = "lnr"; |
| 149 | +} |
| 150 | + |
| 151 | +type ParsedInvoiceRequest = (Vec<u8>, FullInvoiceRequestTlvStream); |
| 152 | + |
| 153 | +type FullInvoiceRequestTlvStream = |
| 154 | + (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream); |
| 155 | + |
| 156 | +type PartialInvoiceRequestTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream); |
| 157 | + |
| 158 | +type ReferencedPartialInvoiceRequestTlvStream<'a> = ( |
| 159 | + payer::reference::PayerTlvStream<'a>, |
| 160 | + offer::reference::OfferTlvStream<'a>, |
| 161 | + reference::InvoiceRequestTlvStream<'a>, |
| 162 | +); |
| 163 | + |
| 164 | +impl FromStr for InvoiceRequest { |
| 165 | + type Err = ParseError; |
| 166 | + |
| 167 | + fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> { |
| 168 | + let (tlv_stream, bytes) = InvoiceRequest::from_bech32_str(s)?; |
| 169 | + InvoiceRequest::try_from((bytes, tlv_stream)) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest { |
| 174 | + type Error = ParseError; |
| 175 | + |
| 176 | + fn try_from(invoice_request: ParsedInvoiceRequest) -> Result<Self, Self::Error> { |
| 177 | + let (bytes, tlv_stream) = invoice_request; |
| 178 | + let ( |
| 179 | + payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream, |
| 180 | + SignatureTlvStream { signature }, |
| 181 | + ) = tlv_stream; |
| 182 | + let contents = InvoiceRequestContents::try_from( |
| 183 | + (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream) |
| 184 | + )?; |
| 185 | + |
| 186 | + if let Some(signature) = &signature { |
| 187 | + let tag = concat!("lightning", "invoice_request", "signature"); |
| 188 | + merkle::verify_signature(signature, tag, &bytes, contents.payer_id)?; |
| 189 | + } |
| 190 | + |
| 191 | + Ok(InvoiceRequest { bytes, contents, signature }) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents { |
| 196 | + type Error = SemanticError; |
| 197 | + |
| 198 | + fn try_from(tlv_stream: PartialInvoiceRequestTlvStream) -> Result<Self, Self::Error> { |
| 199 | + let ( |
| 200 | + PayerTlvStream { payer_info }, |
| 201 | + offer_tlv_stream, |
| 202 | + InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note }, |
| 203 | + ) = tlv_stream; |
| 204 | + |
| 205 | + let payer = PayerContents(payer_info.map(Into::into)); |
| 206 | + let offer = OfferContents::try_from(offer_tlv_stream)?; |
| 207 | + |
| 208 | + let chain = match chain { |
| 209 | + None => None, |
| 210 | + Some(chain) if chain == offer.chain() => Some(chain), |
| 211 | + Some(_) => return Err(SemanticError::UnsupportedChain), |
| 212 | + }; |
| 213 | + |
| 214 | + // TODO: Determine whether quantity should be accounted for |
| 215 | + let amount_msats = match (offer.amount(), amount.map(Into::into)) { |
| 216 | + // TODO: Handle currency case |
| 217 | + (Some(Amount::Currency { .. }), _) => return Err(SemanticError::UnexpectedCurrency), |
| 218 | + (Some(_), None) => return Err(SemanticError::MissingAmount), |
| 219 | + (Some(Amount::Bitcoin { amount_msats: offer_amount_msats }), Some(amount_msats)) => { |
| 220 | + if amount_msats < *offer_amount_msats { |
| 221 | + return Err(SemanticError::InsufficientAmount); |
| 222 | + } else { |
| 223 | + Some(amount_msats) |
| 224 | + } |
| 225 | + }, |
| 226 | + (_, amount_msats) => amount_msats, |
| 227 | + }; |
| 228 | + |
| 229 | + if let Some(features) = &features { |
| 230 | + if features.requires_unknown_bits() { |
| 231 | + return Err(SemanticError::UnknownRequiredFeatures); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + let quantity = match quantity.map(Into::into) { |
| 236 | + None if !offer.expects_quantity() => None, |
| 237 | + Some(quantity) if offer.is_valid_quantity(quantity) => Some(quantity), |
| 238 | + _ => return Err(SemanticError::InvalidQuantity), |
| 239 | + }; |
| 240 | + |
| 241 | + let payer_id = match payer_id { |
| 242 | + None => return Err(SemanticError::MissingPayerId), |
| 243 | + Some(payer_id) => payer_id, |
| 244 | + }; |
| 245 | + |
| 246 | + let payer_note = payer_note.map(Into::into); |
| 247 | + |
| 248 | + Ok(InvoiceRequestContents { |
| 249 | + payer, offer, chain, amount_msats, features, quantity, payer_id, payer_note, |
| 250 | + }) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +impl core::fmt::Display for InvoiceRequest { |
| 255 | + fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { |
| 256 | + self.fmt_bech32_str(f) |
| 257 | + } |
| 258 | +} |
0 commit comments