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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Helper types for working with [`Op`]s

use crate::prelude::*;

#[cfg(test)]
mod test;

/// This trait provides a conversion to a convenience type [`OpType`]
/// for use in the validation call back.
///
/// Not all data is available in the [`OpType`]. This is why the [`Op`]
/// is not taken by value and can still be used after this conversion.
///
/// There is data that is common to all ops and can be accessed via helpers on
/// the op.
/// - Get the [`Op::author()`] of the op.
/// - Get the [`Op::timestamp()`] for when the op was created.
/// - Get the [`Op::action_seq()`] of the op.
/// - Get the [`Op::prev_action()`] of the op.
/// - Get the [`Op::action_type()`] of the op.
pub trait OpHelper {
    /// Converts an [`Op`] to an [`OpType`] without consuming it.
    /// This will clone the required internal data.
    fn to_type<ET, LT>(&self) -> Result<OpType<ET, LT>, WasmError>
    where
        ET: EntryTypesHelper + UnitEnum,
        <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
        LT: LinkTypesHelper,
        WasmError: From<<ET as EntryTypesHelper>::Error>,
        WasmError: From<<LT as LinkTypesHelper>::Error>;
}

#[derive(Debug)]
/// All the possible variants for entries
/// that are in scope for a zome.
enum InScopeEntry<ET>
where
    ET: UnitEnum,
{
    Agent(AgentPubKey),
    App(ET),
    PrivateApp(<ET as UnitEnum>::Unit),
    CapClaim,
    CapGrant,
}

/// [`RecordEntry`]s that takes a reference.
enum RecordEntryRef<'a> {
    Present(&'a Entry),
    Hidden,
    NotApplicable,
    NotStored,
}

/// All possible variants that an [`RegisterAgentActivity`]
/// with an [`Action`] that has an [`EntryType`] can produce.
#[derive(Debug)]
enum ActivityEntry<Unit> {
    App {
        entry_hash: EntryHash,
        entry_type: Option<Unit>,
    },
    PrivateApp {
        entry_hash: EntryHash,
        entry_type: Option<Unit>,
    },
    Agent(AgentPubKey),
    CapClaim(EntryHash),
    CapGrant(EntryHash),
}

