Skip to content

Commit 3795f60

Browse files
committed
Merge rust-bitcoin#515: Add Inventory::network_hash() method
3d524e0 Add Inventory::network_hash() method (Steven Roose) Pull request description: I'm not positive we won't ever had inv items that are not `sha256d::Hash`, though. I would expect them to stay like that, but we never know. Would accept that as a reasonable objection against this helper. ACKs for top commit: apoelstra: ACK 3d524e0 tcharding: ACK 3d524e0 Tree-SHA512: 97d64b08ff99dbec2d907bdb98aa41ede4a15b988551ad8dd87a10ac5c9750ae4d516bb558c1e4f2171612777355c2baf1ffbe671cb92320a1af64d73bd150dd
2 parents 1ad17a6 + 3d524e0 commit 3795f60

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

bitcoin/src/network/message_blockdata.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ pub enum Inventory {
4242
},
4343
}
4444

45+
impl Inventory {
46+
/// Return the item value represented as a SHA256-d hash.
47+
///
48+
/// Returns [None] only for [Inventory::Error].
49+
pub fn network_hash(&self) -> Option<[u8; 32]> {
50+
match self {
51+
Inventory::Error => None,
52+
Inventory::Transaction(t) => Some(t.to_byte_array()),
53+
Inventory::Block(b) => Some(b.to_byte_array()),
54+
Inventory::CompactBlock(b) => Some(b.to_byte_array()),
55+
Inventory::WTx(t) => Some(t.to_byte_array()),
56+
Inventory::WitnessTransaction(t) => Some(t.to_byte_array()),
57+
Inventory::WitnessBlock(b) => Some(b.to_byte_array()),
58+
Inventory::Unknown { hash, .. } => Some(*hash),
59+
}
60+
}
61+
}
62+
4563
impl Encodable for Inventory {
4664
#[inline]
4765
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {

0 commit comments

Comments
 (0)