zebra_chain/transaction/
sighash.rs1use std::sync::Arc;
4
5use zcash_protocol::value::ZatBalance;
6use zcash_transparent::sighash::SighashType;
7
8use super::Transaction;
9
10use crate::parameters::NetworkUpgrade;
11use crate::{transparent, Error};
12
13use crate::primitives::zcash_primitives::{sighash, PrecomputedTxData};
14
15bitflags::bitflags! {
16 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
18 pub struct HashType: u32 {
19 const ALL = 0b0000_0001;
21 const NONE = 0b0000_0010;
23 const SINGLE = Self::ALL.bits() | Self::NONE.bits();
25 const ANYONECANPAY = 0b1000_0000;
27
28 const ALL_ANYONECANPAY = Self::ALL.bits() | Self::ANYONECANPAY.bits();
30 const NONE_ANYONECANPAY = Self::NONE.bits() | Self::ANYONECANPAY.bits();
32 const SINGLE_ANYONECANPAY = Self::SINGLE.bits() | Self::ANYONECANPAY.bits();
34 }
35}
36
37impl TryFrom<HashType> for SighashType {
38 type Error = ();
39
40 fn try_from(hash_type: HashType) -> Result<Self, Self::Error> {
41 Ok(match hash_type {
42 HashType::ALL => Self::ALL,
43 HashType::NONE => Self::NONE,
44 HashType::SINGLE => Self::SINGLE,
45 HashType::ALL_ANYONECANPAY => Self::ALL_ANYONECANPAY,
46 HashType::NONE_ANYONECANPAY => Self::NONE_ANYONECANPAY,
47 HashType::SINGLE_ANYONECANPAY => Self::SINGLE_ANYONECANPAY,
48 _other => return Err(()),
49 })
50 }
51}
52
53#[derive(Copy, Clone, Eq, PartialEq, Debug)]
56pub struct SigHash(pub [u8; 32]);
57
58impl AsRef<[u8; 32]> for SigHash {
59 fn as_ref(&self) -> &[u8; 32] {
60 &self.0
61 }
62}
63
64impl AsRef<[u8]> for SigHash {
65 fn as_ref(&self) -> &[u8] {
66 &self.0
67 }
68}
69
70impl From<SigHash> for [u8; 32] {
71 fn from(sighash: SigHash) -> Self {
72 sighash.0
73 }
74}
75
76#[derive(Debug)]
79pub struct SigHasher {
80 precomputed_tx_data: PrecomputedTxData,
81}
82
83impl SigHasher {
84 pub fn new(
94 trans: &Transaction,
95 nu: NetworkUpgrade,
96 all_previous_outputs: Arc<Vec<transparent::Output>>,
97 ) -> Result<Self, Error> {
98 Ok(SigHasher {
99 precomputed_tx_data: PrecomputedTxData::new(trans, nu, all_previous_outputs)?,
100 })
101 }
102
103 pub fn sighash(
116 &self,
117 hash_type: HashType,
118 input_index_script_code: Option<(usize, Vec<u8>)>,
119 ) -> SigHash {
120 sighash(
121 &self.precomputed_tx_data,
122 hash_type,
123 input_index_script_code,
124 )
125 }
126
127 pub fn orchard_bundle(
129 &self,
130 ) -> Option<::orchard::bundle::Bundle<::orchard::bundle::Authorized, ZatBalance>> {
131 self.precomputed_tx_data.orchard_bundle()
132 }
133
134 pub fn sapling_bundle(
136 &self,
137 ) -> Option<sapling_crypto::Bundle<sapling_crypto::bundle::Authorized, ZatBalance>> {
138 self.precomputed_tx_data.sapling_bundle()
139 }
140}