impl OpHelper for Op {
    fn to_type<ET, LT>(&self) -> Result<OpType<ET, LT>, WasmError>
    where
        ET: EntryTypesHelper + UnitEnum,
        <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
        LT: LinkTypesHelper,
        WasmError: From<<ET as EntryTypesHelper>::Error>,
        WasmError: From<<LT as LinkTypesHelper>::Error>,
    {
        match self {
            Op::StoreRecord(StoreRecord { record }) => {
                let r = match record.action() {
                    Action::Dna(Dna { hash, .. }) => OpRecord::Dna(hash.clone()),
                    Action::AgentValidationPkg(AgentValidationPkg { membrane_proof, .. }) => {
                        OpRecord::AgentValidationPkg(membrane_proof.clone())
                    }
                    Action::InitZomesComplete(_) => OpRecord::InitZomesComplete,
                    Action::CreateLink(CreateLink {
                        zome_id,
                        link_type,
                        base_address,
                        target_address,
                        tag,
                        ..
                    }) => {
                        let link_type = in_scope_link_type(*zome_id, *link_type)?;
                        OpRecord::CreateLink {
                            base_address: base_address.clone(),
                            target_address: target_address.clone(),
                            tag: tag.clone(),
                            link_type,
                        }
                    }
                    Action::DeleteLink(DeleteLink {
                        link_add_address, ..
                    }) => OpRecord::DeleteLink(link_add_address.clone()),
                    Action::OpenChain(OpenChain { prev_dna_hash, .. }) => {
                        OpRecord::OpenChain(prev_dna_hash.clone())
                    }
                    Action::CloseChain(CloseChain { new_dna_hash, .. }) => {
                        OpRecord::CloseChain(new_dna_hash.clone())
                    }
                    Action::Create(Create {
                        entry_type,
                        entry_hash,
                        ..
                    }) => map_entry(entry_type, entry_hash, (&record.entry).into())?
                        .into_op_record(entry_hash)?,
                    Action::Update(Update {
                        entry_type,
                        entry_hash,
                        original_action_address: original_action_hash,
                        original_entry_address: original_entry_hash,
                        ..
                    }) => match map_entry::<ET>(entry_type, entry_hash, (&record.entry).into())? {
                        InScopeEntry::App(entry_type) => OpRecord::UpdateEntry {
                            entry_hash: entry_hash.clone(),
                            original_action_hash: original_action_hash.clone(),
                            original_entry_hash: original_entry_hash.clone(),
                            entry_type,
                        },
                        InScopeEntry::PrivateApp(entry_type) => OpRecord::UpdatePrivateEntry {
                            entry_hash: entry_hash.clone(),
                            original_action_hash: original_action_hash.clone(),
                            original_entry_hash: original_entry_hash.clone(),
                            entry_type,
                        },
                        InScopeEntry::Agent(new_key) => OpRecord::UpdateAgent {
                            original_key: original_entry_hash.clone().into(),
                            original_action_hash: original_action_hash.clone(),
                            new_key,
                        },
                        InScopeEntry::CapClaim => OpRecord::UpdateCapClaim {
                            entry_hash: entry_hash.clone(),
                            original_action_hash: original_action_hash.clone(),
                            original_entry_hash: original_entry_hash.clone(),
                        },
                        InScopeEntry::CapGrant => OpRecord::UpdateCapGrant {
                            entry_hash: entry_hash.clone(),
                            original_action_hash: original_action_hash.clone(),
                            original_entry_hash: original_entry_hash.clone(),
                        },
                    },
                    Action::Delete(Delete {
                        deletes_address,
                        deletes_entry_address,
                        ..
                    }) => OpRecord::DeleteEntry {
                        original_action_hash: deletes_address.clone(),
                        original_entry_hash: deletes_entry_address.clone(),
                    },
                };
                Ok(OpType::StoreRecord(r))
            }
            Op::StoreEntry(StoreEntry { action, entry }) => {
                let r = match &action.hashed.content {
                    EntryCreationAction::Create(Create {
                        entry_type,
                        entry_hash,
                        ..
                    }) => store_entry_create(entry_type, entry_hash, entry)?,
                    EntryCreationAction::Update(Update {
                        original_action_address: original_action_hash,
                        original_entry_address: original_entry_hash,
                        entry_type,
                        entry_hash,
                        ..
                    }) => match store_entry_create::<ET>(entry_type, entry_hash, entry)? {
                        OpEntry::CreateEntry {
                            entry_hash,
                            entry_type,
                        } => OpEntry::UpdateEntry {
                            entry_hash,
                            original_action_hash: original_action_hash.clone(),
                            original_entry_hash: original_entry_hash.clone(),
                            entry_type,
                        },
                        OpEntry::CreateAgent(new_key) => OpEntry::UpdateAgent {
                            original_key: original_entry_hash.clone().into(),
                            original_action_hash: original_action_hash.clone(),
                            new_key,
                        },
                        _ => unreachable!("This record is never created in this arm"),
                    },
                };
                Ok(OpType::StoreEntry(r))
            }
            Op::RegisterUpdate(RegisterUpdate {
                update,
                new_entry,
                original_entry,
                original_action,
            }) => {
                let Update {
                    original_action_address: original_action_hash,
                    original_entry_address: original_entry_hash,
                    entry_type,
                    entry_hash,
                    ..
                } = &update.hashed.content;
                if original_action.entry_type() != entry_type
                    && ((new_entry.is_some() && original_entry.is_some())
                        || (new_entry.is_none() && original_entry.is_none()))
                {
                    return Err(wasm_error!(WasmErrorInner::Guest(format!(
                        "New entry type {:?} doesn't match original entry type {:?}",
                        entry_type,
                        original_action.entry_type()
                    ))));
                }
                let new_entry = new_entry
                    .as_ref()
                    .map_or(RecordEntryRef::Hidden, RecordEntryRef::Present);
                let original_entry = original_entry
                    .as_ref()
                    .map_or(RecordEntryRef::Hidden, RecordEntryRef::Present);
                let r = match map_entry::<ET>(original_action.entry_type(), entry_hash, new_entry)?
                {
                    InScopeEntry::Agent(new_key) => Some(OpUpdate::Agent {
                        original_key: original_entry_hash.clone().into(),
                        original_action_hash: original_action_hash.clone(),
                        new_key,
                    }),
                    InScopeEntry::App(new_entry_type) => {
                        match map_entry::<ET>(entry_type, entry_hash, original_entry)? {
                            InScopeEntry::App(original_entry_type) => Some(OpUpdate::Entry {
                                entry_hash: entry_hash.clone(),
                                original_action_hash: original_action_hash.clone(),
                                original_entry_hash: original_entry_hash.clone(),
                                new_entry_type,
                                original_entry_type,
                            }),
                            _ => None,
                        }
                    }
                    InScopeEntry::PrivateApp(new_entry_type) => {
                        match map_entry::<ET>(entry_type, entry_hash, original_entry)? {
                            InScopeEntry::PrivateApp(original_entry_type) => {
                                Some(OpUpdate::PrivateEntry {
                                    entry_hash: entry_hash.clone(),
                                    original_action_hash: original_action_hash.clone(),
                                    original_entry_hash: original_entry_hash.clone(),
                                    new_entry_type,
                                    original_entry_type,
                                })
                            }
                            _ => None,
                        }
                    }
                    InScopeEntry::CapClaim => Some(OpUpdate::CapClaim {
                        entry_hash: entry_hash.clone(),
                        original_action_hash: original_action_hash.clone(),
                        original_entry_hash: original_entry_hash.clone(),
                    }),
                    InScopeEntry::CapGrant => Some(OpUpdate::CapGrant {
                        entry_hash: entry_hash.clone(),
                        original_action_hash: original_action_hash.clone(),
                        original_entry_hash: original_entry_hash.clone(),
                    }),
                };
                match r {
                    Some(r) => Ok(OpType::RegisterUpdate(r)),
                    None => unreachable!("As entry types are already checked to match"),
                }
            }
            Op::RegisterAgentActivity(RegisterAgentActivity { action, .. }) => {
                let r = match &action.hashed.content {
                    Action::Dna(Dna { hash, .. }) => OpActivity::Dna(hash.clone()),
                    Action::AgentValidationPkg(AgentValidationPkg { membrane_proof, .. }) => {
                        OpActivity::AgentValidationPkg(membrane_proof.clone())
                    }
                    Action::InitZomesComplete(_) => OpActivity::InitZomesComplete,
                    Action::OpenChain(OpenChain { prev_dna_hash, .. }) => {
                        OpActivity::OpenChain(prev_dna_hash.clone())
                    }
                    Action::CloseChain(CloseChain { new_dna_hash, .. }) => {
                        OpActivity::CloseChain(new_dna_hash.clone())
                    }
                    Action::CreateLink(CreateLink {
                        base_address,
                        target_address,
                        zome_id,
                        link_type,
                        tag,
                        ..
                    }) => {
                        let link_type = activity_link_type(*zome_id, *link_type)?;
                        OpActivity::CreateLink {
                            base_address: base_address.clone(),
                            target_address: target_address.clone(),
                            tag: tag.clone(),
                            link_type,
                        }
                    }
                    Action::DeleteLink(DeleteLink {
                        link_add_address, ..
                    }) => OpActivity::DeleteLink(link_add_address.clone()),
                    Action::Create(Create {
                        entry_type,
                        entry_hash,
                        ..
                    }) => activity_entry::<ET>(entry_type, entry_hash)?.into(),
                    Action::Update(Update {
                        original_action_address,
                        original_entry_address,
                        entry_type,
                        entry_hash,
                        ..
                    }) => match activity_entry::<ET>(entry_type, entry_hash)? {
                        ActivityEntry::App {
                            entry_hash,
                            entry_type,
                        } => OpActivity::UpdateEntry {
                            entry_hash,
                            original_action_hash: original_action_address.clone(),
                            original_entry_hash: original_entry_address.clone(),
                            entry_type,
                        },
                        ActivityEntry::PrivateApp {
                            entry_hash,
                            entry_type,
                        } => OpActivity::UpdatePrivateEntry {
                            entry_hash,
                            original_action_hash: original_action_address.clone(),
                            original_entry_hash: original_entry_address.clone(),
                            entry_type,
                        },
                        ActivityEntry::Agent(new_key) => OpActivity::UpdateAgent {
                            original_action_hash: original_action_address.clone(),
                            original_key: original_entry_address.clone().into(),
                            new_key,
                        },
                        ActivityEntry::CapClaim(entry_hash) => OpActivity::UpdateCapClaim {
                            entry_hash,
                            original_action_hash: original_action_address.clone(),
                            original_entry_hash: original_entry_address.clone(),
                        },
                        ActivityEntry::CapGrant(entry_hash) => OpActivity::UpdateCapGrant {
                            entry_hash,
                            original_action_hash: original_action_address.clone(),
                            original_entry_hash: original_entry_address.clone(),
                        },
                    },
                    Action::Delete(Delete {
                        deletes_address,
                        deletes_entry_address,
                        ..
                    }) => OpActivity::DeleteEntry {
                        original_action_hash: deletes_address.clone(),
                        original_entry_hash: deletes_entry_address.clone(),
                    },
                };
                Ok(OpType::RegisterAgentActivity(r))
            }
            Op::RegisterCreateLink(RegisterCreateLink { create_link }) => {
                let CreateLink {
                    base_address,
                    target_address,
                    zome_id,
                    link_type,
                    tag,
                    ..
                } = &create_link.hashed.content;
                let link_type = in_scope_link_type(*zome_id, *link_type)?;
                Ok(OpType::RegisterCreateLink {
                    base_address: base_address.clone(),
                    target_address: target_address.clone(),
                    tag: tag.clone(),
                    link_type,
                })
            }
            Op::RegisterDeleteLink(RegisterDeleteLink {
                delete_link,
                create_link,
            }) => {
                let CreateLink {
                    base_address,
                    target_address,
                    zome_id,
                    link_type,
                    tag,
                    ..
                } = create_link;
                let link_type = in_scope_link_type(*zome_id, *link_type)?;
                Ok(OpType::RegisterDeleteLink {
                    original_link_hash: delete_link.hashed.link_add_address.clone(),
                    base_address: base_address.clone(),
                    target_address: target_address.clone(),
                    tag: tag.clone(),
                    link_type,
                })
            }
            Op::RegisterDelete(RegisterDelete {
                delete,
                original_action,
                original_entry: orig_entry,
            }) => {
                let Delete {
                    deletes_address: original_action_hash,
                    deletes_entry_address: original_entry_hash,
                    ..
                } = &delete.hashed.content;
                let r = match map_entry::<ET>(
                    original_action.entry_type(),
                    original_entry_hash,
                    orig_entry
                        .as_ref()
                        .map_or(RecordEntryRef::Hidden, RecordEntryRef::Present),
                )? {
                    InScopeEntry::Agent(_) => OpDelete::Agent {
                        original_key: original_entry_hash.clone().into(),
                        original_action_hash: original_action_hash.clone(),
                    },
                    InScopeEntry::App(original_entry_type) => OpDelete::Entry {
                        original_action_hash: original_action_hash.clone(),
                        original_entry_hash: original_entry_hash.clone(),
                        original_entry_type,
                    },
                    InScopeEntry::PrivateApp(original_entry_type) => OpDelete::PrivateEntry {
                        original_action_hash: original_action_hash.clone(),
                        original_entry_hash: original_entry_hash.clone(),
                        original_entry_type,
                    },
                    InScopeEntry::CapClaim => OpDelete::CapClaim {
                        original_action_hash: original_action_hash.clone(),
                        original_entry_hash: original_entry_hash.clone(),
                    },
                    InScopeEntry::CapGrant => OpDelete::CapGrant {
                        original_action_hash: original_action_hash.clone(),
                        original_entry_hash: original_entry_hash.clone(),
                    },
                };
                Ok(OpType::RegisterDelete(r))
            }
        }
    }
}

