chia_sdk_driver/layers/
p2_one_of_many_layer.rs1use chia_protocol::Bytes32;
2use chia_puzzles::P2_1_OF_N_HASH;
3use chia_sdk_types::puzzles::{P2OneOfManyArgs, P2OneOfManySolution};
4use clvm_traits::FromClvm;
5use clvm_utils::{CurriedProgram, ToTreeHash, TreeHash};
6use clvmr::{Allocator, NodePtr};
7
8use crate::{DriverError, Layer, Puzzle, SpendContext};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct P2OneOfManyLayer {
13 pub merkle_root: Bytes32,
15}
16
17impl P2OneOfManyLayer {
18 pub fn new(merkle_root: Bytes32) -> Self {
19 Self { merkle_root }
20 }
21}
22
23impl Layer for P2OneOfManyLayer {
24 type Solution = P2OneOfManySolution<NodePtr, NodePtr>;
25
26 fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
27 let Some(puzzle) = puzzle.as_curried() else {
28 return Ok(None);
29 };
30
31 if puzzle.mod_hash != P2_1_OF_N_HASH.into() {
32 return Ok(None);
33 }
34
35 let args = P2OneOfManyArgs::from_clvm(allocator, puzzle.args)?;
36
37 Ok(Some(Self {
38 merkle_root: args.merkle_root,
39 }))
40 }
41
42 fn parse_solution(
43 allocator: &Allocator,
44 solution: NodePtr,
45 ) -> Result<Self::Solution, DriverError> {
46 Ok(P2OneOfManySolution::<NodePtr, NodePtr>::from_clvm(
47 allocator, solution,
48 )?)
49 }
50
51 fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
52 ctx.curry(P2OneOfManyArgs {
53 merkle_root: self.merkle_root,
54 })
55 }
56
57 fn construct_solution(
58 &self,
59 ctx: &mut SpendContext,
60 solution: Self::Solution,
61 ) -> Result<NodePtr, DriverError> {
62 ctx.alloc(&solution)
63 }
64}
65
66impl ToTreeHash for P2OneOfManyLayer {
67 fn tree_hash(&self) -> TreeHash {
68 CurriedProgram {
69 program: TreeHash::new(P2_1_OF_N_HASH),
70 args: P2OneOfManyArgs::new(self.merkle_root),
71 }
72 .tree_hash()
73 }
74}