Skip to content

Commit d4b54ee

Browse files
committed
Invoice request raw byte encoding and decoding
When reading an offer, an `invoice_request` message is sent over the wire. Implement Writeable for encoding the message and TryFrom for decoding it by defining in terms of TLV streams. These streams represent content for the payer metadata (0), reflected `offer` (1-79), `invoice_request` (80-159), and signature (240).
1 parent d28fef0 commit d4b54ee

File tree

7 files changed

+237
-6
lines changed

7 files changed

+237
-6
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
use bitcoin::blockdata::constants::ChainHash;
1313
use bitcoin::secp256k1::PublicKey;
1414
use bitcoin::secp256k1::schnorr::Signature;
15+
use core::convert::TryFrom;
16+
use io;
1517
use ln::features::OfferFeatures;
16-
use offers::offer::OfferContents;
17-
use offers::payer::PayerContents;
18+
use offers::merkle::{SignatureTlvStream, self};
19+
use offers::offer::{Amount, OfferContents, OfferTlvStream};
20+
use offers::parse::{ParseError, SemanticError};
21+
use offers::payer::{PayerContents, PayerTlvStream};
22+
use util::ser::{Readable, WithoutLength, Writeable, Writer};
1823

1924
use prelude::*;
2025

@@ -49,7 +54,7 @@ impl InvoiceRequest {
4954
/// [`payer_id`].
5055
///
5156
/// [`payer_id`]: Self::payer_id
52-
pub fn payer_info(&self) -> Option<&Vec<u8>> {
57+
pub fn metadata(&self) -> Option<&Vec<u8>> {
5358
self.contents.payer.0.as_ref()
5459
}
5560

@@ -100,3 +105,116 @@ impl InvoiceRequest {
100105
self.signature
101106
}
102107
}
108+
109+
impl Writeable for InvoiceRequest {
110+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
111+
WithoutLength(&self.bytes).write(writer)
112+
}
113+
}
114+
115+
impl TryFrom<Vec<u8>> for InvoiceRequest {
116+
type Error = ParseError;
117+
118+
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
119+
let tlv_stream: FullInvoiceRequestTlvStream = Readable::read(&mut &bytes[..])?;
120+
InvoiceRequest::try_from((bytes, tlv_stream))
121+
}
122+
}
123+
124+
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, {
125+
(80, chain: ChainHash),
126+
(82, amount: u64),
127+
(84, features: OfferFeatures),
128+
(86, quantity: u64),
129+
(88, payer_id: PublicKey),
130+
(89, payer_note: String),
131+
});
132+
133+
type ParsedInvoiceRequest = (Vec<u8>, FullInvoiceRequestTlvStream);
134+
135+
type FullInvoiceRequestTlvStream =
136+
(PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream);
137+
138+
type PartialInvoiceRequestTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream);
139+
140+
impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest {
141+
type Error = ParseError;
142+
143+
fn try_from(invoice_request: ParsedInvoiceRequest) -> Result<Self, Self::Error> {
144+
let (bytes, tlv_stream) = invoice_request;
145+
let (
146+
payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream,
147+
SignatureTlvStream { signature },
148+
) = tlv_stream;
149+
let contents = InvoiceRequestContents::try_from(
150+
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
151+
)?;
152+
153+
if let Some(signature) = &signature {
154+
let tag = concat!("lightning", "invoice_request", "signature");
155+
merkle::verify_signature(signature, tag, &bytes, contents.payer_id)?;
156+
}
157+
158+
Ok(InvoiceRequest { bytes, contents, signature })
159+
}
160+
}
161+
162+
impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
163+
type Error = SemanticError;
164+
165+
fn try_from(tlv_stream: PartialInvoiceRequestTlvStream) -> Result<Self, Self::Error> {
166+
let (
167+
PayerTlvStream { metadata },
168+
offer_tlv_stream,
169+
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
170+
) = tlv_stream;
171+
172+
let payer = PayerContents(metadata);
173+
let offer = OfferContents::try_from(offer_tlv_stream)?;
174+
175+
if !offer.supports_chain(chain.unwrap_or_else(|| offer.implied_chain())) {
176+
return Err(SemanticError::UnsupportedChain);
177+
}
178+
179+
let amount_msats = match (offer.amount(), amount) {
180+
(Some(_), None) => return Err(SemanticError::MissingAmount),
181+
// TODO: Handle currency case
182+
(Some(Amount::Currency { .. }), _) => return Err(SemanticError::UnsupportedCurrency),
183+
(_, amount_msats) => amount_msats,
184+
};
185+
186+
if let Some(features) = &features {
187+
if features.requires_unknown_bits() {
188+
return Err(SemanticError::UnknownRequiredFeatures);
189+
}
190+
}
191+
192+
let expects_quantity = offer.expects_quantity();
193+
let quantity = match quantity {
194+
None if expects_quantity => return Err(SemanticError::MissingQuantity),
195+
Some(_) if !expects_quantity => return Err(SemanticError::UnexpectedQuantity),
196+
Some(quantity) if !offer.is_valid_quantity(quantity) => {
197+
return Err(SemanticError::InvalidQuantity);
198+
}
199+
quantity => quantity,
200+
};
201+
202+
{
203+
let amount_msats = amount_msats.unwrap_or(offer.amount_msats());
204+
let quantity = quantity.unwrap_or(1);
205+
if amount_msats < offer.base_invoice_amount_msats(quantity) {
206+
return Err(SemanticError::InsufficientAmount);
207+
}
208+
}
209+
210+
211+
let payer_id = match payer_id {
212+
None => return Err(SemanticError::MissingPayerId),
213+
Some(payer_id) => payer_id,
214+
};
215+
216+
Ok(InvoiceRequestContents {
217+
payer, offer, chain, amount_msats, features, quantity, payer_id, payer_note,
218+
})
219+
}
220+
}

