Skip to content

Commit

Permalink
Move FromBase32 trait into de module
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Aug 25, 2024
1 parent 3d8f887 commit 3582fb1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
3 changes: 1 addition & 2 deletions fuzz/src/bolt11_deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ pub fn do_test<Out: test_logger::Output>(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
);

Expand Down
11 changes: 10 additions & 1 deletion lightning-invoice/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err>;
}

// FromBase32 implementations are here, because the trait is in this module.

impl FromBase32 for Vec<u8> {
Expand Down
13 changes: 4 additions & 9 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err>;
}
#[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.
Expand Down Expand Up @@ -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::<u8>::from(hrp_bytes);

let mut data_part = Vec::from(data_without_signature);
Expand Down
3 changes: 2 additions & 1 deletion lightning-invoice/src/test_ser_de.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::de::FromBase32;
use crate::ser::{Base32Iterable, Base32Len};
use crate::{
sha256, FromBase32, PayeePubKey, PaymentSecret, PositiveTimestamp, RawDataPart, Sha256,
sha256, PayeePubKey, PaymentSecret, PositiveTimestamp, RawDataPart, Sha256,
};
use bech32::Fe32;
use core::fmt::Debug;
Expand Down

0 comments on commit 3582fb1

Please sign in to comment.