use std::{borrow::Borrow, io, sync::Arc};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use halo2::pasta::group::ff::PrimeField;
use hex::FromHex;
use reddsa::{orchard::Binding, orchard::SpendAuth, Signature};
use crate::{
amount,
block::MAX_BLOCK_BYTES,
parameters::{OVERWINTER_VERSION_GROUP_ID, SAPLING_VERSION_GROUP_ID, TX_V5_VERSION_GROUP_ID},
primitives::{Halo2Proof, ZkSnarkProof},
serialization::{
zcash_deserialize_external_count, zcash_serialize_empty_list,
zcash_serialize_external_count, AtLeastOne, CompactSizeMessage, ReadZcashExt,
SerializationError, TrustedPreallocate, ZcashDeserialize, ZcashDeserializeInto,
ZcashSerialize,
},
};
#[cfg(all(zcash_unstable = "nu7", feature = "tx_v6"))]
use crate::parameters::TX_V6_VERSION_GROUP_ID;
use super::*;
use crate::sapling;
impl ZcashDeserialize for jubjub::Fq {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let possible_scalar = jubjub::Fq::from_bytes(&reader.read_32_bytes()?);
if possible_scalar.is_some().into() {
Ok(possible_scalar.unwrap())
} else {
Err(SerializationError::Parse(
"Invalid jubjub::Fq, input not canonical",
))
}
}
}
impl ZcashDeserialize for pallas::Scalar {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let possible_scalar = pallas::Scalar::from_repr(reader.read_32_bytes()?);
if possible_scalar.is_some().into() {
Ok(possible_scalar.unwrap())
} else {
Err(SerializationError::Parse(
"Invalid pallas::Scalar, input not canonical",
))
}
}
}
impl ZcashDeserialize for pallas::Base {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let possible_field_element = pallas::Base::from_repr(reader.read_32_bytes()?);
if possible_field_element.is_some().into() {
Ok(possible_field_element.unwrap())
} else {
Err(SerializationError::Parse(
"Invalid pallas::Base, input not canonical",
))
}
}
}
impl<P: ZkSnarkProof> ZcashSerialize for JoinSplitData<P> {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
let joinsplits: Vec<_> = self.joinsplits().cloned().collect();
joinsplits.zcash_serialize(&mut writer)?;
writer.write_all(&<[u8; 32]>::from(self.pub_key)[..])?;
writer.write_all(&<[u8; 64]>::from(self.sig)[..])?;
Ok(())
}
}
impl<P> ZcashDeserialize for Option<JoinSplitData<P>>
where
P: ZkSnarkProof,
sprout::JoinSplit<P>: TrustedPreallocate,
{
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let joinsplits: Vec<sprout::JoinSplit<P>> = (&mut reader).zcash_deserialize_into()?;
match joinsplits.split_first() {
None => Ok(None),
Some((first, rest)) => {
let pub_key = reader.read_32_bytes()?.into();
let sig = reader.read_64_bytes()?.into();
Ok(Some(JoinSplitData {
first: first.clone(),
rest: rest.to_vec(),
pub_key,
sig,
}))
}
}
}
}
impl ZcashSerialize for Option<sapling::ShieldedData<sapling::SharedAnchor>> {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
match self {
None => {
zcash_serialize_empty_list(&mut writer)?;
zcash_serialize_empty_list(&mut writer)?;
}
Some(sapling_shielded_data) => {
sapling_shielded_data.zcash_serialize(&mut writer)?;
}
}
Ok(())
}
}
impl ZcashSerialize for sapling::ShieldedData<sapling::SharedAnchor> {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
let (spend_prefixes, spend_proofs_sigs): (Vec<_>, Vec<_>) = self
.spends()
.cloned()
.map(sapling::Spend::<sapling::SharedAnchor>::into_v5_parts)
.map(|(prefix, proof, sig)| (prefix, (proof, sig)))
.unzip();
let (spend_proofs, spend_sigs) = spend_proofs_sigs.into_iter().unzip();
let (output_prefixes, output_proofs): (Vec<_>, _) = self
.outputs()
.cloned()
.map(sapling::Output::into_v5_parts)
.unzip();
spend_prefixes.zcash_serialize(&mut writer)?;
output_prefixes.zcash_serialize(&mut writer)?;
self.value_balance.zcash_serialize(&mut writer)?;
if let Some(shared_anchor) = self.shared_anchor() {
writer.write_all(&<[u8; 32]>::from(shared_anchor)[..])?;
}
zcash_serialize_external_count(&spend_proofs, &mut writer)?;
zcash_serialize_external_count(&spend_sigs, &mut writer)?;
zcash_serialize_external_count(&output_proofs, &mut writer)?;
writer.write_all(&<[u8; 64]>::from(self.binding_sig)[..])?;
Ok(())
}
}
impl ZcashDeserialize for Option<sapling::ShieldedData<sapling::SharedAnchor>> {
#[allow(clippy::unwrap_in_result)]
fn zcash_deserialize<R: io::Read>(reader: R) -> Result<Self, SerializationError> {
deserialize_v5_sapling_shielded_data(reader, false)
}
}
#[allow(clippy::unwrap_in_result)]
fn deserialize_v5_sapling_shielded_data<R: io::Read>(
mut reader: R,
is_coinbase: bool,
) -> Result<Option<sapling::ShieldedData<sapling::SharedAnchor>>, SerializationError> {
let spend_count: CompactSizeMessage = (&mut reader).zcash_deserialize_into()?;
let spend_count: usize = spend_count.into();
if is_coinbase && spend_count > 0 {
return Err(SerializationError::Parse(
"coinbase transaction must not have Sapling spends",
));
}
let spend_prefixes: Vec<sapling::SpendPrefixInTransactionV5> =
zcash_deserialize_external_count(spend_count, &mut reader)?;
let output_prefixes: Vec<_> = (&mut reader).zcash_deserialize_into()?;
let spends_count = spend_prefixes.len();
let outputs_count = output_prefixes.len();
if spend_prefixes.is_empty() && output_prefixes.is_empty() {
return Ok(None);
}
let value_balance = (&mut reader).zcash_deserialize_into()?;
let shared_anchor = if spends_count > 0 {
Some((&mut reader).zcash_deserialize_into()?)
} else {
None
};
let spend_proofs = zcash_deserialize_external_count(spends_count, &mut reader)?;
let spend_sigs = zcash_deserialize_external_count(spends_count, &mut reader)?;
let output_proofs = zcash_deserialize_external_count(outputs_count, &mut reader)?;
let binding_sig = reader.read_64_bytes()?.into();
let spends: Vec<_> = spend_prefixes
.into_iter()
.zip(spend_proofs)
.zip(spend_sigs)
.map(|((prefix, proof), sig)| {
sapling::Spend::<sapling::SharedAnchor>::from_v5_parts(prefix, proof, sig)
})
.collect();
let outputs = output_prefixes
.into_iter()
.zip(output_proofs)
.map(|(prefix, proof)| sapling::Output::from_v5_parts(prefix, proof))
.collect();
let transfers = match shared_anchor {
Some(shared_anchor) => sapling::TransferData::SpendsAndMaybeOutputs {
shared_anchor,
spends: spends
.try_into()
.expect("checked spends when parsing shared anchor"),
maybe_outputs: outputs,
},
None => sapling::TransferData::JustOutputs {
outputs: outputs
.try_into()
.expect("checked spends or outputs and returned early"),
},
};
Ok(Some(sapling::ShieldedData {
value_balance,
transfers,
binding_sig,
}))
}
impl ZcashSerialize for Option<orchard::ShieldedData> {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
match self {
None => {
zcash_serialize_empty_list(writer)?;
}
Some(orchard_shielded_data) => {
orchard_shielded_data.zcash_serialize(&mut writer)?;
}
}
Ok(())
}
}
impl ZcashSerialize for orchard::ShieldedData {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
let (actions, sigs): (Vec<orchard::Action>, Vec<Signature<SpendAuth>>) = self
.actions
.iter()
.cloned()
.map(orchard::AuthorizedAction::into_parts)
.unzip();
actions.zcash_serialize(&mut writer)?;
self.flags.zcash_serialize(&mut writer)?;
self.value_balance.zcash_serialize(&mut writer)?;
self.shared_anchor.zcash_serialize(&mut writer)?;
self.proof.zcash_serialize(&mut writer)?;
zcash_serialize_external_count(&sigs, &mut writer)?;
self.binding_sig.zcash_serialize(&mut writer)?;
Ok(())
}
}
impl ZcashDeserialize for Option<orchard::ShieldedData> {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let actions: Vec<orchard::Action> = (&mut reader).zcash_deserialize_into()?;
if actions.is_empty() {
return Ok(None);
}
let flags: orchard::Flags = (&mut reader).zcash_deserialize_into()?;
let value_balance: amount::Amount = (&mut reader).zcash_deserialize_into()?;
let shared_anchor: orchard::tree::Root = (&mut reader).zcash_deserialize_into()?;
let proof: Halo2Proof = (&mut reader).zcash_deserialize_into()?;
let sigs: Vec<Signature<SpendAuth>> =
zcash_deserialize_external_count(actions.len(), &mut reader)?;
let binding_sig: Signature<Binding> = (&mut reader).zcash_deserialize_into()?;
let authorized_actions: Vec<orchard::AuthorizedAction> = actions
.into_iter()
.zip(sigs)
.map(|(action, spend_auth_sig)| {
orchard::AuthorizedAction::from_parts(action, spend_auth_sig)
})
.collect();
let actions: AtLeastOne<orchard::AuthorizedAction> = authorized_actions.try_into()?;
Ok(Some(orchard::ShieldedData {
flags,
value_balance,
shared_anchor,
proof,
actions,
binding_sig,
}))
}
}
impl<T: reddsa::SigType> ZcashSerialize for reddsa::Signature<T> {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
writer.write_all(&<[u8; 64]>::from(*self)[..])?;
Ok(())
}
}
impl<T: reddsa::SigType> ZcashDeserialize for reddsa::Signature<T> {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(reader.read_64_bytes()?.into())
}
}
impl ZcashSerialize for Transaction {
#[allow(clippy::unwrap_in_result)]
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
let overwintered_flag = if self.is_overwintered() { 1 << 31 } else { 0 };
let version = overwintered_flag | self.version();
writer.write_u32::<LittleEndian>(version)?;
match self {
Transaction::V1 {
inputs,
outputs,
lock_time,
} => {
inputs.zcash_serialize(&mut writer)?;
outputs.zcash_serialize(&mut writer)?;
lock_time.zcash_serialize(&mut writer)?;
}
Transaction::V2 {
inputs,
outputs,
lock_time,
joinsplit_data,
} => {
inputs.zcash_serialize(&mut writer)?;
outputs.zcash_serialize(&mut writer)?;
lock_time.zcash_serialize(&mut writer)?;
match joinsplit_data {
None => zcash_serialize_empty_list(writer)?,
Some(jsd) => jsd.zcash_serialize(&mut writer)?,
}
}
Transaction::V3 {
inputs,
outputs,
lock_time,
expiry_height,
joinsplit_data,
} => {
writer.write_u32::<LittleEndian>(OVERWINTER_VERSION_GROUP_ID)?;
inputs.zcash_serialize(&mut writer)?;
outputs.zcash_serialize(&mut writer)?;
lock_time.zcash_serialize(&mut writer)?;
writer.write_u32::<LittleEndian>(expiry_height.0)?;
match joinsplit_data {
None => zcash_serialize_empty_list(writer)?,
Some(jsd) => jsd.zcash_serialize(&mut writer)?,
}
}
Transaction::V4 {
inputs,
outputs,
lock_time,
expiry_height,
sapling_shielded_data,
joinsplit_data,
} => {
writer.write_u32::<LittleEndian>(SAPLING_VERSION_GROUP_ID)?;
inputs.zcash_serialize(&mut writer)?;
outputs.zcash_serialize(&mut writer)?;
lock_time.zcash_serialize(&mut writer)?;
writer.write_u32::<LittleEndian>(expiry_height.0)?;
match sapling_shielded_data {
None => {
writer.write_i64::<LittleEndian>(0)?;
zcash_serialize_empty_list(&mut writer)?;
zcash_serialize_empty_list(&mut writer)?;
}
Some(sapling_shielded_data) => {
sapling_shielded_data
.value_balance
.zcash_serialize(&mut writer)?;
let spends: Vec<_> = sapling_shielded_data.spends().cloned().collect();
spends.zcash_serialize(&mut writer)?;
let outputs: Vec<_> = sapling_shielded_data
.outputs()
.cloned()
.map(sapling::OutputInTransactionV4)
.collect();
outputs.zcash_serialize(&mut writer)?;
}
}
match joinsplit_data {
None => zcash_serialize_empty_list(&mut writer)?,
Some(jsd) => jsd.zcash_serialize(&mut writer)?,
}
if let Some(shielded_data) = sapling_shielded_data {
writer.write_all(&<[u8; 64]>::from(shielded_data.binding_sig)[..])?;
}
}
Transaction::V5 {
network_upgrade,
lock_time,
expiry_height,
inputs,
outputs,
sapling_shielded_data,
orchard_shielded_data,
} => {
writer.write_u32::<LittleEndian>(TX_V5_VERSION_GROUP_ID)?;
writer.write_u32::<LittleEndian>(u32::from(
network_upgrade
.branch_id()
.expect("valid transactions must have a network upgrade with a branch id"),
))?;
lock_time.zcash_serialize(&mut writer)?;
writer.write_u32::<LittleEndian>(expiry_height.0)?;
inputs.zcash_serialize(&mut writer)?;
outputs.zcash_serialize(&mut writer)?;
sapling_shielded_data.zcash_serialize(&mut writer)?;
orchard_shielded_data.zcash_serialize(&mut writer)?;
}
#[cfg(all(zcash_unstable = "nu7", feature = "tx_v6"))]
Transaction::V6 {
network_upgrade,
lock_time,
expiry_height,
zip233_amount,
inputs,
outputs,
sapling_shielded_data,
orchard_shielded_data,
} => {
writer.write_u32::<LittleEndian>(TX_V6_VERSION_GROUP_ID)?;
writer.write_u32::<LittleEndian>(u32::from(
network_upgrade
.branch_id()
.expect("valid transactions must have a network upgrade with a branch id"),
))?;
lock_time.zcash_serialize(&mut writer)?;
writer.write_u32::<LittleEndian>(expiry_height.0)?;
zip233_amount.zcash_serialize(&mut writer)?;
inputs.zcash_serialize(&mut writer)?;
outputs.zcash_serialize(&mut writer)?;
sapling_shielded_data.zcash_serialize(&mut writer)?;
orchard_shielded_data.zcash_serialize(&mut writer)?;
}
}
Ok(())
}
}
impl ZcashDeserialize for Transaction {
#[allow(clippy::unwrap_in_result)]
fn zcash_deserialize<R: io::Read>(reader: R) -> Result<Self, SerializationError> {
let mut limited_reader = reader.take(MAX_BLOCK_BYTES);
let (version, overwintered) = {
const LOW_31_BITS: u32 = (1 << 31) - 1;
let header = limited_reader.read_u32::<LittleEndian>()?;
(header & LOW_31_BITS, header >> 31 != 0)
};
match (version, overwintered) {
(1, false) => Ok(Transaction::V1 {
inputs: Vec::zcash_deserialize(&mut limited_reader)?,
outputs: Vec::zcash_deserialize(&mut limited_reader)?,
lock_time: LockTime::zcash_deserialize(&mut limited_reader)?,
}),
(2, false) => {
type OptV2Jsd = Option<JoinSplitData<Bctv14Proof>>;
Ok(Transaction::V2 {
inputs: Vec::zcash_deserialize(&mut limited_reader)?,
outputs: Vec::zcash_deserialize(&mut limited_reader)?,
lock_time: LockTime::zcash_deserialize(&mut limited_reader)?,
joinsplit_data: OptV2Jsd::zcash_deserialize(&mut limited_reader)?,
})
}
(3, true) => {
let id = limited_reader.read_u32::<LittleEndian>()?;
if id != OVERWINTER_VERSION_GROUP_ID {
return Err(SerializationError::Parse(
"expected OVERWINTER_VERSION_GROUP_ID",
));
}
type OptV3Jsd = Option<JoinSplitData<Bctv14Proof>>;
Ok(Transaction::V3 {
inputs: Vec::zcash_deserialize(&mut limited_reader)?,
outputs: Vec::zcash_deserialize(&mut limited_reader)?,
lock_time: LockTime::zcash_deserialize(&mut limited_reader)?,
expiry_height: block::Height(limited_reader.read_u32::<LittleEndian>()?),
joinsplit_data: OptV3Jsd::zcash_deserialize(&mut limited_reader)?,
})
}
(4, true) => {
let id = limited_reader.read_u32::<LittleEndian>()?;
if id != SAPLING_VERSION_GROUP_ID {
return Err(SerializationError::Parse(
"expected SAPLING_VERSION_GROUP_ID",
));
}
type OptV4Jsd = Option<JoinSplitData<Groth16Proof>>;
let inputs: Vec<transparent::Input> = Vec::zcash_deserialize(&mut limited_reader)?;
let outputs = Vec::zcash_deserialize(&mut limited_reader)?;
let is_coinbase = inputs.len() == 1
&& matches!(inputs.first(), Some(transparent::Input::Coinbase { .. }));
let lock_time = LockTime::zcash_deserialize(&mut limited_reader)?;
let expiry_height = block::Height(limited_reader.read_u32::<LittleEndian>()?);
let value_balance = (&mut limited_reader).zcash_deserialize_into()?;
let spend_count: CompactSizeMessage =
(&mut limited_reader).zcash_deserialize_into()?;
let spend_count: usize = spend_count.into();
if is_coinbase && spend_count > 0 {
return Err(SerializationError::Parse(
"coinbase transaction must not have Sapling spends",
));
}
let shielded_spends: Vec<sapling::Spend<sapling::PerSpendAnchor>> =
zcash_deserialize_external_count(spend_count, &mut limited_reader)?;
let shielded_outputs =
Vec::<sapling::OutputInTransactionV4>::zcash_deserialize(&mut limited_reader)?
.into_iter()
.map(sapling::Output::from_v4)
.collect();
let joinsplit_data = OptV4Jsd::zcash_deserialize(&mut limited_reader)?;
let sapling_transfers = if !shielded_spends.is_empty() {
Some(sapling::TransferData::SpendsAndMaybeOutputs {
shared_anchor: FieldNotPresent,
spends: shielded_spends.try_into().expect("checked for spends"),
maybe_outputs: shielded_outputs,
})
} else if !shielded_outputs.is_empty() {
Some(sapling::TransferData::JustOutputs {
outputs: shielded_outputs.try_into().expect("checked for outputs"),
})
} else {
if value_balance != 0 {
return Err(SerializationError::BadTransactionBalance);
}
None
};
let sapling_shielded_data = match sapling_transfers {
Some(transfers) => Some(sapling::ShieldedData {
value_balance,
transfers,
binding_sig: limited_reader.read_64_bytes()?.into(),
}),
None => None,
};
Ok(Transaction::V4 {
inputs,
outputs,
lock_time,
expiry_height,
sapling_shielded_data,
joinsplit_data,
})
}
(5, true) => {
let id = limited_reader.read_u32::<LittleEndian>()?;
if id != TX_V5_VERSION_GROUP_ID {
return Err(SerializationError::Parse("expected TX_V5_VERSION_GROUP_ID"));
}
let network_upgrade =
NetworkUpgrade::try_from(limited_reader.read_u32::<LittleEndian>()?)?;
if network_upgrade < NetworkUpgrade::Nu5 {
return Err(SerializationError::Parse(
"v5 transaction must have NU5 or later consensus branch ID",
));
}
let lock_time = LockTime::zcash_deserialize(&mut limited_reader)?;
let expiry_height = block::Height(limited_reader.read_u32::<LittleEndian>()?);
let inputs: Vec<transparent::Input> = Vec::zcash_deserialize(&mut limited_reader)?;
let outputs = Vec::zcash_deserialize(&mut limited_reader)?;
let is_coinbase = inputs.len() == 1
&& matches!(inputs.first(), Some(transparent::Input::Coinbase { .. }));
let sapling_shielded_data =
deserialize_v5_sapling_shielded_data(&mut limited_reader, is_coinbase)?;
let orchard_shielded_data = (&mut limited_reader).zcash_deserialize_into()?;
let tx = Transaction::V5 {
network_upgrade,
lock_time,
expiry_height,
inputs,
outputs,
sapling_shielded_data,
orchard_shielded_data,
};
tx.to_librustzcash(network_upgrade)?;
Ok(tx)
}
#[cfg(all(zcash_unstable = "nu7", feature = "tx_v6"))]
(6, true) => {
let id = limited_reader.read_u32::<LittleEndian>()?;
if id != TX_V6_VERSION_GROUP_ID {
return Err(SerializationError::Parse("expected TX_V6_VERSION_GROUP_ID"));
}
let network_upgrade =
NetworkUpgrade::try_from(limited_reader.read_u32::<LittleEndian>()?)?;
if network_upgrade < NetworkUpgrade::Nu5 {
return Err(SerializationError::Parse(
"v6 transaction must have NU5 or later consensus branch ID",
));
}
let lock_time = LockTime::zcash_deserialize(&mut limited_reader)?;
let expiry_height = block::Height(limited_reader.read_u32::<LittleEndian>()?);
let zip233_amount = (&mut limited_reader).zcash_deserialize_into()?;
let inputs: Vec<transparent::Input> = Vec::zcash_deserialize(&mut limited_reader)?;
let outputs = Vec::zcash_deserialize(&mut limited_reader)?;
let is_coinbase = inputs.len() == 1
&& matches!(inputs.first(), Some(transparent::Input::Coinbase { .. }));
let sapling_shielded_data =
deserialize_v5_sapling_shielded_data(&mut limited_reader, is_coinbase)?;
let orchard_shielded_data = (&mut limited_reader).zcash_deserialize_into()?;
Ok(Transaction::V6 {
network_upgrade,
lock_time,
expiry_height,
zip233_amount,
inputs,
outputs,
sapling_shielded_data,
orchard_shielded_data,
})
}
(_, _) => Err(SerializationError::Parse("bad tx header")),
}
}
}
impl<T> ZcashDeserialize for Arc<T>
where
T: ZcashDeserialize,
{
fn zcash_deserialize<R: io::Read>(reader: R) -> Result<Self, SerializationError> {
Ok(Arc::new(T::zcash_deserialize(reader)?))
}
}
impl<T> ZcashSerialize for Arc<T>
where
T: ZcashSerialize,
{
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
T::zcash_serialize(self, writer)
}
}
pub(crate) const MIN_TRANSPARENT_INPUT_SIZE: u64 = 32 + 4 + 4 + 1;
pub(crate) const MIN_TRANSPARENT_OUTPUT_SIZE: u64 = 8 + 1;
pub const MIN_TRANSPARENT_TX_SIZE: u64 =
MIN_TRANSPARENT_INPUT_SIZE + 4 + MIN_TRANSPARENT_OUTPUT_SIZE;
pub const MIN_TRANSPARENT_TX_V4_SIZE: u64 = MIN_TRANSPARENT_TX_SIZE + 4;
pub const MIN_TRANSPARENT_TX_V5_SIZE: u64 = MIN_TRANSPARENT_TX_SIZE + 4 + 4;
impl TrustedPreallocate for Transaction {
fn max_allocation() -> u64 {
MAX_BLOCK_BYTES / MIN_TRANSPARENT_TX_SIZE
}
}
impl TrustedPreallocate for transparent::Input {
fn max_allocation() -> u64 {
MAX_BLOCK_BYTES / MIN_TRANSPARENT_INPUT_SIZE
}
}
impl TrustedPreallocate for transparent::Output {
fn max_allocation() -> u64 {
MAX_BLOCK_BYTES / MIN_TRANSPARENT_OUTPUT_SIZE
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct SerializedTransaction {
bytes: Vec<u8>,
}
impl fmt::Display for SerializedTransaction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&hex::encode(&self.bytes))
}
}
impl fmt::Debug for SerializedTransaction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut data_truncated = hex::encode(&self.bytes);
if data_truncated.len() > 1003 {
let end = data_truncated.len() - 500;
data_truncated.replace_range(500..=end, "...");
}
f.debug_tuple("SerializedTransaction")
.field(&data_truncated)
.finish()
}
}
impl<B: Borrow<Transaction>> From<B> for SerializedTransaction {
fn from(tx: B) -> Self {
SerializedTransaction {
bytes: tx
.borrow()
.zcash_serialize_to_vec()
.expect("Writing to a `Vec` should never fail"),
}
}
}
impl AsRef<[u8]> for SerializedTransaction {
fn as_ref(&self) -> &[u8] {
self.bytes.as_ref()
}
}
impl From<Vec<u8>> for SerializedTransaction {
fn from(bytes: Vec<u8>) -> Self {
Self { bytes }
}
}
impl FromHex for SerializedTransaction {
type Error = <Vec<u8> as FromHex>::Error;
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let bytes = <Vec<u8>>::from_hex(hex)?;
Ok(bytes.into())
}
}