lightning/src/offers/merkle.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,32 @@
1010
//! Tagged hashes for use in signature calculation and verification.
1111
1212
use bitcoin::hashes::{Hash, HashEngine, sha256};
13+
use bitcoin::secp256k1::{Message, PublicKey, Secp256k1, self};
14+
use bitcoin::secp256k1::schnorr::Signature;
1315
use util::ser::{BigSize, Readable};
1416

1517
use prelude::*;
1618

1719
/// Valid type range for signature TLV records.
1820
const SIGNATURE_TYPES: core::ops::RangeInclusive<u64> = 240..=1000;
1921

22+
tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, {
23+
(240, signature: Signature),
24+
});
25+
26+
/// Verifies the signature with a pubkey over the given bytes using a tagged hash as the message
27+
/// digest.
28+
pub(super) fn verify_signature(
29+
signature: &Signature, tag: &str, bytes: &[u8], pubkey: PublicKey,
30+
) -> Result<(), secp256k1::Error> {
31+
let tag = sha256::Hash::hash(tag.as_bytes());
32+
let merkle_root = root_hash(bytes);
33+
let digest = Message::from_slice(&tagged_hash(tag, merkle_root)).unwrap();
34+
let pubkey = pubkey.into();
35+
let secp_ctx = Secp256k1::verification_only();
36+
secp_ctx.verify_schnorr(signature, &digest, &pubkey)
37+
}
38+
2039
/// Computes a merkle root hash for the given data, which must be a well-formed TLV stream
2140
/// containing at least one TLV record.
2241
fn root_hash(data: &[u8]) -> sha256::Hash {

lightning/src/offers/offer.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ impl Offer {
295295
self.contents.chains()
296296
}
297297

298+
/// Returns whether the given chain is supported by the offer.
299+
pub fn supports_chain(&self, chain: ChainHash) -> bool {
300+
self.contents.supports_chain(chain)
301+
}
302+
298303
// TODO: Link to corresponding method in `InvoiceRequest`.
299304
/// Opaque bytes set by the originator. Useful for authentication and validating fields since it
300305
/// is reflected in `invoice_request` messages along with all the other fields from the `offer`.
@@ -304,7 +309,12 @@ impl Offer {
304309

305310
/// The minimum amount required for a successful payment of a single item.
306311
pub fn amount(&self) -> Option<&Amount> {
307-
self.contents.amount.as_ref()
312+
self.contents.amount()
313+
}
314+
315+
/// Returns the minimum amount in msats required for the given quantity.
316+
pub fn base_invoice_amount_msats(&self, quantity: u64) -> u64 {
317+
self.contents.base_invoice_amount_msats(quantity)
308318
}
309319

310320
/// A complete description of the purpose of the payment. Intended to be displayed to the user
@@ -359,6 +369,18 @@ impl Offer {
359369
self.contents.quantity_max()
360370
}
361371

372+
/// Returns whether the given quantity is valid for the offer.
373+
pub fn is_valid_quantity(&self, quantity: u64) -> bool {
374+
self.contents.is_valid_quantity(quantity)
375+
}
376+
377+
/// Returns whether a quantity is expected in an [`InvoiceRequest`] for the offer.
378+
///
379+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
380+
pub fn expects_quantity(&self) -> bool {
381+
self.contents.expects_quantity()
382+
}
383+
362384
/// The public key used by the recipient to sign invoices.
363385
pub fn signing_pubkey(&self) -> PublicKey {
364386
self.contents.signing_pubkey.unwrap()
@@ -385,8 +407,20 @@ impl OfferContents {
385407
ChainHash::using_genesis_block(Network::Bitcoin)
386408
}
387409

410+
pub fn supports_chain(&self, chain: ChainHash) -> bool {
411+
self.chains().contains(&chain)
412+
}
413+
414+
pub fn amount(&self) -> Option<&Amount> {
415+
self.amount.as_ref()
416+
}
417+
388418
pub fn amount_msats(&self) -> u64 {
389-
self.amount.as_ref().map(Amount::as_msats).unwrap_or(0)
419+
self.amount().map(Amount::as_msats).unwrap_or(0)
420+
}
421+
422+
pub fn base_invoice_amount_msats(&self, quantity: u64) -> u64 {
423+
self.amount_msats() * quantity
390424
}
391425

392426
pub fn quantity_min(&self) -> u64 {
@@ -398,6 +432,16 @@ impl OfferContents {
398432
self.quantity_min.map_or(1, |_| u64::max_value()))
399433
}
400434

435+
pub fn is_valid_quantity(&self, quantity: u64) -> bool {
436+
self.expects_quantity()
437+
&& quantity >= self.quantity_min()
438+
&& quantity <= self.quantity_max()
439+
}
440+
441+
pub fn expects_quantity(&self) -> bool {
442+
self.quantity_min.is_some() || self.quantity_max.is_some()
443+
}
444+
401445
fn as_tlv_stream(&self) -> OfferTlvStreamRef {
402446
let (currency, amount) = match &self.amount {
403447
None => (None, None),
@@ -620,6 +664,7 @@ mod tests {
620664

621665
assert_eq!(offer.bytes, buffer.as_slice());
622666
assert_eq!(offer.chains(), vec![ChainHash::using_genesis_block(Network::Bitcoin)]);
667+
assert!(offer.supports_chain(ChainHash::using_genesis_block(Network::Bitcoin)));
623668
assert_eq!(offer.metadata(), None);
624669
assert_eq!(offer.amount(), None);
625670
assert_eq!(offer.description(), PrintableString("foo"));
@@ -663,6 +708,7 @@ mod tests {
663708
.chain(Network::Bitcoin)
664709
.build()
665710
.unwrap();
711+
assert!(offer.supports_chain(chain));
666712
assert_eq!(offer.chains(), vec![chain]);
667713
assert_eq!(offer.as_tlv_stream().chains, Some(&vec![chain]));
668714

@@ -671,6 +717,7 @@ mod tests {
671717
.chain(Network::Bitcoin)
672718
.build()
673719
.unwrap();
720+
assert!(offer.supports_chain(chain));
674721
assert_eq!(offer.chains(), vec![chain]);
675722
assert_eq!(offer.as_tlv_stream().chains, Some(&vec![chain]));
676723

@@ -679,6 +726,8 @@ mod tests {
679726
.chain(Network::Testnet)
680727
.build()
681728
.unwrap();
729+
assert!(offer.supports_chain(chains[0]));
730+
assert!(offer.supports_chain(chains[1]));
682731
assert_eq!(offer.chains(), chains);
683732
assert_eq!(offer.as_tlv_stream().chains, Some(&chains));
684733
}

lightning/src/offers/parse.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use bitcoin::bech32;
1313
use bitcoin::bech32::{FromBase32, ToBase32};
14+
use bitcoin::secp256k1;
1415
use core::convert::TryFrom;
1516
use core::fmt;
1617
use ln::msgs::DecodeError;
@@ -67,24 +68,40 @@ pub enum ParseError {
6768
Decode(DecodeError),
6869
/// The parsed message has invalid semantics.
6970
InvalidSemantics(SemanticError),
71+
/// The parsed message has an invalid signature.
72+
InvalidSignature(secp256k1::Error),
7073
}
7174

7275
/// Error when interpreting a TLV stream as a specific type.
7376
#[derive(Debug, PartialEq)]
7477
pub enum SemanticError {
78+
/// The provided chain hash does not correspond to a supported chain.
79+
UnsupportedChain,
7580
/// An amount was expected but was missing.
7681
MissingAmount,
7782
/// An amount exceeded the maximum number of bitcoin.
7883
InvalidAmount,
84+
/// An amount was provided but was not sufficient in value.
85+
InsufficientAmount,
86+
/// A currency was provided that is not supported.
87+
UnsupportedCurrency,
88+
/// A feature was required but is unknown.
89+
UnknownRequiredFeatures,
7990
/// A required description was not provided.
8091
MissingDescription,
8192
/// A node id was not provided.
8293
MissingNodeId,
8394
/// An empty set of blinded paths was provided.
8495
MissingPaths,
96+
/// A quantity was not provided.
97+
MissingQuantity,
8598
/// A quantity constituting part of an empty range or lying outside of a valid range was
8699
/// provided.
87100
InvalidQuantity,
101+
/// A quantity or quantity bounds was provided but was not expected.
102+
UnexpectedQuantity,
103+
/// A payer id was expected but was missing.
104+
MissingPayerId,
88105
}
89106

90107
impl From<bech32::Error> for ParseError {
@@ -104,3 +121,9 @@ impl From<SemanticError> for ParseError {
104121
Self::InvalidSemantics(error)
105122
}
106123
}
124+
125+
impl From<secp256k1::Error> for ParseError {
126+
fn from(error: secp256k1::Error) -> Self {
127+
Self::InvalidSignature(error)
128+
}
129+
}

lightning/src/offers/payer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ use prelude::*;
1717
/// [`InvoiceRequestContents::payer_id`]: invoice_request::InvoiceRequestContents::payer_id
1818
#[derive(Clone, Debug)]
1919
pub(crate) struct PayerContents(pub Option<Vec<u8>>);
20+
21+
tlv_stream!(PayerTlvStream, PayerTlvStreamRef, {
22+
(0, metadata: Vec<u8>),
23+
});

lightning/src/util/ser.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,24 @@ impl<A: Writeable, B: Writeable, C: Writeable> Writeable for (A, B, C) {
981981
}
982982
}
983983

984+
impl<A: Readable, B: Readable, C: Readable, D: Readable> Readable for (A, B, C, D) {
985+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
986+
let a: A = Readable::read(r)?;
987+
let b: B = Readable::read(r)?;
988+
let c: C = Readable::read(r)?;
989+
let d: D = Readable::read(r)?;
990+
Ok((a, b, c, d))
991+
}
992+
}
993+
impl<A: Writeable, B: Writeable, C: Writeable, D: Writeable> Writeable for (A, B, C, D) {
994+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
995+
self.0.write(w)?;
996+
self.1.write(w)?;
997+
self.2.write(w)?;
998+
self.3.write(w)
999+
}
1000+
}
1001+
9841002
impl Writeable for () {
9851003
fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
9861004
Ok(())

lightning/src/util/ser_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ macro_rules! tlv_stream {
464464
#[derive(Debug)]
465465
pub(crate) struct $name {
466466
$(
467-
$field: Option<tlv_record_type!($fieldty)>,
467+
pub(crate) $field: Option<tlv_record_type!($fieldty)>,
468468
)*
469469
}
470470

0 commit comments

Comments
 (0)