From: Jeffrey Czyz Date: Thu, 13 Jul 2023 21:09:43 +0000 (-0500) Subject: Qualify the BOLT 12 parse error X-Git-Tag: v0.0.116-rc1~3^2~8 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=5627d7cc1f94815915422e669e2f29a1a7854fc4;p=rust-lightning Qualify the BOLT 12 parse error To avoid a naming conflict in bindings with BOLT 11 parse error, qualify the BOLT 12 parse error type. --- diff --git a/fuzz/src/bech32_parse.rs b/fuzz/src/bech32_parse.rs index f3dd5ac9d..ff465ceea 100644 --- a/fuzz/src/bech32_parse.rs +++ b/fuzz/src/bech32_parse.rs @@ -9,7 +9,7 @@ use crate::utils::test_logger; use core::convert::TryFrom; -use lightning::offers::parse::{Bech32Encode, ParseError}; +use lightning::offers::parse::{Bech32Encode, Bolt12ParseError}; #[inline] pub fn do_test(data: &[u8], _out: Out) { @@ -35,8 +35,8 @@ impl AsRef<[u8]> for Bytes { } impl TryFrom> for Bytes { - type Error = ParseError; - fn try_from(data: Vec) -> Result { + type Error = Bolt12ParseError; + fn try_from(data: Vec) -> Result { Ok(Bytes(data)) } } diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 8e0c28b60..03bb95536 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -34,7 +34,7 @@ //! # fn create_payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> { unimplemented!() } //! # fn create_payment_hash() -> PaymentHash { unimplemented!() } //! # -//! # fn parse_invoice_request(bytes: Vec) -> Result<(), lightning::offers::parse::ParseError> { +//! # fn parse_invoice_request(bytes: Vec) -> Result<(), lightning::offers::parse::Bolt12ParseError> { //! let payment_paths = create_payment_paths(); //! let payment_hash = create_payment_hash(); //! let secp_ctx = Secp256k1::new(); @@ -62,7 +62,7 @@ //! # Ok(()) //! # } //! -//! # fn parse_refund(bytes: Vec) -> Result<(), lightning::offers::parse::ParseError> { +//! # fn parse_refund(bytes: Vec) -> Result<(), lightning::offers::parse::Bolt12ParseError> { //! # let payment_paths = create_payment_paths(); //! # let payment_hash = create_payment_hash(); //! # let secp_ctx = Secp256k1::new(); @@ -112,7 +112,7 @@ use crate::ln::msgs::DecodeError; use crate::offers::invoice_request::{INVOICE_REQUEST_PAYER_ID_TYPE, INVOICE_REQUEST_TYPES, IV_BYTES as INVOICE_REQUEST_IV_BYTES, InvoiceRequest, InvoiceRequestContents, InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef}; use crate::offers::merkle::{SignError, SignatureTlvStream, SignatureTlvStreamRef, TlvStream, WithoutSignatures, self}; use crate::offers::offer::{Amount, OFFER_TYPES, OfferTlvStream, OfferTlvStreamRef}; -use crate::offers::parse::{ParseError, ParsedMessage, SemanticError}; +use crate::offers::parse::{Bolt12ParseError, ParsedMessage, SemanticError}; use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef}; use crate::offers::refund::{IV_BYTES as REFUND_IV_BYTES, Refund, RefundContents}; use crate::offers::signer; @@ -728,7 +728,7 @@ impl Writeable for InvoiceContents { } impl TryFrom> for Bolt12Invoice { - type Error = ParseError; + type Error = Bolt12ParseError; fn try_from(bytes: Vec) -> Result { let parsed_invoice = ParsedMessage::::try_from(bytes)?; @@ -840,7 +840,7 @@ type PartialInvoiceTlvStreamRef<'a> = ( ); impl TryFrom> for Bolt12Invoice { - type Error = ParseError; + type Error = Bolt12ParseError; fn try_from(invoice: ParsedMessage) -> Result { let ParsedMessage { bytes, tlv_stream } = invoice; @@ -853,7 +853,7 @@ impl TryFrom> for Bolt12Invoice { )?; let signature = match signature { - None => return Err(ParseError::InvalidSemantics(SemanticError::MissingSignature)), + None => return Err(Bolt12ParseError::InvalidSemantics(SemanticError::MissingSignature)), Some(signature) => signature, }; let pubkey = contents.fields().signing_pubkey; @@ -961,7 +961,7 @@ mod tests { use crate::offers::invoice_request::InvoiceRequestTlvStreamRef; use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self}; use crate::offers::offer::{OfferBuilder, OfferTlvStreamRef, Quantity}; - use crate::offers::parse::{ParseError, SemanticError}; + use crate::offers::parse::{Bolt12ParseError, SemanticError}; use crate::offers::payer::PayerTlvStreamRef; use crate::offers::refund::RefundBuilder; use crate::offers::test_utils::*; @@ -1502,7 +1502,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPaths)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPaths)), } let mut tlv_stream = invoice.as_tlv_stream(); @@ -1510,7 +1510,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)), } let empty_payment_paths = vec![]; @@ -1519,7 +1519,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPaths)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPaths)), } let mut payment_paths = payment_paths(); @@ -1529,7 +1529,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)), } } @@ -1558,7 +1558,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingCreationTime)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingCreationTime)); }, } } @@ -1610,7 +1610,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPaymentHash)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPaymentHash)); }, } } @@ -1639,7 +1639,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingAmount)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingAmount)), } } @@ -1758,7 +1758,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey)); }, } @@ -1769,7 +1769,7 @@ mod tests { match Bolt12Invoice::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidSigningPubkey)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidSigningPubkey)); }, } } @@ -1790,7 +1790,7 @@ mod tests { match Bolt12Invoice::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSignature)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSignature)), } } @@ -1814,7 +1814,7 @@ mod tests { match Bolt12Invoice::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSignature(secp256k1::Error::InvalidSignature)); + assert_eq!(e, Bolt12ParseError::InvalidSignature(secp256k1::Error::InvalidSignature)); }, } } @@ -1839,7 +1839,7 @@ mod tests { match Bolt12Invoice::try_from(encoded_invoice) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), } } } diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index f5cb439b5..6c70a5968 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -30,7 +30,7 @@ //! use lightning::offers::offer::Offer; //! use lightning::util::ser::Writeable; //! -//! # fn parse() -> Result<(), lightning::offers::parse::ParseError> { +//! # fn parse() -> Result<(), lightning::offers::parse::Bolt12ParseError> { //! let secp_ctx = Secp256k1::new(); //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?); //! let pubkey = PublicKey::from(keys); @@ -68,7 +68,7 @@ use crate::ln::msgs::DecodeError; use crate::offers::invoice::{BlindedPayInfo, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder}; use crate::offers::merkle::{SignError, SignatureTlvStream, SignatureTlvStreamRef, self}; use crate::offers::offer::{Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef}; -use crate::offers::parse::{ParseError, ParsedMessage, SemanticError}; +use crate::offers::parse::{Bolt12ParseError, ParsedMessage, SemanticError}; use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef}; use crate::offers::signer::{Metadata, MetadataMaterial}; use crate::util::ser::{HighZeroBytesDroppedBigSize, SeekReadable, WithoutLength, Writeable, Writer}; @@ -708,7 +708,7 @@ type PartialInvoiceRequestTlvStreamRef<'a> = ( ); impl TryFrom> for InvoiceRequest { - type Error = ParseError; + type Error = Bolt12ParseError; fn try_from(bytes: Vec) -> Result { let invoice_request = ParsedMessage::::try_from(bytes)?; @@ -722,7 +722,7 @@ impl TryFrom> for InvoiceRequest { )?; let signature = match signature { - None => return Err(ParseError::InvalidSemantics(SemanticError::MissingSignature)), + None => return Err(Bolt12ParseError::InvalidSemantics(SemanticError::MissingSignature)), Some(signature) => signature, }; merkle::verify_signature(&signature, SIGNATURE_TAG, &bytes, contents.payer_id)?; @@ -792,7 +792,7 @@ mod tests { use crate::offers::invoice::{Bolt12Invoice, SIGNATURE_TAG as INVOICE_SIGNATURE_TAG}; use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self}; use crate::offers::offer::{Amount, OfferBuilder, OfferTlvStreamRef, Quantity}; - use crate::offers::parse::{ParseError, SemanticError}; + use crate::offers::parse::{Bolt12ParseError, SemanticError}; use crate::offers::payer::PayerTlvStreamRef; use crate::offers::test_utils::*; use crate::util::ser::{BigSize, Writeable}; @@ -1438,7 +1438,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnsupportedChain)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnsupportedChain)), } } @@ -1483,7 +1483,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingAmount)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingAmount)), } let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey()) @@ -1499,7 +1499,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InsufficientAmount)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InsufficientAmount)), } let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey()) @@ -1515,7 +1515,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnsupportedCurrency)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnsupportedCurrency)); }, } @@ -1533,7 +1533,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidAmount)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidAmount)), } } @@ -1573,7 +1573,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedQuantity)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedQuantity)); }, } @@ -1609,7 +1609,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidQuantity)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidQuantity)), } let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey()) @@ -1642,7 +1642,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingQuantity)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingQuantity)), } let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey()) @@ -1658,7 +1658,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingQuantity)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingQuantity)), } } @@ -1678,7 +1678,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPayerMetadata)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPayerMetadata)); }, } } @@ -1698,7 +1698,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPayerId)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPayerId)), } } @@ -1718,7 +1718,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey)); }, } } @@ -1736,7 +1736,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSignature)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSignature)), } } @@ -1757,7 +1757,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSignature(secp256k1::Error::InvalidSignature)); + assert_eq!(e, Bolt12ParseError::InvalidSignature(secp256k1::Error::InvalidSignature)); }, } } @@ -1782,7 +1782,7 @@ mod tests { match InvoiceRequest::try_from(encoded_invoice_request) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), } } } diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index fd705a901..1168aff94 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -24,7 +24,7 @@ //! //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey}; //! use lightning::offers::offer::{Offer, OfferBuilder, Quantity}; -//! use lightning::offers::parse::ParseError; +//! use lightning::offers::parse::Bolt12ParseError; //! use lightning::util::ser::{Readable, Writeable}; //! //! # use lightning::blinded_path::BlindedPath; @@ -35,7 +35,7 @@ //! # fn create_another_blinded_path() -> BlindedPath { unimplemented!() } //! # //! # #[cfg(feature = "std")] -//! # fn build() -> Result<(), ParseError> { +//! # fn build() -> Result<(), Bolt12ParseError> { //! let secp_ctx = Secp256k1::new(); //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()); //! let pubkey = PublicKey::from(keys); @@ -82,7 +82,7 @@ use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce}; use crate::ln::msgs::MAX_VALUE_MSAT; use crate::offers::invoice_request::{DerivedPayerId, ExplicitPayerId, InvoiceRequestBuilder}; use crate::offers::merkle::TlvStream; -use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError}; +use crate::offers::parse::{Bech32Encode, Bolt12ParseError, ParsedMessage, SemanticError}; use crate::offers::signer::{Metadata, MetadataMaterial, self}; use crate::util::ser::{HighZeroBytesDroppedBigSize, WithoutLength, Writeable, Writer}; use crate::util::string::PrintableString; @@ -768,7 +768,7 @@ impl Bech32Encode for Offer { } impl FromStr for Offer { - type Err = ParseError; + type Err = Bolt12ParseError; fn from_str(s: &str) -> Result::Err> { Self::from_bech32_str(s) @@ -776,7 +776,7 @@ impl FromStr for Offer { } impl TryFrom> for Offer { - type Error = ParseError; + type Error = Bolt12ParseError; fn try_from(bytes: Vec) -> Result { let offer = ParsedMessage::::try_from(bytes)?; @@ -856,7 +856,7 @@ mod tests { use crate::ln::features::OfferFeatures; use crate::ln::inbound_payment::ExpandedKey; use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT}; - use crate::offers::parse::{ParseError, SemanticError}; + use crate::offers::parse::{Bolt12ParseError, SemanticError}; use crate::offers::test_utils::*; use crate::util::ser::{BigSize, Writeable}; use crate::util::string::PrintableString; @@ -1305,7 +1305,7 @@ mod tests { match Offer::try_from(encoded_offer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingAmount)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingAmount)), } let mut tlv_stream = offer.as_tlv_stream(); @@ -1317,7 +1317,7 @@ mod tests { match Offer::try_from(encoded_offer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidAmount)), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidAmount)), } } @@ -1337,7 +1337,7 @@ mod tests { match Offer::try_from(encoded_offer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingDescription)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingDescription)); }, } } @@ -1427,7 +1427,7 @@ mod tests { match Offer::try_from(encoded_offer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey)); }, } } @@ -1444,14 +1444,14 @@ mod tests { match Offer::try_from(encoded_offer) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), } } } #[cfg(test)] mod bech32_tests { - use super::{Offer, ParseError}; + use super::{Bolt12ParseError, Offer}; use bitcoin::bech32; use crate::ln::msgs::DecodeError; @@ -1493,7 +1493,7 @@ mod bech32_tests { for encoded_offer in &offers { match encoded_offer.parse::() { Ok(_) => panic!("Valid offer: {}", encoded_offer), - Err(e) => assert_eq!(e, ParseError::InvalidContinuation), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidContinuation), } } @@ -1504,7 +1504,7 @@ mod bech32_tests { let encoded_offer = "lni1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg"; match encoded_offer.parse::() { Ok(_) => panic!("Valid offer: {}", encoded_offer), - Err(e) => assert_eq!(e, ParseError::InvalidBech32Hrp), + Err(e) => assert_eq!(e, Bolt12ParseError::InvalidBech32Hrp), } } @@ -1513,7 +1513,7 @@ mod bech32_tests { let encoded_offer = "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxo"; match encoded_offer.parse::() { Ok(_) => panic!("Valid offer: {}", encoded_offer), - Err(e) => assert_eq!(e, ParseError::Bech32(bech32::Error::InvalidChar('o'))), + Err(e) => assert_eq!(e, Bolt12ParseError::Bech32(bech32::Error::InvalidChar('o'))), } } @@ -1522,7 +1522,7 @@ mod bech32_tests { let encoded_offer = "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxgqqqqq"; match encoded_offer.parse::() { Ok(_) => panic!("Valid offer: {}", encoded_offer), - Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), } } } diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index 42ed2e002..064604f3a 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -29,24 +29,24 @@ mod sealed { use bitcoin::bech32::{FromBase32, ToBase32}; use core::convert::TryFrom; use core::fmt; - use super::ParseError; + use super::Bolt12ParseError; use crate::prelude::*; /// Indicates a message can be encoded using bech32. - pub trait Bech32Encode: AsRef<[u8]> + TryFrom, Error=ParseError> { + pub trait Bech32Encode: AsRef<[u8]> + TryFrom, Error=Bolt12ParseError> { /// Human readable part of the message's bech32 encoding. const BECH32_HRP: &'static str; /// Parses a bech32-encoded message into a TLV stream. - fn from_bech32_str(s: &str) -> Result { + fn from_bech32_str(s: &str) -> Result { // Offer encoding may be split by '+' followed by optional whitespace. let encoded = match s.split('+').skip(1).next() { Some(_) => { for chunk in s.split('+') { let chunk = chunk.trim_start(); if chunk.is_empty() || chunk.contains(char::is_whitespace) { - return Err(ParseError::InvalidContinuation); + return Err(Bolt12ParseError::InvalidContinuation); } } @@ -59,7 +59,7 @@ mod sealed { let (hrp, data) = bech32::decode_without_checksum(encoded.as_ref())?; if hrp != Self::BECH32_HRP { - return Err(ParseError::InvalidBech32Hrp); + return Err(Bolt12ParseError::InvalidBech32Hrp); } let data = Vec::::from_base32(&data)?; @@ -116,10 +116,8 @@ impl TryFrom> for ParsedMessage { } /// Error when parsing a bech32 encoded message using [`str::parse`]. -/// -/// This is not exported to bindings users as its name conflicts with the BOLT 11 ParseError type. #[derive(Debug, PartialEq)] -pub enum ParseError { +pub enum Bolt12ParseError { /// The bech32 encoding does not conform to the BOLT 12 requirements for continuing messages /// across multiple parts (i.e., '+' followed by whitespace). InvalidContinuation, @@ -195,25 +193,25 @@ pub enum SemanticError { MissingSignature, } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: bech32::Error) -> Self { Self::Bech32(error) } } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: DecodeError) -> Self { Self::Decode(error) } } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: SemanticError) -> Self { Self::InvalidSemantics(error) } } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: secp256k1::Error) -> Self { Self::InvalidSignature(error) } diff --git a/lightning/src/offers/refund.rs b/lightning/src/offers/refund.rs index 35c1726c9..aea5e49a3 100644 --- a/lightning/src/offers/refund.rs +++ b/lightning/src/offers/refund.rs @@ -28,7 +28,7 @@ //! //! use bitcoin::network::constants::Network; //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey}; -//! use lightning::offers::parse::ParseError; +//! use lightning::offers::parse::Bolt12ParseError; //! use lightning::offers::refund::{Refund, RefundBuilder}; //! use lightning::util::ser::{Readable, Writeable}; //! @@ -40,7 +40,7 @@ //! # fn create_another_blinded_path() -> BlindedPath { unimplemented!() } //! # //! # #[cfg(feature = "std")] -//! # fn build() -> Result<(), ParseError> { +//! # fn build() -> Result<(), Bolt12ParseError> { //! let secp_ctx = Secp256k1::new(); //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()); //! let pubkey = PublicKey::from(keys); @@ -88,7 +88,7 @@ use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT}; use crate::offers::invoice::{BlindedPayInfo, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder}; use crate::offers::invoice_request::{InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef}; use crate::offers::offer::{OfferTlvStream, OfferTlvStreamRef}; -use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError}; +use crate::offers::parse::{Bech32Encode, Bolt12ParseError, ParsedMessage, SemanticError}; use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef}; use crate::offers::signer::{Metadata, MetadataMaterial, self}; use crate::util::ser::{SeekReadable, WithoutLength, Writeable, Writer}; @@ -606,7 +606,7 @@ impl Bech32Encode for Refund { } impl FromStr for Refund { - type Err = ParseError; + type Err = Bolt12ParseError; fn from_str(s: &str) -> Result::Err> { Refund::from_bech32_str(s) @@ -614,7 +614,7 @@ impl FromStr for Refund { } impl TryFrom> for Refund { - type Error = ParseError; + type Error = Bolt12ParseError; fn try_from(bytes: Vec) -> Result { let refund = ParsedMessage::::try_from(bytes)?; @@ -718,7 +718,7 @@ mod tests { use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT}; use crate::offers::invoice_request::InvoiceRequestTlvStreamRef; use crate::offers::offer::OfferTlvStreamRef; - use crate::offers::parse::{ParseError, SemanticError}; + use crate::offers::parse::{Bolt12ParseError, SemanticError}; use crate::offers::payer::PayerTlvStreamRef; use crate::offers::test_utils::*; use crate::util::ser::{BigSize, Writeable}; @@ -1082,7 +1082,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPayerMetadata)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPayerMetadata)); }, } } @@ -1101,7 +1101,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingDescription)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingDescription)); }, } } @@ -1120,7 +1120,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingAmount)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingAmount)); }, } @@ -1130,7 +1130,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidAmount)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidAmount)); }, } } @@ -1149,7 +1149,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPayerId)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPayerId)); }, } } @@ -1218,7 +1218,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedMetadata)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedMetadata)); }, } @@ -1229,7 +1229,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedChain)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedChain)); }, } @@ -1240,7 +1240,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedAmount)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedAmount)); }, } @@ -1251,7 +1251,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedFeatures)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedFeatures)); }, } @@ -1261,7 +1261,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedQuantity)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedQuantity)); }, } @@ -1272,7 +1272,7 @@ mod tests { match Refund::try_from(tlv_stream.to_bytes()) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, ParseError::InvalidSemantics(SemanticError::UnexpectedSigningPubkey)); + assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::UnexpectedSigningPubkey)); }, } } @@ -1292,7 +1292,7 @@ mod tests { match Refund::try_from(encoded_refund) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)), + Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)), } } } diff --git a/lightning/src/onion_message/offers.rs b/lightning/src/onion_message/offers.rs index e335e8aa8..9a1f59b84 100644 --- a/lightning/src/onion_message/offers.rs +++ b/lightning/src/onion_message/offers.rs @@ -15,7 +15,7 @@ use crate::ln::msgs::DecodeError; use crate::offers::invoice_error::InvoiceError; use crate::offers::invoice_request::InvoiceRequest; use crate::offers::invoice::Bolt12Invoice; -use crate::offers::parse::ParseError; +use crate::offers::parse::Bolt12ParseError; use crate::util::logger::Logger; use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer}; @@ -72,11 +72,11 @@ impl OffersMessage { } } - fn parse(tlv_type: u64, bytes: Vec) -> Result { + fn parse(tlv_type: u64, bytes: Vec) -> Result { match tlv_type { INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)), INVOICE_TLV_TYPE => Ok(Self::Invoice(Bolt12Invoice::try_from(bytes)?)), - _ => Err(ParseError::Decode(DecodeError::InvalidValue)), + _ => Err(Bolt12ParseError::Decode(DecodeError::InvalidValue)), } } } @@ -103,12 +103,12 @@ impl ReadableArgs<(u64, &L)> for OffersMessage { match Self::parse(tlv_type, bytes) { Ok(message) => Ok(message), - Err(ParseError::Decode(e)) => Err(e), - Err(ParseError::InvalidSemantics(e)) => { + Err(Bolt12ParseError::Decode(e)) => Err(e), + Err(Bolt12ParseError::InvalidSemantics(e)) => { log_trace!(logger, "Invalid semantics for TLV type {}: {:?}", tlv_type, e); Err(DecodeError::InvalidValue) }, - Err(ParseError::InvalidSignature(e)) => { + Err(Bolt12ParseError::InvalidSignature(e)) => { log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e); Err(DecodeError::InvalidValue) },