Skip to content

Commit

Permalink
expose more granular data in TaggedHash struct
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Oct 27, 2023
1 parent 9de51f0 commit 6488ba1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
22 changes: 20 additions & 2 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,9 @@ mod tests {
use super::{InvoiceRequest, InvoiceRequestTlvStreamRef, SIGNATURE_TAG, UnsignedInvoiceRequest};

use bitcoin::blockdata::constants::ChainHash;
use bitcoin::hashes::{sha256, Hash};
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, self};
use core::convert::{Infallible, TryFrom};
use core::num::NonZeroU64;
#[cfg(feature = "std")]
Expand All @@ -934,7 +935,7 @@ mod tests {
use crate::ln::inbound_payment::ExpandedKey;
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
use crate::offers::invoice::{Bolt12Invoice, SIGNATURE_TAG as INVOICE_SIGNATURE_TAG};
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, TaggedHash, self};
use crate::offers::merkle::{tagged_hash, SignError, SignatureTlvStreamRef, TaggedHash, self};
use crate::offers::offer::{Amount, OfferBuilder, OfferTlvStreamRef, Quantity};
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
use crate::offers::payer::PayerTlvStreamRef;
Expand Down Expand Up @@ -1537,6 +1538,23 @@ mod tests {
assert_eq!(tlv_stream.payer_note, Some(&String::from("baz")));
}

#[test]
fn compute_tagged_hash() {
let unsigned_invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.payer_note("bar".into())
.build().unwrap();

// Simply test that we can grab the tag and merkle root exposed by the accessor
// functions, then use them tosuccesfully compute a tagged hash.
let taggedhash = unsigned_invoice_request.as_ref();
let tag = sha256::Hash::hash(taggedhash.tag().as_bytes());
let _ = Message::from_slice(&tagged_hash(tag, taggedhash.merkle_root_hash()))
.unwrap();
}

#[test]
fn fails_signing_invoice_request() {
match OfferBuilder::new("foo".into(), recipient_pubkey())
Expand Down
26 changes: 22 additions & 4 deletions lightning/src/offers/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,37 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
/// [BIP 340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
/// [BOLT 12]: https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md#signature-calculation
#[derive(Debug, PartialEq)]
pub struct TaggedHash(Message);
pub struct TaggedHash {
tag: String,
merkle_root_hash: sha256::Hash,
digest: Message,
}

impl TaggedHash {
/// Creates a tagged hash with the given parameters.
///
/// Panics if `tlv_stream` is not a well-formed TLV stream containing at least one TLV record.
pub(super) fn new(tag: &str, tlv_stream: &[u8]) -> Self {
Self(message_digest(tag, tlv_stream))
Self{
tag: tag.to_owned(),
merkle_root_hash: root_hash(tlv_stream),
digest: message_digest(tag, tlv_stream),
}
}

/// Returns the digest to sign.
pub fn as_digest(&self) -> &Message {
&self.0
&self.digest
}

/// Returns the tag used in the TaggedHash.
pub fn tag(&self) -> &str {
&self.tag
}

/// Returns the merkle root hash used in the TaggedHash.
pub fn merkle_root_hash(&self) -> sha256::Hash {
self.merkle_root_hash
}
}

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

fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
pub(crate) fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
let engine = tagged_hash_engine(tag);
tagged_hash_from_engine(engine, msg)
}
Expand Down

0 comments on commit 6488ba1

Please sign in to comment.