chik_sdk_driver/layers/
p2_delegated_conditions_layer.rs

1use chik_bls::PublicKey;
2use chik_puzzles::P2_DELEGATED_CONDITIONS_HASH;
3use chik_sdk_types::puzzles::{P2DelegatedConditionsArgs, P2DelegatedConditionsSolution};
4use klvm_traits::FromKlvm;
5use klvmr::{Allocator, NodePtr};
6
7use crate::{DriverError, Layer, Puzzle, SpendContext};
8
9/// The p2 delegated conditions [`Layer`] allows a certain key to spend the coin.
10/// To do so, a list of additional conditions is signed and passed in the solution.
11/// Typically, the [`StandardLayer`](crate::StandardLayer) is used instead, since it adds more flexibility.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct P2DelegatedConditionsLayer {
14    /// The public key that has the ability to spend the coin.
15    pub public_key: PublicKey,
16}
17
18impl Layer for P2DelegatedConditionsLayer {
19    type Solution = P2DelegatedConditionsSolution;
20
21    fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
22        ctx.curry(P2DelegatedConditionsArgs::new(self.public_key))
23    }
24
25    fn construct_solution(
26        &self,
27        ctx: &mut SpendContext,
28        solution: Self::Solution,
29    ) -> Result<NodePtr, DriverError> {
30        ctx.alloc(&solution)
31    }
32
33    fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
34        let Some(puzzle) = puzzle.as_curried() else {
35            return Ok(None);
36        };
37
38        if puzzle.mod_hash != P2_DELEGATED_CONDITIONS_HASH.into() {
39            return Ok(None);
40        }
41
42        let args = P2DelegatedConditionsArgs::from_klvm(allocator, puzzle.args)?;
43
44        Ok(Some(Self {
45            public_key: args.public_key,
46        }))
47    }
48
49    fn parse_solution(
50        allocator: &Allocator,
51        solution: NodePtr,
52    ) -> Result<Self::Solution, DriverError> {
53        Ok(P2DelegatedConditionsSolution::from_klvm(
54            allocator, solution,
55        )?)
56    }
57}