chia_sdk_driver/primitives/action_layer/
verification_asserter.rs

1use chia_protocol::{Bytes32, Coin};
2use chia_puzzle_types::{singleton::SingletonStruct, LineageProof};
3use chia_sdk_types::{
4    puzzles::{
5        CatalogVerificationInnerPuzzleMakerArgs, CatalogVerificationInnerPuzzleMakerSolution,
6        VerificationAsserterArgs, VerificationAsserterSolution, VerificationLayer1stCurryArgs,
7    },
8    Mod,
9};
10use clvm_utils::{ToTreeHash, TreeHash};
11
12use crate::{DriverError, Spend, SpendContext};
13
14#[derive(Debug, Copy, Clone)]
15#[must_use]
16pub struct VerificationAsserter {
17    pub verifier_singleton_struct_hash: Bytes32,
18    pub verification_inner_puzzle_self_hash: Bytes32,
19    pub version: u32,
20    pub tail_hash_hash: Bytes32,
21    pub data_hash_hash: Bytes32,
22}
23
24impl VerificationAsserter {
25    pub fn new(
26        verifier_singleton_struct_hash: Bytes32,
27        verification_inner_puzzle_self_hash: Bytes32,
28        version: u32,
29        tail_hash_hash: TreeHash,
30        data_hash_hash: TreeHash,
31    ) -> Self {
32        Self {
33            verifier_singleton_struct_hash,
34            verification_inner_puzzle_self_hash,
35            version,
36            tail_hash_hash: tail_hash_hash.into(),
37            data_hash_hash: data_hash_hash.into(),
38        }
39    }
40
41    pub fn from(
42        verifier_launcher_id: Bytes32,
43        version: u32,
44        tail_hash_hash: TreeHash,
45        data_hash_hash: TreeHash,
46    ) -> Self {
47        Self::new(
48            SingletonStruct::new(verifier_launcher_id)
49                .tree_hash()
50                .into(),
51            VerificationLayer1stCurryArgs::curry_tree_hash(verifier_launcher_id).into(),
52            version,
53            tail_hash_hash,
54            data_hash_hash,
55        )
56    }
57
58    pub fn tree_hash(&self) -> TreeHash {
59        VerificationAsserterArgs::new(
60            self.verifier_singleton_struct_hash,
61            CatalogVerificationInnerPuzzleMakerArgs::new(
62                self.verification_inner_puzzle_self_hash,
63                self.version,
64                self.tail_hash_hash.into(),
65                self.data_hash_hash.into(),
66            )
67            .curry_tree_hash(),
68        )
69        .curry_tree_hash()
70    }
71
72    pub fn inner_spend(
73        &self,
74        ctx: &mut SpendContext,
75        verifier_proof: LineageProof,
76        launcher_amount: u64,
77        comment: String,
78    ) -> Result<Spend, DriverError> {
79        let verification_inner_puzzle_maker =
80            ctx.curry(CatalogVerificationInnerPuzzleMakerArgs::new(
81                self.verification_inner_puzzle_self_hash,
82                self.version,
83                self.tail_hash_hash.into(),
84                self.data_hash_hash.into(),
85            ))?;
86
87        let puzzle = ctx.curry(VerificationAsserterArgs::new(
88            self.verifier_singleton_struct_hash,
89            verification_inner_puzzle_maker,
90        ))?;
91
92        let solution = ctx.alloc(&VerificationAsserterSolution {
93            verifier_proof,
94            verification_inner_puzzle_maker_solution: CatalogVerificationInnerPuzzleMakerSolution {
95                comment,
96            },
97            launcher_amount,
98        })?;
99
100        Ok(Spend::new(puzzle, solution))
101    }
102
103    pub fn spend(
104        &self,
105        ctx: &mut SpendContext,
106        coin: Coin,
107        verifier_proof: LineageProof,
108        launcher_amount: u64,
109        comment: String,
110    ) -> Result<(), DriverError> {
111        let spend = self.inner_spend(ctx, verifier_proof, launcher_amount, comment)?;
112
113        ctx.spend(coin, spend)?;
114        Ok(())
115    }
116}