zebra_chain/transaction/
sighash.rs1use std::sync::Arc;
4
5use zcash_transparent::sighash::SighashType;
6
7use super::Transaction;
8
9use crate::parameters::NetworkUpgrade;
10use crate::{transparent, Error};
11
12use crate::primitives::zcash_primitives::{sighash, PrecomputedTxData};
13
14bitflags::bitflags! {
15 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
17 pub struct HashType: u32 {
18 const ALL = 0b0000_0001;
20 const NONE = 0b0000_0010;
22 const SINGLE = Self::ALL.bits() | Self::NONE.bits();
24 const ANYONECANPAY = 0b1000_0000;
26
27 const ALL_ANYONECANPAY = Self::ALL.bits() | Self::ANYONECANPAY.bits();
29 const NONE_ANYONECANPAY = Self::NONE.bits() | Self::ANYONECANPAY.bits();
31 const SINGLE_ANYONECANPAY = Self::SINGLE.bits() | Self::ANYONECANPAY.bits();
33 }
34}
35
36impl TryFrom<HashType> for SighashType {
37 type Error = ();
38
39 fn try_from(hash_type: HashType) -> Result<Self, Self::Error> {
40 Ok(match hash_type {
41 HashType::ALL => Self::ALL,
42 HashType::NONE => Self::NONE,
43 HashType::SINGLE => Self::SINGLE,
44 HashType::ALL_ANYONECANPAY => Self::ALL_ANYONECANPAY,
45 HashType::NONE_ANYONECANPAY => Self::NONE_ANYONECANPAY,
46 HashType::SINGLE_ANYONECANPAY => Self::SINGLE_ANYONECANPAY,
47 _other => return Err(()),
48 })
49 }
50}
51
52#[derive(Copy, Clone, Eq, PartialEq, Debug)]
55pub struct SigHash(pub [u8; 32]);
56
57impl AsRef<[u8; 32]> for SigHash {
58 fn as_ref(&self) -> &[u8; 32] {
59 &self.0
60 }
61}
62
63impl AsRef<[u8]> for SigHash {
64 fn as_ref(&self) -> &[u8] {
65 &self.0
66 }
67}
68
69#[derive(Debug)]
72pub struct SigHasher {
73 precomputed_tx_data: PrecomputedTxData,
74}
75
76impl SigHasher {
77 pub fn new(
87 trans: &Transaction,
88 nu: NetworkUpgrade,
89 all_previous_outputs: Arc<Vec<transparent::Output>>,
90 ) -> Result<Self, Error> {
91 Ok(SigHasher {
92 precomputed_tx_data: PrecomputedTxData::new(trans, nu, all_previous_outputs)?,
93 })
94 }
95
96 pub fn sighash(
109 &self,
110 hash_type: HashType,
111 input_index_script_code: Option<(usize, Vec<u8>)>,
112 ) -> SigHash {
113 sighash(
114 &self.precomputed_tx_data,
115 hash_type,
116 input_index_script_code,
117 )
118 }
119}