junobuild-shared 0.8.0

Shared utilities for Juno.
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
pub mod state {
    use crate::types::core::DomainName;
    use crate::types::monitoring::{CyclesBalance, FundingFailure};
    use candid::CandidType;
    use candid::Principal;
    use serde::{Deserialize, Serialize};
    use std::cmp::Ordering;
    use std::collections::HashMap;

    pub type UserId = Principal;

    pub type AccessKeyId = Principal;

    pub type SegmentId = Principal;
    pub type MissionControlId = SegmentId;
    pub type SatelliteId = SegmentId;
    pub type OrbiterId = SegmentId;

    pub type Metadata = HashMap<String, String>;

    pub type AccessKeys = HashMap<AccessKeyId, AccessKey>;

    pub type Timestamp = u64;

    pub type Version = u64;

    pub trait Timestamped {
        fn created_at(&self) -> Timestamp;
        fn updated_at(&self) -> Timestamp;
        fn cmp_updated_at(&self, other: &Self) -> Ordering;
        fn cmp_created_at(&self, other: &Self) -> Ordering;
    }

    pub trait Versioned {
        fn version(&self) -> Option<Version>;
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct AccessKey {
        pub metadata: Metadata,
        pub created_at: Timestamp,
        pub updated_at: Timestamp,
        pub expires_at: Option<Timestamp>,
        pub scope: AccessKeyScope,
        pub kind: Option<AccessKeyKind>,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum AccessKeyScope {
        Write,
        Admin,
        Submit,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum AccessKeyKind {
        Automation,
        Emulator,
    }

    #[derive(CandidType, Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub enum SegmentKind {
        Satellite,
        MissionControl,
        Orbiter,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct Segment {
        pub id: SegmentId,
        pub kind: SegmentKind,
        pub metadata: Option<Metadata>,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct OrbiterSatelliteConfig {
        pub features: Option<OrbiterSatelliteFeatures>,
        pub restricted_origin: Option<DomainName>,
        pub created_at: Timestamp,
        pub updated_at: Timestamp,
        pub version: Option<Version>,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct OrbiterSatelliteFeatures {
        pub page_views: bool,
        pub track_events: bool,
        pub performance_metrics: bool,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum NotificationKind {
        DepositedCyclesEmail(DepositedCyclesEmailNotification),
        FailedCyclesDepositEmail(FailedCyclesDepositEmailNotification),
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct DepositedCyclesEmailNotification {
        pub to: String,
        pub deposited_cycles: CyclesBalance,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct FailedCyclesDepositEmailNotification {
        pub to: String,
        pub funding_failure: FundingFailure,
    }
}

pub mod interface {
    use crate::mgmt::types::cmc::SubnetId;
    use crate::types::state::{
        AccessKeyId, AccessKeyKind, AccessKeyScope, Metadata, MissionControlId, NotificationKind,
        Segment, Timestamp, UserId,
    };
    use candid::{CandidType, Principal};
    use ic_ledger_types::BlockIndex;
    use serde::{Deserialize, Serialize};

    #[derive(CandidType, Deserialize)]
    pub struct CreateOrbiterArgs {
        pub user: UserId,
        pub block_index: Option<BlockIndex>,
        pub subnet_id: Option<SubnetId>,
        pub name: Option<String>,
    }

    #[derive(CandidType, Deserialize)]
    pub struct CreateMissionControlArgs {
        pub subnet_id: Option<SubnetId>,
    }

    #[derive(CandidType, Deserialize)]
    pub struct CreateSatelliteArgs {
        pub user: UserId,
        pub block_index: Option<BlockIndex>,
        pub subnet_id: Option<SubnetId>,
        pub storage: Option<InitStorageArgs>,
        pub name: Option<String>,
    }

    #[derive(CandidType, Deserialize)]
    pub struct GetCreateCanisterFeeArgs {
        pub user: UserId,
    }

    #[derive(CandidType, Deserialize)]
    pub struct InitMissionControlArgs {
        pub user: UserId,
    }

    #[derive(CandidType, Deserialize)]
    pub struct InitOrbiterArgs {
        pub controllers: Vec<AccessKeyId>,
    }

    #[derive(CandidType, Deserialize)]
    pub struct InitSatelliteArgs {
        pub controllers: Vec<AccessKeyId>,
        pub storage: Option<InitStorageArgs>,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct InitStorageArgs {
        pub system_memory: Option<InitStorageMemory>,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum InitStorageMemory {
        Heap,
        Stable,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct SetAccessKey {
        pub metadata: Metadata,
        pub expires_at: Option<Timestamp>,
        pub scope: AccessKeyScope,
        pub kind: Option<AccessKeyKind>,
    }

    #[derive(CandidType, Deserialize)]
    pub struct SetAccessKeysArgs {
        pub access_key_ids: Vec<AccessKeyId>,
        pub access_key: SetAccessKey,
    }

    #[deprecated(note = "use SetAccessKeysArgs instead")]
    #[derive(CandidType, Deserialize)]
    pub struct SetControllersArgs {
        pub controllers: Vec<AccessKeyId>,
        pub controller: SetAccessKey,
    }

    #[derive(CandidType, Deserialize)]
    pub struct DeleteAccessKeysArgs {
        pub access_key_ids: Vec<AccessKeyId>,
    }

    #[deprecated(note = "use DeleteAccessKeysArgs instead")]
    #[derive(CandidType, Deserialize)]
    pub struct DeleteControllersArgs {
        pub controllers: Vec<AccessKeyId>,
    }

    #[derive(CandidType, Deserialize)]
    pub struct AssertMissionControlCenterArgs {
        pub user: UserId,
        pub mission_control_id: MissionControlId,
    }

    #[derive(CandidType, Deserialize)]
    pub struct DepositCyclesArgs {
        pub destination_id: Principal,
        pub cycles: u128,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct MemorySize {
        pub heap: u64,
        pub stable: u64,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct NotifyArgs {
        pub user: UserId,
        pub segment: Segment,
        pub kind: NotificationKind,
    }
}

pub mod utils {
    use candid::CandidType;
    use serde::Deserialize;

    #[derive(Default, CandidType, Deserialize, Clone, PartialEq, Eq, Hash, Debug)]
    pub struct CalendarDate {
        pub year: i32,
        pub month: u8,
        pub day: u8,
    }
}

pub mod memory {
    use ic_stable_structures::memory_manager::VirtualMemory;
    use ic_stable_structures::DefaultMemoryImpl;

    pub type Memory = VirtualMemory<DefaultMemoryImpl>;
}

pub mod core {
    /// Represents a unique identifier or key.
    ///
    /// This type, `Key`, is an alias for `String`, used to represent unique identifiers or keys within the context
    /// of various data structures and operations.
    ///
    /// `Key` is commonly employed as a unique identifier or key in Rust code.
    pub type Key = String;

    /// Represents binary data as a vector of bytes.
    ///
    /// This type, `Blob`, is an alias for `Vec<u8>`, providing a convenient way to represent binary data
    /// as a collection of bytes.
    pub type Blob = Vec<u8>;

    /// Represents the domain name used in various configurations across the satellite.
    ///
    /// This type alias simplifies the reuse of `String` for domain names, providing a clear and
    /// specific semantic meaning when used in structs and function signatures. It is used to
    /// identify domains for authentication, custom domains, and potentially more areas where
    /// domain names are needed.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// let main_domain: junobuild_shared::types::core::DomainName = "example.com".to_string();
    /// ```
    pub type DomainName = String;

    /// Sha256 Digest: 32 bytes
    pub type Hash = [u8; 32];

    pub trait Hashable {
        fn hash(&self) -> Hash;
    }
}

pub mod list {
    use crate::types::core::Key;
    use crate::types::state::{Timestamp, UserId};
    use candid::CandidType;
    use serde::Deserialize;

    #[derive(Default, CandidType, Deserialize, Clone)]
    pub struct ListPaginate {
        pub start_after: Option<Key>,
        pub limit: Option<usize>,
    }

    #[derive(Default, CandidType, Deserialize, Clone)]
    pub enum ListOrderField {
        #[default]
        Keys,
        CreatedAt,
        UpdatedAt,
    }

    #[derive(Default, CandidType, Deserialize, Clone)]
    pub struct ListOrder {
        pub desc: bool,
        pub field: ListOrderField,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub enum TimestampMatcher {
        Equal(Timestamp),
        GreaterThan(Timestamp),
        LessThan(Timestamp),
        Between(Timestamp, Timestamp),
    }

    #[derive(Default, CandidType, Deserialize, Clone)]
    pub struct ListMatcher {
        pub key: Option<Key>,
        pub description: Option<String>,
        pub created_at: Option<TimestampMatcher>,
        pub updated_at: Option<TimestampMatcher>,
    }

    #[derive(Default, CandidType, Deserialize, Clone)]
    pub struct ListParams {
        pub matcher: Option<ListMatcher>,
        pub paginate: Option<ListPaginate>,
        pub order: Option<ListOrder>,
        pub owner: Option<UserId>,
    }

    #[derive(Default, CandidType, Deserialize, Clone)]
    pub struct ListResults<T> {
        pub items: Vec<(Key, T)>,
        pub items_length: usize,
        pub items_page: Option<usize>,
        pub matches_length: usize,
        pub matches_pages: Option<usize>,
    }
}

pub mod domain {
    use crate::types::core::DomainName;
    use crate::types::state::{Timestamp, Version};
    use candid::CandidType;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    pub type CustomDomains = HashMap<DomainName, CustomDomain>;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct CustomDomain {
        pub bn_id: Option<String>,
        pub created_at: Timestamp,
        pub updated_at: Timestamp,
        pub version: Option<Version>,
    }
}

pub mod config {
    use candid::{CandidType, Deserialize};
    use serde::Serialize;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct ConfigMaxMemorySize {
        pub heap: Option<usize>,
        pub stable: Option<usize>,
    }
}

pub mod monitoring {
    use crate::types::state::Timestamp;
    use candid::{CandidType, Deserialize};
    use serde::Serialize;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct CyclesBalance {
        pub amount: u128,
        pub timestamp: Timestamp,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum FundingErrorCode {
        InsufficientCycles, // Funding canister has insufficient cycles
        DepositFailed,      // The deposit of cycles failed
        ObtainCyclesFailed, // Obtaining cycles failed
        BalanceCheckFailed, // Fetching cycles balance failed
        Other(String),      // Other errors with a custom message
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct FundingFailure {
        pub error_code: FundingErrorCode,
        pub timestamp: Timestamp,
    }
}