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
use super::CapSecret;
use crate::zome::FunctionName;
use crate::zome::ZomeName;
use holo_hash::*;
use holochain_serialized_bytes::SerializedBytes;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
use std::collections::HashSet;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum CapGrant {
ChainAuthor(AgentPubKey),
RemoteAgent(ZomeCallCapGrant),
}
impl From<holo_hash::AgentPubKey> for CapGrant {
fn from(agent_hash: holo_hash::AgentPubKey) -> Self {
CapGrant::ChainAuthor(agent_hash)
}
}
#[derive(Default, PartialEq, Eq, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CurryPayloads(pub BTreeMap<GrantedFunction, SerializedBytes>);
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct ZomeCallCapGrant {
pub tag: String,
pub access: CapAccess,
pub functions: GrantedFunctions,
}
impl ZomeCallCapGrant {
pub fn new(
tag: String,
access: CapAccess,
functions: GrantedFunctions,
) -> Self {
Self {
tag,
access,
functions,
}
}
}
impl From<ZomeCallCapGrant> for CapGrant {
fn from(zccg: ZomeCallCapGrant) -> Self {
CapGrant::RemoteAgent(zccg)
}
}
impl CapGrant {
pub fn is_valid(
&self,
check_function: &GrantedFunction,
check_agent: &AgentPubKey,
check_secret: Option<&CapSecret>,
) -> bool {
match self {
CapGrant::ChainAuthor(author) => author == check_agent,
CapGrant::RemoteAgent(ZomeCallCapGrant {
access, functions, ..
}) => {
functions.contains(check_function)
&& match access {
CapAccess::Assigned { assignees, .. } => assignees.contains(check_agent),
_ => true,
}
&& match access {
CapAccess::Unrestricted => true,
CapAccess::Transferable { secret, .. } => check_secret.map(|given| secret == given).unwrap_or(false),
CapAccess::Assigned { secret, .. } => check_secret.map(|given| secret == given).unwrap_or(false),
}
}
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum CapAccess {
Unrestricted,
Transferable {
secret: CapSecret,
},
Assigned {
secret: CapSecret,
assignees: HashSet<AgentPubKey>,
},
}
impl From<()> for CapAccess {
fn from(_: ()) -> Self {
Self::Unrestricted
}
}
impl From<CapSecret> for CapAccess {
fn from(secret: CapSecret) -> Self {
Self::Transferable { secret }
}
}
impl From<(CapSecret, HashSet<AgentPubKey>)> for CapAccess {
fn from((secret, assignees): (CapSecret, HashSet<AgentPubKey>)) -> Self {
Self::Assigned { secret, assignees }
}
}
impl From<(CapSecret, AgentPubKey)> for CapAccess {
fn from((secret, assignee): (CapSecret, AgentPubKey)) -> Self {
let mut assignees = HashSet::new();
assignees.insert(assignee);
Self::from((secret, assignees))
}
}
pub type GrantedFunction = (ZomeName, FunctionName);
pub type GrantedFunctions = HashSet<GrantedFunction>;