-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a WtxId struct, and use it in Zebra's external network protocol #2618
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2dee9df
Make the `AuthDigest` display order match transaction IDs
teor2345 5bba0cd
Move transaction::Hash test to tests module
teor2345 bc55b63
Add a simple AuthDigest display order test
teor2345 423669e
Add a WtxId type for wide transaction IDs
teor2345 b3edc2e
Add conversions between transaction IDs and bytes
teor2345 fd803c2
Use the WtxId type in external network protocol messages
teor2345 eb50da5
Replace From/Borrow impls, because of unhelpful error messages
teor2345 df09a55
Merge branch 'main' into wtxid-struct-network
conradoplg f2efc24
Merge branch 'main' into wtxid-struct-network
teor2345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,20 +1,108 @@ | ||
use crate::primitives::zcash_primitives::auth_digest; | ||
use std::fmt; | ||
|
||
#[cfg(any(test, feature = "proptest-impl"))] | ||
use proptest_derive::Arbitrary; | ||
|
||
use crate::{ | ||
primitives::zcash_primitives::auth_digest, | ||
serialization::{ | ||
ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize, | ||
}, | ||
}; | ||
|
||
use super::Transaction; | ||
|
||
/// An authorizing data commitment hash as specified in [ZIP-244]. | ||
/// | ||
/// [ZIP-244]: https://zips.z.cash/zip-0244.. | ||
#[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
/// Note: Zebra displays transaction and block hashes in big-endian byte-order, | ||
/// following the u256 convention set by Bitcoin and zcashd. | ||
/// | ||
/// [ZIP-244]: https://zips.z.cash/zip-0244 | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash)] | ||
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))] | ||
pub struct AuthDigest(pub(crate) [u8; 32]); | ||
|
||
impl<'a> From<&'a Transaction> for AuthDigest { | ||
impl From<Transaction> for AuthDigest { | ||
/// Computes the authorizing data commitment for a transaction. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If passed a pre-v5 transaction. | ||
fn from(transaction: Transaction) -> Self { | ||
// use the ref implementation, to avoid cloning the transaction | ||
AuthDigest::from(&transaction) | ||
} | ||
} | ||
|
||
impl From<&Transaction> for AuthDigest { | ||
/// Computes the authorizing data commitment for a transaction. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If passed a pre-v5 transaction. | ||
fn from(transaction: &'a Transaction) -> Self { | ||
fn from(transaction: &Transaction) -> Self { | ||
auth_digest(transaction) | ||
} | ||
} | ||
|
||
impl From<[u8; 32]> for AuthDigest { | ||
fn from(bytes: [u8; 32]) -> Self { | ||
Self(bytes) | ||
} | ||
} | ||
|
||
impl From<AuthDigest> for [u8; 32] { | ||
fn from(auth_digest: AuthDigest) -> Self { | ||
auth_digest.0 | ||
} | ||
} | ||
|
||
impl From<&AuthDigest> for [u8; 32] { | ||
fn from(auth_digest: &AuthDigest) -> Self { | ||
(*auth_digest).into() | ||
} | ||
} | ||
|
||
impl fmt::Display for AuthDigest { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut reversed_bytes = self.0; | ||
reversed_bytes.reverse(); | ||
f.write_str(&hex::encode(&reversed_bytes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} | ||
|
||
impl fmt::Debug for AuthDigest { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut reversed_bytes = self.0; | ||
reversed_bytes.reverse(); | ||
f.debug_tuple("AuthDigest") | ||
.field(&hex::encode(reversed_bytes)) | ||
.finish() | ||
} | ||
} | ||
|
||
impl std::str::FromStr for AuthDigest { | ||
type Err = SerializationError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut bytes = [0; 32]; | ||
if hex::decode_to_slice(s, &mut bytes[..]).is_err() { | ||
Err(SerializationError::Parse("hex decoding error")) | ||
} else { | ||
bytes.reverse(); | ||
Ok(AuthDigest(bytes)) | ||
} | ||
} | ||
} | ||
|
||
impl ZcashSerialize for AuthDigest { | ||
fn zcash_serialize<W: std::io::Write>(&self, mut writer: W) -> Result<(), std::io::Error> { | ||
writer.write_32_bytes(&self.into()) | ||
} | ||
} | ||
|
||
impl ZcashDeserialize for AuthDigest { | ||
fn zcash_deserialize<R: std::io::Read>(mut reader: R) -> Result<Self, SerializationError> { | ||
Ok(reader.read_32_bytes()?.into()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