apphub_types/
lib.rs

1mod holochain_types;
2mod app_entry;
3mod ui_entry;
4mod webapp_entry;
5mod webapp_package_entry;
6mod webapp_package_version_entry;
7
8pub use hdi_extensions;
9pub use hdi_extensions::hdi;
10pub use holochain_types::*;
11pub use mere_memory_types;
12
13pub use app_entry::*;
14pub use ui_entry::*;
15
16pub use webapp_entry::*;
17pub use webapp_package_entry::*;
18pub use webapp_package_version_entry::*;
19
20use std::{
21    iter::{
22        FromIterator,
23    },
24    collections::{
25        BTreeMap,
26    },
27};
28use hdi::prelude::*;
29use hdi_extensions::{
30    guest_error,
31};
32use holochain_zome_types::{
33    DnaModifiersOpt,
34    YamlProperties,
35};
36use rmp_serde;
37use sha2::{ Digest, Sha256 };
38use dnahub_types::{
39    DnaToken,
40};
41
42
43
44pub type EntityId = ActionHash;
45pub type BundleAddr = EntryHash;
46pub type MemoryAddr = EntryHash;
47pub type RmpvValue = rmpv::Value;
48
49
50#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
51pub struct RolesDnaTokens(pub BTreeMap<String, DnaToken>);
52
53impl RolesDnaTokens {
54    pub fn integrity_hashes(&self) -> Vec<Vec<u8>> {
55        let mut dnas_integrity_hashes = self.0.iter()
56            .map( |(_, dna_token)| dna_token.integrity_hash.clone() )
57            .collect::<Vec<Vec<u8>>>();
58
59        dnas_integrity_hashes.sort();
60
61        dnas_integrity_hashes
62    }
63
64    pub fn integrity_hash(&self) -> ExternResult<Vec<u8>> {
65        hash( &self.integrity_hashes() )
66    }
67
68    pub fn dna_token(&self, role_name: &str) -> ExternResult<DnaToken> {
69        match self.0.iter().find( |(name, _)| name.as_str() == role_name ) {
70            Some((_, dna_token)) => Ok( dna_token.clone() ),
71            None => Err(guest_error!(format!(
72                "Missing DnaToken for role '{}'", role_name
73            ))),
74        }
75    }
76}
77
78impl FromIterator<(String, DnaToken)> for RolesDnaTokens {
79    fn from_iter<I: IntoIterator<Item = (String, DnaToken)>>(iter: I) -> Self {
80        let map: BTreeMap<String, DnaToken> = iter.into_iter().collect();
81        RolesDnaTokens(map)
82    }
83}
84
85
86#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Ord, Eq)]
87pub struct RoleToken {
88    pub integrity_hash: Vec<u8>,
89    pub integrities_token_hash: Vec<u8>,
90    pub coordinators_token_hash: Vec<u8>,
91    pub modifiers_hash: Vec<u8>,
92}
93
94impl RoleToken {
95    pub fn new(dna_token: &DnaToken, modifiers: &DnaModifiersOpt<YamlProperties>) ->
96        ExternResult<Self>
97    {
98        Ok(
99            Self {
100                integrity_hash: dna_token.integrity_hash.to_owned(),
101                integrities_token_hash: dna_token.integrities_token_hash.to_owned(),
102                coordinators_token_hash: dna_token.coordinators_token_hash.to_owned(),
103                modifiers_hash: hash( modifiers )?,
104            }
105        )
106    }
107
108    pub fn dna_token(&self) -> DnaToken {
109        DnaToken {
110            integrity_hash: self.integrity_hash.to_owned(),
111            integrities_token_hash: self.integrities_token_hash.to_owned(),
112            coordinators_token_hash: self.coordinators_token_hash.to_owned(),
113        }
114    }
115}
116
117
118#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
119pub struct RolesToken(pub Vec<(String, RoleToken)>);
120
121impl RolesToken {
122    pub fn integrity_hashes(&self) -> Vec<Vec<u8>> {
123        let mut dnas_integrity_hashes = self.0.iter()
124            .map( |(_, role_token)| role_token.integrity_hash.clone() )
125            .collect::<Vec<Vec<u8>>>();
126
127        dnas_integrity_hashes.sort();
128
129        dnas_integrity_hashes
130    }
131
132    pub fn integrity_hash(&self) -> ExternResult<Vec<u8>> {
133        hash( &self.integrity_hashes() )
134    }
135
136    pub fn role_token(&self, role_name: &str) -> ExternResult<RoleToken> {
137        match self.0.iter()
138            .find( |(name, _)| name.as_str() == role_name ) {
139                Some((_, role_token)) => Ok( role_token.clone() ),
140                None => Err(guest_error!(format!(
141                    "Missing RoleToken for role '{}'", role_name
142                ))),
143            }
144    }
145
146    pub fn dna_token(&self, role_name: &str) -> ExternResult<DnaToken> {
147        match self.0.iter()
148            .find( |(name, _)| name.as_str() == role_name ) {
149                Some((_, role_token)) => Ok( role_token.dna_token() ),
150                None => Err(guest_error!(format!(
151                    "Missing DnaToken for role '{}'", role_name
152                ))),
153            }
154    }
155}
156
157impl FromIterator<(String, RoleToken)> for RolesToken {
158    fn from_iter<I: IntoIterator<Item = (String, RoleToken)>>(iter: I) -> Self {
159        let vec: Vec<(String, RoleToken)> = iter.into_iter().collect();
160        RolesToken(vec)
161    }
162}
163
164
165#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
166pub struct AppToken {
167    pub integrity_hash: Vec<u8>,
168    pub roles_token_hash: Vec<u8>,
169    pub roles_token: RolesToken,
170}
171
172
173#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
174pub struct WebAppToken {
175    pub ui_hash: Vec<u8>,
176    pub app_token: AppToken,
177}
178
179
180
181#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
182#[serde(tag = "type", content = "content")]
183#[serde(rename_all = "snake_case")]
184pub enum Authority {
185    // Group(ActionHash, ActionHash),
186    Agent(AgentPubKey),
187}
188
189impl From<AgentPubKey> for Authority {
190    fn from(agent_pub_key: AgentPubKey) -> Self {
191        Authority::Agent(agent_pub_key)
192    }
193}
194
195#[derive(Serialize, Deserialize, Clone, Debug)]
196pub struct DeprecationNotice {
197    pub message: String,
198    #[serde(default)]
199    pub recommended_alternatives: Vec<ActionHash>,
200}
201
202
203pub fn serialize<T>(target: &T) -> ExternResult<Vec<u8>>
204where
205    T: Serialize + ?Sized,
206{
207    rmp_serde::encode::to_vec( target )
208        .map_err( |err| guest_error!(format!(
209            "Failed to serialize target: {:?}", err
210        )) )
211}
212
213
214pub fn hash<T>(target: &T) -> ExternResult<Vec<u8>>
215where
216    T: Serialize + ?Sized,
217{
218    Ok(
219        Sha256::digest( &serialize( target )? ).to_vec()
220    )
221}