2021-08-25 14:02:47 -07:00
|
|
|
use std::{fmt, sync::Arc};
|
2021-08-16 14:26:08 -07:00
|
|
|
|
|
|
|
#[cfg(any(test, feature = "proptest-impl"))]
|
|
|
|
use proptest_derive::Arbitrary;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
primitives::zcash_primitives::auth_digest,
|
|
|
|
serialization::{
|
|
|
|
ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize,
|
|
|
|
},
|
|
|
|
};
|
2021-08-13 09:58:04 -07:00
|
|
|
|
|
|
|
use super::Transaction;
|
|
|
|
|
|
|
|
/// An authorizing data commitment hash as specified in [ZIP-244].
|
|
|
|
///
|
2021-08-16 14:26:08 -07:00
|
|
|
/// 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))]
|
2021-10-11 17:35:50 -07:00
|
|
|
pub struct AuthDigest(pub [u8; 32]);
|
2021-08-13 09:58:04 -07:00
|
|
|
|
2021-08-16 14:26:08 -07:00
|
|
|
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 {
|
2021-08-13 09:58:04 -07:00
|
|
|
/// Computes the authorizing data commitment for a transaction.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If passed a pre-v5 transaction.
|
2021-08-16 14:26:08 -07:00
|
|
|
fn from(transaction: &Transaction) -> Self {
|
2021-08-13 09:58:04 -07:00
|
|
|
auth_digest(transaction)
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 14:26:08 -07:00
|
|
|
|
2021-08-25 14:02:47 -07:00
|
|
|
impl From<Arc<Transaction>> for AuthDigest {
|
|
|
|
/// Computes the authorizing data commitment for a transaction.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If passed a pre-v5 transaction.
|
|
|
|
fn from(transaction: Arc<Transaction>) -> Self {
|
|
|
|
transaction.as_ref().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 14:26:08 -07:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|