X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Foffers%2Finvoice_error.rs;h=5ae5f457a84822172d970035286a562be0d67973;hb=6264a442599fbadd54b0cc24949fc0fd9de08fb3;hp=122049b9295438b0147224a76c6efbc75455146c;hpb=d7e3320c030654ca3ff2b6ffd83ae65dfbe5e3c8;p=rust-lightning diff --git a/lightning/src/offers/invoice_error.rs b/lightning/src/offers/invoice_error.rs index 122049b9..5ae5f457 100644 --- a/lightning/src/offers/invoice_error.rs +++ b/lightning/src/offers/invoice_error.rs @@ -11,10 +11,12 @@ use crate::io; use crate::ln::msgs::DecodeError; +use crate::offers::merkle::SignError; use crate::offers::parse::Bolt12SemanticError; use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer}; use crate::util::string::UntrustedString; +#[allow(unused_imports)] use crate::prelude::*; /// An error in response to an [`InvoiceRequest`] or an [`Bolt12Invoice`]. @@ -48,6 +50,16 @@ pub struct ErroneousField { pub suggested_value: Option>, } +impl InvoiceError { + /// Creates an [`InvoiceError`] with the given message. + pub fn from_string(s: String) -> Self { + Self { + erroneous_field: None, + message: UntrustedString(s), + } + } +} + impl core::fmt::Display for InvoiceError { fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { self.message.fmt(f) @@ -70,7 +82,7 @@ impl Writeable for InvoiceError { impl Readable for InvoiceError { fn read(reader: &mut R) -> Result { - _init_and_read_tlv_fields!(reader, { + _init_and_read_len_prefixed_tlv_fields!(reader, { (1, erroneous_field, (option, encoding: (u64, HighZeroBytesDroppedBigSize))), (3, suggested_value, (option, encoding: (Vec, WithoutLength))), (5, error, (option, encoding: (UntrustedString, WithoutLength))), @@ -102,6 +114,19 @@ impl From for InvoiceError { } } +impl From for InvoiceError { + fn from(error: SignError) -> Self { + let message = match error { + SignError::Signing => "Failed signing invoice", + SignError::Verification(_) => "Failed invoice signature verification", + }; + InvoiceError { + erroneous_field: None, + message: UntrustedString(message.to_string()), + } + } +} + #[cfg(test)] mod tests { use super::{ErroneousField, InvoiceError};