simplicity/policy/
sighash.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: CC0-1.0

use std::borrow::Borrow;
use std::ops::Deref;

use hashes::sha256;

use crate::jet::elements::ElementsUtxo;
use crate::Cmr;

pub struct SighashCache<T: Deref<Target = elements::Transaction>> {
    tx: T,
    fallback: elements::sighash::SighashCache<T>,
}

impl<T: Deref<Target = elements::Transaction> + Clone> SighashCache<T> {
    pub fn new(tx: T) -> Self {
        Self {
            tx: tx.clone(),
            fallback: elements::sighash::SighashCache::new(tx),
        }
    }

    pub fn taproot_key_spend_signature_hash<O>(
        &mut self,
        input_index: usize,
        prevouts: &elements::sighash::Prevouts<O>,
        sighash_type: elements::sighash::SchnorrSighashType,
        genesis_hash: elements::BlockHash,
    ) -> Result<elements::taproot::TapSighashHash, elements::sighash::Error>
    where
        O: Borrow<elements::TxOut>,
    {
        self.fallback.taproot_key_spend_signature_hash(
            input_index,
            prevouts,
            sighash_type,
            genesis_hash,
        )
    }

    pub fn taproot_script_spend_signature_hash<S, O>(
        &mut self,
        input_index: usize,
        prevouts: &elements::sighash::Prevouts<O>,
        leaf_hash: S,
        sighash_type: elements::sighash::SchnorrSighashType,
        genesis_hash: elements::BlockHash,
    ) -> Result<elements::taproot::TapSighashHash, elements::sighash::Error>
    where
        S: Into<elements::taproot::TapLeafHash>,
        O: Borrow<elements::TxOut>,
    {
        self.fallback.taproot_script_spend_signature_hash(
            input_index,
            prevouts,
            leaf_hash,
            sighash_type,
            genesis_hash,
        )
    }

    pub fn simplicity_spend_signature_hash<O>(
        &mut self,
        input_index: usize,
        prevouts: &elements::sighash::Prevouts<O>,
        script_cmr: Cmr,
        control_block: elements::taproot::ControlBlock,
        genesis_hash: elements::BlockHash,
    ) -> Result<sha256::Hash, elements::sighash::Error>
    where
        O: Borrow<elements::TxOut>,
    {
        let all = match prevouts {
            elements::sighash::Prevouts::All(prevouts) => *prevouts,
            _ => return Err(elements::sighash::Error::PrevoutKind),
        };
        let utxos: Vec<_> = all
            .iter()
            .map(|o| ElementsUtxo::from(O::borrow(o).clone()))
            .collect();

        let simplicity_env = crate::jet::elements::ElementsEnv::new(
            self.tx.clone(),
            utxos,
            input_index as u32,
            script_cmr,
            control_block,
            None,
            genesis_hash,
        );
        let simplicity_sighash = simplicity_env.c_tx_env().sighash_all();

        Ok(simplicity_sighash)
    }
}