X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Foffers%2Fmerkle.rs;h=a3979866926170e8b5dc467ef10c59aae9c91c54;hb=478911d42a8b47020511bdcb793756723f3801be;hp=7330541220f625240dee1db5e13539a73aaea824;hpb=6e40e5f18a8b6cde673e1ea80c2a2113d5c80483;p=rust-lightning diff --git a/lightning/src/offers/merkle.rs b/lightning/src/offers/merkle.rs index 73305412..a3979866 100644 --- a/lightning/src/offers/merkle.rs +++ b/lightning/src/offers/merkle.rs @@ -12,10 +12,10 @@ use bitcoin::hashes::{Hash, HashEngine, sha256}; use bitcoin::secp256k1::{Message, PublicKey, Secp256k1, self}; use bitcoin::secp256k1::schnorr::Signature; -use core::convert::AsRef; use crate::io; use crate::util::ser::{BigSize, Readable, Writeable, Writer}; +#[allow(unused_imports)] use crate::prelude::*; /// Valid type range for signature TLV records. @@ -38,13 +38,23 @@ pub struct TaggedHash { } impl TaggedHash { + /// Creates a tagged hash with the given parameters. + /// + /// Panics if `bytes` is not a well-formed TLV stream containing at least one TLV record. + pub(super) fn from_valid_tlv_stream_bytes(tag: &'static str, bytes: &[u8]) -> Self { + let tlv_stream = TlvStream::new(bytes); + Self::from_tlv_stream(tag, tlv_stream) + } + /// 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: &'static str, tlv_stream: &[u8]) -> Self { + pub(super) fn from_tlv_stream<'a, I: core::iter::Iterator>>( + tag: &'static str, tlv_stream: I + ) -> Self { let tag_hash = sha256::Hash::hash(tag.as_bytes()); let merkle_root = root_hash(tlv_stream); - let digest = Message::from_slice(&tagged_hash(tag_hash, merkle_root)).unwrap(); + let digest = Message::from_slice(tagged_hash(tag_hash, merkle_root).as_byte_array()).unwrap(); Self { tag, merkle_root, @@ -66,6 +76,10 @@ impl TaggedHash { pub fn merkle_root(&self) -> sha256::Hash { self.merkle_root } + + pub(super) fn to_bytes(&self) -> [u8; 32] { + *self.digest.as_ref() + } } impl AsRef for TaggedHash { @@ -76,13 +90,28 @@ impl AsRef for TaggedHash { /// Error when signing messages. #[derive(Debug, PartialEq)] -pub enum SignError { +pub enum SignError { /// User-defined error when signing the message. - Signing(E), + Signing, /// Error when verifying the produced signature using the given pubkey. Verification(secp256k1::Error), } +/// A function for signing a [`TaggedHash`]. +pub(super) trait SignFn> { + /// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream. + fn sign(&self, message: &T) -> Result; +} + +impl SignFn for F +where + F: Fn(&TaggedHash) -> Result, +{ + fn sign(&self, message: &TaggedHash) -> Result { + self(message) + } +} + /// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream, checking if it /// can be verified with the supplied `pubkey`. /// @@ -92,14 +121,14 @@ pub enum SignError { /// /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest -pub(super) fn sign_message( - sign: F, message: &T, pubkey: PublicKey, -) -> Result> +pub(super) fn sign_message( + f: F, message: &T, pubkey: PublicKey, +) -> Result where - F: FnOnce(&T) -> Result, + F: SignFn, T: AsRef, { - let signature = sign(message).map_err(|e| SignError::Signing(e))?; + let signature = f.sign(message).map_err(|()| SignError::Signing)?; let digest = message.as_ref().as_digest(); let pubkey = pubkey.into(); @@ -122,9 +151,10 @@ pub(super) fn verify_signature( /// Computes a merkle root hash for the given data, which must be a well-formed TLV stream /// containing at least one TLV record. -fn root_hash(data: &[u8]) -> sha256::Hash { +fn root_hash<'a, I: core::iter::Iterator>>(tlv_stream: I) -> sha256::Hash { + let mut tlv_stream = tlv_stream.peekable(); let nonce_tag = tagged_hash_engine(sha256::Hash::from_engine({ - let first_tlv_record = TlvStream::new(&data[..]).next().unwrap(); + let first_tlv_record = tlv_stream.peek().unwrap(); let mut engine = sha256::Hash::engine(); engine.input("LnNonce".as_bytes()); engine.input(first_tlv_record.record_bytes); @@ -134,8 +164,7 @@ fn root_hash(data: &[u8]) -> sha256::Hash { let branch_tag = tagged_hash_engine(sha256::Hash::hash("LnBranch".as_bytes())); let mut leaves = Vec::new(); - let tlv_stream = TlvStream::new(&data[..]); - for record in tlv_stream.skip_signatures() { + for record in TlvStream::skip_signatures(tlv_stream) { leaves.push(tagged_hash_from_engine(leaf_tag.clone(), &record.record_bytes)); leaves.push(tagged_hash_from_engine(nonce_tag.clone(), &record.type_bytes)); } @@ -212,8 +241,10 @@ impl<'a> TlvStream<'a> { .take_while(move |record| take_range.contains(&record.r#type)) } - fn skip_signatures(self) -> core::iter::Filter, fn(&TlvRecord) -> bool> { - self.filter(|record| !SIGNATURE_TYPES.contains(&record.r#type)) + fn skip_signatures( + tlv_stream: impl core::iter::Iterator> + ) -> impl core::iter::Iterator> { + tlv_stream.filter(|record| !SIGNATURE_TYPES.contains(&record.r#type)) } } @@ -261,7 +292,7 @@ impl<'a> Writeable for WithoutSignatures<'a> { #[inline] fn write(&self, writer: &mut W) -> Result<(), io::Error> { let tlv_stream = TlvStream::new(self.0); - for record in tlv_stream.skip_signatures() { + for record in TlvStream::skip_signatures(tlv_stream) { writer.write_all(record.record_bytes)?; } Ok(()) @@ -273,11 +304,11 @@ mod tests { use super::{SIGNATURE_TYPES, TlvStream, WithoutSignatures}; use bitcoin::hashes::{Hash, sha256}; + use bitcoin::hashes::hex::FromHex; use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey}; use bitcoin::secp256k1::schnorr::Signature; - use core::convert::Infallible; use crate::offers::offer::{Amount, OfferBuilder}; - use crate::offers::invoice_request::InvoiceRequest; + use crate::offers::invoice_request::{InvoiceRequest, UnsignedInvoiceRequest}; use crate::offers::parse::Bech32Encode; use crate::offers::test_utils::{payer_pubkey, recipient_pubkey}; use crate::util::ser::Writeable; @@ -289,16 +320,16 @@ mod tests { macro_rules! tlv2 { () => { "02080000010000020003" } } macro_rules! tlv3 { () => { "03310266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c0351800000000000000010000000000000002" } } assert_eq!( - super::root_hash(&hex::decode(tlv1!()).unwrap()), - sha256::Hash::from_slice(&hex::decode("b013756c8fee86503a0b4abdab4cddeb1af5d344ca6fc2fa8b6c08938caa6f93").unwrap()).unwrap(), + super::root_hash(TlvStream::new(&>::from_hex(tlv1!()).unwrap())), + sha256::Hash::from_slice(&>::from_hex("b013756c8fee86503a0b4abdab4cddeb1af5d344ca6fc2fa8b6c08938caa6f93").unwrap()).unwrap(), ); assert_eq!( - super::root_hash(&hex::decode(concat!(tlv1!(), tlv2!())).unwrap()), - sha256::Hash::from_slice(&hex::decode("c3774abbf4815aa54ccaa026bff6581f01f3be5fe814c620a252534f434bc0d1").unwrap()).unwrap(), + super::root_hash(TlvStream::new(&>::from_hex(concat!(tlv1!(), tlv2!())).unwrap())), + sha256::Hash::from_slice(&>::from_hex("c3774abbf4815aa54ccaa026bff6581f01f3be5fe814c620a252534f434bc0d1").unwrap()).unwrap(), ); assert_eq!( - super::root_hash(&hex::decode(concat!(tlv1!(), tlv2!(), tlv3!())).unwrap()), - sha256::Hash::from_slice(&hex::decode("ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d").unwrap()).unwrap(), + super::root_hash(TlvStream::new(&>::from_hex(concat!(tlv1!(), tlv2!(), tlv3!())).unwrap())), + sha256::Hash::from_slice(&>::from_hex("ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d").unwrap()).unwrap(), ); } @@ -306,11 +337,11 @@ mod tests { fn calculates_merkle_root_hash_from_invoice_request() { let secp_ctx = Secp256k1::new(); let recipient_pubkey = { - let secret_key = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap(); + let secret_key = SecretKey::from_slice(&>::from_hex("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap(); KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key() }; let payer_keys = { - let secret_key = SecretKey::from_slice(&hex::decode("4242424242424242424242424242424242424242424242424242424242424242").unwrap()).unwrap(); + let secret_key = SecretKey::from_slice(&>::from_hex("4242424242424242424242424242424242424242424242424242424242424242").unwrap()).unwrap(); KeyPair::from_secret_key(&secp_ctx, &secret_key) }; @@ -320,8 +351,8 @@ mod tests { .build_unchecked() .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap() .build_unchecked() - .sign::<_, Infallible>( - |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys)) + .sign(|message: &UnsignedInvoiceRequest| + Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys)) ) .unwrap(); assert_eq!( @@ -329,12 +360,12 @@ mod tests { "lnr1qqyqqqqqqqqqqqqqqcp4256ypqqkgzshgysy6ct5dpjk6ct5d93kzmpq23ex2ct5d9ek293pqthvwfzadd7jejes8q9lhc4rvjxd022zv5l44g6qah82ru5rdpnpjkppqvjx204vgdzgsqpvcp4mldl3plscny0rt707gvpdh6ndydfacz43euzqhrurageg3n7kafgsek6gz3e9w52parv8gs2hlxzk95tzeswywffxlkeyhml0hh46kndmwf4m6xma3tkq2lu04qz3slje2rfthc89vss", ); assert_eq!( - super::root_hash(&invoice_request.bytes[..]), - sha256::Hash::from_slice(&hex::decode("608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624").unwrap()).unwrap(), + super::root_hash(TlvStream::new(&invoice_request.bytes[..])), + sha256::Hash::from_slice(&>::from_hex("608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624").unwrap()).unwrap(), ); assert_eq!( invoice_request.signature(), - Signature::from_slice(&hex::decode("b8f83ea3288cfd6ea510cdb481472575141e8d8744157f98562d162cc1c472526fdb24befefbdebab4dbb726bbd1b7d8aec057f8fa805187e5950d2bbe0e5642").unwrap()).unwrap(), + Signature::from_slice(&>::from_hex("b8f83ea3288cfd6ea510cdb481472575141e8d8744157f98562d162cc1c472526fdb24befefbdebab4dbb726bbd1b7d8aec057f8fa805187e5950d2bbe0e5642").unwrap()).unwrap(), ); } @@ -352,7 +383,7 @@ mod tests { let tagged_hash = unsigned_invoice_request.as_ref(); let expected_digest = unsigned_invoice_request.as_ref().as_digest(); let tag = sha256::Hash::hash(tagged_hash.tag().as_bytes()); - let actual_digest = Message::from_slice(&super::tagged_hash(tag, tagged_hash.merkle_root())) + let actual_digest = Message::from_slice(super::tagged_hash(tag, tagged_hash.merkle_root()).as_byte_array()) .unwrap(); assert_eq!(*expected_digest, actual_digest); } @@ -374,8 +405,8 @@ mod tests { .build_unchecked() .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap() .build_unchecked() - .sign::<_, Infallible>( - |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys)) + .sign(|message: &UnsignedInvoiceRequest| + Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys)) ) .unwrap(); @@ -406,8 +437,8 @@ mod tests { .build_unchecked() .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap() .build_unchecked() - .sign::<_, Infallible>( - |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys)) + .sign(|message: &UnsignedInvoiceRequest| + Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys)) ) .unwrap();