Skip to content

Commit

Permalink
Merge pull request #3349 from TheBlueMatt/2024-10-3270-followups
Browse files Browse the repository at this point in the history
Minor #3270 Followups
  • Loading branch information
TheBlueMatt authored Oct 11, 2024
2 parents cb4584a + 5c1440a commit ad19d93
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lightning-invoice/src/de.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use alloc::string;
#[cfg(feature = "std")]
use std::error;
#[cfg(not(feature = "std"))]
use core::convert::TryFrom;
use core::fmt;
use core::fmt::{Display, Formatter};
use core::num::ParseIntError;
use core::str;
use core::str::FromStr;

use bech32::primitives::decode::{CheckedHrpstring, CheckedHrpstringError};
Expand Down Expand Up @@ -613,7 +613,7 @@ impl FromBase32 for Description {

fn from_base32(field_data: &[Fe32]) -> Result<Description, Bolt11ParseError> {
let bytes = Vec::<u8>::from_base32(field_data)?;
let description = String::from(str::from_utf8(&bytes)?);
let description = String::from_utf8(bytes)?;
Ok(Description::new(description).expect(
"Max len is 639=floor(1023*5/8) since the len field is only 10bits long"
))
Expand Down Expand Up @@ -712,7 +712,7 @@ impl FromBase32 for PrivateRoute {
return Err(Bolt11ParseError::UnexpectedEndOfTaggedFields);
}

let mut route_hops = Vec::<RouteHintHop>::new();
let mut route_hops = Vec::with_capacity(bytes.len() / 51);

let mut bytes = bytes.as_slice();
while !bytes.is_empty() {
Expand Down Expand Up @@ -824,7 +824,7 @@ macro_rules! from_error {

from_error!(Bolt11ParseError::MalformedSignature, bitcoin::secp256k1::Error);
from_error!(Bolt11ParseError::ParseAmountError, ParseIntError);
from_error!(Bolt11ParseError::DescriptionDecodeError, str::Utf8Error);
from_error!(Bolt11ParseError::DescriptionDecodeError, string::FromUtf8Error);

impl From<CheckedHrpstringError> for Bolt11ParseError {
fn from(e: CheckedHrpstringError) -> Self {
Expand Down
5 changes: 3 additions & 2 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use core::num::ParseIntError;
use core::ops::Deref;
use core::slice::Iter;
use core::time::Duration;
use core::str;
use alloc::string;

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
Expand Down Expand Up @@ -98,7 +98,7 @@ pub enum Bolt11ParseError {
MalformedHRP,
TooShortDataPart,
UnexpectedEndOfTaggedFields,
DescriptionDecodeError(str::Utf8Error),
DescriptionDecodeError(string::FromUtf8Error),
PaddingError,
IntegerOverflowError,
InvalidSegWitProgramLength,
Expand Down Expand Up @@ -313,6 +313,7 @@ impl RawHrp {
pub fn to_hrp(&self) -> bech32::Hrp {
let hrp_str = self.to_string();
let s = core::str::from_utf8(&hrp_str.as_bytes()).expect("HRP bytes should be ASCII");
debug_assert!(bech32::Hrp::parse(s).is_ok(), "We should always build BIP 173-valid HRPs");
bech32::Hrp::parse_unchecked(s)
}
}
Expand Down
21 changes: 11 additions & 10 deletions lightning-invoice/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) trait Base32Len: Base32Iterable {

impl<const N: usize> Base32Iterable for [u8; N] {
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
Box::new((*self).into_iter().bytes_to_fes())
self[..].fe_iter()
}
}

Expand Down Expand Up @@ -218,19 +218,20 @@ impl Display for SiPrefix {
}

/// Encode an integer to base32, big endian, without leading zeros
fn encode_int_be_base32(int: u64) -> Vec<Fe32> {
fn encode_int_be_base32(int: u64) -> impl ExactSizeIterator<Item=Fe32> {
let base = 32u64;

// (64 + 4) / 5 == 13
let mut out_vec = Vec::<Fe32>::with_capacity(13);
let mut out = [Fe32::Q; 13];
let mut out_pos = 0;
let mut rem_int = int;
while rem_int != 0 {
out_vec.push(Fe32::try_from((rem_int % base) as u8).expect("always <32"));
out[out_pos] = Fe32::try_from((rem_int % base) as u8).expect("always <32");
out_pos += 1;
rem_int /= base;
}

out_vec.reverse();
out_vec
out.into_iter().take(out_pos).rev()
}

/// The length of the output of `encode_int_be_base32`.
Expand All @@ -252,7 +253,7 @@ impl Base32Iterable for PositiveTimestamp {
let fes = encode_int_be_base32(self.as_unix_timestamp());
debug_assert!(fes.len() <= 7, "Invalid timestamp length");
let to_pad = 7 - fes.len();
Box::new(core::iter::repeat(Fe32::Q).take(to_pad).chain(fes.into_iter()))
Box::new(core::iter::repeat(Fe32::Q).take(to_pad).chain(fes))
}
}

Expand Down Expand Up @@ -305,7 +306,7 @@ impl Base32Len for PayeePubKey {

impl Base32Iterable for ExpiryTime {
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
Box::new(encode_int_be_base32(self.as_seconds()).into_iter())
Box::new(encode_int_be_base32(self.as_seconds()))
}
}

Expand All @@ -317,7 +318,7 @@ impl Base32Len for ExpiryTime {

impl Base32Iterable for MinFinalCltvExpiryDelta {
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
Box::new(encode_int_be_base32(self.0).into_iter())
Box::new(encode_int_be_base32(self.0))
}
}

Expand Down Expand Up @@ -504,6 +505,6 @@ mod test {
.map(|v| Fe32::try_from(v).expect("<= 31"))
.collect::<Vec<Fe32>>();

assert_eq!(expected_out, encode_int_be_base32(input));
assert_eq!(expected_out, encode_int_be_base32(input).collect::<Vec<Fe32>>());
}
}
2 changes: 1 addition & 1 deletion lightning/src/offers/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod sealed {

let parsed = CheckedHrpstring::new::<NoChecksum>(encoded.as_ref())?;
let hrp = parsed.hrp();
if hrp.to_string() != Self::BECH32_HRP {
if hrp.as_str() != Self::BECH32_HRP {
return Err(Bolt12ParseError::InvalidBech32Hrp);
}

Expand Down

0 comments on commit ad19d93

Please sign in to comment.