|
| 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::secp256k1::schnorr::Signature; |
| 13 | +use bitcoin::util::address::{Address, Payload, WitnessVersion}; |
| 14 | +use core::convert::TryFrom; |
| 15 | +use core::time::Duration; |
| 16 | +use io; |
| 17 | +use ln::PaymentHash; |
| 18 | +use ln::features::OfferFeatures; |
| 19 | +use offers::merkle::{SignatureTlvStream, self}; |
| 20 | +use offers::offer::OfferTlvStream; |
| 21 | +use offers::parse::{ParseError, SemanticError}; |
| 22 | +use offers::payer::PayerTlvStream; |
| 23 | +use offers::invoice_request::{InvoiceRequestContents, InvoiceRequestTlvStream}; |
| 24 | +use onion_message::BlindedPath; |
| 25 | +use util::ser::{Readable, WithoutLength, Writeable, Writer}; |
| 26 | + |
| 27 | +use prelude::*; |
| 28 | + |
| 29 | +/// |
| 30 | +const SIGNATURE_TAG: &'static str = concat!("lightning", "invoice", "signature"); |
| 31 | + |
| 32 | +/// |
| 33 | +pub struct Invoice { |
| 34 | + bytes: Vec<u8>, |
| 35 | + contents: InvoiceContents, |
| 36 | + signature: Option<Signature>, |
| 37 | +} |
| 38 | + |
| 39 | +/// |
| 40 | +pub(crate) struct InvoiceContents { |
| 41 | + invoice_request: InvoiceRequestContents, |
| 42 | + paths: Option<Vec<BlindedPath>>, |
| 43 | + payinfo: Option<Vec<BlindedPayInfo>>, |
| 44 | + created_at: Duration, |
| 45 | + relative_expiry: Option<Duration>, |
| 46 | + payment_hash: PaymentHash, |
| 47 | + amount_msats: u64, |
| 48 | + fallbacks: Option<Vec<Address>>, |
| 49 | + features: Option<OfferFeatures>, |
| 50 | + code: Option<String>, |
| 51 | +} |
| 52 | + |
| 53 | +impl Invoice { |
| 54 | + /// |
| 55 | + pub fn fallbacks(&self) -> Vec<&Address> { |
| 56 | + let is_valid = |address: &&Address| { |
| 57 | + if let Address { payload: Payload::WitnessProgram { program, .. }, .. } = address { |
| 58 | + if address.is_standard() { |
| 59 | + return true; |
| 60 | + } else if program.len() < 2 || program.len() > 40 { |
| 61 | + return false; |
| 62 | + } else { |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + unreachable!() |
| 68 | + }; |
| 69 | + self.contents.fallbacks |
| 70 | + .as_ref() |
| 71 | + .map(|fallbacks| fallbacks.iter().filter(is_valid).collect()) |
| 72 | + .unwrap_or_else(Vec::new) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl Writeable for Invoice { |
| 77 | + fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { |
| 78 | + WithoutLength(&self.bytes).write(writer) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl TryFrom<Vec<u8>> for Invoice { |
| 83 | + type Error = ParseError; |
| 84 | + |
| 85 | + fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { |
| 86 | + let tlv_stream: FullInvoiceTlvStream = Readable::read(&mut &bytes[..])?; |
| 87 | + Invoice::try_from((bytes, tlv_stream)) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef, { |
| 92 | + (160, paths: Vec<BlindedPath>), |
| 93 | + (162, payinfo: Vec<BlindedPayInfo>), |
| 94 | + (164, created_at: u64), |
| 95 | + (166, relative_expiry: u32), |
| 96 | + (168, payment_hash: PaymentHash), |
| 97 | + (170, amount: u64), |
| 98 | + (172, fallbacks: Vec<FallbackAddress>), |
| 99 | + (174, features: OfferFeatures), |
| 100 | + (176, code: String), |
| 101 | +}); |
| 102 | + |
| 103 | +/// |
| 104 | +#[derive(Debug)] |
| 105 | +pub struct BlindedPayInfo { |
| 106 | + fee_base_msat: u32, |
| 107 | + fee_proportional_millionths: u32, |
| 108 | + cltv_expiry_delta: u16, |
| 109 | + htlc_minimum_msat: u64, |
| 110 | + htlc_maximum_msat: u64, |
| 111 | + features_len: u16, |
| 112 | + features: OfferFeatures, |
| 113 | +} |
| 114 | + |
| 115 | +impl_writeable!(BlindedPayInfo, { |
| 116 | + fee_base_msat, |
| 117 | + fee_proportional_millionths, |
| 118 | + cltv_expiry_delta, |
| 119 | + htlc_minimum_msat, |
| 120 | + htlc_maximum_msat, |
| 121 | + features_len, |
| 122 | + features |
| 123 | +}); |
| 124 | + |
| 125 | +/// |
| 126 | +#[derive(Debug)] |
| 127 | +pub struct FallbackAddress { |
| 128 | + version: WitnessVersion, |
| 129 | + program: Vec<u8>, |
| 130 | +} |
| 131 | + |
| 132 | +impl_writeable!(FallbackAddress, { version, program }); |
| 133 | + |
| 134 | +type ParsedInvoice = (Vec<u8>, FullInvoiceTlvStream); |
| 135 | + |
| 136 | +type FullInvoiceTlvStream = |
| 137 | + (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, InvoiceTlvStream, SignatureTlvStream); |
| 138 | + |
| 139 | +type PartialInvoiceTlvStream = |
| 140 | + (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, InvoiceTlvStream); |
| 141 | + |
| 142 | +impl TryFrom<ParsedInvoice> for Invoice { |
| 143 | + type Error = ParseError; |
| 144 | + |
| 145 | + fn try_from(invoice: ParsedInvoice) -> Result<Self, Self::Error> { |
| 146 | + let (bytes, tlv_stream) = invoice; |
| 147 | + let ( |
| 148 | + payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream, invoice_tlv_stream, |
| 149 | + SignatureTlvStream { signature }, |
| 150 | + ) = tlv_stream; |
| 151 | + let contents = InvoiceContents::try_from( |
| 152 | + (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream, invoice_tlv_stream) |
| 153 | + )?; |
| 154 | + |
| 155 | + if let Some(signature) = &signature { |
| 156 | + let pubkey = contents.invoice_request.offer.signing_pubkey(); |
| 157 | + merkle::verify_signature(signature, SIGNATURE_TAG, &bytes, pubkey)?; |
| 158 | + } |
| 159 | + |
| 160 | + Ok(Invoice { bytes, contents, signature }) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents { |
| 165 | + type Error = SemanticError; |
| 166 | + |
| 167 | + fn try_from(tlv_stream: PartialInvoiceTlvStream) -> Result<Self, Self::Error> { |
| 168 | + let ( |
| 169 | + payer_tlv_stream, |
| 170 | + offer_tlv_stream, |
| 171 | + invoice_request_tlv_stream, |
| 172 | + InvoiceTlvStream { |
| 173 | + paths, payinfo, created_at, relative_expiry, payment_hash, amount, fallbacks, |
| 174 | + features, code, |
| 175 | + }, |
| 176 | + ) = tlv_stream; |
| 177 | + |
| 178 | + let invoice_request = InvoiceRequestContents::try_from( |
| 179 | + (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream) |
| 180 | + )?; |
| 181 | + |
| 182 | + let (paths, payinfo) = match (paths, payinfo) { |
| 183 | + (None, _) => return Err(SemanticError::MissingPaths), |
| 184 | + (_, None) => return Err(SemanticError::InvalidPayInfo), |
| 185 | + (Some(paths), _) if paths.is_empty() => return Err(SemanticError::MissingPaths), |
| 186 | + (Some(paths), Some(payinfo)) if paths.len() != payinfo.len() => { |
| 187 | + return Err(SemanticError::InvalidPayInfo); |
| 188 | + }, |
| 189 | + (paths, payinfo) => (paths, payinfo), |
| 190 | + }; |
| 191 | + |
| 192 | + let created_at = match created_at { |
| 193 | + None => return Err(SemanticError::MissingCreationTime), |
| 194 | + Some(timestamp) => Duration::from_secs(timestamp), |
| 195 | + }; |
| 196 | + |
| 197 | + let relative_expiry = relative_expiry |
| 198 | + .map(Into::<u64>::into) |
| 199 | + .map(Duration::from_secs); |
| 200 | + |
| 201 | + let payment_hash = match payment_hash { |
| 202 | + None => return Err(SemanticError::MissingPaymentHash), |
| 203 | + Some(payment_hash) => payment_hash, |
| 204 | + }; |
| 205 | + |
| 206 | + let amount_msats = match amount { |
| 207 | + None => return Err(SemanticError::MissingAmount), |
| 208 | + Some(amount) => amount, |
| 209 | + }; |
| 210 | + |
| 211 | + let fallbacks = match fallbacks { |
| 212 | + None => None, |
| 213 | + Some(fallbacks) => { |
| 214 | + let mut addresses = Vec::with_capacity(fallbacks.len()); |
| 215 | + for FallbackAddress { version, program } in fallbacks { |
| 216 | + addresses.push(Address { |
| 217 | + payload: Payload::WitnessProgram { version, program }, |
| 218 | + network: invoice_request.network(), |
| 219 | + }); |
| 220 | + } |
| 221 | + Some(addresses) |
| 222 | + }, |
| 223 | + }; |
| 224 | + |
| 225 | + Ok(InvoiceContents { |
| 226 | + invoice_request, paths, payinfo, created_at, relative_expiry, payment_hash, |
| 227 | + amount_msats, fallbacks, features, code, |
| 228 | + }) |
| 229 | + } |
| 230 | +} |
0 commit comments