1 // This file is Copyright its original authors, visible in version control
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
10 //! Data structures and encoding for `invoice_request` messages.
12 //! An [`InvoiceRequest`] can be built from a parsed [`Offer`] as an "offer to be paid". It is
13 //! typically constructed by a customer and sent to the merchant who had published the corresponding
14 //! offer. The recipient of the request responds with an [`Invoice`].
16 //! For an "offer for money" (e.g., refund, ATM withdrawal), where an offer doesn't exist as a
17 //! precursor, see [`Refund`].
19 //! [`Invoice`]: crate::offers::invoice::Invoice
20 //! [`Refund`]: crate::offers::refund::Refund
23 //! extern crate bitcoin;
24 //! extern crate lightning;
26 //! use bitcoin::network::constants::Network;
27 //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
28 //! use core::convert::Infallible;
29 //! use lightning::ln::features::OfferFeatures;
30 //! use lightning::offers::offer::Offer;
31 //! use lightning::util::ser::Writeable;
33 //! # fn parse() -> Result<(), lightning::offers::parse::ParseError> {
34 //! let secp_ctx = Secp256k1::new();
35 //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
36 //! let pubkey = PublicKey::from(keys);
37 //! let mut buffer = Vec::new();
40 //! .parse::<Offer>()?
41 //! .request_invoice(vec![42; 64], pubkey)?
42 //! .chain(Network::Testnet)?
43 //! .amount_msats(1000)?
45 //! .payer_note("foo".to_string())
47 //! .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
48 //! .expect("failed verifying signature")
49 //! .write(&mut buffer)
55 use bitcoin::blockdata::constants::ChainHash;
56 use bitcoin::network::constants::Network;
57 use bitcoin::secp256k1::{Message, PublicKey};
58 use bitcoin::secp256k1::schnorr::Signature;
59 use core::convert::TryFrom;
61 use crate::ln::PaymentHash;
62 use crate::ln::features::InvoiceRequestFeatures;
63 use crate::ln::msgs::DecodeError;
64 use crate::offers::invoice::{BlindedPayInfo, InvoiceBuilder};
65 use crate::offers::merkle::{SignError, SignatureTlvStream, SignatureTlvStreamRef, self};
66 use crate::offers::offer::{Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef};
67 use crate::offers::parse::{ParseError, ParsedMessage, SemanticError};
68 use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
69 use crate::onion_message::BlindedPath;
70 use crate::util::ser::{HighZeroBytesDroppedBigSize, SeekReadable, WithoutLength, Writeable, Writer};
71 use crate::util::string::PrintableString;
73 use crate::prelude::*;
75 const SIGNATURE_TAG: &'static str = concat!("lightning", "invoice_request", "signature");
77 /// Builds an [`InvoiceRequest`] from an [`Offer`] for the "offer to be paid" flow.
79 /// See [module-level documentation] for usage.
81 /// [module-level documentation]: self
82 pub struct InvoiceRequestBuilder<'a> {
84 invoice_request: InvoiceRequestContents,
87 impl<'a> InvoiceRequestBuilder<'a> {
88 pub(super) fn new(offer: &'a Offer, metadata: Vec<u8>, payer_id: PublicKey) -> Self {
91 invoice_request: InvoiceRequestContents {
92 payer: PayerContents(metadata), offer: offer.contents.clone(), chain: None,
93 amount_msats: None, features: InvoiceRequestFeatures::empty(), quantity: None,
94 payer_id, payer_note: None,
99 /// Sets the [`InvoiceRequest::chain`] of the given [`Network`] for paying an invoice. If not
100 /// called, [`Network::Bitcoin`] is assumed. Errors if the chain for `network` is not supported
103 /// Successive calls to this method will override the previous setting.
104 pub fn chain(mut self, network: Network) -> Result<Self, SemanticError> {
105 let chain = ChainHash::using_genesis_block(network);
106 if !self.offer.supports_chain(chain) {
107 return Err(SemanticError::UnsupportedChain);
110 self.invoice_request.chain = Some(chain);
114 /// Sets the [`InvoiceRequest::amount_msats`] for paying an invoice. Errors if `amount_msats` is
115 /// not at least the expected invoice amount (i.e., [`Offer::amount`] times [`quantity`]).
117 /// Successive calls to this method will override the previous setting.
119 /// [`quantity`]: Self::quantity
120 pub fn amount_msats(mut self, amount_msats: u64) -> Result<Self, SemanticError> {
121 self.invoice_request.offer.check_amount_msats_for_quantity(
122 Some(amount_msats), self.invoice_request.quantity
124 self.invoice_request.amount_msats = Some(amount_msats);
128 /// Sets [`InvoiceRequest::quantity`] of items. If not set, `1` is assumed. Errors if `quantity`
129 /// does not conform to [`Offer::is_valid_quantity`].
131 /// Successive calls to this method will override the previous setting.
132 pub fn quantity(mut self, quantity: u64) -> Result<Self, SemanticError> {
133 self.invoice_request.offer.check_quantity(Some(quantity))?;
134 self.invoice_request.quantity = Some(quantity);
138 /// Sets the [`InvoiceRequest::payer_note`].
140 /// Successive calls to this method will override the previous setting.
141 pub fn payer_note(mut self, payer_note: String) -> Self {
142 self.invoice_request.payer_note = Some(payer_note);
146 /// Builds an unsigned [`InvoiceRequest`] after checking for valid semantics. It can be signed
147 /// by [`UnsignedInvoiceRequest::sign`].
148 pub fn build(mut self) -> Result<UnsignedInvoiceRequest<'a>, SemanticError> {
149 #[cfg(feature = "std")] {
150 if self.offer.is_expired() {
151 return Err(SemanticError::AlreadyExpired);
155 let chain = self.invoice_request.chain();
156 if !self.offer.supports_chain(chain) {
157 return Err(SemanticError::UnsupportedChain);
160 if chain == self.offer.implied_chain() {
161 self.invoice_request.chain = None;
164 if self.offer.amount().is_none() && self.invoice_request.amount_msats.is_none() {
165 return Err(SemanticError::MissingAmount);
168 self.invoice_request.offer.check_quantity(self.invoice_request.quantity)?;
169 self.invoice_request.offer.check_amount_msats_for_quantity(
170 self.invoice_request.amount_msats, self.invoice_request.quantity
173 let InvoiceRequestBuilder { offer, invoice_request } = self;
174 Ok(UnsignedInvoiceRequest { offer, invoice_request })
179 impl<'a> InvoiceRequestBuilder<'a> {
180 fn chain_unchecked(mut self, network: Network) -> Self {
181 let chain = ChainHash::using_genesis_block(network);
182 self.invoice_request.chain = Some(chain);
186 fn amount_msats_unchecked(mut self, amount_msats: u64) -> Self {
187 self.invoice_request.amount_msats = Some(amount_msats);
191 fn features_unchecked(mut self, features: InvoiceRequestFeatures) -> Self {
192 self.invoice_request.features = features;
196 fn quantity_unchecked(mut self, quantity: u64) -> Self {
197 self.invoice_request.quantity = Some(quantity);
201 pub(super) fn build_unchecked(self) -> UnsignedInvoiceRequest<'a> {
202 let InvoiceRequestBuilder { offer, invoice_request } = self;
203 UnsignedInvoiceRequest { offer, invoice_request }
207 /// A semantically valid [`InvoiceRequest`] that hasn't been signed.
208 pub struct UnsignedInvoiceRequest<'a> {
210 invoice_request: InvoiceRequestContents,
213 impl<'a> UnsignedInvoiceRequest<'a> {
214 /// Signs the invoice request using the given function.
215 pub fn sign<F, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>>
217 F: FnOnce(&Message) -> Result<Signature, E>
219 // Use the offer bytes instead of the offer TLV stream as the offer may have contained
220 // unknown TLV records, which are not stored in `OfferContents`.
221 let (payer_tlv_stream, _offer_tlv_stream, invoice_request_tlv_stream) =
222 self.invoice_request.as_tlv_stream();
223 let offer_bytes = WithoutLength(&self.offer.bytes);
224 let unsigned_tlv_stream = (payer_tlv_stream, offer_bytes, invoice_request_tlv_stream);
226 let mut bytes = Vec::new();
227 unsigned_tlv_stream.write(&mut bytes).unwrap();
229 let pubkey = self.invoice_request.payer_id;
230 let signature = merkle::sign_message(sign, SIGNATURE_TAG, &bytes, pubkey)?;
232 // Append the signature TLV record to the bytes.
233 let signature_tlv_stream = SignatureTlvStreamRef {
234 signature: Some(&signature),
236 signature_tlv_stream.write(&mut bytes).unwrap();
240 contents: self.invoice_request,
246 /// An `InvoiceRequest` is a request for an [`Invoice`] formulated from an [`Offer`].
248 /// An offer may provide choices such as quantity, amount, chain, features, etc. An invoice request
249 /// specifies these such that its recipient can send an invoice for payment.
251 /// [`Invoice`]: crate::offers::invoice::Invoice
252 /// [`Offer`]: crate::offers::offer::Offer
253 #[derive(Clone, Debug)]
254 pub struct InvoiceRequest {
255 pub(super) bytes: Vec<u8>,
256 pub(super) contents: InvoiceRequestContents,
257 signature: Signature,
260 /// The contents of an [`InvoiceRequest`], which may be shared with an [`Invoice`].
262 /// [`Invoice`]: crate::offers::invoice::Invoice
263 #[derive(Clone, Debug)]
264 pub(super) struct InvoiceRequestContents {
265 payer: PayerContents,
266 pub(super) offer: OfferContents,
267 chain: Option<ChainHash>,
268 amount_msats: Option<u64>,
269 features: InvoiceRequestFeatures,
270 quantity: Option<u64>,
272 payer_note: Option<String>,
275 impl InvoiceRequest {
276 /// An unpredictable series of bytes, typically containing information about the derivation of
279 /// [`payer_id`]: Self::payer_id
280 pub fn metadata(&self) -> &[u8] {
281 &self.contents.payer.0[..]
284 /// A chain from [`Offer::chains`] that the offer is valid for.
285 pub fn chain(&self) -> ChainHash {
286 self.contents.chain()
289 /// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
290 /// must be greater than or equal to [`Offer::amount`], converted if necessary.
292 /// [`chain`]: Self::chain
293 pub fn amount_msats(&self) -> Option<u64> {
294 self.contents.amount_msats
297 /// Features pertaining to requesting an invoice.
298 pub fn features(&self) -> &InvoiceRequestFeatures {
299 &self.contents.features
302 /// The quantity of the offer's item conforming to [`Offer::is_valid_quantity`].
303 pub fn quantity(&self) -> Option<u64> {
304 self.contents.quantity
307 /// A possibly transient pubkey used to sign the invoice request.
308 pub fn payer_id(&self) -> PublicKey {
309 self.contents.payer_id
312 /// A payer-provided note which will be seen by the recipient and reflected back in the invoice
314 pub fn payer_note(&self) -> Option<PrintableString> {
315 self.contents.payer_note.as_ref().map(|payer_note| PrintableString(payer_note.as_str()))
318 /// Signature of the invoice request using [`payer_id`].
320 /// [`payer_id`]: Self::payer_id
321 pub fn signature(&self) -> Signature {
325 /// Creates an [`Invoice`] for the request with the given required fields and using the
326 /// [`Duration`] since [`std::time::SystemTime::UNIX_EPOCH`] as the creation time.
328 /// See [`InvoiceRequest::respond_with_no_std`] for further details where the aforementioned
329 /// creation time is used for the `created_at` parameter.
331 /// [`Invoice`]: crate::offers::invoice::Invoice
332 /// [`Duration`]: core::time::Duration
333 #[cfg(feature = "std")]
335 &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash
336 ) -> Result<InvoiceBuilder, SemanticError> {
337 let created_at = std::time::SystemTime::now()
338 .duration_since(std::time::SystemTime::UNIX_EPOCH)
339 .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
341 self.respond_with_no_std(payment_paths, payment_hash, created_at)
344 /// Creates an [`Invoice`] for the request with the given required fields.
346 /// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
347 /// `created_at`, which is used to set [`Invoice::created_at`]. Useful for `no-std` builds where
348 /// [`std::time::SystemTime`] is not available.
350 /// The caller is expected to remember the preimage of `payment_hash` in order to claim a payment
353 /// The `payment_paths` parameter is useful for maintaining the payment recipient's privacy. It
354 /// must contain one or more elements ordered from most-preferred to least-preferred, if there's
355 /// a preference. Note, however, that any privacy is lost if a public node id was used for
356 /// [`Offer::signing_pubkey`].
358 /// Errors if the request contains unknown required features.
360 /// [`Invoice`]: crate::offers::invoice::Invoice
361 /// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
362 pub fn respond_with_no_std(
363 &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
364 created_at: core::time::Duration
365 ) -> Result<InvoiceBuilder, SemanticError> {
366 if self.features().requires_unknown_bits() {
367 return Err(SemanticError::UnknownRequiredFeatures);
370 InvoiceBuilder::for_offer(self, payment_paths, created_at, payment_hash)
374 fn as_tlv_stream(&self) -> FullInvoiceRequestTlvStreamRef {
375 let (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream) =
376 self.contents.as_tlv_stream();
377 let signature_tlv_stream = SignatureTlvStreamRef {
378 signature: Some(&self.signature),
380 (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream, signature_tlv_stream)
384 impl InvoiceRequestContents {
385 pub(super) fn chain(&self) -> ChainHash {
386 self.chain.unwrap_or_else(|| self.offer.implied_chain())
389 pub(super) fn as_tlv_stream(&self) -> PartialInvoiceRequestTlvStreamRef {
390 let payer = PayerTlvStreamRef {
391 metadata: Some(&self.payer.0),
394 let offer = self.offer.as_tlv_stream();
397 if self.features == InvoiceRequestFeatures::empty() { None }
398 else { Some(&self.features) }
401 let invoice_request = InvoiceRequestTlvStreamRef {
402 chain: self.chain.as_ref(),
403 amount: self.amount_msats,
405 quantity: self.quantity,
406 payer_id: Some(&self.payer_id),
407 payer_note: self.payer_note.as_ref(),
410 (payer, offer, invoice_request)
414 impl Writeable for InvoiceRequest {
415 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
416 WithoutLength(&self.bytes).write(writer)
420 impl Writeable for InvoiceRequestContents {
421 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
422 self.as_tlv_stream().write(writer)
426 tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, 80..160, {
427 (80, chain: ChainHash),
428 (82, amount: (u64, HighZeroBytesDroppedBigSize)),
429 (84, features: (InvoiceRequestFeatures, WithoutLength)),
430 (86, quantity: (u64, HighZeroBytesDroppedBigSize)),
431 (88, payer_id: PublicKey),
432 (89, payer_note: (String, WithoutLength)),
435 type FullInvoiceRequestTlvStream =
436 (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream);
439 type FullInvoiceRequestTlvStreamRef<'a> = (
440 PayerTlvStreamRef<'a>,
441 OfferTlvStreamRef<'a>,
442 InvoiceRequestTlvStreamRef<'a>,
443 SignatureTlvStreamRef<'a>,
446 impl SeekReadable for FullInvoiceRequestTlvStream {
447 fn read<R: io::Read + io::Seek>(r: &mut R) -> Result<Self, DecodeError> {
448 let payer = SeekReadable::read(r)?;
449 let offer = SeekReadable::read(r)?;
450 let invoice_request = SeekReadable::read(r)?;
451 let signature = SeekReadable::read(r)?;
453 Ok((payer, offer, invoice_request, signature))
457 type PartialInvoiceRequestTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream);
459 type PartialInvoiceRequestTlvStreamRef<'a> = (
460 PayerTlvStreamRef<'a>,
461 OfferTlvStreamRef<'a>,
462 InvoiceRequestTlvStreamRef<'a>,
465 impl TryFrom<Vec<u8>> for InvoiceRequest {
466 type Error = ParseError;
468 fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
469 let invoice_request = ParsedMessage::<FullInvoiceRequestTlvStream>::try_from(bytes)?;
470 let ParsedMessage { bytes, tlv_stream } = invoice_request;
472 payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream,
473 SignatureTlvStream { signature },
475 let contents = InvoiceRequestContents::try_from(
476 (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
479 let signature = match signature {
480 None => return Err(ParseError::InvalidSemantics(SemanticError::MissingSignature)),
481 Some(signature) => signature,
483 merkle::verify_signature(&signature, SIGNATURE_TAG, &bytes, contents.payer_id)?;
485 Ok(InvoiceRequest { bytes, contents, signature })
489 impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
490 type Error = SemanticError;
492 fn try_from(tlv_stream: PartialInvoiceRequestTlvStream) -> Result<Self, Self::Error> {
494 PayerTlvStream { metadata },
496 InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
499 let payer = match metadata {
500 None => return Err(SemanticError::MissingPayerMetadata),
501 Some(metadata) => PayerContents(metadata),
503 let offer = OfferContents::try_from(offer_tlv_stream)?;
505 if !offer.supports_chain(chain.unwrap_or_else(|| offer.implied_chain())) {
506 return Err(SemanticError::UnsupportedChain);
509 if offer.amount().is_none() && amount.is_none() {
510 return Err(SemanticError::MissingAmount);
513 offer.check_quantity(quantity)?;
514 offer.check_amount_msats_for_quantity(amount, quantity)?;
516 let features = features.unwrap_or_else(InvoiceRequestFeatures::empty);
518 let payer_id = match payer_id {
519 None => return Err(SemanticError::MissingPayerId),
520 Some(payer_id) => payer_id,
523 Ok(InvoiceRequestContents {
524 payer, offer, chain, amount_msats: amount, features, quantity, payer_id, payer_note,
531 use super::{InvoiceRequest, InvoiceRequestTlvStreamRef, SIGNATURE_TAG};
533 use bitcoin::blockdata::constants::ChainHash;
534 use bitcoin::network::constants::Network;
535 use bitcoin::secp256k1::{KeyPair, Message, PublicKey, Secp256k1, SecretKey, self};
536 use bitcoin::secp256k1::schnorr::Signature;
537 use core::convert::{Infallible, TryFrom};
538 use core::num::NonZeroU64;
539 #[cfg(feature = "std")]
540 use core::time::Duration;
541 use crate::ln::features::InvoiceRequestFeatures;
542 use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
543 use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self};
544 use crate::offers::offer::{Amount, OfferBuilder, OfferTlvStreamRef, Quantity};
545 use crate::offers::parse::{ParseError, SemanticError};
546 use crate::offers::payer::PayerTlvStreamRef;
547 use crate::util::ser::{BigSize, Writeable};
548 use crate::util::string::PrintableString;
550 fn payer_keys() -> KeyPair {
551 let secp_ctx = Secp256k1::new();
552 KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
555 fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
556 let secp_ctx = Secp256k1::new();
557 let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
558 Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
561 fn payer_pubkey() -> PublicKey {
562 payer_keys().public_key()
565 fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
566 let secp_ctx = Secp256k1::new();
567 let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
568 Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
571 fn recipient_pubkey() -> PublicKey {
572 let secp_ctx = Secp256k1::new();
573 KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap()).public_key()
577 fn builds_invoice_request_with_defaults() {
578 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
581 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
583 .sign(payer_sign).unwrap();
585 let mut buffer = Vec::new();
586 invoice_request.write(&mut buffer).unwrap();
588 assert_eq!(invoice_request.bytes, buffer.as_slice());
589 assert_eq!(invoice_request.metadata(), &[1; 32]);
590 assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
591 assert_eq!(invoice_request.amount_msats(), None);
592 assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::empty());
593 assert_eq!(invoice_request.quantity(), None);
594 assert_eq!(invoice_request.payer_id(), payer_pubkey());
595 assert_eq!(invoice_request.payer_note(), None);
597 merkle::verify_signature(
598 &invoice_request.signature, SIGNATURE_TAG, &invoice_request.bytes, payer_pubkey()
603 invoice_request.as_tlv_stream(),
605 PayerTlvStreamRef { metadata: Some(&vec![1; 32]) },
611 description: Some(&String::from("foo")),
613 absolute_expiry: None,
617 node_id: Some(&recipient_pubkey()),
619 InvoiceRequestTlvStreamRef {
624 payer_id: Some(&payer_pubkey()),
627 SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
631 if let Err(e) = InvoiceRequest::try_from(buffer) {
632 panic!("error parsing invoice request: {:?}", e);
636 #[cfg(feature = "std")]
638 fn builds_invoice_request_from_offer_with_expiration() {
639 let future_expiry = Duration::from_secs(u64::max_value());
640 let past_expiry = Duration::from_secs(0);
642 if let Err(e) = OfferBuilder::new("foo".into(), recipient_pubkey())
644 .absolute_expiry(future_expiry)
646 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
649 panic!("error building invoice_request: {:?}", e);
652 match OfferBuilder::new("foo".into(), recipient_pubkey())
654 .absolute_expiry(past_expiry)
656 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
659 Ok(_) => panic!("expected error"),
660 Err(e) => assert_eq!(e, SemanticError::AlreadyExpired),
665 fn builds_invoice_request_with_chain() {
666 let mainnet = ChainHash::using_genesis_block(Network::Bitcoin);
667 let testnet = ChainHash::using_genesis_block(Network::Testnet);
669 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
672 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
673 .chain(Network::Bitcoin).unwrap()
675 .sign(payer_sign).unwrap();
676 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
677 assert_eq!(invoice_request.chain(), mainnet);
678 assert_eq!(tlv_stream.chain, None);
680 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
682 .chain(Network::Testnet)
684 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
685 .chain(Network::Testnet).unwrap()
687 .sign(payer_sign).unwrap();
688 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
689 assert_eq!(invoice_request.chain(), testnet);
690 assert_eq!(tlv_stream.chain, Some(&testnet));
692 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
694 .chain(Network::Bitcoin)
695 .chain(Network::Testnet)
697 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
698 .chain(Network::Bitcoin).unwrap()
700 .sign(payer_sign).unwrap();
701 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
702 assert_eq!(invoice_request.chain(), mainnet);
703 assert_eq!(tlv_stream.chain, None);
705 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
707 .chain(Network::Bitcoin)
708 .chain(Network::Testnet)
710 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
711 .chain(Network::Bitcoin).unwrap()
712 .chain(Network::Testnet).unwrap()
714 .sign(payer_sign).unwrap();
715 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
716 assert_eq!(invoice_request.chain(), testnet);
717 assert_eq!(tlv_stream.chain, Some(&testnet));
719 match OfferBuilder::new("foo".into(), recipient_pubkey())
721 .chain(Network::Testnet)
723 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
724 .chain(Network::Bitcoin)
726 Ok(_) => panic!("expected error"),
727 Err(e) => assert_eq!(e, SemanticError::UnsupportedChain),
730 match OfferBuilder::new("foo".into(), recipient_pubkey())
732 .chain(Network::Testnet)
734 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
737 Ok(_) => panic!("expected error"),
738 Err(e) => assert_eq!(e, SemanticError::UnsupportedChain),
743 fn builds_invoice_request_with_amount() {
744 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
747 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
748 .amount_msats(1000).unwrap()
750 .sign(payer_sign).unwrap();
751 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
752 assert_eq!(invoice_request.amount_msats(), Some(1000));
753 assert_eq!(tlv_stream.amount, Some(1000));
755 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
758 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
759 .amount_msats(1001).unwrap()
760 .amount_msats(1000).unwrap()
762 .sign(payer_sign).unwrap();
763 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
764 assert_eq!(invoice_request.amount_msats(), Some(1000));
765 assert_eq!(tlv_stream.amount, Some(1000));
767 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
770 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
771 .amount_msats(1001).unwrap()
773 .sign(payer_sign).unwrap();
774 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
775 assert_eq!(invoice_request.amount_msats(), Some(1001));
776 assert_eq!(tlv_stream.amount, Some(1001));
778 match OfferBuilder::new("foo".into(), recipient_pubkey())
781 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
784 Ok(_) => panic!("expected error"),
785 Err(e) => assert_eq!(e, SemanticError::InsufficientAmount),
788 match OfferBuilder::new("foo".into(), recipient_pubkey())
790 .supported_quantity(Quantity::Unbounded)
792 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
793 .quantity(2).unwrap()
796 Ok(_) => panic!("expected error"),
797 Err(e) => assert_eq!(e, SemanticError::InsufficientAmount),
800 match OfferBuilder::new("foo".into(), recipient_pubkey())
803 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
804 .amount_msats(MAX_VALUE_MSAT + 1)
806 Ok(_) => panic!("expected error"),
807 Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
810 match OfferBuilder::new("foo".into(), recipient_pubkey())
812 .supported_quantity(Quantity::Unbounded)
814 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
815 .amount_msats(1000).unwrap()
816 .quantity(2).unwrap()
819 Ok(_) => panic!("expected error"),
820 Err(e) => assert_eq!(e, SemanticError::InsufficientAmount),
823 match OfferBuilder::new("foo".into(), recipient_pubkey())
825 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
828 Ok(_) => panic!("expected error"),
829 Err(e) => assert_eq!(e, SemanticError::MissingAmount),
834 fn builds_invoice_request_with_features() {
835 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
838 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
839 .features_unchecked(InvoiceRequestFeatures::unknown())
841 .sign(payer_sign).unwrap();
842 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
843 assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::unknown());
844 assert_eq!(tlv_stream.features, Some(&InvoiceRequestFeatures::unknown()));
846 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
849 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
850 .features_unchecked(InvoiceRequestFeatures::unknown())
851 .features_unchecked(InvoiceRequestFeatures::empty())
853 .sign(payer_sign).unwrap();
854 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
855 assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::empty());
856 assert_eq!(tlv_stream.features, None);
860 fn builds_invoice_request_with_quantity() {
861 let one = NonZeroU64::new(1).unwrap();
862 let ten = NonZeroU64::new(10).unwrap();
864 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
866 .supported_quantity(Quantity::One)
868 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
870 .sign(payer_sign).unwrap();
871 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
872 assert_eq!(invoice_request.quantity(), None);
873 assert_eq!(tlv_stream.quantity, None);
875 match OfferBuilder::new("foo".into(), recipient_pubkey())
877 .supported_quantity(Quantity::One)
879 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
880 .amount_msats(2_000).unwrap()
883 Ok(_) => panic!("expected error"),
884 Err(e) => assert_eq!(e, SemanticError::UnexpectedQuantity),
887 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
889 .supported_quantity(Quantity::Bounded(ten))
891 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
892 .amount_msats(10_000).unwrap()
893 .quantity(10).unwrap()
895 .sign(payer_sign).unwrap();
896 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
897 assert_eq!(invoice_request.amount_msats(), Some(10_000));
898 assert_eq!(tlv_stream.amount, Some(10_000));
900 match OfferBuilder::new("foo".into(), recipient_pubkey())
902 .supported_quantity(Quantity::Bounded(ten))
904 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
905 .amount_msats(11_000).unwrap()
908 Ok(_) => panic!("expected error"),
909 Err(e) => assert_eq!(e, SemanticError::InvalidQuantity),
912 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
914 .supported_quantity(Quantity::Unbounded)
916 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
917 .amount_msats(2_000).unwrap()
918 .quantity(2).unwrap()
920 .sign(payer_sign).unwrap();
921 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
922 assert_eq!(invoice_request.amount_msats(), Some(2_000));
923 assert_eq!(tlv_stream.amount, Some(2_000));
925 match OfferBuilder::new("foo".into(), recipient_pubkey())
927 .supported_quantity(Quantity::Unbounded)
929 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
932 Ok(_) => panic!("expected error"),
933 Err(e) => assert_eq!(e, SemanticError::MissingQuantity),
936 match OfferBuilder::new("foo".into(), recipient_pubkey())
938 .supported_quantity(Quantity::Bounded(one))
940 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
943 Ok(_) => panic!("expected error"),
944 Err(e) => assert_eq!(e, SemanticError::MissingQuantity),
949 fn builds_invoice_request_with_payer_note() {
950 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
953 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
954 .payer_note("bar".into())
956 .sign(payer_sign).unwrap();
957 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
958 assert_eq!(invoice_request.payer_note(), Some(PrintableString("bar")));
959 assert_eq!(tlv_stream.payer_note, Some(&String::from("bar")));
961 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
964 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
965 .payer_note("bar".into())
966 .payer_note("baz".into())
968 .sign(payer_sign).unwrap();
969 let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
970 assert_eq!(invoice_request.payer_note(), Some(PrintableString("baz")));
971 assert_eq!(tlv_stream.payer_note, Some(&String::from("baz")));
975 fn fails_signing_invoice_request() {
976 match OfferBuilder::new("foo".into(), recipient_pubkey())
979 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
983 Ok(_) => panic!("expected error"),
984 Err(e) => assert_eq!(e, SignError::Signing(())),
987 match OfferBuilder::new("foo".into(), recipient_pubkey())
990 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
992 .sign(recipient_sign)
994 Ok(_) => panic!("expected error"),
995 Err(e) => assert_eq!(e, SignError::Verification(secp256k1::Error::InvalidSignature)),
1000 fn parses_invoice_request_with_metadata() {
1001 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1004 .request_invoice(vec![42; 32], payer_pubkey()).unwrap()
1006 .sign(payer_sign).unwrap();
1008 let mut buffer = Vec::new();
1009 invoice_request.write(&mut buffer).unwrap();
1011 if let Err(e) = InvoiceRequest::try_from(buffer) {
1012 panic!("error parsing invoice_request: {:?}", e);
1017 fn parses_invoice_request_with_chain() {
1018 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1021 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1022 .chain(Network::Bitcoin).unwrap()
1024 .sign(payer_sign).unwrap();
1026 let mut buffer = Vec::new();
1027 invoice_request.write(&mut buffer).unwrap();
1029 if let Err(e) = InvoiceRequest::try_from(buffer) {
1030 panic!("error parsing invoice_request: {:?}", e);
1033 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1036 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1037 .chain_unchecked(Network::Testnet)
1039 .sign(payer_sign).unwrap();
1041 let mut buffer = Vec::new();
1042 invoice_request.write(&mut buffer).unwrap();
1044 match InvoiceRequest::try_from(buffer) {
1045 Ok(_) => panic!("expected error"),
1046 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnsupportedChain)),
1051 fn parses_invoice_request_with_amount() {
1052 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1055 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1057 .sign(payer_sign).unwrap();
1059 let mut buffer = Vec::new();
1060 invoice_request.write(&mut buffer).unwrap();
1062 if let Err(e) = InvoiceRequest::try_from(buffer) {
1063 panic!("error parsing invoice_request: {:?}", e);
1066 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1068 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1069 .amount_msats(1000).unwrap()
1071 .sign(payer_sign).unwrap();
1073 let mut buffer = Vec::new();
1074 invoice_request.write(&mut buffer).unwrap();
1076 if let Err(e) = InvoiceRequest::try_from(buffer) {
1077 panic!("error parsing invoice_request: {:?}", e);
1080 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1082 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1084 .sign(payer_sign).unwrap();
1086 let mut buffer = Vec::new();
1087 invoice_request.write(&mut buffer).unwrap();
1089 match InvoiceRequest::try_from(buffer) {
1090 Ok(_) => panic!("expected error"),
1091 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingAmount)),
1094 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1097 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1098 .amount_msats_unchecked(999)
1100 .sign(payer_sign).unwrap();
1102 let mut buffer = Vec::new();
1103 invoice_request.write(&mut buffer).unwrap();
1105 match InvoiceRequest::try_from(buffer) {
1106 Ok(_) => panic!("expected error"),
1107 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InsufficientAmount)),
1110 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1111 .amount(Amount::Currency { iso4217_code: *b"USD", amount: 1000 })
1113 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1115 .sign(payer_sign).unwrap();
1117 let mut buffer = Vec::new();
1118 invoice_request.write(&mut buffer).unwrap();
1120 match InvoiceRequest::try_from(buffer) {
1121 Ok(_) => panic!("expected error"),
1123 assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnsupportedCurrency));
1129 fn parses_invoice_request_with_quantity() {
1130 let one = NonZeroU64::new(1).unwrap();
1131 let ten = NonZeroU64::new(10).unwrap();
1133 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1135 .supported_quantity(Quantity::One)
1137 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1139 .sign(payer_sign).unwrap();
1141 let mut buffer = Vec::new();
1142 invoice_request.write(&mut buffer).unwrap();
1144 if let Err(e) = InvoiceRequest::try_from(buffer) {
1145 panic!("error parsing invoice_request: {:?}", e);
1148 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1150 .supported_quantity(Quantity::One)
1152 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1153 .amount_msats(2_000).unwrap()
1154 .quantity_unchecked(2)
1156 .sign(payer_sign).unwrap();
1158 let mut buffer = Vec::new();
1159 invoice_request.write(&mut buffer).unwrap();
1161 match InvoiceRequest::try_from(buffer) {
1162 Ok(_) => panic!("expected error"),
1164 assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedQuantity));
1168 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1170 .supported_quantity(Quantity::Bounded(ten))
1172 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1173 .amount_msats(10_000).unwrap()
1174 .quantity(10).unwrap()
1176 .sign(payer_sign).unwrap();
1178 let mut buffer = Vec::new();
1179 invoice_request.write(&mut buffer).unwrap();
1181 if let Err(e) = InvoiceRequest::try_from(buffer) {
1182 panic!("error parsing invoice_request: {:?}", e);
1185 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1187 .supported_quantity(Quantity::Bounded(ten))
1189 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1190 .amount_msats(11_000).unwrap()
1191 .quantity_unchecked(11)
1193 .sign(payer_sign).unwrap();
1195 let mut buffer = Vec::new();
1196 invoice_request.write(&mut buffer).unwrap();
1198 match InvoiceRequest::try_from(buffer) {
1199 Ok(_) => panic!("expected error"),
1200 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidQuantity)),
1203 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1205 .supported_quantity(Quantity::Unbounded)
1207 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1208 .amount_msats(2_000).unwrap()
1209 .quantity(2).unwrap()
1211 .sign(payer_sign).unwrap();
1213 let mut buffer = Vec::new();
1214 invoice_request.write(&mut buffer).unwrap();
1216 if let Err(e) = InvoiceRequest::try_from(buffer) {
1217 panic!("error parsing invoice_request: {:?}", e);
1220 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1222 .supported_quantity(Quantity::Unbounded)
1224 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1226 .sign(payer_sign).unwrap();
1228 let mut buffer = Vec::new();
1229 invoice_request.write(&mut buffer).unwrap();
1231 match InvoiceRequest::try_from(buffer) {
1232 Ok(_) => panic!("expected error"),
1233 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingQuantity)),
1236 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1238 .supported_quantity(Quantity::Bounded(one))
1240 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1242 .sign(payer_sign).unwrap();
1244 let mut buffer = Vec::new();
1245 invoice_request.write(&mut buffer).unwrap();
1247 match InvoiceRequest::try_from(buffer) {
1248 Ok(_) => panic!("expected error"),
1249 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingQuantity)),
1254 fn fails_parsing_invoice_request_without_metadata() {
1255 let offer = OfferBuilder::new("foo".into(), recipient_pubkey())
1258 let unsigned_invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1260 let mut tlv_stream = unsigned_invoice_request.invoice_request.as_tlv_stream();
1261 tlv_stream.0.metadata = None;
1263 let mut buffer = Vec::new();
1264 tlv_stream.write(&mut buffer).unwrap();
1266 match InvoiceRequest::try_from(buffer) {
1267 Ok(_) => panic!("expected error"),
1269 assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPayerMetadata));
1275 fn fails_parsing_invoice_request_without_payer_id() {
1276 let offer = OfferBuilder::new("foo".into(), recipient_pubkey())
1279 let unsigned_invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1281 let mut tlv_stream = unsigned_invoice_request.invoice_request.as_tlv_stream();
1282 tlv_stream.2.payer_id = None;
1284 let mut buffer = Vec::new();
1285 tlv_stream.write(&mut buffer).unwrap();
1287 match InvoiceRequest::try_from(buffer) {
1288 Ok(_) => panic!("expected error"),
1289 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPayerId)),
1294 fn fails_parsing_invoice_request_without_node_id() {
1295 let offer = OfferBuilder::new("foo".into(), recipient_pubkey())
1298 let unsigned_invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1300 let mut tlv_stream = unsigned_invoice_request.invoice_request.as_tlv_stream();
1301 tlv_stream.1.node_id = None;
1303 let mut buffer = Vec::new();
1304 tlv_stream.write(&mut buffer).unwrap();
1306 match InvoiceRequest::try_from(buffer) {
1307 Ok(_) => panic!("expected error"),
1309 assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey));
1315 fn fails_parsing_invoice_request_without_signature() {
1316 let mut buffer = Vec::new();
1317 OfferBuilder::new("foo".into(), recipient_pubkey())
1320 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1323 .write(&mut buffer).unwrap();
1325 match InvoiceRequest::try_from(buffer) {
1326 Ok(_) => panic!("expected error"),
1327 Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSignature)),
1332 fn fails_parsing_invoice_request_with_invalid_signature() {
1333 let mut invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1336 .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1338 .sign(payer_sign).unwrap();
1339 let last_signature_byte = invoice_request.bytes.last_mut().unwrap();
1340 *last_signature_byte = last_signature_byte.wrapping_add(1);
1342 let mut buffer = Vec::new();
1343 invoice_request.write(&mut buffer).unwrap();
1345 match InvoiceRequest::try_from(buffer) {
1346 Ok(_) => panic!("expected error"),
1348 assert_eq!(e, ParseError::InvalidSignature(secp256k1::Error::InvalidSignature));
1354 fn fails_parsing_invoice_request_with_extra_tlv_records() {
1355 let secp_ctx = Secp256k1::new();
1356 let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
1357 let invoice_request = OfferBuilder::new("foo".into(), keys.public_key())
1360 .request_invoice(vec![1; 32], keys.public_key()).unwrap()
1362 .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
1365 let mut encoded_invoice_request = Vec::new();
1366 invoice_request.write(&mut encoded_invoice_request).unwrap();
1367 BigSize(1002).write(&mut encoded_invoice_request).unwrap();
1368 BigSize(32).write(&mut encoded_invoice_request).unwrap();
1369 [42u8; 32].write(&mut encoded_invoice_request).unwrap();
1371 match InvoiceRequest::try_from(encoded_invoice_request) {
1372 Ok(_) => panic!("expected error"),
1373 Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)),