Skip to content

Commit f3ab732

Browse files
committed
Adds conversion impl from HashType for SighashType
1 parent 3e2cb0a commit f3ab732

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

zebra-chain/src/primitives/zcash_primitives.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub(crate) fn sighash(
295295
unlock_script = zcash_primitives::legacy::Script(script_code);
296296
zp_tx::sighash::SignableInput::Transparent(
297297
zcash_transparent::sighash::SignableInput::from_parts(
298-
hash_type.bits() as _,
298+
hash_type.try_into().expect("hash type should be ALL"),
299299
input_index,
300300
&unlock_script,
301301
&lock_script,

zebra-chain/src/transaction/sighash.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Signature hashes for Zcash transactions
22
3+
use zcash_transparent::sighash::SighashType;
4+
35
use super::Transaction;
46

57
use crate::parameters::NetworkUpgrade;
@@ -19,6 +21,29 @@ bitflags::bitflags! {
1921
const SINGLE = Self::ALL.bits() | Self::NONE.bits();
2022
/// Anyone can add inputs to this transaction
2123
const ANYONECANPAY = 0b1000_0000;
24+
25+
/// Sign all the outputs and Anyone can add inputs to this transaction
26+
const ALL_ANYONECANPAY = Self::ALL.bits() | Self::ANYONECANPAY.bits();
27+
/// Sign none of the outputs and Anyone can add inputs to this transaction
28+
const NONE_ANYONECANPAY = Self::NONE.bits() | Self::ANYONECANPAY.bits();
29+
/// Sign one of the outputs and Anyone can add inputs to this transaction
30+
const SINGLE_ANYONECANPAY = Self::SINGLE.bits() | Self::ANYONECANPAY.bits();
31+
}
32+
}
33+
34+
impl TryFrom<HashType> for SighashType {
35+
type Error = ();
36+
37+
fn try_from(hash_type: HashType) -> Result<Self, Self::Error> {
38+
Ok(match hash_type {
39+
HashType::ALL => Self::ALL,
40+
HashType::NONE => Self::NONE,
41+
HashType::SINGLE => Self::SINGLE,
42+
HashType::ALL_ANYONECANPAY => Self::ALL_ANYONECANPAY,
43+
HashType::NONE_ANYONECANPAY => Self::NONE_ANYONECANPAY,
44+
HashType::SINGLE_ANYONECANPAY => Self::SINGLE_ANYONECANPAY,
45+
_other => return Err(()),
46+
})
2247
}
2348
}
2449

0 commit comments

Comments
 (0)