holochain-conductor-runtime-types-ffi 0.2.3

Types used in holochain-conductor-runtime-ffi, used by both client and service.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use holochain_conductor_api::{
    AppAuthenticationTokenIssued, AppInfo, AppInfoStatus, CellInfo, ProvisionedCell, StemCell,
    ZomeCallParamsSigned,
};
use holochain_types::{
    app::{
        AppBundleError, AppBundleSource, DisabledAppReason, InstallAppPayload, PausedAppReason,
        RoleSettings,
    },
    dna::{
        hash_type::{Agent, Dna},
        HoloHash,
    },
    prelude::{
        CapSecret, CellId, ClonedCell, DnaModifiers, DnaModifiersOpt, ExternIO, FunctionName,
        Nonce256Bits, SerializedBytes, Timestamp, UnsafeBytes, YamlProperties, ZomeCallParams,
        ZomeName,
    },
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(uniffi::Record)]
pub struct DnaModifiersFfi {
    pub network_seed: String,
    pub properties: Vec<u8>,
}

impl From<DnaModifiers> for DnaModifiersFfi {
    fn from(value: DnaModifiers) -> Self {
        Self {
            network_seed: value.network_seed,
            properties: value.properties.bytes().to_owned(),
        }
    }
}

#[derive(uniffi::Record, Serialize, Deserialize, Clone, Debug)]
pub struct DnaModifiersOptFfi {
    pub network_seed: Option<String>,
    pub properties: Option<Vec<u8>>,
}

impl From<DnaModifiersOptFfi> for DnaModifiersOpt<YamlProperties> {
    fn from(val: DnaModifiersOptFfi) -> Self {
        DnaModifiersOpt {
            network_seed: val.network_seed,
            properties: val.properties.map(|p| {
                YamlProperties::try_from(SerializedBytes::from(UnsafeBytes::from(p))).unwrap()
            }),
        }
    }
}

#[derive(uniffi::Record)]
pub struct ProvisionedCellFfi {
    pub cell_id: CellIdFfi,
    pub dna_modifiers: DnaModifiersFfi,
    pub name: String,
}

impl From<ProvisionedCell> for ProvisionedCellFfi {
    fn from(value: ProvisionedCell) -> Self {
        Self {
            cell_id: value.cell_id.into(),
            dna_modifiers: value.dna_modifiers.into(),
            name: value.name,
        }
    }
}

#[derive(uniffi::Record)]
pub struct ClonedCellFfi {
    pub cell_id: CellIdFfi,
    pub clone_id: String,
    pub original_dna_hash: Vec<u8>,
    pub dna_modifiers: DnaModifiersFfi,
    pub name: String,
    pub enabled: bool,
}

impl From<ClonedCell> for ClonedCellFfi {
    fn from(value: ClonedCell) -> Self {
        Self {
            cell_id: value.cell_id.into(),
            clone_id: value.clone_id.0,
            original_dna_hash: value.original_dna_hash.get_raw_39().to_vec(),
            dna_modifiers: value.dna_modifiers.into(),
            name: value.name,
            enabled: value.enabled,
        }
    }
}

#[derive(uniffi::Record)]
pub struct StemCellFfi {
    pub original_dna_hash: Vec<u8>,
    pub dna_modifiers: DnaModifiersFfi,
    pub name: Option<String>,
}

impl From<StemCell> for StemCellFfi {
    fn from(value: StemCell) -> Self {
        Self {
            original_dna_hash: value.original_dna_hash.get_raw_39().to_vec(),
            dna_modifiers: value.dna_modifiers.into(),
            name: value.name,
        }
    }
}

#[derive(uniffi::Enum)]
pub enum CellInfoFfi {
    Provisioned(ProvisionedCellFfi),
    Cloned(ClonedCellFfi),
    Stem(StemCellFfi),
}

impl From<CellInfo> for CellInfoFfi {
    fn from(value: CellInfo) -> Self {
        match value {
            CellInfo::Provisioned(provisioned) => CellInfoFfi::Provisioned(provisioned.into()),
            CellInfo::Cloned(cloned) => CellInfoFfi::Cloned(cloned.into()),
            CellInfo::Stem(stem) => CellInfoFfi::Stem(stem.into()),
        }
    }
}

#[derive(uniffi::Enum, Eq, PartialEq, Debug)]
pub enum PausedAppReasonFfi {
    Error(String),
}

impl From<PausedAppReason> for PausedAppReasonFfi {
    fn from(value: PausedAppReason) -> Self {
        match value {
            PausedAppReason::Error(error) => PausedAppReasonFfi::Error(error),
        }
    }
}

