Skip to content

Commit babfa97

Browse files
committed
Struggle with features serialization/deserialization with iterators
1 parent 05dd6a1 commit babfa97

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lightning-invoice/src/de.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ impl FromBase32 for Bolt11InvoiceFeatures {
8585
/// and taking the resulting 8-bit values (right to left),
8686
/// with the leading 0's skipped.
8787
fn from_base32(field_data: &[Fe32]) -> Result<Self, Self::Err> {
88+
// fes_to_bytes() trims, input needs to be padded, find padding size
89+
let input_len = field_data.len();
90+
let mut padding = 0;
91+
while ((input_len + padding) * 5) % 8 != 0 {
92+
padding += 1;
93+
}
94+
let mut output = field_data
95+
.iter()
96+
.map(|f| Fe32::try_from(f.to_u8().reverse_bits() >> 3).expect("<32"))
97+
.rev()
98+
.chain(core::iter::repeat(Fe32::Q).take(padding))
99+
.fes_to_bytes()
100+
.map(|b| b.reverse_bits())
101+
.collect::<Vec<u8>>();
102+
// Trim the highest feature bits -<-- COULD NOT DO WITH ITER
103+
while !output.is_empty() && output[output.len() - 1] == 0 {
104+
output.pop();
105+
}
106+
Ok(Bolt11InvoiceFeatures::from_le_bytes(output))
107+
108+
/*
88109
// Fe32 conversion cannot be used, because this unpacks from right, right-to-left
89110
// Carry bits, 0, 1, 2, 3, or 4 bits
90111
let mut carry_bits = 0;
@@ -116,6 +137,7 @@ impl FromBase32 for Bolt11InvoiceFeatures {
116137
output.pop();
117138
}
118139
Ok(Bolt11InvoiceFeatures::from_le_bytes(output))
140+
*/
119141
}
120142
}
121143

lightning-invoice/src/ser.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ impl Base32Iterable for Bolt11InvoiceFeatures {
8585
/// and taking the resulting 5-bit values in reverse (left-to-right),
8686
/// with the leading 0's skipped.
8787
fn fe_iter<'s>(&'s self) -> Box<dyn Iterator<Item = Fe32> + 's> {
88+
Box::new(
89+
self
90+
.le_flags()
91+
.iter()
92+
.map(|b| b.reverse_bits())
93+
.bytes_to_fes()
94+
.collect::<Vec<Fe32>>().into_iter() // <-- COULD NOT DO WITHOUT COLLECT
95+
.rev()
96+
.map(|f| Fe32::try_from(f.to_u8().reverse_bits() >> 3).expect("<32"))
97+
.skip_while(|e| *e == Fe32::Q)
98+
)
99+
100+
/*
88101
// Fe32 conversion cannot be used, because this packs from right, right-to-left
89102
let mut input_iter = self.le_flags().iter();
90103
// Carry bits, 0..7 bits
@@ -123,6 +136,7 @@ impl Base32Iterable for Bolt11InvoiceFeatures {
123136
}
124137
// Take result in reverse order, and skip leading 0s
125138
Box::new(output.into_iter().rev().skip_while(|e| *e == Fe32::Q))
139+
*/
126140
}
127141
}
128142

0 commit comments

Comments
 (0)