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
use core::fmt;
#[cfg(feature = "dev")]
use arbitrary::Arbitrary;
use ufotofu::codec_prelude::*;
use willow_data_model::prelude as wdm;
use crate::prelude::*;
wrapper! {
/// The metadata associated with each Willow [Payload](https://willowprotocol.org/specs/data-model/index.html#Payload) string.
///
/// [Entries](https://willowprotocol.org/specs/data-model/index.html#Entry) are the central concept in Willow. In order to make any bytestring of data accessible to Willow, you need to create an Entry describing its metadata. Specifically, an Entry consists of
///
/// - a [namespace_id](https://willowprotocol.org/specs/data-model/index.html#entry_namespace_id) (roughly, this addresses a universe of Willow data, fully independent from all data (i.e., Entries) of different namespace ids) of type [`NamespaceId`],
/// - a [subspace_id](https://willowprotocol.org/specs/data-model/index.html#entry_subspace_id) (roughly, a fully indendent part of a namespace, typically subspaces correspond to individual users) of type [`SubspaceId`],
/// - a [path](https://willowprotocol.org/specs/data-model/index.html#entry_path) (roughly, a file-system-like way of arranging payloads hierarchically within a subspace) of type [`Path`],
/// - a [timestamp](https://willowprotocol.org/specs/data-model/index.html#entry_timestamp) (newer Entries can overwrite certain older Entries) of type [`Timestamp`],
/// - a [payload_length](https://willowprotocol.org/specs/data-model/index.html#entry_payload_length) (the length of the payload string), and
/// - a [payload_digest](https://willowprotocol.org/specs/data-model/index.html#entry_payload_digest) (a secure hash of the payload string being inserted into Willow) of type [`PayloadDigest`].
///
/// To access these six fields, use the methods of the [`Entrylike`] trait (which [`Entry`] implements). The [`EntrylikeExt`] trait provides additional helper methods, for example, methods to check which Entries can delete which other Entries.
///
/// To create Entries, use the [`Entry::builder`] or [`Entry::prefilled_builder`] functions.
///
/// # Example
///
/// ```
/// use willow25::prelude::*;
///
/// let entry = Entry::builder()
/// .namespace_id([0; 32].into())
/// .subspace_id([1; 32].into())
/// .path(path!("/vacation/plan"))
/// .timestamp(12345)
/// .payload_digest([77; 32].into())
/// .payload_length(17)
/// .build().unwrap();
///
/// assert_eq!(*entry.subspace_id(), [1; 32].into());
///
/// let newer = Entry::prefilled_builder(&entry).timestamp(99999).build().unwrap();
/// assert!(newer.prunes(&entry));
/// ```
///
/// [Spec definition](https://willowprotocol.org/specs/data-model/index.html#Entry).
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
#[cfg_attr(feature = "dev", derive(Arbitrary))]
Entry; wdm::Entry<MCL, MCC, MPL, NamespaceId, SubspaceId, PayloadDigest>
}
impl fmt::Debug for Entry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Entry {
/// Creates a builder for [`Entry`].
///
/// # Examples
///
/// ```
/// use willow25::prelude::*;
///
/// // Supplying incomplete data errors.
/// assert!(
/// Entry::builder()
/// .path(path!(""))
/// .namespace_id([0; 32].into())
/// .subspace_id([1; 32].into())
/// .payload_digest([77; 32].into())
/// // timestamp and payload_length are missing!
/// .build().is_err()
/// );
///
/// // Supplying all necessary data yields an entry.
/// let entry = Entry::builder()
/// .namespace_id([0; 32].into())
/// .subspace_id([1; 32].into())
/// .path(path!(""))
/// .timestamp(12345)
/// .payload_digest([77; 32].into())
/// .payload_length(17)
/// .build().unwrap();
///
/// assert_eq!(*entry.subspace_id(), [1; 32].into());
/// ```
pub fn builder() -> EntryBuilder {
wdm::Entry::builder().into()
}
/// Creates a builder which is prefilled with the data from some other entry.
///
/// Use this function to create modified copies of entries.
///
/// # Examples
///
/// ```
/// use willow25::prelude::*;
///
/// // Supplying all necessary data yields an entry.
/// let first_entry = Entry::builder()
/// .namespace_id([0; 32].into())
/// .subspace_id([1; 32].into())
/// .path(path!(""))
/// .timestamp(12345)
/// .payload_digest([77; 32].into())
/// .payload_length(17)
/// .build().unwrap();
///
/// assert_eq!(*first_entry.payload_digest(), [77; 32].into());
///
/// let second_entry = Entry::prefilled_builder(&first_entry)
/// .timestamp(67890)
/// .payload_digest([78; 32].into())
/// .payload_length(4)
/// .build().unwrap();
///
/// assert_eq!(*second_entry.payload_digest(), [78; 32].into());
/// ```
pub fn prefilled_builder<E>(source: &E) -> EntryBuilder
where
E: Entrylike + ?Sized,
{
wdm::Entry::prefilled_builder(source).into()
}
/// Creates an [`Entry`] with [equal data](EntrylikeExt::entry_eq) to that of the given [`Entrylike`].
///
/// ```
/// # #[cfg(feature = "dev")] {
/// use willow_data_model::prelude::*;
/// use willow_data_model::test_parameters::*;
///
/// let entry1 = Entry::builder()
/// .namespace_id(Family)
/// .subspace_id(Alfie)
/// .path(Path::<4, 4, 4>::new())
/// .timestamp(12345)
/// .payload_digest(Spades)
/// .payload_length(2)
/// .build().unwrap();
///
/// let entry2 = Entry::from_entrylike(&entry1);
/// assert_eq!(entry1, entry2);
/// # }
/// ```
pub fn from_entrylike<E>(entrylike_to_clone: &E) -> Self
where
E: Entrylike + ?Sized,
{
Self::prefilled_builder(entrylike_to_clone).build().unwrap()
}
/// Turns `self` into an [`AuthorisedEntry`] by creating an authorisation token for it.
///
/// ```
/// use rand::rngs::OsRng;
/// use willow25::prelude::*;
/// use willow25::authorisation::PossiblyAuthorisedEntry;
///
/// # #[cfg(feature = "dev")] {
/// let mut csprng = OsRng;
/// let (subspace_id, secret) = randomly_generate_subspace(&mut csprng);
/// let namespace_id = NamespaceId::from_bytes(&[17; 32]);
///
/// let entry = Entry::builder()
/// .namespace_id(namespace_id.clone())
/// .subspace_id(subspace_id.clone())
/// .path(path!("/ideas"))
/// .timestamp(12345)
/// .payload(b"chocolate with mustard")
/// .build().unwrap();
///
/// let mut cap = WriteCapability::new_communal(
/// namespace_id.clone(),
/// subspace_id.clone(),
/// );
///
/// let authed = entry.into_authorised_entry(&cap, &secret);
/// assert!(authed.is_ok());
/// # }
/// ```
pub fn into_authorised_entry(
self,
write_capability: &WriteCapability,
secret: &SubspaceSecret,
) -> Result<AuthorisedEntry, DoesNotAuthorise> {
let authorisation_token =
AuthorisationToken::new_for_entry(&self, write_capability, secret)?;
Ok(crate::authorisation::PossiblyAuthorisedEntry::new(
self,
authorisation_token,
)
.into_authorised_entry().expect("AuthorisationToken::new_for_entry must produce an authorisation token that authorises the entry"))
}
}
impl wdm::Keylike<MCL, MCC, MPL, SubspaceId> for Entry {
fn wdm_subspace_id(&self) -> &SubspaceId {
wdm::Keylike::wdm_subspace_id(&self.0)
}
fn wdm_path(&self) -> &wdm::Path<MCL, MCC, MPL> {
wdm::Keylike::wdm_path(&self.0)
}
}
impl wdm::Coordinatelike<MCL, MCC, MPL, SubspaceId> for Entry {
fn wdm_timestamp(&self) -> Timestamp {
wdm::Coordinatelike::wdm_timestamp(&self.0)
}
}
impl wdm::Namespaced<NamespaceId> for Entry {
fn wdm_namespace_id(&self) -> &NamespaceId {
self.0.wdm_namespace_id()
}
}
impl wdm::Entrylike<MCL, MCC, MPL, NamespaceId, SubspaceId, PayloadDigest> for Entry {
fn wdm_payload_length(&self) -> u64 {
self.0.wdm_payload_length()
}
fn wdm_payload_digest(&self) -> &PayloadDigest {
self.0.wdm_payload_digest()
}
}
/// Implements [encode_entry](https://willowprotocol.org/specs/encodings/index.html#encode_entry).
impl Encodable for Entry {
async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
where
C: BulkConsumer<Item = u8> + ?Sized,
{
self.0.encode(consumer).await
}
}
/// Implements [encode_entry](https://willowprotocol.org/specs/encodings/index.html#encode_entry).
impl EncodableKnownLength for Entry {
fn len_of_encoding(&self) -> usize {
self.0.len_of_encoding()
}
}
/// Implements [EncodeEntry](https://willowprotocol.org/specs/encodings/index.html#EncodeEntry).
impl Decodable for Entry {
type ErrorReason = Blame;
async fn decode<P>(
producer: &mut P,
) -> Result<Self, DecodeError<P::Final, P::Error, Self::ErrorReason>>
where
P: BulkProducer<Item = u8> + ?Sized,
Self: Sized,
{
wdm::Entry::<MCL, MCC, MPL, NamespaceId, SubspaceId, PayloadDigest>::decode(producer)
.await
.map(Into::into)
}
}
/// Implements [encode_entry](https://willowprotocol.org/specs/encodings/index.html#encode_entry).
impl DecodableCanonic for Entry {
type ErrorCanonic = Blame;
async fn decode_canonic<P>(
producer: &mut P,
) -> Result<Self, DecodeError<P::Final, P::Error, Self::ErrorCanonic>>
where
P: BulkProducer<Item = u8> + ?Sized,
Self: Sized,
{
wdm::Entry::<MCL, MCC, MPL, NamespaceId, SubspaceId, PayloadDigest>::decode_canonic(
producer,
)
.await
.map(Into::into)
}
}