fn store_entry_create<ET>(
    entry_type: &EntryType,
    entry_hash: &EntryHash,
    entry: &Entry,
) -> Result<OpEntry<ET>, WasmError>
where
    ET: EntryTypesHelper + UnitEnum,
    <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
    WasmError: From<<ET as EntryTypesHelper>::Error>,
{
    match map_entry::<ET>(entry_type, entry_hash, RecordEntryRef::Present(entry))? {
        InScopeEntry::App(entry_type) => Ok(OpEntry::CreateEntry {
            entry_hash: entry_hash.clone(),
            entry_type,
        }),
        InScopeEntry::Agent(agent_key) => Ok(OpEntry::CreateAgent(agent_key)),
        _ => Err(wasm_error!(WasmErrorInner::Guest(
            "StoreEntry should not exist for private entries Id".to_string()
        ))),
    }
}

/// Maps an entry type and entry to an
/// [`InScopeEntry`]. This will return a guest error
/// and invalidate the op if the zome id is this zome but
/// entry type is not in scope.
fn map_entry<ET>(
    entry_type: &EntryType,
    entry_hash: &EntryHash,
    entry: RecordEntryRef,
) -> Result<InScopeEntry<ET>, WasmError>
where
    ET: EntryTypesHelper + UnitEnum,
    <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
    WasmError: From<<ET as EntryTypesHelper>::Error>,
{
    match entry {
        RecordEntryRef::Present(entry) => match entry_type {
            EntryType::App(AppEntryType {
                zome_id,
                id: entry_def_index,
                visibility: EntryVisibility::Public,
                ..
            }) => {
                if !matches!(entry, Entry::App(_)) {
                    return Err(wasm_error!(WasmErrorInner::Guest(
                        "Entry type is App but Entry is not App".to_string()
                    )));
                }
                let entry_type = <ET as EntryTypesHelper>::deserialize_from_type(
                    *zome_id,
                    *entry_def_index,
                    entry,
                )?;
                match entry_type {
                    Some(entry_type) => Ok(InScopeEntry::App(entry_type)),
                    None => Err(deny_other_zome()),
                }
            }
            EntryType::AgentPubKey => {
                if !matches!(entry, Entry::Agent(_)) {
                    return Err(wasm_error!(WasmErrorInner::Guest(
                        "Entry type is AgentPubKey but Entry is not AgentPubKey".to_string()
                    )));
                }
                Ok(InScopeEntry::Agent(entry_hash.clone().into()))
            }
            _ => Err(wasm_error!(WasmErrorInner::Guest(
                "Entry type is a capability and should be private but there is an entry present"
                    .to_string()
            ))),
        },
        RecordEntryRef::Hidden => match entry_type {
            EntryType::App(AppEntryType {
                zome_id,
                id: entry_def_index,
                visibility: EntryVisibility::Private,
            }) => match get_unit_entry_type::<ET>(*zome_id, *entry_def_index)? {
                Some(unit) => Ok(InScopeEntry::PrivateApp(unit)),
                None => Err(deny_other_zome()),
            },
            EntryType::App(AppEntryType {
                visibility: EntryVisibility::Public,
                ..
            }) => Err(wasm_error!(WasmErrorInner::Guest(
                "Entry type is public but entry is hidden".to_string()
            ))),
            EntryType::CapClaim => Ok(InScopeEntry::CapClaim),
            EntryType::CapGrant => Ok(InScopeEntry::CapGrant),
            EntryType::AgentPubKey => Err(wasm_error!(WasmErrorInner::Guest(
                "Entry type AgentPubKey is missing entry.".to_string()
            ))),
        },
        RecordEntryRef::NotApplicable => Err(wasm_error!(WasmErrorInner::Guest(
            "Has Entry type but entry is marked not applicable".to_string()
        ))),
        RecordEntryRef::NotStored => match entry_type {
            EntryType::CapClaim | EntryType::CapGrant => Err(wasm_error!(WasmErrorInner::Guest(
                "Capability tokens are never publicly stored.".to_string()
            ))),
            _ => Err(wasm_error!(WasmErrorInner::Host(
                "Has Entry type but the entry is not currently stored.".to_string()
            ))),
        },
    }
}

