apphub_sdk/
lib.rs

1pub use apphub_types;
2pub use devhub_sdk;
3pub use devhub_sdk::*;
4
5use std::collections::BTreeMap;
6use hdk::prelude::*;
7use hdk_extensions::{
8    agent_id,
9    must_get,
10};
11use serde_bytes::ByteBuf;
12use apphub_types::{
13    Authority,
14    MemoryAddr,
15    BundleAddr,
16    DeprecationNotice,
17    RmpvValue,
18
19    RoleToken,
20    AppManifestV1,
21    AppToken,
22    AppEntry,
23
24    RolesToken,
25    RolesDnaTokens,
26    WebAppManifestV1,
27    WebAppToken,
28    WebAppEntry,
29
30    WebAppPackageEntry,
31    WebAppPackageVersionEntry,
32};
33use dnahub_sdk::{
34    DnaTokenInput,
35};
36use hc_crud::{
37    Entity, EntityId,
38};
39
40
41pub type EntityMap<T> = BTreeMap<String, Entity<T>>;
42pub type EntityPointerMap = BTreeMap<String, EntityId>;
43
44pub type WebAppMap = EntityMap<WebAppEntry>;
45pub type WebAppPackageMap = EntityMap<WebAppPackageEntry>;
46pub type WebAppPackageVersionMap = EntityMap<WebAppPackageVersionEntry>;
47
48
49#[derive(Serialize, Deserialize, Clone, Debug)]
50pub struct RolesTokenInput(pub Vec<(String, RoleTokenInput)>);
51
52impl From<RolesTokenInput> for RolesToken {
53    fn from(input: RolesTokenInput) -> Self {
54        Self(
55            input.0.into_iter()
56                .map( |(role_name, role_token_input)| (role_name, role_token_input.into()) )
57                .collect()
58        )
59    }
60}
61
62
63#[derive(Serialize, Deserialize, Clone, Debug)]
64pub struct RolesDnaTokensInput(pub BTreeMap<String, DnaTokenInput>);
65
66impl From<RolesDnaTokensInput> for RolesDnaTokens {
67    fn from(input: RolesDnaTokensInput) -> Self {
68        Self(
69            input.0.into_iter()
70                .map( |(role_name, dna_token_input)| (role_name, dna_token_input.into()) )
71                .collect()
72        )
73    }
74}
75
76
77#[derive(Serialize, Deserialize, Clone, Debug)]
78pub struct RoleTokenInput {
79    pub integrity_hash: ByteBuf,
80    pub integrities_token_hash: ByteBuf,
81    pub coordinators_token_hash: ByteBuf,
82    pub modifiers_hash: ByteBuf,
83}
84
85impl From<RoleTokenInput> for RoleToken {
86    fn from(input: RoleTokenInput) -> Self {
87        Self {
88            integrity_hash: input.integrity_hash.to_vec(),
89            integrities_token_hash: input.integrities_token_hash.to_vec(),
90            coordinators_token_hash: input.coordinators_token_hash.to_vec(),
91            modifiers_hash: input.modifiers_hash.to_vec(),
92        }
93    }
94}
95
96
97#[derive(Serialize, Deserialize, Clone, Debug)]
98pub struct AppTokenInput {
99    pub integrity_hash: ByteBuf,
100    pub roles_token_hash: ByteBuf,
101    pub roles_token: RolesTokenInput,
102}
103
104impl From<AppTokenInput> for AppToken {
105    fn from(input: AppTokenInput) -> Self {
106        Self {
107            integrity_hash: input.integrity_hash.to_vec(),
108            roles_token_hash: input.roles_token_hash.to_vec(),
109            roles_token: input.roles_token.into(),
110        }
111    }
112}
113
114
115#[derive(Serialize, Deserialize, Clone, Debug)]
116pub struct AppEntryInput {
117    pub manifest: AppManifestV1,
118    pub app_token: AppTokenInput,
119}
120
121impl From<AppEntryInput> for AppEntry {
122    fn from(input: AppEntryInput) -> Self {
123        Self {
124            manifest: input.manifest,
125            app_token: input.app_token.into(),
126        }
127    }
128}
129
130
131#[derive(Serialize, Deserialize, Clone, Debug)]
132pub struct CreateAppInput {
133    pub manifest: AppManifestV1,
134    pub roles_dna_tokens: RolesDnaTokensInput,
135}
136
137impl TryFrom<CreateAppInput> for AppEntry {
138    type Error = WasmError;
139
140    fn try_from(input: CreateAppInput) -> ExternResult<Self> {
141        Self::new( input.manifest, input.roles_dna_tokens.into() )
142    }
143}
144
145
146#[derive(Serialize, Deserialize, Clone, Debug)]
147pub struct WebAppTokenInput {
148    pub ui_hash: ByteBuf,
149    pub app_token: AppTokenInput,
150}
151
152impl From<WebAppTokenInput> for WebAppToken {
153    fn from(input: WebAppTokenInput) -> Self {
154        Self {
155            ui_hash: input.ui_hash.to_vec(),
156            app_token: input.app_token.into(),
157        }
158    }
159}
160
161
162#[derive(Serialize, Deserialize, Clone, Debug)]
163pub struct WebAppEntryInput {
164    pub manifest: WebAppManifestV1,
165
166    pub webapp_token: WebAppTokenInput,
167}
168
169impl From<WebAppEntryInput> for WebAppEntry {
170    fn from(input: WebAppEntryInput) -> Self {
171        Self {
172            manifest: input.manifest,
173            webapp_token: input.webapp_token.into(),
174        }
175    }
176}
177
178
179#[derive(Serialize, Deserialize, Clone, Debug)]
180pub struct CreateWebAppInput {
181    pub manifest: WebAppManifestV1,
182}
183
184impl TryFrom<CreateWebAppInput> for WebAppEntry {
185    type Error = WasmError;
186
187    fn try_from(input: CreateWebAppInput) -> ExternResult<Self> {
188        Self::new( input.manifest )
189    }
190}
191
192
193#[derive(Serialize, Deserialize, Clone, Debug)]
194pub struct WebAppPackageEntryInput {
195    pub title: String,
196    pub subtitle: String,
197    pub description: String,
198    pub maintainer: Authority,
199    pub icon: MemoryAddr,
200    pub source_code_uri: Option<String>,
201    #[serde(default)]
202    pub deprecation: Option<DeprecationNotice>,
203    pub metadata: BTreeMap<String, RmpvValue>,
204}
205
206impl From<WebAppPackageEntryInput> for WebAppPackageEntry {
207    fn from(input: WebAppPackageEntryInput) -> Self {
208        Self {
209            title: input.title,
210            subtitle: input.subtitle,
211            description: input.description,
212            maintainer: input.maintainer,
213            icon: input.icon,
214            source_code_uri: input.source_code_uri,
215            deprecation: input.deprecation,
216            metadata: input.metadata,
217        }
218    }
219}
220
221
222#[derive(Serialize, Deserialize, Clone, Debug)]
223pub struct CreateWebAppPackageInput {
224    pub title: String,
225    pub subtitle: String,
226    pub description: String,
227    pub icon: MemoryAddr,
228    #[serde(default)]
229    pub metadata: BTreeMap<String, RmpvValue>,
230    pub maintainer: Option<Authority>,
231    pub source_code_uri: Option<String>,
232}
233
234impl TryFrom<CreateWebAppPackageInput> for WebAppPackageEntry {
235    type Error = WasmError;
236
237    fn try_from(input: CreateWebAppPackageInput) -> ExternResult<Self> {
238        Ok(
239            Self {
240                title: input.title,
241                subtitle: input.subtitle,
242                description: input.description,
243                maintainer: input.maintainer
244                    .unwrap_or( agent_id()?.into() ),
245                icon: input.icon,
246                source_code_uri: input.source_code_uri,
247                deprecation: None,
248                metadata: input.metadata,
249            }
250        )
251    }
252}
253
254
255#[derive(Serialize, Deserialize, Clone, Debug)]
256pub struct WebAppPackageVersionEntryInput {
257    pub for_package: EntityId,
258    pub maintainer: Authority,
259    pub webapp: BundleAddr,
260    pub webapp_token: WebAppTokenInput,
261    pub changelog: Option<String>,
262    pub source_code_revision_uri: Option<String>,
263    #[serde(default)]
264    pub metadata: BTreeMap<String, RmpvValue>,
265}
266
267impl From<WebAppPackageVersionEntryInput> for WebAppPackageVersionEntry {
268    fn from(input: WebAppPackageVersionEntryInput) -> Self {
269        Self {
270            for_package: input.for_package,
271            webapp: input.webapp,
272            webapp_token: input.webapp_token.into(),
273            changelog: input.changelog,
274            maintainer: input.maintainer,
275            source_code_revision_uri: input.source_code_revision_uri,
276            metadata: input.metadata,
277        }
278    }
279}
280
281
282#[derive(Serialize, Deserialize, Clone, Debug)]
283pub struct CreateWebAppPackageVersionInput {
284    pub for_package: EntityId,
285    pub version: String,
286    pub webapp: BundleAddr,
287    #[serde(default)]
288    pub metadata: BTreeMap<String, RmpvValue>,
289
290    // Optional
291    pub changelog: Option<String>,
292    pub maintainer: Option<Authority>,
293    pub source_code_revision_uri: Option<String>,
294}
295
296impl TryFrom<CreateWebAppPackageVersionInput> for WebAppPackageVersionEntry {
297    type Error = WasmError;
298
299    fn try_from(input: CreateWebAppPackageVersionInput) -> ExternResult<Self> {
300        let webapp_entry : WebAppEntry = must_get( &input.webapp )?.try_into()?;
301
302        Ok(
303            Self {
304                for_package: input.for_package,
305                webapp: input.webapp,
306                webapp_token: webapp_entry.webapp_token,
307                changelog: input.changelog,
308                maintainer: input.maintainer
309                    .unwrap_or( agent_id()?.into() ),
310                source_code_revision_uri: input.source_code_revision_uri,
311                metadata: input.metadata,
312            }
313        )
314    }
315}