#[derive(uniffi::Enum, Eq, PartialEq, Debug)]
pub enum DisabledAppReasonFfi {
    NeverStarted,
    NotStartedAfterProvidingMemproofs,
    DeletingAgentKey,
    User,
    Error(String),
}

impl From<DisabledAppReason> for DisabledAppReasonFfi {
    fn from(value: DisabledAppReason) -> Self {
        match value {
            DisabledAppReason::NeverStarted => DisabledAppReasonFfi::NeverStarted,
            DisabledAppReason::NotStartedAfterProvidingMemproofs => {
                DisabledAppReasonFfi::NotStartedAfterProvidingMemproofs
            }
            DisabledAppReason::DeletingAgentKey => DisabledAppReasonFfi::DeletingAgentKey,
            DisabledAppReason::User => DisabledAppReasonFfi::User,
            DisabledAppReason::Error(error) => DisabledAppReasonFfi::Error(error),
        }
    }
}

#[derive(uniffi::Enum, Eq, PartialEq, Debug)]
pub enum AppInfoStatusFfi {
    Paused { reason: PausedAppReasonFfi },
    Disabled { reason: DisabledAppReasonFfi },
    Running,
    AwaitingMemproofs,
}

impl From<AppInfoStatus> for AppInfoStatusFfi {
    fn from(value: AppInfoStatus) -> Self {
        match value {
            AppInfoStatus::Paused { reason: paused } => AppInfoStatusFfi::Paused {
                reason: paused.into(),
            },
            AppInfoStatus::Disabled { reason: disabled } => AppInfoStatusFfi::Disabled {
                reason: disabled.into(),
            },
            AppInfoStatus::Running => AppInfoStatusFfi::Running,
            AppInfoStatus::AwaitingMemproofs => AppInfoStatusFfi::AwaitingMemproofs,
        }
    }
}

#[derive(uniffi::Record)]
pub struct AppInfoFfi {
    /// The unique identifier for an installed app in this conductor
    pub installed_app_id: String,
    pub cell_info: HashMap<String, Vec<CellInfoFfi>>,
    pub status: AppInfoStatusFfi,
    pub agent_pub_key: Vec<u8>,
}

impl From<AppInfo> for AppInfoFfi {
    fn from(value: AppInfo) -> Self {
        let mut cell_info: HashMap<String, Vec<CellInfoFfi>> = HashMap::new();
        for entry in value.cell_info.into_iter() {
            let entry_cell_infos: Vec<CellInfoFfi> =
                entry.1.into_iter().map(|val| val.into()).collect();
            cell_info.insert(entry.0, entry_cell_infos);
        }

        Self {
            installed_app_id: value.installed_app_id,
            cell_info,
            status: value.status.into(),
            agent_pub_key: value.agent_pub_key.into_inner(),
        }
    }
}

#[derive(uniffi::Record, Serialize, Deserialize, Clone, Debug)]
pub struct AppAuthenticationTokenIssuedFfi {
    pub token: Vec<u8>,
    pub expires_at: Option<i64>,
}

impl From<AppAuthenticationTokenIssued> for AppAuthenticationTokenIssuedFfi {
    fn from(value: AppAuthenticationTokenIssued) -> Self {
        Self {
            token: value.token,
            expires_at: value.expires_at.map(|v| v.0),
        }
    }
}

#[derive(uniffi::Record, Serialize, Deserialize, Clone, Debug)]
pub struct AppAuthFfi {
    pub authentication: AppAuthenticationTokenIssuedFfi,
    pub port: u16,
}

#[derive(uniffi::Record, Serialize, Deserialize, Clone, Debug)]
pub struct CellIdFfi {
    pub dna_hash: Vec<u8>,
    pub agent_pub_key: Vec<u8>,
}

impl From<CellId> for CellIdFfi {
    fn from(value: CellId) -> Self {
        Self {
            dna_hash: value.dna_hash().get_raw_39().to_vec(),
            agent_pub_key: value.agent_pubkey().get_raw_39().to_vec(),
        }
    }
}

impl From<CellIdFfi> for CellId {
    fn from(val: CellIdFfi) -> Self {
        CellId::new(
            HoloHash::<Dna>::from_raw_39(val.dna_hash),
            HoloHash::<Agent>::from_raw_39(val.agent_pub_key),
        )
    }
}

#[derive(uniffi::Record)]
pub struct ZomeCallParamsFfi {
    pub provenance: Vec<u8>,
    pub cell_id: CellIdFfi,
    pub zome_name: String,
    pub fn_name: String,
    pub cap_secret: Option<Vec<u8>>,
    pub payload: Vec<u8>,
    pub nonce: Vec<u8>,
    pub expires_at: i64,
}