/// Maps [`RegisterAgentActivity`] ops to their
/// entries. The entry type will be [`None`] if
/// the zome id is not a dependency of this zome.
fn activity_entry<ET>(
    entry_type: &EntryType,
    entry_hash: &EntryHash,
) -> Result<ActivityEntry<<ET as UnitEnum>::Unit>, WasmError>
where
    ET: UnitEnum,
    <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
{
    match entry_type {
        EntryType::App(AppEntryType {
            zome_id,
            id: entry_def_index,
            visibility,
        }) => {
            let unit = get_unit_entry_type::<ET>(*zome_id, *entry_def_index)?;
            match visibility {
                EntryVisibility::Public => Ok(ActivityEntry::App {
                    entry_hash: entry_hash.clone(),
                    entry_type: unit,
                }),
                EntryVisibility::Private => Ok(ActivityEntry::PrivateApp {
                    entry_hash: entry_hash.clone(),
                    entry_type: unit,
                }),
            }
        }
        EntryType::AgentPubKey => Ok(ActivityEntry::Agent(entry_hash.clone().into())),
        EntryType::CapClaim => Ok(ActivityEntry::CapClaim(entry_hash.clone())),
        EntryType::CapGrant => Ok(ActivityEntry::CapGrant(entry_hash.clone())),
    }
}

