Skip to content

Commit 6a77736

Browse files
committed
Formatting
1 parent 1872fa5 commit 6a77736

File tree

3 files changed

+90
-41
lines changed

3 files changed

+90
-41
lines changed

lightning-invoice/src/de.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use core::num::ParseIntError;
88
use core::str;
99
use core::str::FromStr;
1010

11-
use bech32::{Bech32, Fe32, Fe32IterExt};
1211
use bech32::primitives::decode::{CheckedHrpstring, CheckedHrpstringError};
12+
use bech32::{Bech32, Fe32, Fe32IterExt};
1313

1414
use bitcoin::{PubkeyHash, ScriptHash, WitnessVersion};
1515
use bitcoin::hashes::Hash;
@@ -50,8 +50,13 @@ impl<const N: usize> FromBase32 for [u8; N] {
5050
type Err = Bolt11ParseError;
5151

5252
fn from_base32(data: &[Fe32]) -> Result<Self, Self::Err> {
53-
data.iter().copied().fes_to_bytes().collect::<Vec<_>>().try_into()
54-
.map_err(|_| Bolt11ParseError::InvalidSliceLength(data.len(), (N * 8 + 4) / 5, "<[u8; N]>::from_base32()".into()))
53+
data.iter().copied().fes_to_bytes().collect::<Vec<_>>().try_into().map_err(|_| {
54+
Bolt11ParseError::InvalidSliceLength(
55+
data.len(),
56+
(N * 8 + 4) / 5,
57+
"<[u8; N]>::from_base32()".into(),
58+
)
59+
})
5560
}
5661
}
5762

