chik_sdk_bindings/puzzle/
did.rs1use std::sync::{Arc, Mutex};
2
3use binky::Result;
4use chik_protocol::{Bytes32, Coin};
5use chik_sdk_driver::{Did as SdkDid, DidInfo as SdkDidInfo, HashedPtr, SpendContext};
6use klvm_utils::TreeHash;
7use klvmr::Allocator;
8
9use crate::{AsProgram, AsPtr, Program, Proof};
10
11use super::Puzzle;
12
13#[derive(Clone)]
14pub struct Did {
15 pub coin: Coin,
16 pub proof: Proof,
17 pub info: DidInfo,
18}
19
20impl Did {
21 pub fn child_proof(&self) -> Result<Proof> {
22 let ctx = self.info.metadata.0.lock().unwrap();
23 Ok(self.as_ptr(&ctx).child_lineage_proof().into())
24 }
25
26 pub fn child(&self, p2_puzzle_hash: Bytes32, metadata: Program) -> Result<Self> {
27 let ctx = metadata.0.lock().unwrap();
28 Ok(self
29 .as_ptr(&ctx)
30 .child(p2_puzzle_hash, metadata.as_ptr(&ctx))
31 .as_program(&metadata.0))
32 }
33
34 pub fn child_with(&self, info: DidInfo) -> Result<Self> {
35 let ctx = self.info.metadata.0.lock().unwrap();
36 Ok(self
37 .as_ptr(&ctx)
38 .child_with(info.as_ptr(&ctx))
39 .as_program(&self.info.metadata.0))
40 }
41}
42
43impl AsProgram for SdkDid<HashedPtr> {
44 type AsProgram = Did;
45
46 fn as_program(&self, klvm: &Arc<Mutex<SpendContext>>) -> Self::AsProgram {
47 Did {
48 coin: self.coin,
49 proof: self.proof.into(),
50 info: self.info.as_program(klvm),
51 }
52 }
53}
54
55impl AsPtr for Did {
56 type AsPtr = SdkDid<HashedPtr>;
57
58 fn as_ptr(&self, allocator: &Allocator) -> Self::AsPtr {
59 SdkDid::new(
60 self.coin,
61 self.proof.clone().into(),
62 self.info.as_ptr(allocator),
63 )
64 }
65}
66
67#[derive(Clone)]
68pub struct DidInfo {
69 pub launcher_id: Bytes32,
70 pub recovery_list_hash: Option<Bytes32>,
71 pub num_verifications_required: u64,
72 pub metadata: Program,
73 pub p2_puzzle_hash: Bytes32,
74}
75
76impl DidInfo {
77 pub fn inner_puzzle_hash(&self) -> Result<TreeHash> {
78 let ctx = self.metadata.0.lock().unwrap();
79 Ok(self.as_ptr(&ctx).inner_puzzle_hash())
80 }
81
82 pub fn puzzle_hash(&self) -> Result<TreeHash> {
83 let ctx = self.metadata.0.lock().unwrap();
84 Ok(self.as_ptr(&ctx).puzzle_hash())
85 }
86}
87
88impl AsProgram for SdkDidInfo<HashedPtr> {
89 type AsProgram = DidInfo;
90
91 fn as_program(&self, klvm: &Arc<Mutex<SpendContext>>) -> Self::AsProgram {
92 DidInfo {
93 launcher_id: self.launcher_id,
94 recovery_list_hash: self.recovery_list_hash,
95 num_verifications_required: self.num_verifications_required,
96 metadata: self.metadata.as_program(klvm),
97 p2_puzzle_hash: self.p2_puzzle_hash,
98 }
99 }
100}
101
102impl AsPtr for DidInfo {
103 type AsPtr = SdkDidInfo<HashedPtr>;
104
105 fn as_ptr(&self, allocator: &Allocator) -> Self::AsPtr {
106 SdkDidInfo::new(
107 self.launcher_id,
108 self.recovery_list_hash,
109 self.num_verifications_required,
110 self.metadata.as_ptr(allocator),
111 self.p2_puzzle_hash,
112 )
113 }
114}
115
116#[derive(Clone)]
117pub struct ParsedDid {
118 pub info: DidInfo,
119 pub p2_puzzle: Puzzle,
120}
121
122#[derive(Clone)]
123pub struct CreatedDid {
124 pub did: Did,
125 pub parent_conditions: Vec<Program>,
126}