|
| 1 | +use chia::{ |
| 2 | + clvm_traits::{FromClvm, ToClvm}, |
| 3 | + protocol::{Bytes32, Program}, |
| 4 | +}; |
| 5 | +use chia_wallet_sdk::{ |
| 6 | + driver::{Did, DidInfo, DriverError, HashedPtr, Nft, NftInfo, Singleton}, |
| 7 | + prelude::Allocator, |
| 8 | +}; |
| 9 | + |
| 10 | +pub type SerializedNft = Singleton<SerializedNftInfo>; |
| 11 | +pub type SerializedDid = Singleton<SerializedDidInfo>; |
| 12 | + |
| 13 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 14 | +pub struct SerializedNftInfo { |
| 15 | + pub launcher_id: Bytes32, |
| 16 | + pub metadata: Program, |
| 17 | + pub metadata_updater_puzzle_hash: Bytes32, |
| 18 | + pub current_owner: Option<Bytes32>, |
| 19 | + pub royalty_puzzle_hash: Bytes32, |
| 20 | + pub royalty_basis_points: u16, |
| 21 | + pub p2_puzzle_hash: Bytes32, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | +pub struct SerializedDidInfo { |
| 26 | + pub launcher_id: Bytes32, |
| 27 | + pub recovery_list_hash: Option<Bytes32>, |
| 28 | + pub num_verifications_required: u64, |
| 29 | + pub metadata: Program, |
| 30 | + pub p2_puzzle_hash: Bytes32, |
| 31 | +} |
| 32 | + |
| 33 | +pub trait DeserializePrimitive { |
| 34 | + type Primitive; |
| 35 | + |
| 36 | + fn deserialize(self, allocator: &mut Allocator) -> Result<Self::Primitive, DriverError>; |
| 37 | +} |
| 38 | + |
| 39 | +impl DeserializePrimitive for SerializedNft { |
| 40 | + type Primitive = Nft; |
| 41 | + |
| 42 | + fn deserialize(self, allocator: &mut Allocator) -> Result<Self::Primitive, DriverError> { |
| 43 | + Ok(Nft::new( |
| 44 | + self.coin, |
| 45 | + self.proof, |
| 46 | + self.info.deserialize(allocator)?, |
| 47 | + )) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl DeserializePrimitive for SerializedNftInfo { |
| 52 | + type Primitive = NftInfo; |
| 53 | + |
| 54 | + fn deserialize(self, allocator: &mut Allocator) -> Result<Self::Primitive, DriverError> { |
| 55 | + let ptr = self.metadata.to_clvm(allocator)?; |
| 56 | + |
| 57 | + Ok(NftInfo::new( |
| 58 | + self.launcher_id, |
| 59 | + HashedPtr::from_ptr(allocator, ptr), |
| 60 | + self.metadata_updater_puzzle_hash, |
| 61 | + self.current_owner, |
| 62 | + self.royalty_puzzle_hash, |
| 63 | + self.royalty_basis_points, |
| 64 | + self.p2_puzzle_hash, |
| 65 | + )) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl DeserializePrimitive for SerializedDid { |
| 70 | + type Primitive = Did; |
| 71 | + |
| 72 | + fn deserialize(self, allocator: &mut Allocator) -> Result<Self::Primitive, DriverError> { |
| 73 | + Ok(Did::new( |
| 74 | + self.coin, |
| 75 | + self.proof, |
| 76 | + self.info.deserialize(allocator)?, |
| 77 | + )) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl DeserializePrimitive for SerializedDidInfo { |
| 82 | + type Primitive = DidInfo; |
| 83 | + |
| 84 | + fn deserialize(self, allocator: &mut Allocator) -> Result<Self::Primitive, DriverError> { |
| 85 | + let ptr = self.metadata.to_clvm(allocator)?; |
| 86 | + |
| 87 | + Ok(DidInfo::new( |
| 88 | + self.launcher_id, |
| 89 | + self.recovery_list_hash, |
| 90 | + self.num_verifications_required, |
| 91 | + HashedPtr::from_ptr(allocator, ptr), |
| 92 | + self.p2_puzzle_hash, |
| 93 | + )) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub trait SerializePrimitive: Sized { |
| 98 | + type Primitive; |
| 99 | + |
| 100 | + fn serialize(&self, allocator: &Allocator) -> Result<Self::Primitive, DriverError>; |
| 101 | +} |
| 102 | + |
| 103 | +impl SerializePrimitive for Nft { |
| 104 | + type Primitive = SerializedNft; |
| 105 | + |
| 106 | + fn serialize(&self, allocator: &Allocator) -> Result<Self::Primitive, DriverError> { |
| 107 | + Ok(SerializedNft { |
| 108 | + coin: self.coin, |
| 109 | + proof: self.proof, |
| 110 | + info: self.info.serialize(allocator)?, |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl SerializePrimitive for NftInfo { |
| 116 | + type Primitive = SerializedNftInfo; |
| 117 | + |
| 118 | + fn serialize(&self, allocator: &Allocator) -> Result<Self::Primitive, DriverError> { |
| 119 | + Ok(SerializedNftInfo { |
| 120 | + launcher_id: self.launcher_id, |
| 121 | + metadata: Program::from_clvm(allocator, self.metadata.ptr())?, |
| 122 | + metadata_updater_puzzle_hash: self.metadata_updater_puzzle_hash, |
| 123 | + current_owner: self.current_owner, |
| 124 | + royalty_puzzle_hash: self.royalty_puzzle_hash, |
| 125 | + royalty_basis_points: self.royalty_basis_points, |
| 126 | + p2_puzzle_hash: self.p2_puzzle_hash, |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl SerializePrimitive for Did { |
| 132 | + type Primitive = SerializedDid; |
| 133 | + |
| 134 | + fn serialize(&self, allocator: &Allocator) -> Result<Self::Primitive, DriverError> { |
| 135 | + Ok(SerializedDid { |
| 136 | + coin: self.coin, |
| 137 | + proof: self.proof, |
| 138 | + info: self.info.serialize(allocator)?, |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl SerializePrimitive for DidInfo { |
| 144 | + type Primitive = SerializedDidInfo; |
| 145 | + |
| 146 | + fn serialize(&self, allocator: &Allocator) -> Result<Self::Primitive, DriverError> { |
| 147 | + Ok(SerializedDidInfo { |
| 148 | + launcher_id: self.launcher_id, |
| 149 | + recovery_list_hash: self.recovery_list_hash, |
| 150 | + num_verifications_required: self.num_verifications_required, |
| 151 | + metadata: Program::from_clvm(allocator, self.metadata.ptr())?, |
| 152 | + p2_puzzle_hash: self.p2_puzzle_hash, |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments