Skip to content

Commit 39ff602

Browse files
authored
Merge pull request #3549 from lightning-signer/2025-01-invoice-base32
RawBolt11Invoice serialization utilities
2 parents 79267d3 + 80f6f38 commit 39ff602

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lightning-invoice/src/lib.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use core::iter::FilterMap;
4848
use core::num::ParseIntError;
4949
use core::ops::Deref;
5050
use core::slice::Iter;
51+
use core::str::FromStr;
5152
use core::time::Duration;
5253

5354
#[cfg(feature = "serde")]
@@ -78,8 +79,12 @@ use crate::prelude::*;
7879
/// Re-export serialization traits
7980
#[cfg(fuzzing)]
8081
pub use crate::de::FromBase32;
82+
#[cfg(not(fuzzing))]
83+
use crate::de::FromBase32;
8184
#[cfg(fuzzing)]
8285
pub use crate::ser::Base32Iterable;
86+
#[cfg(not(fuzzing))]
87+
use crate::ser::Base32Iterable;
8388

8489
/// Errors that indicate what is wrong with the invoice. They have some granularity for debug
8590
/// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
@@ -1086,9 +1091,6 @@ impl RawBolt11Invoice {
10861091

10871092
/// Calculate the hash of the encoded `RawBolt11Invoice` which should be signed.
10881093
pub fn signable_hash(&self) -> [u8; 32] {
1089-
#[cfg(not(fuzzing))]
1090-
use crate::ser::Base32Iterable;
1091-
10921094
Self::hash_from_parts(self.hrp.to_string().as_bytes(), self.data.fe_iter())
10931095
}
10941096

@@ -1189,6 +1191,21 @@ impl RawBolt11Invoice {
11891191
pub fn currency(&self) -> Currency {
11901192
self.hrp.currency.clone()
11911193
}
1194+
1195+
/// Convert to HRP prefix and Fe32 encoded data part.
1196+
/// Can be used to transmit unsigned invoices for remote signing.
1197+
pub fn to_raw(&self) -> (String, Vec<Fe32>) {
1198+
(self.hrp.to_string(), self.data.fe_iter().collect())
1199+
}
1200+
1201+
/// Convert from HRP prefix and Fe32 encoded data part.
1202+
/// Can be used to receive unsigned invoices for remote signing.
1203+
pub fn from_raw(hrp: &str, data: &[Fe32]) -> Result<Self, Bolt11ParseError> {
1204+
let raw_hrp: RawHrp = RawHrp::from_str(hrp)?;
1205+
let data_part = RawDataPart::from_base32(data)?;
1206+
1207+
Ok(Self { hrp: raw_hrp, data: data_part })
1208+
}
11921209
}
11931210

11941211
impl PositiveTimestamp {

0 commit comments

Comments
 (0)