/// Get the app defined link type from a [`ZomeId`] and [`LinkType`].
/// If the [`ZomeId`] is not a dependency of this zome then return a host error.
fn in_scope_link_type<LT>(zome_id: ZomeId, link_type: LinkType) -> Result<LT, WasmError>
where
    LT: LinkTypesHelper,
    WasmError: From<<LT as LinkTypesHelper>::Error>,
{
    match <LT as LinkTypesHelper>::from_type(*zome_id, *link_type)? {
        Some(link_type) => Ok(link_type),
        None => Err(deny_other_zome()),
    }
}

/// Get the app defined link type from a [`ZomeId`] and [`LinkType`].
/// If the [`ZomeId`] is not a dependency of this zome then return a host error.
fn activity_link_type<LT>(zome_id: ZomeId, link_type: LinkType) -> Result<Option<LT>, WasmError>
where
    LT: LinkTypesHelper,
    WasmError: From<<LT as LinkTypesHelper>::Error>,
{
    Ok(<LT as LinkTypesHelper>::from_type(*zome_id, *link_type)?)
}

/// Produce the unit variant given a zome id and entry def index.
/// Returns [`None`] if the zome id is not a dependency of this zome.
/// Returns a [`WasmErrorInner::Guest`] error if the zome id is a
/// dependency but the [`EntryDefIndex`] is out of range.
fn get_unit_entry_type<ET>(
    zome_id: ZomeId,
    entry_def_index: EntryDefIndex,
) -> Result<Option<<ET as UnitEnum>::Unit>, WasmError>
where
    ET: UnitEnum,
    <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
{
    let entries = zome_info()?.zome_types.entries;
    let unit = entries.find(
        <ET as UnitEnum>::unit_iter(),
        ScopedEntryDefIndex {
            zome_id,
            zome_type: entry_def_index,
        },
    );
    let unit = match unit {
        Some(unit) => Some(unit),
        None => {
            if entries.dependencies().any(|z| z == zome_id) {
                return Err(wasm_error!(WasmErrorInner::Guest(format!(
                    "Entry type: {:?} is out of range for this zome.",
                    entry_def_index
                ))));
            } else {
                None
            }
        }
    };
    Ok(unit)
}

