use core::convert::Infallible;
use crate::transaction::fees::zip317::P2PKH_STANDARD_INPUT_SIZE;
use transparent::{
address::{Script, TransparentAddress},
bundle::{OutPoint, TxOut},
};
use zcash_protocol::value::Zatoshis;
#[cfg(feature = "transparent-inputs")]
use transparent::builder::TransparentInputInfo;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InputSize {
Known(usize),
Unknown(OutPoint),
}
impl InputSize {
pub const STANDARD_P2PKH: InputSize = InputSize::Known(P2PKH_STANDARD_INPUT_SIZE);
}
pub trait InputView: core::fmt::Debug {
fn outpoint(&self) -> &OutPoint;
fn coin(&self) -> &TxOut;
fn serialized_size(&self) -> InputSize {
match self.coin().script_pubkey.address() {
Some(TransparentAddress::PublicKeyHash(_)) => InputSize::STANDARD_P2PKH,
_ => InputSize::Unknown(self.outpoint().clone()),
}
}
}
#[cfg(feature = "transparent-inputs")]
impl InputView for TransparentInputInfo {
fn outpoint(&self) -> &OutPoint {
self.outpoint()
}
fn coin(&self) -> &TxOut {
self.coin()
}
}
impl InputView for Infallible {
fn outpoint(&self) -> &OutPoint {
unreachable!()
}
fn coin(&self) -> &TxOut {
unreachable!()
}
}
pub trait OutputView: core::fmt::Debug {
fn value(&self) -> Zatoshis;
fn script_pubkey(&self) -> &Script;
fn serialized_size(&self) -> usize {
8 + self.script_pubkey().serialized_size()
}
}
impl OutputView for TxOut {
fn value(&self) -> Zatoshis {
self.value
}
fn script_pubkey(&self) -> &Script {
&self.script_pubkey
}
}