]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/util/invoice.rs
Merge pull request #2704 from jkczyz/2023-11-channelmanager-docs
[rust-lightning] / lightning / src / util / invoice.rs
1 //! Low level invoice utilities.
2
3 use bitcoin::bech32::{u5, FromBase32};
4
5 #[allow(unused)]
6 use crate::prelude::*;
7
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);
11
12         let mut data_part = Vec::from(data_without_signature);
13         let overhang = (data_part.len() * 5) % 8;
14         if overhang > 0 {
15                 // add padding if data does not end at a byte boundary
16                 data_part.push(u5::try_from_u8(0).unwrap());
17
18                 // if overhang is in (1..3) we need to add u5(0) padding two times
19                 if overhang < 3 {
20                         data_part.push(u5::try_from_u8(0).unwrap());
21                 }
22         }
23
24         preimage.extend_from_slice(&Vec::<u8>::from_base32(&data_part)
25                 .expect("No padding error may occur due to appended zero above."));
26         preimage
27 }
28