1mod parsed_member;
2mod parsed_restriction;
3mod parsed_wrapper;
4
5use chia_consensus::opcodes::{
6 CREATE_COIN_ANNOUNCEMENT, CREATE_PUZZLE_ANNOUNCEMENT, RECEIVE_MESSAGE, SEND_MESSAGE,
7};
8use chia_puzzles::{
9 FORCE_ASSERT_COIN_ANNOUNCEMENT_HASH, FORCE_COIN_MESSAGE_HASH,
10 PREVENT_MULTIPLE_CREATE_COINS_HASH,
11};
12pub use parsed_member::*;
13pub use parsed_restriction::*;
14pub use parsed_wrapper::*;
15
16use chia_bls::PublicKey;
17use chia_protocol::Bytes32;
18use chia_sdk_types::{
19 puzzles::{
20 BlsMember, BlsTaprootMember, EnforceDelegatedPuzzleWrappers, FixedPuzzleMember,
21 Force1of2RestrictedVariable, K1Member, K1MemberPuzzleAssert, PasskeyMember,
22 PasskeyMemberPuzzleAssert, PreventConditionOpcode, R1Member, R1MemberPuzzleAssert,
23 SingletonMember, Timelock,
24 },
25 Mod,
26};
27use chia_secp::{K1PublicKey, R1PublicKey};
28use clvm_traits::{apply_constants, FromClvm, ToClvm};
29use clvm_utils::TreeHash;
30use clvmr::{Allocator, NodePtr};
31
32use crate::DriverError;
33
34#[derive(ToClvm, FromClvm)]
35#[apply_constants]
36#[derive(Debug, Clone, PartialEq, Eq)]
37#[clvm(list)]
38pub struct MipsMemo<T = NodePtr> {
39 #[clvm(constant = "CHIP-0043".to_string())]
40 pub namespace: String,
41 pub inner_puzzle: InnerPuzzleMemo<T>,
42}
43
44impl MipsMemo<NodePtr> {
45 pub fn new(inner_puzzle: InnerPuzzleMemo) -> Self {
46 Self { inner_puzzle }
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
51#[clvm(list)]
52pub struct InnerPuzzleMemo<T = NodePtr> {
53 pub nonce: usize,
54 pub restrictions: Vec<RestrictionMemo<T>>,
55 #[clvm(rest)]
56 pub kind: MemoKind<T>,
57}
58
59impl InnerPuzzleMemo<NodePtr> {
60 pub fn new(nonce: usize, restrictions: Vec<RestrictionMemo>, kind: MemoKind) -> Self {
61 Self {
62 nonce,
63 restrictions,
64 kind,
65 }
66 }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
70#[clvm(list)]
71pub struct RestrictionMemo<T = NodePtr> {
72 pub member_condition_validator: bool,
73 pub puzzle_hash: Bytes32,
74 pub memo: T,
75}
76
77impl RestrictionMemo<NodePtr> {
78 pub fn new(member_condition_validator: bool, puzzle_hash: Bytes32, memo: NodePtr) -> Self {
79 Self {
80 member_condition_validator,
81 puzzle_hash,
82 memo,
83 }
84 }
85
86 pub fn force_1_of_2_restricted_variable(
87 allocator: &mut Allocator,
88 left_side_subtree_hash: Bytes32,
89 nonce: usize,
90 member_validator_list_hash: Bytes32,
91 delegated_puzzle_validator_list_hash: Bytes32,
92 ) -> Result<Self, DriverError> {
93 Ok(Self::new(
94 false,
95 Force1of2RestrictedVariable::new(
96 left_side_subtree_hash,
97 nonce,
98 member_validator_list_hash,
99 delegated_puzzle_validator_list_hash,
100 )
101 .curry_tree_hash()
102 .into(),
103 Force1of2RestrictedVariableMemo::new(
104 left_side_subtree_hash,
105 nonce,
106 member_validator_list_hash,
107 delegated_puzzle_validator_list_hash,
108 )
109 .to_clvm(allocator)?,
110 ))
111 }
112
113 pub fn enforce_delegated_puzzle_wrappers(
114 allocator: &mut Allocator,
115 wrapper_memos: &[WrapperMemo],
116 ) -> Result<Self, DriverError> {
117 let wrapper_stack: Vec<TreeHash> = wrapper_memos
118 .iter()
119 .map(|item| TreeHash::from(item.puzzle_hash))
120 .collect();
121
122 let memos = wrapper_memos
123 .iter()
124 .map(|item| item.memo)
125 .collect::<Vec<NodePtr>>();
126
127 Ok(Self::new(
128 false,
129 EnforceDelegatedPuzzleWrappers::new(&wrapper_stack)
130 .curry_tree_hash()
131 .into(),
132 memos.to_clvm(allocator)?,
133 ))
134 }
135
136 pub fn timelock(
137 allocator: &mut Allocator,
138 seconds: u64,
139 reveal: bool,
140 ) -> Result<Self, DriverError> {
141 Ok(Self::new(
142 true,
143 Timelock::new(seconds).curry_tree_hash().into(),
144 if reveal {
145 seconds.to_clvm(allocator)?
146 } else {
147 NodePtr::NIL
148 },
149 ))
150 }
151
152 pub fn parse(&self, allocator: &Allocator, ctx: &MipsMemoContext) -> Option<ParsedRestriction> {
153 if let Ok(items) = Vec::<WrapperMemo>::from_clvm(allocator, self.memo) {
154 let wrapper_stack: Vec<TreeHash> = items
155 .iter()
156 .map(|item| TreeHash::from(item.puzzle_hash))
157 .collect();
158
159 let restriction = EnforceDelegatedPuzzleWrappers::new(&wrapper_stack);
160
161 if restriction.curry_tree_hash() == self.puzzle_hash.into() {
162 return Some(ParsedRestriction::EnforceDelegatedPuzzleWrappers(
163 restriction,
164 items.iter().map(|item| item.memo).collect(),
165 ));
166 }
167 }
168
169 if let Ok(memo) = Force1of2RestrictedVariableMemo::from_clvm(allocator, self.memo) {
170 let restriction = Force1of2RestrictedVariable::new(
171 memo.left_side_subtree_hash,
172 memo.nonce,
173 memo.member_validator_list_hash,
174 memo.delegated_puzzle_validator_list_hash,
175 );
176
177 if restriction.curry_tree_hash() == self.puzzle_hash.into() {
178 return Some(ParsedRestriction::Force1of2RestrictedVariable(restriction));
179 }
180 }
181
182 if let Ok(seconds) = Option::<u64>::from_clvm(allocator, self.memo) {
183 for &seconds in seconds.iter().chain(ctx.timelocks.iter()) {
184 let restriction = Timelock::new(seconds);
185
186 if restriction.curry_tree_hash() == self.puzzle_hash.into() {
187 return Some(ParsedRestriction::Timelock(restriction));
188 }
189 }
190 }
191
192 None
193 }
194}
195
196#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
197#[clvm(list)]
198pub struct WrapperMemo<T = NodePtr> {
199 pub puzzle_hash: Bytes32,
200 pub memo: T,
201}
202
203impl WrapperMemo<NodePtr> {
204 pub fn new(puzzle_hash: Bytes32, memo: NodePtr) -> Self {
205 Self { puzzle_hash, memo }
206 }
207
208 pub fn force_assert_coin_announcement() -> Self {
209 Self {
210 puzzle_hash: FORCE_ASSERT_COIN_ANNOUNCEMENT_HASH.into(),
211 memo: NodePtr::NIL,
212 }
213 }
214
215 pub fn force_coin_message() -> Self {
216 Self {
217 puzzle_hash: FORCE_COIN_MESSAGE_HASH.into(),
218 memo: NodePtr::NIL,
219 }
220 }
221
222 pub fn prevent_multiple_create_coins() -> Self {
223 Self {
224 puzzle_hash: PREVENT_MULTIPLE_CREATE_COINS_HASH.into(),
225 memo: NodePtr::NIL,
226 }
227 }
228
229 pub fn timelock(
230 allocator: &mut Allocator,
231 seconds: u64,
232 reveal: bool,
233 ) -> Result<Self, DriverError> {
234 Ok(Self {
235 puzzle_hash: Timelock::new(seconds).curry_tree_hash().into(),
236 memo: if reveal {
237 seconds.to_clvm(allocator)?
238 } else {
239 NodePtr::NIL
240 },
241 })
242 }
243
244 pub fn prevent_condition_opcode(
245 allocator: &mut Allocator,
246 opcode: u16,
247 reveal: bool,
248 ) -> Result<Self, DriverError> {
249 Ok(Self {
250 puzzle_hash: PreventConditionOpcode::new(opcode).curry_tree_hash().into(),
251 memo: if reveal {
252 opcode.to_clvm(allocator)?
253 } else {
254 NodePtr::NIL
255 },
256 })
257 }
258
259 pub fn parse(&self, allocator: &Allocator, ctx: &MipsMemoContext) -> Option<ParsedWrapper> {
260 if self.puzzle_hash == FORCE_ASSERT_COIN_ANNOUNCEMENT_HASH.into() {
261 return Some(ParsedWrapper::ForceAssertCoinAnnouncement);
262 }
263
264 if self.puzzle_hash == FORCE_COIN_MESSAGE_HASH.into() {
265 return Some(ParsedWrapper::ForceCoinMessage);
266 }
267
268 if self.puzzle_hash == PREVENT_MULTIPLE_CREATE_COINS_HASH.into() {
269 return Some(ParsedWrapper::PreventMultipleCreateCoins);
270 }
271
272 if let Ok(seconds) = Option::<u64>::from_clvm(allocator, self.memo) {
273 for &seconds in seconds.iter().chain(ctx.timelocks.iter()) {
274 let wrapper = Timelock::new(seconds);
275
276 if wrapper.curry_tree_hash() == self.puzzle_hash.into() {
277 return Some(ParsedWrapper::Timelock(wrapper));
278 }
279 }
280 }
281
282 if let Ok(opcode) = Option::<u16>::from_clvm(allocator, self.memo) {
283 for &opcode in opcode.iter().chain(ctx.opcodes.iter()) {
284 let wrapper = PreventConditionOpcode::new(opcode);
285
286 if wrapper.curry_tree_hash() == self.puzzle_hash.into() {
287 return Some(ParsedWrapper::PreventConditionOpcode(wrapper));
288 }
289 }
290 }
291
292 None
293 }
294}
295
296#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
297#[clvm(list)]
298pub struct Force1of2RestrictedVariableMemo {
299 pub left_side_subtree_hash: Bytes32,
300 pub nonce: usize,
301 pub member_validator_list_hash: Bytes32,
302 pub delegated_puzzle_validator_list_hash: Bytes32,
303}
304
305impl Force1of2RestrictedVariableMemo {
306 pub fn new(
307 left_side_subtree_hash: Bytes32,
308 nonce: usize,
309 member_validator_list_hash: Bytes32,
310 delegated_puzzle_validator_list_hash: Bytes32,
311 ) -> Self {
312 Self {
313 left_side_subtree_hash,
314 nonce,
315 member_validator_list_hash,
316 delegated_puzzle_validator_list_hash,
317 }
318 }
319}
320
321#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
322#[clvm(list)]
323pub enum MemoKind<T = NodePtr> {
324 Member(MemberMemo<T>),
325 MofN(MofNMemo<T>),
326}
327
328#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
329#[clvm(list)]
330pub struct MemberMemo<T = NodePtr> {
331 pub puzzle_hash: Bytes32,
332 pub memo: T,
333}
334
335impl MemberMemo<NodePtr> {
336 pub fn new(puzzle_hash: Bytes32, memo: NodePtr) -> Self {
337 Self { puzzle_hash, memo }
338 }
339
340 pub fn k1(
341 allocator: &mut Allocator,
342 public_key: K1PublicKey,
343 fast_forward: bool,
344 reveal: bool,
345 ) -> Result<Self, DriverError> {
346 Ok(Self::new(
347 if fast_forward {
348 K1MemberPuzzleAssert::new(public_key).curry_tree_hash()
349 } else {
350 K1Member::new(public_key).curry_tree_hash()
351 }
352 .into(),
353 if reveal {
354 public_key.to_clvm(allocator)?
355 } else {
356 NodePtr::NIL
357 },
358 ))
359 }
360
361 pub fn r1(
362 allocator: &mut Allocator,
363 public_key: R1PublicKey,
364 fast_forward: bool,
365 reveal: bool,
366 ) -> Result<Self, DriverError> {
367 Ok(Self::new(
368 if fast_forward {
369 R1MemberPuzzleAssert::new(public_key).curry_tree_hash()
370 } else {
371 R1Member::new(public_key).curry_tree_hash()
372 }
373 .into(),
374 if reveal {
375 public_key.to_clvm(allocator)?
376 } else {
377 NodePtr::NIL
378 },
379 ))
380 }
381
382 pub fn bls(
383 allocator: &mut Allocator,
384 public_key: PublicKey,
385 taproot: bool,
386 reveal: bool,
387 ) -> Result<Self, DriverError> {
388 Ok(Self::new(
389 if taproot {
390 BlsTaprootMember::new(public_key).curry_tree_hash().into()
391 } else {
392 BlsMember::new(public_key).curry_tree_hash().into()
393 },
394 if reveal {
395 public_key.to_clvm(allocator)?
396 } else {
397 NodePtr::NIL
398 },
399 ))
400 }
401
402 pub fn passkey(
403 allocator: &mut Allocator,
404 public_key: R1PublicKey,
405 fast_forward: bool,
406 reveal: bool,
407 ) -> Result<Self, DriverError> {
408 Ok(Self::new(
409 if fast_forward {
410 PasskeyMemberPuzzleAssert::new(public_key).curry_tree_hash()
411 } else {
412 PasskeyMember::new(public_key).curry_tree_hash()
413 }
414 .into(),
415 if reveal {
416 public_key.to_clvm(allocator)?
417 } else {
418 NodePtr::NIL
419 },
420 ))
421 }
422
423 pub fn singleton(
424 allocator: &mut Allocator,
425 launcher_id: Bytes32,
426 reveal: bool,
427 ) -> Result<Self, DriverError> {
428 Ok(Self::new(
429 SingletonMember::new(launcher_id).curry_tree_hash().into(),
430 if reveal {
431 launcher_id.to_clvm(allocator)?
432 } else {
433 NodePtr::NIL
434 },
435 ))
436 }
437
438 pub fn fixed_puzzle(
439 allocator: &mut Allocator,
440 puzzle_hash: Bytes32,
441 reveal: bool,
442 ) -> Result<Self, DriverError> {
443 Ok(Self::new(
444 FixedPuzzleMember::new(puzzle_hash).curry_tree_hash().into(),
445 if reveal {
446 puzzle_hash.to_clvm(allocator)?
447 } else {
448 NodePtr::NIL
449 },
450 ))
451 }
452
453 pub fn parse(&self, allocator: &Allocator, ctx: &MipsMemoContext) -> Option<ParsedMember> {
454 for &public_key in Option::<PublicKey>::from_clvm(allocator, self.memo)
455 .ok()
456 .flatten()
457 .iter()
458 .chain(ctx.bls.iter())
459 {
460 let member = BlsMember::new(public_key);
461 if member.curry_tree_hash() == self.puzzle_hash.into() {
462 return Some(ParsedMember::Bls(member));
463 }
464
465 let member = BlsTaprootMember::new(public_key);
466 if member.curry_tree_hash() == self.puzzle_hash.into() {
467 return Some(ParsedMember::BlsTaproot(member));
468 }
469 }
470
471 for &public_key in Option::<K1PublicKey>::from_clvm(allocator, self.memo)
472 .ok()
473 .flatten()
474 .iter()
475 .chain(ctx.k1.iter())
476 {
477 let member = K1Member::new(public_key);
478 if member.curry_tree_hash() == self.puzzle_hash.into() {
479 return Some(ParsedMember::K1(member));
480 }
481
482 let member = K1MemberPuzzleAssert::new(public_key);
483 if member.curry_tree_hash() == self.puzzle_hash.into() {
484 return Some(ParsedMember::K1PuzzleAssert(member));
485 }
486 }
487
488 for &public_key in Option::<R1PublicKey>::from_clvm(allocator, self.memo)
489 .ok()
490 .flatten()
491 .iter()
492 .chain(ctx.r1.iter())
493 {
494 let member = R1Member::new(public_key);
495 if member.curry_tree_hash() == self.puzzle_hash.into() {
496 return Some(ParsedMember::R1(member));
497 }
498
499 let member = R1MemberPuzzleAssert::new(public_key);
500 if member.curry_tree_hash() == self.puzzle_hash.into() {
501 return Some(ParsedMember::R1PuzzleAssert(member));
502 }
503
504 let member = PasskeyMember::new(public_key);
505 if member.curry_tree_hash() == self.puzzle_hash.into() {
506 return Some(ParsedMember::Passkey(member));
507 }
508
509 let member = PasskeyMemberPuzzleAssert::new(public_key);
510 if member.curry_tree_hash() == self.puzzle_hash.into() {
511 return Some(ParsedMember::PasskeyPuzzleAssert(member));
512 }
513 }
514
515 for &hash in Option::<Bytes32>::from_clvm(allocator, self.memo)
516 .ok()
517 .flatten()
518 .iter()
519 .chain(ctx.hashes.iter())
520 {
521 let member = SingletonMember::new(hash);
522 if member.curry_tree_hash() == self.puzzle_hash.into() {
523 return Some(ParsedMember::Singleton(member));
524 }
525
526 let member = FixedPuzzleMember::new(hash);
527 if member.curry_tree_hash() == self.puzzle_hash.into() {
528 return Some(ParsedMember::FixedPuzzle(member));
529 }
530 }
531
532 None
533 }
534}
535
536#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
537#[clvm(list)]
538pub struct MofNMemo<T = NodePtr> {
539 pub required: usize,
540 pub items: Vec<InnerPuzzleMemo<T>>,
541}
542
543impl MofNMemo<NodePtr> {
544 pub fn new(required: usize, items: Vec<InnerPuzzleMemo>) -> Self {
545 Self { required, items }
546 }
547}
548
549#[derive(Debug, Clone)]
550pub struct MipsMemoContext {
551 pub k1: Vec<K1PublicKey>,
552 pub r1: Vec<R1PublicKey>,
553 pub bls: Vec<PublicKey>,
554 pub hashes: Vec<Bytes32>,
555 pub timelocks: Vec<u64>,
556 pub opcodes: Vec<u16>,
557}
558
559impl Default for MipsMemoContext {
560 fn default() -> Self {
561 Self {
562 k1: vec![],
563 r1: vec![],
564 bls: vec![],
565 hashes: vec![],
566 timelocks: vec![],
567 opcodes: vec![
568 SEND_MESSAGE,
569 RECEIVE_MESSAGE,
570 CREATE_PUZZLE_ANNOUNCEMENT,
571 CREATE_COIN_ANNOUNCEMENT,
572 ],
573 }
574 }
575}