-
Notifications
You must be signed in to change notification settings - Fork 85
/
update.rs
407 lines (346 loc) · 12.9 KB
/
update.rs
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
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use crypto::signatures::ed25519;
use identity_core::common::Fragment;
use identity_core::common::Object;
use identity_core::common::Timestamp;
use identity_core::crypto::KeyPair;
use identity_core::crypto::KeyType;
use identity_core::crypto::PublicKey;
use identity_did::did::CoreDIDUrl;
use identity_did::did::DID;
use identity_did::service::Service;
use identity_did::service::ServiceEndpoint;
use identity_did::verification::MethodRelationship;
use identity_did::verification::MethodScope;
use identity_did::verification::MethodType;
use identity_iota::did::IotaDID;
use identity_iota::did::IotaDIDUrl;
use identity_iota::document::IotaDocument;
use identity_iota::document::IotaVerificationMethod;
use identity_iota::tangle::NetworkName;
use crate::account::Account;
use crate::error::Result;
use crate::identity::DIDLease;
use crate::identity::IdentitySetup;
use crate::identity::IdentityState;
use crate::storage::Storage;
use crate::types::Generation;
use crate::types::KeyLocation;
use crate::types::MethodSecret;
use crate::updates::UpdateError;
pub const DEFAULT_UPDATE_METHOD_PREFIX: &str = "sign-";
pub(crate) async fn create_identity(
setup: IdentitySetup,
network: NetworkName,
store: &dyn Storage,
) -> Result<(DIDLease, IdentityState)> {
let method_type = match setup.key_type {
KeyType::Ed25519 => MethodType::Ed25519VerificationKey2018,
};
// The method type must be able to sign document updates.
ensure!(
IotaDocument::is_signing_method_type(method_type),
UpdateError::InvalidMethodType(method_type)
);
let generation = Generation::new();
let fragment: String = format!("{}{}", DEFAULT_UPDATE_METHOD_PREFIX, generation.to_u32());
let location: KeyLocation = KeyLocation::new(method_type, fragment, generation);
let keypair: KeyPair = if let Some(MethodSecret::Ed25519(private_key)) = &setup.method_secret {
ensure!(
private_key.as_ref().len() == ed25519::SECRET_KEY_LENGTH,
UpdateError::InvalidMethodSecret(format!(
"an ed25519 private key requires {} bytes, found {}",
ed25519::SECRET_KEY_LENGTH,
private_key.as_ref().len()
))
);
KeyPair::try_from_ed25519_bytes(private_key.as_ref())?
} else {
KeyPair::new_ed25519()?
};
// Generate a new DID from the public key
let did: IotaDID = IotaDID::new_with_network(keypair.public().as_ref(), network)?;
ensure!(
!store.key_exists(&did, &location).await?,
UpdateError::DocumentAlreadyExists
);
let did_lease = store.lease_did(&did).await?;
let private_key = keypair.private().to_owned();
std::mem::drop(keypair);
let public: PublicKey =
insert_method_secret(store, &did, &location, method_type, MethodSecret::Ed25519(private_key)).await?;
let method_fragment = location.fragment().to_owned();
let method: IotaVerificationMethod =
IotaVerificationMethod::from_did(did, setup.key_type, &public, method_fragment.name())?;
let document = IotaDocument::from_verification_method(method)?;
let mut state = IdentityState::new(document);
// Store the generations at which the method was added
state.store_method_generations(method_fragment);
Ok((did_lease, state))
}
#[derive(Clone, Debug)]
pub(crate) enum Update {
CreateMethod {
scope: MethodScope,
type_: MethodType,
fragment: String,
method_secret: Option<MethodSecret>,
},
DeleteMethod {
fragment: String,
},
AttachMethodRelationship {
fragment: String,
relationships: Vec<MethodRelationship>,
},
DetachMethodRelationship {
fragment: String,
relationships: Vec<MethodRelationship>,
},
CreateService {
fragment: String,
type_: String,
endpoint: ServiceEndpoint,
properties: Option<Object>,
},
DeleteService {
fragment: String,
},
}
impl Update {
pub(crate) async fn process(self, did: &IotaDID, state: &mut IdentityState, storage: &dyn Storage) -> Result<()> {
debug!("[Update::process] Update = {:?}", self);
trace!("[Update::process] State = {:?}", state);
trace!("[Update::process] Store = {:?}", storage);
match self {
Self::CreateMethod {
type_,
scope,
fragment,
method_secret,
} => {
let location: KeyLocation = state.key_location(type_, fragment)?;
// The key location must be available.
// TODO: config: strict
ensure!(
!storage.key_exists(did, &location).await?,
UpdateError::DuplicateKeyLocation(location)
);
let method_url: CoreDIDUrl = did.as_ref().to_url().join(location.fragment().identifier())?;
if state.document().resolve_method(method_url).is_some() {
return Err(crate::Error::DIDError(identity_did::Error::MethodAlreadyExists));
}
let public: PublicKey = if let Some(method_private_key) = method_secret {
insert_method_secret(storage, did, &location, type_, method_private_key).await
} else {
storage.key_new(did, &location).await
}?;
let method: IotaVerificationMethod =
IotaVerificationMethod::from_did(did.to_owned(), KeyType::Ed25519, &public, location.fragment().name())?;
state.store_method_generations(location.fragment().clone());
state.document_mut().insert_method(method, scope)?;
}
Self::DeleteMethod { fragment } => {
let fragment: Fragment = Fragment::new(fragment);
let method_url: IotaDIDUrl = did.to_url().join(fragment.identifier())?;
let core_method_url: CoreDIDUrl = CoreDIDUrl::from(method_url.clone());
// Prevent deleting the last method capable of signing the DID document.
let capability_invocation_set = state.document().core_document().capability_invocation();
let is_capability_invocation = capability_invocation_set
.iter()
.any(|method_ref| method_ref.id() == &core_method_url);
ensure!(
!(is_capability_invocation && capability_invocation_set.len() == 1),
UpdateError::InvalidMethodFragment("cannot remove last signing method")
);
state.document_mut().remove_method(method_url)?;
}
Self::AttachMethodRelationship {
fragment,
relationships,
} => {
let fragment: Fragment = Fragment::new(fragment);
let method_url: IotaDIDUrl = did.to_url().join(fragment.identifier())?;
for relationship in relationships {
// Ignore result: attaching is idempotent.
let _ = state
.document_mut()
.attach_method_relationship(method_url.clone(), relationship)?;
}
}
Self::DetachMethodRelationship {
fragment,
relationships,
} => {
let fragment: Fragment = Fragment::new(fragment);
let method_url: IotaDIDUrl = did.to_url().join(fragment.identifier())?;
let core_method_url: CoreDIDUrl = CoreDIDUrl::from(method_url.clone());
// Prevent detaching the last method capable of signing the DID document.
let capability_invocation_set = state.document().core_document().capability_invocation();
let is_capability_invocation = capability_invocation_set
.iter()
.any(|method_ref| method_ref.id() == &core_method_url);
ensure!(
!(is_capability_invocation && capability_invocation_set.len() == 1),
UpdateError::InvalidMethodFragment("cannot remove last signing method")
);
for relationship in relationships {
// Ignore result: detaching is idempotent.
let _ = state
.document_mut()
.detach_method_relationship(method_url.clone(), relationship)?;
}
}
Self::CreateService {
fragment,
type_,
endpoint,
properties,
} => {
let fragment = Fragment::new(fragment);
let did_url: CoreDIDUrl = did.as_ref().to_owned().join(fragment.identifier())?;
// The service must not exist
ensure!(
state.document().service().query(&did_url).is_none(),
UpdateError::DuplicateServiceFragment(fragment.name().to_owned()),
);
let service: Service = Service::builder(properties.unwrap_or_default())
.id(did_url)
.service_endpoint(endpoint)
.type_(type_)
.build()?;
state.document_mut().insert_service(service);
}
Self::DeleteService { fragment } => {
let fragment: Fragment = Fragment::new(fragment);
let service_url = did.to_url().join(fragment.identifier())?;
// The service must exist
ensure!(
state.document().service().query(&service_url).is_some(),
UpdateError::ServiceNotFound
);
state.document_mut().remove_service(service_url)?;
}
}
state.document_mut().metadata.updated = Timestamp::now_utc();
Ok(())
}
}
async fn insert_method_secret(
store: &dyn Storage,
did: &IotaDID,
location: &KeyLocation,
method_type: MethodType,
method_secret: MethodSecret,
) -> Result<PublicKey> {
match method_secret {
MethodSecret::Ed25519(private_key) => {
ensure!(
private_key.as_ref().len() == ed25519::SECRET_KEY_LENGTH,
UpdateError::InvalidMethodSecret(format!(
"an ed25519 private key requires {} bytes, found {}",
ed25519::SECRET_KEY_LENGTH,
private_key.as_ref().len()
))
);
ensure!(
matches!(method_type, MethodType::Ed25519VerificationKey2018),
UpdateError::InvalidMethodSecret(
"MethodType::Ed25519VerificationKey2018 can only be used with an ed25519 method secret".to_owned(),
)
);
store.key_insert(did, location, private_key).await
}
MethodSecret::MerkleKeyCollection(_) => {
ensure!(
matches!(method_type, MethodType::MerkleKeyCollection2021),
UpdateError::InvalidMethodSecret(
"MethodType::MerkleKeyCollection2021 can only be used with a MerkleKeyCollection method secret".to_owned(),
)
);
todo!("[Update::CreateMethod] Handle MerkleKeyCollection")
}
}
}
// =============================================================================
// Update Builders
// =============================================================================
impl_update_builder!(
/// Create a new method on an identity.
///
/// # Parameters
/// - `type_`: the type of the method, defaults to [`MethodType::Ed25519VerificationKey2018`].
/// - `scope`: the scope of the method, defaults to [`MethodScope::default`].
/// - `fragment`: the identifier of the method in the document, required.
/// - `method_secret`: the secret key to use for the method, optional. Will be generated when omitted.
CreateMethod {
@defaulte type_ MethodType = Ed25519VerificationKey2018,
@default scope MethodScope,
@required fragment String,
@optional method_secret MethodSecret
});
impl_update_builder!(
/// Delete a method on an identity.
///
/// # Parameters
/// - `fragment`: the identifier of the method in the document, required.
DeleteMethod {
@required fragment String,
});
impl_update_builder!(
/// Attach one or more verification relationships to a method on an identity.
///
/// # Parameters
/// - `relationships`: the relationships to add, defaults to an empty [`Vec`].
/// - `fragment`: the identifier of the method in the document, required.
AttachMethodRelationship {
@required fragment String,
@default relationships Vec<MethodRelationship>,
});
impl<'account> AttachMethodRelationshipBuilder<'account> {
#[must_use]
pub fn relationship(mut self, value: MethodRelationship) -> Self {
self.relationships.get_or_insert_with(Default::default).push(value);
self
}
}
impl_update_builder!(
/// Detaches one or more verification relationships from a method on an identity.
///
/// # Parameters
/// - `relationships`: the relationships to remove, defaults to an empty [`Vec`].
/// - `fragment`: the identifier of the method in the document, required.
DetachMethodRelationship {
@required fragment String,
@default relationships Vec<MethodRelationship>,
});
impl<'account> DetachMethodRelationshipBuilder<'account> {
#[must_use]
pub fn relationship(mut self, value: MethodRelationship) -> Self {
self.relationships.get_or_insert_with(Default::default).push(value);
self
}
}
impl_update_builder!(
/// Create a new service on an identity.
///
/// # Parameters
/// - `type_`: the type of the service, e.g. `"LinkedDomains"`, required.
/// - `fragment`: the identifier of the service in the document, required.
/// - `endpoint`: the `ServiceEndpoint` of the service, required.
/// - `properties`: additional properties of the service, optional.
CreateService {
@required fragment String,
@required type_ String,
@required endpoint ServiceEndpoint,
@optional properties Object,
});
impl_update_builder!(
/// Delete a service on an identity.
///
/// # Parameters
/// - `fragment`: the identifier of the service in the document, required.
DeleteService {
@required fragment String,
});