Skip to content

Commit 9011867

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

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

lightning-invoice/src/lib.rs

Lines changed: 30 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};
@@ -80,6 +80,10 @@ use crate::prelude::*;
8080
pub use crate::de::FromBase32;
8181
#[cfg(fuzzing)]
8282
pub use crate::ser::Base32Iterable;
83+
#[cfg(not(fuzzing))]
84+
use crate::de::FromBase32;
85+
#[cfg(not(fuzzing))]
86+
use crate::ser::Base32Iterable;
8387

8488
/// Errors that indicate what is wrong with the invoice. They have some granularity for debug
8589
/// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
@@ -1189,6 +1193,31 @@ impl RawBolt11Invoice {
11891193
pub fn currency(&self) -> Currency {
11901194
self.hrp.currency.clone()
11911195
}
1196+
1197+
/// Convert to ascii bytes with HRP prefix and base32 encoded data part.
1198+
/// Can be used to transmit unsigned invoices for remote signing.
1199+
pub fn to_ascii(&self) -> Vec<u8> {
1200+
self.hrp.to_string().into_bytes().into_iter()
1201+
.chain(self.data.fe_iter().map(|e| e.to_char() as u8)).collect()
1202+
}
1203+
1204+
/// Convert from ascii bytes with HRP prefix and base32 encoded data part.
1205+
/// Can be used to receive unsigned invoices for remote signing.
1206+
pub fn from_ascii(s: &str) -> Result<Self, Bolt11ParseError> {
1207+
let parsed = UncheckedHrpstring::new(s)
1208+
.map_err(|_| Bolt11ParseError::MalformedHRP)?;
1209+
let hrp = parsed.hrp();
1210+
let raw_hrp: RawHrp = hrp.to_string().to_lowercase().parse()?;
1211+
1212+
let data = parsed.data_part_ascii().iter().map(|c| Fe32::from_char(*c as char)).collect::<Result<Vec<_>, _>>()
1213+
.map_err(|_| Bolt11ParseError::MalformedHRP)?;
1214+
let data_part = RawDataPart::from_base32(&data)?;
1215+
1216+
Ok(Self {
1217+
hrp: raw_hrp,
1218+
data: data_part,
1219+
})
1220+
}
11921221
}
11931222

11941223
impl PositiveTimestamp {

0 commit comments

Comments
 (0)