impl From<ZomeCallParams> for ZomeCallParamsFfi {
    fn from(value: ZomeCallParams) -> Self {
        Self {
            provenance: value.provenance.get_raw_39().to_vec(),
            cell_id: value.cell_id.into(),
            zome_name: value.zome_name.0.to_string(),
            fn_name: value.fn_name.into(),
            cap_secret: value.cap_secret.map(|s| s.as_ref().to_vec()),
            payload: value.payload.into(),
            nonce: value.nonce.into_inner().to_vec(),
            expires_at: value.expires_at.0,
        }
    }
}

impl From<ZomeCallParamsFfi> for ZomeCallParams {
    fn from(val: ZomeCallParamsFfi) -> Self {
        let nonce: [u8; 32] = val.nonce.as_slice().try_into().unwrap();
        let cap_secret: Option<[u8; 64]> = val.cap_secret.map(|s| s.as_slice().try_into().unwrap());

        ZomeCallParams {
            provenance: HoloHash::<Agent>::from_raw_39(val.provenance),
            cell_id: val.cell_id.into(),
            zome_name: ZomeName::new(val.zome_name),
            fn_name: FunctionName::new(val.fn_name),
            cap_secret: cap_secret.map(CapSecret::from),
            payload: ExternIO::from(val.payload),
            nonce: Nonce256Bits::from(nonce),
            expires_at: Timestamp(val.expires_at),
        }
    }
}

#[derive(uniffi::Record)]
pub struct ZomeCallParamsSignedFfi {
    pub bytes: Vec<u8>,
    pub signature: Vec<u8>,
}

impl From<ZomeCallParamsSigned> for ZomeCallParamsSignedFfi {
    fn from(value: ZomeCallParamsSigned) -> Self {
        Self {
            bytes: value.bytes.into(),
            signature: value.signature.0.into(),
        }
    }
}

#[derive(uniffi::Enum, Serialize, Deserialize, Clone, Debug)]
pub enum RoleSettingsFfi {
    UseExisting {
        cell_id: CellIdFfi,
    },
    Provisioned {
        membrane_proof: Option<Vec<u8>>,
        modifiers: Option<DnaModifiersOptFfi>,
    },
}

impl From<RoleSettingsFfi> for RoleSettings {
    fn from(val: RoleSettingsFfi) -> Self {
        match val {
            RoleSettingsFfi::UseExisting { cell_id } => RoleSettings::UseExisting {
                cell_id: cell_id.into(),
            },
            RoleSettingsFfi::Provisioned {
                membrane_proof,
                modifiers,
            } => RoleSettings::Provisioned {
                membrane_proof: membrane_proof
                    .map(|p| std::sync::Arc::new(SerializedBytes::from(UnsafeBytes::from(p)))),
                modifiers: modifiers.map(|m| m.into()),
            },
        }
    }
}

#[derive(uniffi::Record)]
pub struct InstallAppPayloadFfi {
    /// Raw bytes of encoded AppBundle
    pub source: Vec<u8>,
    pub installed_app_id: String,
    pub network_seed: Option<String>,
    pub roles_settings: Option<HashMap<String, RoleSettingsFfi>>,
}

impl TryInto<InstallAppPayload> for InstallAppPayloadFfi {
    type Error = AppBundleError;
    fn try_into(self) -> Result<InstallAppPayload, Self::Error> {
        Ok(InstallAppPayload {
            source: AppBundleSource::Bytes(self.source),
            agent_key: None,
            installed_app_id: Some(self.installed_app_id),
            network_seed: self.network_seed,
            roles_settings: self
                .roles_settings
                .map(|r| r.into_iter().map(|(k, v)| (k, v.into())).collect()),
            ignore_genesis_failure: false,
            allow_throwaway_random_agent_key: false,
        })
    }
}

#[derive(uniffi::Record, Clone, Debug)]
pub struct RuntimeConfigFfi {
    /// Path where conductor data is stored
    pub data_root_path: String,

    /// Network config
    pub network: RuntimeNetworkConfigFfi,
}

#[derive(uniffi::Record, Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeNetworkConfigFfi {
    /// URL of the bootstrap server
    pub bootstrap_url: String,

    /// URL of the sbd server
    pub signal_url: String,

    /// URLs of ICE servers
    pub ice_urls: Vec<String>,
}

impl Default for RuntimeNetworkConfigFfi {
    fn default() -> Self {
        Self {
            bootstrap_url: "https://dev-test-bootstrap2.holochain.org".to_string(),
            signal_url: "wss://dev-test-bootstrap2.holochain.org".to_string(),
            ice_urls: vec![
                "stun:stun.cloudflare.com:3478".to_string(),
                "stun:stun.l.google.com:19302".to_string(),
            ],
        }
    }
}