Skip to content

Commit 8b37bda

Browse files
committed
refactor to remove message_digest
1 parent b3e7aac commit 8b37bda

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

lightning/src/offers/invoice.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl UnsignedBolt12Invoice {
439439
bytes: self.bytes,
440440
contents: self.contents,
441441
signature,
442+
tagged_hash: self.tagged_hash,
442443
})
443444
}
444445
}
@@ -463,6 +464,7 @@ pub struct Bolt12Invoice {
463464
bytes: Vec<u8>,
464465
contents: InvoiceContents,
465466
signature: Signature,
467+
tagged_hash: TaggedHash,
466468
}
467469

468470
/// The contents of an [`Bolt12Invoice`] for responding to either an [`Offer`] or a [`Refund`].
@@ -707,7 +709,7 @@ impl Bolt12Invoice {
707709

708710
/// Hash that was used for signing the invoice.
709711
pub fn signable_hash(&self) -> [u8; 32] {
710-
merkle::message_digest(SIGNATURE_TAG, &self.bytes).as_ref().clone()
712+
self.tagged_hash.as_digest().as_ref().clone()
711713
}
712714

713715
/// Verifies that the invoice was for a request or refund created using the given key. Returns
@@ -1212,11 +1214,11 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
12121214
None => return Err(Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature)),
12131215
Some(signature) => signature,
12141216
};
1215-
let message = TaggedHash::new(SIGNATURE_TAG, &bytes);
1217+
let tagged_hash = TaggedHash::new(SIGNATURE_TAG, &bytes);
12161218
let pubkey = contents.fields().signing_pubkey;
1217-
merkle::verify_signature(&signature, message, pubkey)?;
1219+
merkle::verify_signature(&signature, tagged_hash.clone(), pubkey)?;
12181220

1219-
Ok(Bolt12Invoice { bytes, contents, signature })
1221+
Ok(Bolt12Invoice { bytes, contents, signature, tagged_hash })
12201222
}
12211223
}
12221224

lightning/src/offers/merkle.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
3030
///
3131
/// [BIP 340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
3232
/// [BOLT 12]: https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md#signature-calculation
33-
#[derive(Debug, PartialEq)]
33+
#[derive(Clone, Debug, PartialEq)]
3434
pub struct TaggedHash(Message);
3535

3636
impl TaggedHash {
3737
/// Creates a tagged hash with the given parameters.
3838
///
3939
/// Panics if `tlv_stream` is not a well-formed TLV stream containing at least one TLV record.
4040
pub(super) fn new(tag: &str, tlv_stream: &[u8]) -> Self {
41-
Self(message_digest(tag, tlv_stream))
41+
let tag = sha256::Hash::hash(tag.as_bytes());
42+
let merkle_root = root_hash(tlv_stream);
43+
Self(Message::from_slice(&tagged_hash(tag, merkle_root)).unwrap())
4244
}
4345

4446
/// Returns the digest to sign.
@@ -99,12 +101,6 @@ pub(super) fn verify_signature(
99101
secp_ctx.verify_schnorr(signature, digest, &pubkey)
100102
}
101103

102-
pub(super) fn message_digest(tag: &str, bytes: &[u8]) -> Message {
103-
let tag = sha256::Hash::hash(tag.as_bytes());
104-
let merkle_root = root_hash(bytes);
105-
Message::from_slice(&tagged_hash(tag, merkle_root)).unwrap()
106-
}
107-
108104
/// Computes a merkle root hash for the given data, which must be a well-formed TLV stream
109105
/// containing at least one TLV record.
110106
fn root_hash(data: &[u8]) -> sha256::Hash {

0 commit comments

Comments
 (0)