holochain_integrity_types/
entry.rs1use crate::capability::CapClaim;
9use crate::capability::CapGrant;
10use crate::capability::ZomeCallCapGrant;
11use crate::countersigning::CounterSigningSessionData;
12use crate::AppEntryDef;
13use crate::EntryDefIndex;
14use crate::EntryType;
15use crate::ZomeIndex;
16use holo_hash::hash_type;
17use holo_hash::ActionHash;
18use holo_hash::AgentPubKey;
19use holo_hash::EntryHash;
20use holo_hash::HashableContent;
21use holo_hash::HashableContentBytes;
22use holochain_serialized_bytes::prelude::*;
23
24mod app_entry_bytes;
25mod error;
26pub use app_entry_bytes::*;
27pub use error::*;
28
29pub const ENTRY_SIZE_LIMIT: usize = 4 * 1000 * 1000; pub type CapGrantEntry = ZomeCallCapGrant;
38
39pub type CapClaimEntry = CapClaim;
41
42pub type EntryHashed = holo_hash::HoloHashed<Entry>;
44
45pub trait EntryTypesHelper: Sized {
49 type Error;
51 fn deserialize_from_type<Z, I>(
55 zome_index: Z,
56 entry_def_index: I,
57 entry: &Entry,
58 ) -> Result<Option<Self>, Self::Error>
59 where
60 Z: Into<ZomeIndex>,
61 I: Into<EntryDefIndex>;
62}
63
64impl EntryTypesHelper for () {
65 type Error = core::convert::Infallible;
66
67 fn deserialize_from_type<Z, I>(
68 _zome_index: Z,
69 _entry_def_index: I,
70 _entry: &Entry,
71 ) -> Result<Option<Self>, Self::Error>
72 where
73 Z: Into<ZomeIndex>,
74 I: Into<EntryDefIndex>,
75 {
76 Ok(Some(()))
77 }
78}
79
80impl From<EntryHashed> for Entry {
81 fn from(entry_hashed: EntryHashed) -> Self {
82 entry_hashed.into_content()
83 }
84}
85
86#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, SerializedBytes)]
88#[cfg_attr(
89 feature = "fuzzing",
90 derive(arbitrary::Arbitrary, proptest_derive::Arbitrary)
91)]
92#[serde(tag = "entry_type", content = "entry")]
93pub enum Entry {
94 Agent(AgentPubKey),
97 App(AppEntryBytes),
99 CounterSign(Box<CounterSigningSessionData>, AppEntryBytes),
101 CapClaim(CapClaimEntry),
104 CapGrant(CapGrantEntry),
107}
108
109impl Entry {
110 pub fn as_cap_grant(&self) -> Option<CapGrant> {
112 match self {
113 Entry::Agent(key) => Some(CapGrant::ChainAuthor(key.clone())),
114 Entry::CapGrant(data) => Some(CapGrant::RemoteAgent(data.clone())),
115 _ => None,
116 }
117 }
118
119 pub fn as_cap_claim(&self) -> Option<&CapClaim> {
121 match self {
122 Entry::CapClaim(claim) => Some(claim),
123 _ => None,
124 }
125 }
126
127 pub fn as_app_entry(&self) -> Option<&AppEntryBytes> {
129 match self {
130 Entry::App(bytes) => Some(bytes),
131 _ => None,
132 }
133 }
134
135 pub fn app(sb: SerializedBytes) -> Result<Self, EntryError> {
137 Ok(Entry::App(AppEntryBytes::try_from(sb)?))
138 }
139
140 pub fn app_fancy<SB: TryInto<SerializedBytes, Error = SerializedBytesError>>(
142 sb: SB,
143 ) -> Result<Self, EntryError> {
144 Ok(Entry::App(AppEntryBytes::try_from(sb.try_into()?)?))
145 }
146
147 pub fn entry_type(&self, entry_def: Option<AppEntryDef>) -> Option<EntryType> {
150 match (self, entry_def) {
151 (Entry::Agent(_), _) => Some(EntryType::AgentPubKey),
152 (Entry::CapClaim(_), _) => Some(EntryType::CapClaim),
153 (Entry::CapGrant(_), _) => Some(EntryType::CapGrant),
154 (Entry::App(_), Some(aed)) | (Entry::CounterSign(_, _), Some(aed)) => {
155 Some(EntryType::App(aed))
156 }
157 _ => None,
158 }
159 }
160}
161
162impl HashableContent for Entry {
163 type HashType = hash_type::Entry;
164
165 fn hash_type(&self) -> Self::HashType {
166 hash_type::Entry
167 }
168
169 fn hashable_content(&self) -> HashableContentBytes {
170 match self {
171 Entry::Agent(agent_pubkey) => {
172 HashableContentBytes::Prehashed39(
175 EntryHash::from(agent_pubkey.clone()).into_inner(),
176 )
177 }
178 entry => HashableContentBytes::Content(
179 entry
180 .try_into()
181 .expect("Could not serialize HashableContent"),
182 ),
183 }
184 }
185}
186
187#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
189pub struct MustGetValidRecordInput(pub ActionHash);
190
191impl MustGetValidRecordInput {
192 pub fn new(action_hash: ActionHash) -> Self {
194 Self(action_hash)
195 }
196
197 pub fn into_inner(self) -> ActionHash {
199 self.0
200 }
201}
202
203#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
205pub struct MustGetEntryInput(pub EntryHash);
206
207impl MustGetEntryInput {
208 pub fn new(entry_hash: EntryHash) -> Self {
210 Self(entry_hash)
211 }
212
213 pub fn into_inner(self) -> EntryHash {
215 self.0
216 }
217}
218
219#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
221pub struct MustGetActionInput(pub ActionHash);
222
223impl MustGetActionInput {
224 pub fn new(action_hash: ActionHash) -> Self {
226 Self(action_hash)
227 }
228
229 pub fn into_inner(self) -> ActionHash {
231 self.0
232 }
233}