Skip to content

Commit 6488ba1

Browse files
committed
expose more granular data in TaggedHash struct
1 parent 9de51f0 commit 6488ba1

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,9 @@ mod tests {
922922
use super::{InvoiceRequest, InvoiceRequestTlvStreamRef, SIGNATURE_TAG, UnsignedInvoiceRequest};
923923

924924
use bitcoin::blockdata::constants::ChainHash;
925+
use bitcoin::hashes::{sha256, Hash};
925926
use bitcoin::network::constants::Network;
926-
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey, self};
927+
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, self};
927928
use core::convert::{Infallible, TryFrom};
928929
use core::num::NonZeroU64;
929930
#[cfg(feature = "std")]
@@ -934,7 +935,7 @@ mod tests {
934935
use crate::ln::inbound_payment::ExpandedKey;
935936
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
936937
use crate::offers::invoice::{Bolt12Invoice, SIGNATURE_TAG as INVOICE_SIGNATURE_TAG};
937-
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, TaggedHash, self};
938+
use crate::offers::merkle::{tagged_hash, SignError, SignatureTlvStreamRef, TaggedHash, self};
938939
use crate::offers::offer::{Amount, OfferBuilder, OfferTlvStreamRef, Quantity};
939940
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
940941
use crate::offers::payer::PayerTlvStreamRef;
@@ -1537,6 +1538,23 @@ mod tests {
15371538
assert_eq!(tlv_stream.payer_note, Some(&String::from("baz")));
15381539
}
15391540

1541+
#[test]
1542+
fn compute_tagged_hash() {
1543+
let unsigned_invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
1544+
.amount_msats(1000)
1545+
.build().unwrap()
1546+
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1547+
.payer_note("bar".into())
1548+
.build().unwrap();
1549+
1550+
// Simply test that we can grab the tag and merkle root exposed by the accessor
1551+
// functions, then use them tosuccesfully compute a tagged hash.
1552+
let taggedhash = unsigned_invoice_request.as_ref();
1553+
let tag = sha256::Hash::hash(taggedhash.tag().as_bytes());
1554+
let _ = Message::from_slice(&tagged_hash(tag, taggedhash.merkle_root_hash()))
1555+
.unwrap();
1556+
}
1557+
15401558
#[test]
15411559
fn fails_signing_invoice_request() {
15421560
match OfferBuilder::new("foo".into(), recipient_pubkey())

lightning/src/offers/merkle.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,37 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
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
3333
#[derive(Debug, PartialEq)]
34-
pub struct TaggedHash(Message);
34+
pub struct TaggedHash {
35+
tag: String,
36+
merkle_root_hash: sha256::Hash,
37+
digest: Message,
38+
}
3539

3640
impl TaggedHash {
3741
/// Creates a tagged hash with the given parameters.
3842
///
3943
/// Panics if `tlv_stream` is not a well-formed TLV stream containing at least one TLV record.
4044
pub(super) fn new(tag: &str, tlv_stream: &[u8]) -> Self {
41-
Self(message_digest(tag, tlv_stream))
45+
Self{
46+
tag: tag.to_owned(),
47+
merkle_root_hash: root_hash(tlv_stream),
48+
digest: message_digest(tag, tlv_stream),
49+
}
4250
}
4351

4452
/// Returns the digest to sign.
4553
pub fn as_digest(&self) -> &Message {
46-
&self.0
54+
&self.digest
55+
}
56+
57+
/// Returns the tag used in the TaggedHash.
58+
pub fn tag(&self) -> &str {
59+
&self.tag
60+
}
61+
62+
/// Returns the merkle root hash used in the TaggedHash.
63+
pub fn merkle_root_hash(&self) -> sha256::Hash {
64+
self.merkle_root_hash
4765
}
4866
}
4967

@@ -144,7 +162,7 @@ fn root_hash(data: &[u8]) -> sha256::Hash {
144162
*leaves.first().unwrap()
145163
}
146164

147-
fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
165+
pub(crate) fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
148166
let engine = tagged_hash_engine(tag);
149167
tagged_hash_from_engine(engine, msg)
150168
}

0 commit comments

Comments
 (0)