From 8eb0c4bd2457f3d2fda6d2c409f94be2d99d6909 Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Sun, 25 Aug 2024 22:41:32 +0200 Subject: [PATCH] Move FromBase32 trait into de module --- fuzz/src/bolt11_deser.rs | 3 +-- lightning-invoice/src/de.rs | 11 ++++++++++- lightning-invoice/src/lib.rs | 13 ++++--------- lightning-invoice/src/test_ser_de.rs | 5 ++--- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/fuzz/src/bolt11_deser.rs b/fuzz/src/bolt11_deser.rs index b86ae4eec78..92dde80aa66 100644 --- a/fuzz/src/bolt11_deser.rs +++ b/fuzz/src/bolt11_deser.rs @@ -38,8 +38,7 @@ pub fn do_test(data: &[u8], _out: Out) { // Our data serialization is loss-less assert_eq!( - RawDataPart::from_base32(&invoice_data_base32) - .expect("faild parsing out own encoding"), + RawDataPart::from_base32(&invoice_data_base32).expect("faild parsing out own encoding"), invoice_data ); diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index 272a55b0e5f..3330e490d20 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -23,10 +23,19 @@ use bitcoin::secp256k1::PublicKey; use super::{Bolt11Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiryDelta, Fallback, PayeePubKey, Bolt11InvoiceSignature, PositiveTimestamp, Bolt11SemanticError, PrivateRoute, Bolt11ParseError, ParseOrSemanticError, Description, RawTaggedField, Currency, RawHrp, SiPrefix, RawBolt11Invoice, - constants, SignedRawBolt11Invoice, RawDataPart, Bolt11InvoiceFeatures, FromBase32}; + constants, SignedRawBolt11Invoice, RawDataPart, Bolt11InvoiceFeatures}; use self::hrp_sm::parse_hrp; +/// Trait for paring/converting base32 slice. +pub trait FromBase32: Sized { + /// The associated error which can be returned from parsing (e.g. because of bad padding). + type Err; + + /// Convert a base32 slice to `Self`. + fn from_base32(b32: &[Fe32]) -> Result; +} + // FromBase32 implementations are here, because the trait is in this module. impl FromBase32 for Vec { diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index c7b8f0716c5..ecc5be37e78 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -80,15 +80,8 @@ use crate::prelude::*; /// Re-export serialization traits #[cfg(fuzzing)] pub use crate::ser::Base32Iterable; - -/// Trait for paring/converting base32 slice. -pub trait FromBase32: Sized { - /// The associated error which can be returned from parsing (e.g. because of bad padding). - type Err; - - /// Convert a base32 slice to `Self`. - fn from_base32(b32: &[Fe32]) -> Result; -} +#[cfg(fuzzing)] +pub use crate::de::FromBase32; /// Errors that indicate what is wrong with the invoice. They have some granularity for debug /// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user. @@ -1000,6 +993,8 @@ macro_rules! find_all_extract { impl RawBolt11Invoice { /// Hash the HRP as bytes and signatureless data part. fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[Fe32]) -> [u8; 32] { + use crate::de::FromBase32; + let mut preimage = Vec::::from(hrp_bytes); let mut data_part = Vec::from(data_without_signature); diff --git a/lightning-invoice/src/test_ser_de.rs b/lightning-invoice/src/test_ser_de.rs index 90791c08518..dac9b1495f9 100644 --- a/lightning-invoice/src/test_ser_de.rs +++ b/lightning-invoice/src/test_ser_de.rs @@ -1,7 +1,6 @@ +use crate::de::FromBase32; use crate::ser::{Base32Iterable, Base32Len}; -use crate::{ - sha256, FromBase32, PayeePubKey, PaymentSecret, PositiveTimestamp, RawDataPart, Sha256, -}; +use crate::{sha256, PayeePubKey, PaymentSecret, PositiveTimestamp, RawDataPart, Sha256}; use bech32::Fe32; use core::fmt::Debug; use std::str::FromStr;