Skip to main content

chia_sdk_driver/layers/mips/
index_wrapper_layer.rs

1use chia_sdk_types::puzzles::{INDEX_WRAPPER_HASH, IndexWrapperArgs};
2use clvm_traits::{FromClvm, ToClvm};
3use clvmr::{Allocator, NodePtr};
4
5use crate::{DriverError, Layer, Puzzle, SpendContext};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct IndexWrapperLayer<N, I> {
9    pub nonce: N,
10    pub inner_puzzle: I,
11}
12
13impl<N, I> IndexWrapperLayer<N, I> {
14    pub fn new(nonce: N, inner_puzzle: I) -> Self {
15        Self {
16            nonce,
17            inner_puzzle,
18        }
19    }
20}
21
22impl<N, I> Layer for IndexWrapperLayer<N, I>
23where
24    N: ToClvm<Allocator> + FromClvm<Allocator> + Clone,
25    I: Layer,
26{
27    type Solution = I::Solution;
28
29    fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
30        let inner_puzzle = self.inner_puzzle.construct_puzzle(ctx)?;
31        ctx.curry(IndexWrapperArgs::new(self.nonce.clone(), inner_puzzle))
32    }
33
34    fn construct_solution(
35        &self,
36        ctx: &mut SpendContext,
37        solution: Self::Solution,
38    ) -> Result<NodePtr, DriverError> {
39        self.inner_puzzle.construct_solution(ctx, solution)
40    }
41
42    fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError>
43    where
44        Self: Sized,
45    {
46        let Some(curried) = puzzle.as_curried() else {
47            return Ok(None);
48        };
49
50        if curried.mod_hash != INDEX_WRAPPER_HASH {
51            return Ok(None);
52        }
53
54        let args = IndexWrapperArgs::<N, Puzzle>::from_clvm(allocator, curried.args)?;
55
56        let Some(inner_puzzle) = I::parse_puzzle(allocator, args.inner_puzzle)? else {
57            return Ok(None);
58        };
59
60        Ok(Some(Self::new(args.nonce, inner_puzzle)))
61    }
62
63    fn parse_solution(
64        allocator: &Allocator,
65        solution: NodePtr,
66    ) -> Result<Self::Solution, DriverError> {
67        I::parse_solution(allocator, solution)
68    }
69}