diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index f9c7f628b99..548ad14b103 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -374,16 +374,15 @@ impl FromStr for SignedRawBolt11Invoice { let raw_hrp: RawHrp = hrp.to_string().to_lowercase().parse()?; let data_part = RawDataPart::from_base32(&data[..data.len() - SIGNATURE_LEN5])?; + let raw_invoice = RawBolt11Invoice { + hrp: raw_hrp, + data: data_part, + }; + let hash = raw_invoice.signable_hash(); Ok(SignedRawBolt11Invoice { - raw_invoice: RawBolt11Invoice { - hrp: raw_hrp, - data: data_part, - }, - hash: RawBolt11Invoice::hash_from_parts( - hrp.to_string().as_bytes(), - &data[..data.len() - SIGNATURE_LEN5], - ), + raw_invoice, + hash, signature: Bolt11InvoiceSignature::from_base32(&data[data.len() - SIGNATURE_LEN5..])?, }) } diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 0f78aad61c6..8f9678c91b3 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -1001,18 +1001,12 @@ macro_rules! find_all_extract { #[allow(missing_docs)] impl RawBolt11Invoice { /// Hash the HRP (as bytes) and signatureless data part (as Fe32 iterator) - fn hash_from_parts_iter<'s>(hrp_bytes: &[u8], data_without_signature_iter: Box + 's>) -> [u8; 32] { - let data_part_signature = data_without_signature_iter.collect::>(); - Self::hash_from_parts(hrp_bytes, &data_part_signature[..]) - } - - /// Hash the HRP as bytes and signatureless data part. - fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[Fe32]) -> [u8; 32] { + fn hash_from_parts<'s>(hrp_bytes: &[u8], data_without_signature: Box + 's>) -> [u8; 32] { use crate::de::FromBase32; - let mut preimage = Vec::::from(hrp_bytes); + let mut data_part = data_without_signature.collect::>(); - let mut data_part = Vec::from(data_without_signature); + // Need to pad before from_base32 conversion let overhang = (data_part.len() * 5) % 8; if overhang > 0 { // add padding if data does not end at a byte boundary @@ -1024,6 +1018,7 @@ impl RawBolt11Invoice { } } + let mut preimage = Vec::::from(hrp_bytes); preimage.extend_from_slice(&Vec::::from_base32(&data_part) .expect("No padding error may occur due to appended zero above.")); @@ -1036,7 +1031,7 @@ impl RawBolt11Invoice { pub fn signable_hash(&self) -> [u8; 32] { use crate::ser::Base32Iterable; - RawBolt11Invoice::hash_from_parts_iter( + Self::hash_from_parts( self.hrp.to_string().as_bytes(), self.data.fe_iter(), )