chain: add Transaction::hash() method.

This makes Transaction and Block have a consistent API.
This commit is contained in:
Henry de Valence 2020-09-02 14:59:16 -07:00 committed by Deirdre Connolly
parent a9029beb87
commit ca4a5ce30c
3 changed files with 8 additions and 3 deletions

View File

@ -50,7 +50,7 @@ impl Block {
}) })
} }
/// Get the hash for the current block /// Compute the hash of this block.
pub fn hash(&self) -> Hash { pub fn hash(&self) -> Hash {
Hash::from(self) Hash::from(self)
} }

View File

@ -97,6 +97,11 @@ pub enum Transaction {
} }
impl Transaction { impl Transaction {
/// Compute the hash of this transaction.
pub fn hash(&self) -> Hash {
Hash::from(self)
}
/// Access the transparent inputs of this transaction, regardless of version. /// Access the transparent inputs of this transaction, regardless of version.
pub fn inputs(&self) -> &[transparent::Input] { pub fn inputs(&self) -> &[transparent::Input] {
match self { match self {

View File

@ -17,8 +17,8 @@ use super::Transaction;
#[cfg_attr(test, derive(Arbitrary))] #[cfg_attr(test, derive(Arbitrary))]
pub struct Hash(pub [u8; 32]); pub struct Hash(pub [u8; 32]);
impl From<Transaction> for Hash { impl<'a> From<&'a Transaction> for Hash {
fn from(transaction: Transaction) -> Self { fn from(transaction: &'a Transaction) -> Self {
let mut hash_writer = sha256d::Writer::default(); let mut hash_writer = sha256d::Writer::default();
transaction transaction
.zcash_serialize(&mut hash_writer) .zcash_serialize(&mut hash_writer)