/// Produce an error because this zome
/// should never be called with a zome id
/// that is not a dependency.
fn deny_other_zome() -> WasmError {
    wasm_error!(WasmErrorInner::Host(
        "Op called for zome it was not defined in. This is a Holochain bug".to_string()
    ))
}

impl<ET> InScopeEntry<ET>
where
    ET: UnitEnum,
    <ET as UnitEnum>::Unit: Into<ZomeEntryTypesKey>,
{
    fn into_op_record<LT>(self, entry_hash: &EntryHash) -> Result<OpRecord<ET, LT>, WasmError>
    where
        LT: LinkTypesHelper,
    {
        match self {
            InScopeEntry::Agent(a) => Ok(OpRecord::CreateAgent(a)),
            InScopeEntry::App(entry_type) => Ok(OpRecord::CreateEntry {
                entry_hash: entry_hash.clone(),
                entry_type,
            }),
            InScopeEntry::PrivateApp(entry_type) => Ok(OpRecord::CreatePrivateEntry {
                entry_hash: entry_hash.clone(),
                entry_type,
            }),
            InScopeEntry::CapClaim => Ok(OpRecord::CreateCapClaim(entry_hash.clone())),
            InScopeEntry::CapGrant => Ok(OpRecord::CreateCapGrant(entry_hash.clone())),
        }
    }
}

impl<'a> From<&'a RecordEntry> for RecordEntryRef<'a> {
    fn from(r: &'a RecordEntry) -> Self {
        match r {
            RecordEntry::Present(e) => RecordEntryRef::Present(e),
            RecordEntry::Hidden => RecordEntryRef::Hidden,
            RecordEntry::NotApplicable => RecordEntryRef::NotApplicable,
            RecordEntry::NotStored => RecordEntryRef::NotStored,
        }
    }
}

impl<Unit, LT> From<ActivityEntry<Unit>> for OpActivity<Unit, LT> {
    fn from(e: ActivityEntry<Unit>) -> Self {
        match e {
            ActivityEntry::App {
                entry_hash,
                entry_type,
            } => OpActivity::CreateEntry {
                entry_hash,
                entry_type,
            },
            ActivityEntry::PrivateApp {
                entry_hash,
                entry_type,
            } => OpActivity::CreatePrivateEntry {
                entry_hash,
                entry_type,
            },
            ActivityEntry::Agent(key) => OpActivity::CreateAgent(key),
            ActivityEntry::CapClaim(hash) => OpActivity::CreateCapClaim(hash),
            ActivityEntry::CapGrant(hash) => OpActivity::CreateCapGrant(hash),
        }
    }
}