zcash_transparent/
sighash.rs

1use 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/// A [ZIP 244] sighash type.
14///
15/// [ZIP 244]: https://zips.z.cash/zip-0244#s-2a-hash-type
16#[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    /// Parses the given `hash_type` using the [ZIP 244] rules.
28    ///
29    /// [ZIP 244]: https://zips.z.cash/zip-0244#s-2a-hash-type
30    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    /// Encodes this `SighashType` using the [ZIP 244] rules.
38    ///
39    /// [ZIP 244]: https://zips.z.cash/zip-0244#s-2a-hash-type
40    pub fn encode(&self) -> u8 {
41        // Correct by construction.
42        self.0
43    }
44}
45
46/// Additional context that is needed to compute signature hashes
47/// for transactions that include transparent inputs or outputs.
48pub trait TransparentAuthorizingContext: Authorization {
49    /// Returns the list of all transparent input amounts, provided
50    /// so that wallets can commit to the transparent input breakdown
51    /// without requiring the full data of the previous transactions
52    /// providing these inputs.
53    fn input_amounts(&self) -> Vec<Zatoshis>;
54    /// Returns the list of all transparent input scriptPubKeys, provided
55    /// so that wallets can commit to the transparent input breakdown
56    /// without requiring the full data of the previous transactions
57    /// providing these inputs.
58    fn input_scriptpubkeys(&self) -> Vec<Script>;
59}
60
61/// A transparent input that is signable because we know its value and `script_pubkey`.
62#[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    /// Constructs a signable input from its parts.
74    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}