Skip to content

Commit 67d736a

Browse files
committed
RawBolt11Invoice to/from ascii utilities
for remote signing of invoices
1 parent 3fbf97d commit 67d736a

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lightning-invoice/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern crate serde;
3030
#[cfg(feature = "std")]
3131
use std::time::SystemTime;
3232

33-
use bech32::primitives::decode::CheckedHrpstringError;
33+
use bech32::primitives::decode::{CheckedHrpstringError, UncheckedHrpstring};
3434
use bech32::Fe32;
3535
use bitcoin::hashes::{sha256, Hash};
3636
use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion};
@@ -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")]
@@ -80,6 +81,10 @@ use crate::prelude::*;
8081
pub use crate::de::FromBase32;
8182
#[cfg(fuzzing)]
8283
pub use crate::ser::Base32Iterable;
84+
#[cfg(not(fuzzing))]
85+
use crate::de::FromBase32;
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.
@@ -1189,6 +1194,24 @@ impl RawBolt11Invoice {
11891194
pub fn currency(&self) -> Currency {
11901195
self.hrp.currency.clone()
11911196
}
1197+
1198+
/// Convert to HRP prefix and Fe32 encoded data part.
1199+
/// Can be used to transmit unsigned invoices for remote signing.
1200+
pub fn to_raw(&self) -> (String, Vec<Fe32>) {
1201+
(self.hrp.to_string(), self.data.fe_iter().collect())
1202+
}
1203+
1204+
/// Convert from HRP prefix and Fe32 encoded data part.
1205+
/// Can be used to receive unsigned invoices for remote signing.
1206+
pub fn from_raw(hrp: &str, data: &[Fe32]) -> Result<Self, Bolt11ParseError> {
1207+
let raw_hrp: RawHrp = RawHrp::from_str(hrp)?;
1208+
let data_part = RawDataPart::from_base32(data)?;
1209+
1210+
Ok(Self {
1211+
hrp: raw_hrp,
1212+
data: data_part,
1213+
})
1214+
}
11921215
}
11931216

11941217
impl PositiveTimestamp {

0 commit comments

Comments
 (0)