-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b6688f
commit cb172f5
Showing
8 changed files
with
1,760 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Contains code that interfaces with the zcash_primitives crate from | ||
//! librustzcash. | ||
use std::{ | ||
convert::{TryFrom, TryInto}, | ||
io, | ||
}; | ||
|
||
use crate::{serialization::ZcashSerialize, transaction::Transaction}; | ||
|
||
impl TryFrom<&Transaction> for zcash_primitives::transaction::Transaction { | ||
type Error = io::Error; | ||
|
||
/// Convert a Zebra transaction into a librustzcash one. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If the transaction is not V5. (Currently there is no need for this | ||
/// conversion for other versions.) | ||
fn try_from(trans: &Transaction) -> Result<Self, Self::Error> { | ||
let network_upgrade = match trans { | ||
Transaction::V5 { | ||
network_upgrade, .. | ||
} => network_upgrade, | ||
Transaction::V1 { .. } | ||
| Transaction::V2 { .. } | ||
| Transaction::V3 { .. } | ||
| Transaction::V4 { .. } => panic!("Zebra only uses librustzcash for V5 transactions"), | ||
}; | ||
|
||
let serialized_tx = trans.zcash_serialize_to_vec()?; | ||
// The `read` method currently ignores the BranchId for V5 transactions; | ||
// but we use the correct BranchId anyway. | ||
let branch_id: u32 = network_upgrade | ||
.branch_id() | ||
.expect("Network upgrade must have a Branch ID") | ||
.into(); | ||
// We've already parsed this transaction, so its network upgrade must be valid. | ||
let branch_id: zcash_primitives::consensus::BranchId = branch_id | ||
.try_into() | ||
.expect("zcash_primitives and Zebra have the same branch ids"); | ||
let alt_tx = | ||
zcash_primitives::transaction::Transaction::read(&serialized_tx[..], branch_id)?; | ||
Ok(alt_tx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Transaction ID hashing. Contains code for generating the Transaction ID | ||
//! from the transaction, using hashing. | ||
use std::{convert::TryInto, io}; | ||
|
||
use super::Transaction; | ||
|
||
use crate::serialization::{sha256d, ZcashSerialize}; | ||
|
||
use super::Hash; | ||
|
||
/// A Transaction ID hasher. It computes the transaction ID by hashing | ||
/// different parts of the transaction, depending on the transaction version. | ||
/// For V5 transactions, it follows ZIP-244 and ZIP-225. | ||
pub(super) struct TxIdHasher<'a> { | ||
trans: &'a Transaction, | ||
} | ||
|
||
impl<'a> TxIdHasher<'a> { | ||
/// Return a new TxIdHasher for the given transaction. | ||
pub fn new(trans: &'a Transaction) -> Self { | ||
TxIdHasher { trans } | ||
} | ||
|
||
/// Compute the Transaction ID for the previously specified transaction. | ||
pub(super) fn txid(self) -> Result<Hash, io::Error> { | ||
match self.trans { | ||
Transaction::V1 { .. } | ||
| Transaction::V2 { .. } | ||
| Transaction::V3 { .. } | ||
| Transaction::V4 { .. } => self.txid_v1_to_v4(), | ||
Transaction::V5 { .. } => self.txid_v5(), | ||
} | ||
} | ||
|
||
/// Compute the Transaction ID for transactions V1 to V4. | ||
/// In these cases it's simply the hash of the serialized transaction. | ||
fn txid_v1_to_v4(self) -> Result<Hash, io::Error> { | ||
let mut hash_writer = sha256d::Writer::default(); | ||
self.trans | ||
.zcash_serialize(&mut hash_writer) | ||
.expect("Transactions must serialize into the hash."); | ||
Ok(Hash(hash_writer.finish())) | ||
} | ||
|
||
/// Compute the Transaction ID for a V5 transaction in the given network upgrade. | ||
/// In this case it's the hash of a tree of hashes of specific parts of the | ||
/// transaction, as specified in ZIP-244 and ZIP-225. | ||
fn txid_v5(self) -> Result<Hash, io::Error> { | ||
// The v5 txid (from ZIP-244) is computed using librustzcash. Convert the zebra | ||
// transaction to a librustzcash transaction. | ||
let alt_tx: zcash_primitives::transaction::Transaction = self.trans.try_into()?; | ||
Ok(Hash(*alt_tx.txid().as_ref())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.