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_error` messages.
13 use crate::ln::msgs::DecodeError;
14 use crate::offers::parse::Bolt12SemanticError;
15 use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};
16 use crate::util::string::UntrustedString;
18 use crate::prelude::*;
20 /// An error in response to an [`InvoiceRequest`] or an [`Bolt12Invoice`].
22 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
23 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
24 #[derive(Clone, Debug)]
25 #[cfg_attr(test, derive(PartialEq))]
26 pub struct InvoiceError {
27 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
29 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
30 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
31 pub erroneous_field: Option<ErroneousField>,
33 /// An explanation of the error.
34 pub message: UntrustedString,
37 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
39 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
40 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
41 #[derive(Clone, Debug)]
42 #[cfg_attr(test, derive(PartialEq))]
43 pub struct ErroneousField {
44 /// The type number of the TLV field containing the error.
45 pub tlv_fieldnum: u64,
47 /// A value to use for the TLV field to avoid the error.
48 pub suggested_value: Option<Vec<u8>>,
52 /// Creates an [`InvoiceError`] with the given message.
53 pub fn from_string(s: String) -> Self {
55 erroneous_field: None,
56 message: UntrustedString(s),
61 impl core::fmt::Display for InvoiceError {
62 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
67 impl Writeable for InvoiceError {
68 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
69 let tlv_fieldnum = self.erroneous_field.as_ref().map(|f| f.tlv_fieldnum);
71 self.erroneous_field.as_ref().and_then(|f| f.suggested_value.as_ref());
72 write_tlv_fields!(writer, {
73 (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
74 (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
75 (5, WithoutLength(&self.message), required),
81 impl Readable for InvoiceError {
82 fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
83 _init_and_read_len_prefixed_tlv_fields!(reader, {
84 (1, erroneous_field, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
85 (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
86 (5, error, (option, encoding: (UntrustedString, WithoutLength))),
89 let erroneous_field = match (erroneous_field, suggested_value) {
91 (None, Some(_)) => return Err(DecodeError::InvalidValue),
92 (Some(tlv_fieldnum), suggested_value) => {
93 Some(ErroneousField { tlv_fieldnum, suggested_value })
97 let message = match error {
98 None => return Err(DecodeError::InvalidValue),
102 Ok(InvoiceError { erroneous_field, message })
106 impl From<Bolt12SemanticError> for InvoiceError {
107 fn from(error: Bolt12SemanticError) -> Self {
109 erroneous_field: None,
110 message: UntrustedString(format!("{:?}", error)),
117 use super::{ErroneousField, InvoiceError};
119 use crate::ln::msgs::DecodeError;
120 use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, VecWriter, WithoutLength, Writeable};
121 use crate::util::string::UntrustedString;
124 fn parses_invoice_error_without_erroneous_field() {
125 let mut writer = VecWriter(Vec::new());
126 let invoice_error = InvoiceError {
127 erroneous_field: None,
128 message: UntrustedString("Invalid value".to_string()),
130 invoice_error.write(&mut writer).unwrap();
132 let buffer = writer.0;
133 match InvoiceError::read(&mut &buffer[..]) {
134 Ok(invoice_error) => {
135 assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
136 assert_eq!(invoice_error.erroneous_field, None);
138 Err(e) => panic!("Unexpected error: {:?}", e),
143 fn parses_invoice_error_with_erroneous_field() {
144 let mut writer = VecWriter(Vec::new());
145 let invoice_error = InvoiceError {
146 erroneous_field: Some(ErroneousField {
148 suggested_value: Some(vec![42; 32]),
150 message: UntrustedString("Invalid value".to_string()),
152 invoice_error.write(&mut writer).unwrap();
154 let buffer = writer.0;
155 match InvoiceError::read(&mut &buffer[..]) {
156 Ok(invoice_error) => {
157 assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
159 invoice_error.erroneous_field,
160 Some(ErroneousField { tlv_fieldnum: 42, suggested_value: Some(vec![42; 32]) }),
163 Err(e) => panic!("Unexpected error: {:?}", e),
168 fn parses_invoice_error_without_suggested_value() {
169 let mut writer = VecWriter(Vec::new());
170 let invoice_error = InvoiceError {
171 erroneous_field: Some(ErroneousField {
173 suggested_value: None,
175 message: UntrustedString("Invalid value".to_string()),
177 invoice_error.write(&mut writer).unwrap();
179 let buffer = writer.0;
180 match InvoiceError::read(&mut &buffer[..]) {
181 Ok(invoice_error) => {
182 assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
184 invoice_error.erroneous_field,
185 Some(ErroneousField { tlv_fieldnum: 42, suggested_value: None }),
188 Err(e) => panic!("Unexpected error: {:?}", e),
193 fn fails_parsing_invoice_error_without_message() {
194 let tlv_fieldnum: Option<u64> = None;
195 let suggested_value: Option<&Vec<u8>> = None;
196 let error: Option<&String> = None;
198 let mut writer = VecWriter(Vec::new());
199 let mut write_tlv = || -> Result<(), DecodeError> {
200 write_tlv_fields!(&mut writer, {
201 (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
202 (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
203 (5, error, (option, encoding: (String, WithoutLength))),
207 write_tlv().unwrap();
209 let buffer = writer.0;
210 match InvoiceError::read(&mut &buffer[..]) {
211 Ok(_) => panic!("Expected error"),
213 assert_eq!(e, DecodeError::InvalidValue);
219 fn fails_parsing_invoice_error_without_field() {
220 let tlv_fieldnum: Option<u64> = None;
221 let suggested_value = vec![42; 32];
222 let error = "Invalid value".to_string();
224 let mut writer = VecWriter(Vec::new());
225 let mut write_tlv = || -> Result<(), DecodeError> {
226 write_tlv_fields!(&mut writer, {
227 (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
228 (3, Some(&suggested_value), (option, encoding: (Vec<u8>, WithoutLength))),
229 (5, Some(&error), (option, encoding: (String, WithoutLength))),
233 write_tlv().unwrap();
235 let buffer = writer.0;
236 match InvoiceError::read(&mut &buffer[..]) {
237 Ok(_) => panic!("Expected error"),
239 assert_eq!(e, DecodeError::InvalidValue);