1 //! Low level invoice utilities.
3 use bitcoin::bech32::{u5, FromBase32};
8 /// Construct the invoice's HRP and signatureless data into a preimage to be hashed.
9 pub fn construct_invoice_preimage(hrp_bytes: &[u8], data_without_signature: &[u5]) -> Vec<u8> {
10 let mut preimage = Vec::<u8>::from(hrp_bytes);
12 let mut data_part = Vec::from(data_without_signature);
13 let overhang = (data_part.len() * 5) % 8;
15 // add padding if data does not end at a byte boundary
16 data_part.push(u5::try_from_u8(0).unwrap());
18 // if overhang is in (1..3) we need to add u5(0) padding two times
20 data_part.push(u5::try_from_u8(0).unwrap());
24 preimage.extend_from_slice(&Vec::<u8>::from_base32(&data_part)
25 .expect("No padding error may occur due to appended zero above."));