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::merkle::SignError;
15 use crate::offers::parse::Bolt12SemanticError;
16 use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};
17 use crate::util::string::UntrustedString;
19 #[allow(unused_imports)]
20 use crate::prelude::*;
22 /// An error in response to an [`InvoiceRequest`] or an [`Bolt12Invoice`].
24 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
25 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
26 #[derive(Clone, Debug)]
27 #[cfg_attr(test, derive(PartialEq))]
28 pub struct InvoiceError {
29 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
31 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
32 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
33 pub erroneous_field: Option<ErroneousField>,
35 /// An explanation of the error.
36 pub message: UntrustedString,
39 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
41 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
42 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
43 #[derive(Clone, Debug)]
44 #[cfg_attr(test, derive(PartialEq))]
45 pub struct ErroneousField {
46 /// The type number of the TLV field containing the error.
47 pub tlv_fieldnum: u64,
49 /// A value to use for the TLV field to avoid the error.
50 pub suggested_value: Option<Vec<u8>>,
54 /// Creates an [`InvoiceError`] with the given message.
55 pub fn from_string(s: String) -> Self {
57 erroneous_field: None,
58 message: UntrustedString(s),
63 impl core::fmt::Display for InvoiceError {
64 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
69 impl Writeable for InvoiceError {
70 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
71 let tlv_fieldnum = self.erroneous_field.as_ref().map(|f| f.tlv_fieldnum);
73 self.erroneous_field.as_ref().and_then(|f| f.suggested_value.as_ref());
74 write_tlv_fields!(writer, {
75 (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
76 (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
77 (5, WithoutLength(&self.message), required),
83 impl Readable for InvoiceError {
84 fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
85 _init_and_read_len_prefixed_tlv_fields!(reader, {
86 (1, erroneous_field, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
87 (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
88 (5, error, (option, encoding: (UntrustedString, WithoutLength))),
91 let erroneous_field = match (erroneous_field, suggested_value) {
93 (None, Some(_)) => return Err(DecodeError::InvalidValue),
94 (Some(tlv_fieldnum), suggested_value) => {
95 Some(ErroneousField { tlv_fieldnum, suggested_value })
99 let message = match error {
100 None => return Err(DecodeError::InvalidValue),
101 Some(error) => error,
104 Ok(InvoiceError { erroneous_field, message })
108 impl From<Bolt12SemanticError> for InvoiceError {
109 fn from(error: Bolt12SemanticError) -> Self {
111 erroneous_field: None,
112 message: UntrustedString(format!("{:?}", error)),
117 impl From<SignError> for InvoiceError {
118 fn from(error: SignError) -> Self {
119 let message = match error {
120 SignError::Signing => "Failed signing invoice",
121 SignError::Verification(_) => "Failed invoice signature verification",
124 erroneous_field: None,
125 message: UntrustedString(message.to_string()),
132 use super::{ErroneousField, InvoiceError};
134 use crate::ln::msgs::DecodeError;
135 use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, VecWriter, WithoutLength, Writeable};
136 use crate::util::string::UntrustedString;
139 fn parses_invoice_error_without_erroneous_field() {
140 let mut writer = VecWriter(Vec::new());
141 let invoice_error = InvoiceError {
142 erroneous_field: None,
143 message: UntrustedString("Invalid value".to_string()),
145 invoice_error.write(&mut writer).unwrap();
147 let buffer = writer.0;
148 match InvoiceError::read(&mut &buffer[..]) {
149 Ok(invoice_error) => {
150 assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
151 assert_eq!(invoice_error.erroneous_field, None);
153 Err(e) => panic!("Unexpected error: {:?}", e),
158 fn parses_invoice_error_with_erroneous_field() {
159 let mut writer = VecWriter(Vec::new());
160 let invoice_error = InvoiceError {
161 erroneous_field: Some(ErroneousField {
163 suggested_value: Some(vec![42; 32]),
165 message: UntrustedString("Invalid value".to_string()),
167 invoice_error.write(&mut writer).unwrap();
169 let buffer = writer.0;
170 match InvoiceError::read(&mut &buffer[..]) {
171 Ok(invoice_error) => {
172 assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
174 invoice_error.erroneous_field,
175 Some(ErroneousField { tlv_fieldnum: 42, suggested_value: Some(vec![42; 32]) }),
178 Err(e) => panic!("Unexpected error: {:?}", e),
183 fn parses_invoice_error_without_suggested_value() {
184 let mut writer = VecWriter(Vec::new());
185 let invoice_error = InvoiceError {
186 erroneous_field: Some(ErroneousField {
188 suggested_value: None,
190 message: UntrustedString("Invalid value".to_string()),
192 invoice_error.write(&mut writer).unwrap();
194 let buffer = writer.0;
195 match InvoiceError::read(&mut &buffer[..]) {
196 Ok(invoice_error) => {
197 assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
199 invoice_error.erroneous_field,
200 Some(ErroneousField { tlv_fieldnum: 42, suggested_value: None }),
203 Err(e) => panic!("Unexpected error: {:?}", e),
208 fn fails_parsing_invoice_error_without_message() {
209 let tlv_fieldnum: Option<u64> = None;
210 let suggested_value: Option<&Vec<u8>> = None;
211 let error: Option<&String> = None;
213 let mut writer = VecWriter(Vec::new());
214 let mut write_tlv = || -> Result<(), DecodeError> {
215 write_tlv_fields!(&mut writer, {
216 (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
217 (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
218 (5, error, (option, encoding: (String, WithoutLength))),
222 write_tlv().unwrap();
224 let buffer = writer.0;
225 match InvoiceError::read(&mut &buffer[..]) {
226 Ok(_) => panic!("Expected error"),
228 assert_eq!(e, DecodeError::InvalidValue);
234 fn fails_parsing_invoice_error_without_field() {
235 let tlv_fieldnum: Option<u64> = None;
236 let suggested_value = vec![42; 32];
237 let error = "Invalid value".to_string();
239 let mut writer = VecWriter(Vec::new());
240 let mut write_tlv = || -> Result<(), DecodeError> {
241 write_tlv_fields!(&mut writer, {
242 (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
243 (3, Some(&suggested_value), (option, encoding: (Vec<u8>, WithoutLength))),
244 (5, Some(&error), (option, encoding: (String, WithoutLength))),
248 write_tlv().unwrap();
250 let buffer = writer.0;
251 match InvoiceError::read(&mut &buffer[..]) {
252 Ok(_) => panic!("Expected error"),
254 assert_eq!(e, DecodeError::InvalidValue);