@@ -60,7 +65,11 @@ impl FromBase32 for PaymentSecret {
6065

6166
fn from_base32(field_data: &[Fe32]) -> Result<Self, Self::Err> {
6267
if field_data.len() != 52 {
63-
return Err(Bolt11ParseError::InvalidSliceLength(field_data.len(), 52, "PaymentSecret::from_base32()".into()));
68+
return Err(Bolt11ParseError::InvalidSliceLength(
69+
field_data.len(),
70+
52,
71+
"PaymentSecret::from_base32()".into(),
72+
));
6473
}
6574
let data_bytes = Vec::<u8>::from_base32(field_data)?;
6675
let mut payment_secret = [0; 32];
@@ -368,7 +377,7 @@ impl FromStr for SignedRawBolt11Invoice {
368377
}
369378

370379
let raw_hrp: RawHrp = hrp.to_string().to_lowercase().parse()?;
371-
let data_part = RawDataPart::from_base32(&data[..data.len()-SIGNATURE_LEN5])?;
380+
let data_part = RawDataPart::from_base32(&data[..data.len() - SIGNATURE_LEN5])?;
372381

373382
Ok(SignedRawBolt11Invoice {
374383
raw_invoice: RawBolt11Invoice {
@@ -377,9 +386,9 @@ impl FromStr for SignedRawBolt11Invoice {
377386
},
378387
hash: RawBolt11Invoice::hash_from_parts(
379388
hrp.to_string().as_bytes(),
380-
&data[..data.len()-SIGNATURE_LEN5]
389+
&data[..data.len() - SIGNATURE_LEN5],
381390
),
382-
signature: Bolt11InvoiceSignature::from_base32(&data[data.len()-SIGNATURE_LEN5..])?,
391+
signature: Bolt11InvoiceSignature::from_base32(&data[data.len() - SIGNATURE_LEN5..])?,
383392
})
384393
}
385394
}
@@ -441,7 +450,11 @@ impl FromBase32 for PositiveTimestamp {
441450

442451
fn from_base32(b32: &[Fe32]) -> Result<Self, Self::Err> {
443452
if b32.len() != 7 {
444-
return Err(Bolt11ParseError::InvalidSliceLength(b32.len(), 7, "PositiveTimestamp::from_base32()".into()));
453+
return Err(Bolt11ParseError::InvalidSliceLength(
454+
b32.len(),
455+
7,
456+
"PositiveTimestamp::from_base32()".into(),
457+
));
445458
}
446459
let timestamp: u64 = parse_u64_be(b32)
447460
.expect("7*5bit < 64bit, no overflow possible");
@@ -456,7 +469,11 @@ impl FromBase32 for Bolt11InvoiceSignature {
456469
type Err = Bolt11ParseError;
457470
fn from_base32(signature: &[Fe32]) -> Result<Self, Self::Err> {
458471
if signature.len() != 104 {
459-
return Err(Bolt11ParseError::InvalidSliceLength(signature.len(), 104, "Bolt11InvoiceSignature::from_base32()".into()));
472+
return Err(Bolt11ParseError::InvalidSliceLength(
473+
signature.len(),
474+
104,
475+
"Bolt11InvoiceSignature::from_base32()".into(),
476+
));
460477
}
461478
let recoverable_signature_bytes = Vec::<u8>::from_base32(signature)?;
462479
let signature = &recoverable_signature_bytes[0..64];
@@ -509,7 +526,9 @@ fn parse_tagged_parts(data: &[Fe32]) -> Result<Vec<RawTaggedField>, Bolt11ParseE
509526
Ok(field) => {
510527
parts.push(RawTaggedField::KnownSemantics(field))
511528
},
512-
Err(Bolt11ParseError::Skip) | Err(Bolt11ParseError::InvalidSliceLength(_, _, _)) | Err(Bolt11ParseError::Bech32Error(_)) => {
529+
Err(Bolt11ParseError::Skip)
530+
| Err(Bolt11ParseError::InvalidSliceLength(_, _, _))
531+
| Err(Bolt11ParseError::Bech32Error(_)) => {
513532
parts.push(RawTaggedField::UnknownSemantics(field.into()))
514533
},
515534
Err(e) => {return Err(e)}
@@ -727,8 +746,12 @@ impl Display for Bolt11ParseError {
727746
write!(f, "Description is not a valid utf-8 string: {}", e)
728747
}
729748
Bolt11ParseError::InvalidSliceLength(ref len, ref expected, ref function) => {
730-
write!(f, "Slice had length {} instead of {} in function {}", len, expected, function)
731-
}
749+
write!(
750+
f,
751+
"Slice had length {} instead of {} in function {}",
752+
len, expected, function
753+
)
754+
},
732755
Bolt11ParseError::BadPrefix => f.write_str("did not begin with 'ln'"),
733756
Bolt11ParseError::UnknownCurrency => f.write_str("currency code unknown"),
734757
Bolt11ParseError::UnknownSiPrefix => f.write_str("unknown SI prefix"),

lightning-invoice/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ extern crate serde;
3333
#[cfg(feature = "std")]
3434
use std::time::SystemTime;
3535

36-
use bech32::Fe32;
3736
use bech32::primitives::decode::CheckedHrpstringError;
37+
use bech32::Fe32;
3838
use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion};
3939
use bitcoin::hashes::{Hash, sha256};
4040
use lightning_types::features::Bolt11InvoiceFeatures;
@@ -79,9 +79,9 @@ use crate::prelude::*;
7979

8080
/// Re-export serialization traits
8181
#[cfg(fuzzing)]
82-
pub use crate::ser::Base32Iterable;
83-
#[cfg(fuzzing)]
8482
pub use crate::de::FromBase32;
83+
#[cfg(fuzzing)]
84+
pub use crate::ser::Base32Iterable;
8585

8686
/// Errors that indicate what is wrong with the invoice. They have some granularity for debug
8787
/// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
@@ -440,10 +440,18 @@ impl PartialOrd for RawTaggedField {
440440
impl Ord for RawTaggedField {
441441
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
442442
match (self, other) {
443-
(RawTaggedField::KnownSemantics(ref a), RawTaggedField::KnownSemantics(ref b)) => a.cmp(b),
444-
(RawTaggedField::UnknownSemantics(ref a), RawTaggedField::UnknownSemantics(ref b)) => a.iter().map(|a| a.to_u8()).cmp(b.iter().map(|b| b.to_u8())),
445-
(RawTaggedField::KnownSemantics(..), RawTaggedField::UnknownSemantics(..)) => core::cmp::Ordering::Less,
446-
(RawTaggedField::UnknownSemantics(..), RawTaggedField::KnownSemantics(..)) => core::cmp::Ordering::Greater,
443+
(RawTaggedField::KnownSemantics(ref a), RawTaggedField::KnownSemantics(ref b)) => {
444+
a.cmp(b)
445+
},
446+
(RawTaggedField::UnknownSemantics(ref a), RawTaggedField::UnknownSemantics(ref b)) => {
447+
a.iter().map(|a| a.to_u8()).cmp(b.iter().map(|b| b.to_u8()))
448+
},
449+
(RawTaggedField::KnownSemantics(..), RawTaggedField::UnknownSemantics(..)) => {
450+
core::cmp::Ordering::Less
451+
},
452+
(RawTaggedField::UnknownSemantics(..), RawTaggedField::KnownSemantics(..)) => {
453+
core::cmp::Ordering::Greater
454+
},
447455
}
448456
}
449457
}

lightning-invoice/src/ser.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<const N: usize> Base32Iterable for [u8; N] {
3636
impl<const N: usize> Base32Len for [u8; N] {
3737
/// Calculate the base32 serialized length
3838
fn base32_len(&self) -> usize {
39-
bytes_size_to_base32_size(N)
39+
bytes_size_to_base32_size(N)
4040
}
4141
}
4242

@@ -154,10 +154,13 @@ impl Display for Bolt11Invoice {
154154
impl Display for SignedRawBolt11Invoice {
155155
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
156156
let hrp = self.raw_invoice.hrp.to_hrp();
157-
for ch in
158-
self.raw_invoice.data.fe_iter()
157+
for ch in self
158+
.raw_invoice
159+
.data
160+
.fe_iter()
159161
.chain(self.signature.fe_iter())
160-
.with_checksum::<bech32::Bech32>(&hrp).chars()
162+
.with_checksum::<bech32::Bech32>(&hrp)
163+
.chars()
161164
{
162165
write!(f, "{}", ch)?;
163166
}
@@ -259,9 +262,7 @@ impl Base32Iterable for RawTaggedField {
259262
// Annoyingly, when we move to explicit types, we will need an
260263
// explicit enum holding the two iterator variants.
261264
match *self {
262-
RawTaggedField::UnknownSemantics(ref content) => {
263-
Box::new(content.iter().copied())
264-
},
265+
RawTaggedField::UnknownSemantics(ref content) => Box::new(content.iter().copied()),
265266
RawTaggedField::KnownSemantics(ref tagged_field) => tagged_field.fe_iter(),
266267
}
267268
}
@@ -330,37 +331,44 @@ impl Base32Len for MinFinalCltvExpiryDelta {
330331
impl Base32Iterable for Fallback {
331332
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
332333
Box::new(match *self {
333-
Fallback::SegWitProgram {version: v, program: ref p} => {
334+
Fallback::SegWitProgram { version: v, program: ref p } => {
334335
let v = Fe32::try_from(v.to_num()).expect("valid version");
335336
core::iter::once(v).chain(p[..].fe_iter())
336-
}
337+
},
337338
Fallback::PubKeyHash(ref hash) => {
338339
// 17 '3'
339340
core::iter::once(Fe32::_3).chain(hash[..].fe_iter())
340-
}
341+
},
341342
Fallback::ScriptHash(ref hash) => {
342343
// 18 'J'
343344
core::iter::once(Fe32::J).chain(hash[..].fe_iter())
344-
}
345+
},
345346
})
346347
}
347348
}
348349

349350
impl Base32Len for Fallback {
350351
fn base32_len(&self) -> usize {
351352
match *self {
352-
Fallback::SegWitProgram {program: ref p, ..} => {
353+
Fallback::SegWitProgram { program: ref p, .. } => {
353354
bytes_size_to_base32_size(p.len()) + 1
354355
},
355-
Fallback::PubKeyHash(_) | Fallback::ScriptHash(_) => {
356-
33
357-
},
356+
Fallback::PubKeyHash(_) | Fallback::ScriptHash(_) => 33,
358357
}
359358
}
360359
}
361360

362361
// Shorthand type
363-
type RouteHintHopIter = iter::Chain<iter::Chain<iter::Chain<iter::Chain<array::IntoIter<u8, 33>, array::IntoIter<u8, 8>>, array::IntoIter<u8, 4>>, array::IntoIter<u8, 4>>, array::IntoIter<u8, 2>>;
362+
type RouteHintHopIter = iter::Chain<
363+
iter::Chain<
364+
iter::Chain<
365+
iter::Chain<array::IntoIter<u8, 33>, array::IntoIter<u8, 8>>,
366+
array::IntoIter<u8, 4>,
367+
>,
368+
array::IntoIter<u8, 4>,
369+
>,
370+
array::IntoIter<u8, 2>,
371+
>;
364372

365373
impl Base32Iterable for PrivateRoute {
366374
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
@@ -390,8 +398,11 @@ impl Base32Iterable for TaggedField {
390398
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
391399
/// Writes a tagged field: tag, length and data. `tag` should be in `0..32` otherwise the
392400
/// function will panic.
393-
fn write_tagged_field<'s, P>(tag: u8, payload: &'s P) -> TaggedFieldIter<Box<dyn Iterator<Item = Fe32> + 's>>
394-
where P: Base32Iterable + Base32Len + ?Sized
401+
fn write_tagged_field<'s, P>(
402+
tag: u8, payload: &'s P,
403+
) -> TaggedFieldIter<Box<dyn Iterator<Item = Fe32> + 's>>
404+
where
405+
P: Base32Iterable + Base32Len + ?Sized,
395406
{
396407
let len = payload.base32_len();
397408
assert!(len < 1024, "Every tagged field data can be at most 1023 bytes long.");
@@ -400,7 +411,9 @@ impl Base32Iterable for TaggedField {
400411
Fe32::try_from(tag).expect("invalid tag, not in 0..32"),
401412
Fe32::try_from((len / 32) as u8).expect("< 32"),
402413
Fe32::try_from((len % 32) as u8).expect("< 32"),
403-
].into_iter().chain(payload.fe_iter())
414+
]
415+
.into_iter()
416+
.chain(payload.fe_iter())
404417
}
405418

406419
// we will also need a giant enum for this
@@ -446,9 +459,10 @@ impl Base32Iterable for Bolt11InvoiceSignature {
446459
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
447460
let (recovery_id, signature) = self.0.serialize_compact();
448461
Box::new(
449-
signature.into_iter()
462+
signature
463+
.into_iter()
450464
.chain(core::iter::once(recovery_id.to_i32() as u8))
451-
.bytes_to_fes()
465+
.bytes_to_fes(),
452466
)
453467
}
454468
}
@@ -485,7 +499,11 @@ mod test {
485499
use bech32::Fe32;
486500

487501
let input: u64 = 33764;
488-
let expected_out = [1, 0, 31, 4].iter().copied().map(|v| Fe32::try_from(v).expect("<= 31")).collect::<Vec<Fe32>>();
502+
let expected_out = [1, 0, 31, 4]
503+
.iter()
504+
.copied()
505+
.map(|v| Fe32::try_from(v).expect("<= 31"))
506+
.collect::<Vec<Fe32>>();
489507

490508
assert_eq!(expected_out, encode_int_be_base32(input));
491509
}

0 commit comments

Comments
 (0)