zcash_transparent/
sighash.rs1use alloc::vec::Vec;
2use getset::Getters;
3use zcash_protocol::value::Zatoshis;
4
5use crate::{address::Script, bundle::Authorization};
6
7pub const SIGHASH_ALL: u8 = 0x01;
8pub const SIGHASH_NONE: u8 = 0x02;
9pub const SIGHASH_SINGLE: u8 = 0x03;
10pub const SIGHASH_MASK: u8 = 0x1f;
11pub const SIGHASH_ANYONECANPAY: u8 = 0x80;
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct SighashType(u8);
18
19impl SighashType {
20 pub const ALL: Self = Self(SIGHASH_ALL);
21 pub const NONE: Self = Self(SIGHASH_NONE);
22 pub const SINGLE: Self = Self(SIGHASH_SINGLE);
23 pub const ALL_ANYONECANPAY: Self = Self(SIGHASH_ALL | SIGHASH_ANYONECANPAY);
24 pub const NONE_ANYONECANPAY: Self = Self(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
25 pub const SINGLE_ANYONECANPAY: Self = Self(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY);
26
27 pub fn parse(hash_type: u8) -> Option<Self> {
31 match hash_type & !SIGHASH_ANYONECANPAY {
32 SIGHASH_ALL | SIGHASH_NONE | SIGHASH_SINGLE => Some(Self(hash_type)),
33 _ => None,
34 }
35 }
36
37 pub fn encode(&self) -> u8 {
41 self.0
43 }
44}
45
46pub trait TransparentAuthorizingContext: Authorization {
49 fn input_amounts(&self) -> Vec<Zatoshis>;
54 fn input_scriptpubkeys(&self) -> Vec<Script>;
59}
60
61#[derive(Debug, Getters)]
63#[getset(get = "pub")]
64pub struct SignableInput<'a> {
65 pub(crate) hash_type: SighashType,
66 pub(crate) index: usize,
67 pub(crate) script_code: &'a Script,
68 pub(crate) script_pubkey: &'a Script,
69 pub(crate) value: Zatoshis,
70}
71
72impl<'a> SignableInput<'a> {
73 pub fn from_parts(
75 hash_type: SighashType,
76 index: usize,
77 script_code: &'a Script,
78 script_pubkey: &'a Script,
79 value: Zatoshis,
80 ) -> Self {
81 Self {
82 hash_type,
83 index,
84 script_code,
85 script_pubkey,
86 value,
87 }
88 }
89}