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
use anyhow::{anyhow, Result};
use cid::Cid;
use libipld_cbor::DagCborCodec;
use ucan::crypto::KeyMaterial;
use crate::{
authority::Authorization,
data::{
AddressIpld, ChangelogIpld, CidKey, DelegationIpld, MapOperation, MemoIpld, RevocationIpld,
VersionedMapKey, VersionedMapValue,
},
};
use noosphere_storage::BlockStore;
#[derive(Debug)]
pub struct SphereRevision<S: BlockStore> {
pub store: S,
pub memo: MemoIpld,
}
impl<S: BlockStore> SphereRevision<S> {
pub async fn try_sign<Credential: KeyMaterial>(
&mut self,
credential: &Credential,
authorization: Option<&Authorization>,
) -> Result<Cid> {
self.memo.sign(credential, authorization).await?;
self.store.save::<DagCborCodec, _>(&self.memo).await
}
}
#[derive(Debug)]
pub struct SphereMutation {
did: String,
links: LinksMutation,
names: NamesMutation,
allowed_ucans: AllowedUcansMutation,
revoked_ucans: RevokedUcansMutation,
}
impl<'a> SphereMutation {
pub fn new(did: &str) -> Self {
SphereMutation {
did: did.into(),
links: LinksMutation::new(did),
names: NamesMutation::new(did),
allowed_ucans: AllowedUcansMutation::new(did),
revoked_ucans: RevokedUcansMutation::new(did),
}
}
pub fn did(&self) -> &str {
&self.did
}
pub fn links_mut(&mut self) -> &mut LinksMutation {
&mut self.links
}
pub fn links(&self) -> &LinksMutation {
&self.links
}
pub fn names_mut(&mut self) -> &mut NamesMutation {
&mut self.names
}
pub fn names(&self) -> &NamesMutation {
&self.names
}
pub fn allowed_ucans_mut(&mut self) -> &mut AllowedUcansMutation {
&mut self.allowed_ucans
}
pub fn allowed_ucans(&self) -> &AllowedUcansMutation {
&self.allowed_ucans
}
pub fn revoked_ucans_mut(&mut self) -> &mut RevokedUcansMutation {
&mut self.revoked_ucans
}
pub fn revoked_ucans(&self) -> &RevokedUcansMutation {
&self.revoked_ucans
}
pub fn is_empty(&self) -> bool {
self.links.changes.len() == 0
&& self.allowed_ucans.changes.len() == 0
&& self.revoked_ucans.changes.len() == 0
}
}
pub type LinksMutation = VersionedMapMutation<String, Cid>;
pub type NamesMutation = VersionedMapMutation<String, AddressIpld>;
pub type AllowedUcansMutation = VersionedMapMutation<CidKey, DelegationIpld>;
pub type RevokedUcansMutation = VersionedMapMutation<CidKey, RevocationIpld>;
#[derive(Default, Debug)]
pub struct VersionedMapMutation<K, V>
where
K: VersionedMapKey,
V: VersionedMapValue,
{
did: String,
changes: Vec<MapOperation<K, V>>,
}
impl<K, V> VersionedMapMutation<K, V>
where
K: VersionedMapKey,
V: VersionedMapValue,
{
pub fn try_apply_changelog(
&mut self,
changelog: &ChangelogIpld<MapOperation<K, V>>,
) -> Result<()> {
let did = changelog
.did
.as_ref()
.ok_or_else(|| anyhow!("Changelog did not have an author DID"))?;
if did != &self.did {
return Err(anyhow!(
"Changelog has unexpected author (was {}, expected {})",
did,
self.did
));
}
self.changes = changelog.changes.clone();
Ok(())
}
pub fn new(did: &str) -> Self {
VersionedMapMutation {
did: did.into(),
changes: Default::default(),
}
}
pub fn did(&self) -> &str {
&self.did
}
pub fn changes(&self) -> &[MapOperation<K, V>] {
&self.changes
}
pub fn set(&mut self, key: &K, value: &V) {
self.changes.push(MapOperation::Add {
key: key.clone(),
value: value.clone(),
});
}
pub fn remove(&mut self, key: &K) {
self.changes.push(MapOperation::Remove { key: key.clone() });
}
}