2020-08-15 21:55:28 -07:00
|
|
|
//! Transactions and transaction-related structures.
|
2019-09-19 07:45:37 -07:00
|
|
|
|
2020-06-15 15:08:14 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2019-12-05 12:56:58 -08:00
|
|
|
mod hash;
|
|
|
|
mod joinsplit;
|
2020-08-14 23:37:25 -07:00
|
|
|
mod lock_time;
|
2020-08-15 18:50:18 -07:00
|
|
|
mod memo;
|
2019-12-05 12:56:58 -08:00
|
|
|
mod serialize;
|
|
|
|
mod shielded_data;
|
2019-12-04 21:47:50 -08:00
|
|
|
|
2019-12-05 12:56:58 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2019-09-19 07:45:37 -07:00
|
|
|
|
2019-12-05 12:56:58 -08:00
|
|
|
pub use hash::TransactionHash;
|
2020-08-17 02:06:38 -07:00
|
|
|
pub use joinsplit::JoinSplitData;
|
2020-08-14 23:37:25 -07:00
|
|
|
pub use lock_time::LockTime;
|
2020-08-15 18:50:18 -07:00
|
|
|
pub use memo::Memo;
|
2020-08-17 01:48:47 -07:00
|
|
|
pub use shielded_data::ShieldedData;
|
2019-09-19 07:45:37 -07:00
|
|
|
|
2020-08-17 01:24:33 -07:00
|
|
|
use crate::{
|
|
|
|
amount::Amount,
|
|
|
|
block,
|
|
|
|
primitives::{Bctv14Proof, Groth16Proof},
|
|
|
|
transparent,
|
|
|
|
};
|
2019-09-19 07:45:37 -07:00
|
|
|
|
2019-12-05 12:56:58 -08:00
|
|
|
/// A Zcash transaction.
|
2019-09-25 18:25:46 -07:00
|
|
|
///
|
2019-12-05 12:56:58 -08:00
|
|
|
/// A transaction is an encoded data structure that facilitates the transfer of
|
|
|
|
/// value between two public key addresses on the Zcash ecosystem. Everything is
|
|
|
|
/// designed to ensure that transactions can created, propagated on the network,
|
|
|
|
/// validated, and finally added to the global ledger of transactions (the
|
|
|
|
/// blockchain).
|
2019-09-25 18:25:46 -07:00
|
|
|
///
|
2019-12-05 12:56:58 -08:00
|
|
|
/// Zcash has a number of different transaction formats. They are represented
|
|
|
|
/// internally by different enum variants. Because we checkpoint on Sapling
|
|
|
|
/// activation, we do not parse any pre-Sapling transaction types.
|
2020-06-15 15:08:14 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
2020-02-04 22:53:24 -08:00
|
|
|
// XXX consider boxing the Optional fields of V4 txs
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
2019-12-05 12:56:58 -08:00
|
|
|
pub enum Transaction {
|
|
|
|
/// A fully transparent transaction (`version = 1`).
|
|
|
|
V1 {
|
|
|
|
/// The transparent inputs to the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
inputs: Vec<transparent::Input>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The transparent outputs from the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
outputs: Vec<transparent::Output>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The earliest time or block height that this transaction can be added to the
|
|
|
|
/// chain.
|
|
|
|
lock_time: LockTime,
|
|
|
|
},
|
|
|
|
/// A Sprout transaction (`version = 2`).
|
|
|
|
V2 {
|
|
|
|
/// The transparent inputs to the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
inputs: Vec<transparent::Input>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The transparent outputs from the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
outputs: Vec<transparent::Output>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The earliest time or block height that this transaction can be added to the
|
|
|
|
/// chain.
|
|
|
|
lock_time: LockTime,
|
|
|
|
/// The JoinSplit data for this transaction, if any.
|
|
|
|
joinsplit_data: Option<JoinSplitData<Bctv14Proof>>,
|
|
|
|
},
|
|
|
|
/// An Overwinter transaction (`version = 3`).
|
|
|
|
V3 {
|
|
|
|
/// The transparent inputs to the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
inputs: Vec<transparent::Input>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The transparent outputs from the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
outputs: Vec<transparent::Output>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The earliest time or block height that this transaction can be added to the
|
|
|
|
/// chain.
|
|
|
|
lock_time: LockTime,
|
|
|
|
/// The latest block height that this transaction can be added to the chain.
|
2020-08-16 11:42:02 -07:00
|
|
|
expiry_height: block::Height,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The JoinSplit data for this transaction, if any.
|
|
|
|
joinsplit_data: Option<JoinSplitData<Bctv14Proof>>,
|
|
|
|
},
|
|
|
|
/// A Sapling transaction (`version = 4`).
|
|
|
|
V4 {
|
|
|
|
/// The transparent inputs to the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
inputs: Vec<transparent::Input>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The transparent outputs from the transaction.
|
2020-08-17 01:24:33 -07:00
|
|
|
outputs: Vec<transparent::Output>,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The earliest time or block height that this transaction can be added to the
|
|
|
|
/// chain.
|
|
|
|
lock_time: LockTime,
|
|
|
|
/// The latest block height that this transaction can be added to the chain.
|
2020-08-16 11:42:02 -07:00
|
|
|
expiry_height: block::Height,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The net value of Sapling spend transfers minus output transfers.
|
2020-07-01 16:31:30 -07:00
|
|
|
value_balance: Amount,
|
2019-12-05 12:56:58 -08:00
|
|
|
/// The shielded data for this transaction, if any.
|
|
|
|
shielded_data: Option<ShieldedData>,
|
|
|
|
/// The JoinSplit data for this transaction, if any.
|
|
|
|
joinsplit_data: Option<JoinSplitData<Groth16Proof>>,
|
|
|
|
},
|
2019-12-04 21:47:50 -08:00
|
|
|
}
|
2020-02-09 20:55:52 -08:00
|
|
|
|
|
|
|
impl Transaction {
|
2020-08-13 14:04:43 -07:00
|
|
|
/// Access the transparent inputs of this transaction, regardless of version.
|
2020-08-17 01:24:33 -07:00
|
|
|
pub fn inputs(&self) -> &[transparent::Input] {
|
2020-02-09 20:55:52 -08:00
|
|
|
match self {
|
2020-08-13 14:04:43 -07:00
|
|
|
Transaction::V1 { ref inputs, .. } => inputs,
|
|
|
|
Transaction::V2 { ref inputs, .. } => inputs,
|
|
|
|
Transaction::V3 { ref inputs, .. } => inputs,
|
|
|
|
Transaction::V4 { ref inputs, .. } => inputs,
|
2020-02-09 20:55:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 14:04:43 -07:00
|
|
|
/// Access the transparent outputs of this transaction, regardless of version.
|
2020-08-17 01:24:33 -07:00
|
|
|
pub fn outputs(&self) -> &[transparent::Output] {
|
2020-02-09 20:55:52 -08:00
|
|
|
match self {
|
2020-08-13 14:04:43 -07:00
|
|
|
Transaction::V1 { ref outputs, .. } => outputs,
|
|
|
|
Transaction::V2 { ref outputs, .. } => outputs,
|
|
|
|
Transaction::V3 { ref outputs, .. } => outputs,
|
|
|
|
Transaction::V4 { ref outputs, .. } => outputs,
|
2020-02-09 20:55:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get this transaction's lock time.
|
|
|
|
pub fn lock_time(&self) -> LockTime {
|
|
|
|
match self {
|
|
|
|
Transaction::V1 { lock_time, .. } => *lock_time,
|
|
|
|
Transaction::V2 { lock_time, .. } => *lock_time,
|
|
|
|
Transaction::V3 { lock_time, .. } => *lock_time,
|
|
|
|
Transaction::V4 { lock_time, .. } => *lock_time,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get this transaction's expiry height, if any.
|
2020-08-16 11:42:02 -07:00
|
|
|
pub fn expiry_height(&self) -> Option<block::Height> {
|
2020-02-09 20:55:52 -08:00
|
|
|
match self {
|
|
|
|
Transaction::V1 { .. } => None,
|
|
|
|
Transaction::V2 { .. } => None,
|
|
|
|
Transaction::V3 { expiry_height, .. } => Some(*expiry_height),
|
|
|
|
Transaction::V4 { expiry_height, .. } => Some(*expiry_height),
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 16:13:44 -07:00
|
|
|
|
|
|
|
/// Returns `true` if transaction contains any coinbase inputs.
|
|
|
|
pub fn contains_coinbase_input(&self) -> bool {
|
2020-07-14 18:52:23 -07:00
|
|
|
self.inputs()
|
2020-08-13 14:04:43 -07:00
|
|
|
.iter()
|
2020-08-17 01:24:33 -07:00
|
|
|
.any(|input| matches!(input, transparent::Input::Coinbase { .. }))
|
2020-07-09 16:13:44 -07:00
|
|
|
}
|
2020-08-13 14:04:43 -07:00
|
|
|
|
|
|
|
/// Returns `true` if this transaction is a coinbase transaction.
|
|
|
|
pub fn is_coinbase(&self) -> bool {
|
|
|
|
self.inputs().len() == 1
|
|
|
|
&& matches!(
|
|
|
|
self.inputs().get(0),
|
2020-08-17 01:24:33 -07:00
|
|
|
Some(transparent::Input::Coinbase { .. })
|
2020-08-13 14:04:43 -07:00
|
|
|
)
|
|
|
|
}
|
2020-02-09 20:55:52 -08:00
|
|
|
}
|