chik_sdk_bindings/mips/
restrictions.rs1use binky::Result;
2use chik_protocol::Bytes32;
3use chik_sdk_driver as sdk;
4use chik_sdk_types::{
5 puzzles::{
6 Force1of2RestrictedVariable, PreventConditionOpcode, Timelock,
7 PREVENT_MULTIPLE_CREATE_COINS_PUZZLE_HASH,
8 },
9 Mod,
10};
11use klvm_utils::TreeHash;
12
13#[derive(Clone)]
14pub struct Restriction {
15 pub kind: RestrictionKind,
16 pub puzzle_hash: TreeHash,
17}
18
19impl From<Restriction> for sdk::Restriction {
20 fn from(value: Restriction) -> Self {
21 sdk::Restriction {
22 kind: value.kind.into(),
23 puzzle_hash: value.puzzle_hash,
24 }
25 }
26}
27
28#[derive(Clone)]
29pub enum RestrictionKind {
30 MemberCondition,
31 DelegatedPuzzleHash,
32 DelegatedPuzzleWrapper,
33}
34
35impl From<RestrictionKind> for sdk::RestrictionKind {
36 fn from(value: RestrictionKind) -> Self {
37 match value {
38 RestrictionKind::MemberCondition => sdk::RestrictionKind::MemberCondition,
39 RestrictionKind::DelegatedPuzzleHash => sdk::RestrictionKind::DelegatedPuzzleHash,
40 RestrictionKind::DelegatedPuzzleWrapper => sdk::RestrictionKind::DelegatedPuzzleWrapper,
41 }
42 }
43}
44
45pub(crate) fn convert_restrictions(restrictions: Vec<Restriction>) -> Vec<sdk::Restriction> {
46 restrictions.into_iter().map(Into::into).collect()
47}
48
49pub fn timelock_restriction(timelock: u64) -> Result<Restriction> {
50 Ok(Restriction {
51 kind: RestrictionKind::MemberCondition,
52 puzzle_hash: Timelock::new(timelock).curry_tree_hash(),
53 })
54}
55
56pub fn force_1_of_2_restriction(
57 left_side_subtree_hash: Bytes32,
58 nonce: u32,
59 member_validator_list_hash: Bytes32,
60 delegated_puzzle_validator_list_hash: Bytes32,
61) -> Result<Restriction> {
62 Ok(Restriction {
63 kind: RestrictionKind::DelegatedPuzzleWrapper,
64 puzzle_hash: Force1of2RestrictedVariable::new(
65 left_side_subtree_hash,
66 nonce.try_into().unwrap(),
67 member_validator_list_hash,
68 delegated_puzzle_validator_list_hash,
69 )
70 .curry_tree_hash(),
71 })
72}
73
74pub fn prevent_condition_opcode_restriction(condition_opcode: u16) -> Result<Restriction> {
75 Ok(Restriction {
76 kind: RestrictionKind::DelegatedPuzzleWrapper,
77 puzzle_hash: PreventConditionOpcode::new(condition_opcode).curry_tree_hash(),
78 })
79}
80
81pub fn prevent_multiple_create_coins_restriction() -> Result<Restriction> {
82 Ok(Restriction {
83 kind: RestrictionKind::DelegatedPuzzleWrapper,
84 puzzle_hash: PREVENT_MULTIPLE_CREATE_COINS_PUZZLE_HASH,
85 })
86}
87
88pub fn prevent_vault_side_effects_restriction() -> Result<Vec<Restriction>> {
89 Ok(vec![
90 prevent_condition_opcode_restriction(60)?,
91 prevent_condition_opcode_restriction(62)?,
92 prevent_condition_opcode_restriction(66)?,
93 prevent_condition_opcode_restriction(67)?,
94 prevent_multiple_create_coins_restriction()?,
95 ])
96}