Skip to content

Commit 9d300d2

Browse files
committed
Define TlvStream::skip_signatures
Provide a helper for skipping signature TLV records from a TLV stream. This prevents needing to duplicate the check for signature TLV records when writing a TLV stream without signatures in an upcoming commit.
1 parent ee0a46e commit 9d300d2

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lightning/src/offers/merkle.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,21 @@ fn message_digest(tag: &str, bytes: &[u8]) -> Message {
7575
/// Computes a merkle root hash for the given data, which must be a well-formed TLV stream
7676
/// containing at least one TLV record.
7777
fn root_hash(data: &[u8]) -> sha256::Hash {
78-
let mut tlv_stream = TlvStream::new(&data[..]).peekable();
7978
let nonce_tag = tagged_hash_engine(sha256::Hash::from_engine({
79+
let first_tlv_record = TlvStream::new(&data[..]).next().unwrap();
8080
let mut engine = sha256::Hash::engine();
8181
engine.input("LnNonce".as_bytes());
82-
engine.input(tlv_stream.peek().unwrap().record_bytes);
82+
engine.input(first_tlv_record.record_bytes);
8383
engine
8484
}));
8585
let leaf_tag = tagged_hash_engine(sha256::Hash::hash("LnLeaf".as_bytes()));
8686
let branch_tag = tagged_hash_engine(sha256::Hash::hash("LnBranch".as_bytes()));
8787

8888
let mut leaves = Vec::new();
89-
for record in tlv_stream {
90-
if !SIGNATURE_TYPES.contains(&record.r#type) {
91-
leaves.push(tagged_hash_from_engine(leaf_tag.clone(), &record.record_bytes));
92-
leaves.push(tagged_hash_from_engine(nonce_tag.clone(), &record.type_bytes));
93-
}
89+
let tlv_stream = TlvStream::new(&data[..]);
90+
for record in tlv_stream.skip_signatures() {
91+
leaves.push(tagged_hash_from_engine(leaf_tag.clone(), &record.record_bytes));
92+
leaves.push(tagged_hash_from_engine(nonce_tag.clone(), &record.type_bytes));
9493
}
9594

9695
// Calculate the merkle root hash in place.
@@ -154,6 +153,10 @@ impl<'a> TlvStream<'a> {
154153
data: io::Cursor::new(data),
155154
}
156155
}
156+
157+
fn skip_signatures(self) -> core::iter::Filter<TlvStream<'a>, fn(&TlvRecord) -> bool> {
158+
self.filter(|record| !SIGNATURE_TYPES.contains(&record.r#type))
159+
}
157160
}
158161

159162
/// A slice into a [`TlvStream`] for a record.

0 commit comments

Comments
 (0)