]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/util/invoice.rs
Merge pull request #2248 from TheBlueMatt/2023-04-gossip-check
[rust-lightning] / lightning / src / util / invoice.rs
1 //! Low level invoice utilities.
2
3 use bitcoin::bech32::{u5, FromBase32};
4 use crate::prelude::*;
5
6 /// Construct the invoice's HRP and signatureless data into a preimage to be hashed.
7 pub fn construct_invoice_preimage(hrp_bytes: &[u8], data_without_signature: &[u5]) -> Vec<u8> {
8         let mut preimage = Vec::<u8>::from(hrp_bytes);
9
10         let mut data_part = Vec::from(data_without_signature);
11         let overhang = (data_part.len() * 5) % 8;
12         if overhang > 0 {
13                 // add padding if data does not end at a byte boundary
14                 data_part.push(u5::try_from_u8(0).unwrap());
15
16                 // if overhang is in (1..3) we need to add u5(0) padding two times
17                 if overhang < 3 {
18                         data_part.push(u5::try_from_u8(0).unwrap());
19                 }
20         }
21
22         preimage.extend_from_slice(&Vec::<u8>::from_base32(&data_part)
23                 .expect("No padding error may occur due to appended zero above."));
24         preimage
25 }
26