-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdid_document.rs
More file actions
416 lines (352 loc) · 14.7 KB
/
Copy pathdid_document.rs
File metadata and controls
416 lines (352 loc) · 14.7 KB
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
//! DID Document implementation
//!
//! Implements the W3C DID Document specification with Nexa-specific extensions.
//! A DID Document describes the public keys, services, and verification methods
//! associated with a DID. It is the machine-readable representation of an identity.
//!
//! # W3C DID Document Structure
//!
//! Per the W3C DID Core specification and Nexa-net IDENTITY_LAYER.md,
//! a DID Document contains:
//! - `@context`: W3C DID context + Nexa context URIs
//! - `id`: The DID identifier
//! - `controller`: The controller DID (typically self-controlled)
//! - `verificationMethod`: Ed25519 signing key + X25519 key agreement key
//! - `authentication`: Reference to signing verification method
//! - `keyAgreement`: X25519 key for Diffie-Hellman key exchange
//! - `service`: NexaProxyEndpoint service
use crate::error::Result;
use crate::identity::{Did, IdentityKeys};
use ed25519_dalek::VerifyingKey;
use serde::{Deserialize, Serialize};
/// DID Document as per W3C DID specification with Nexa extensions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DidDocument {
/// DID context URIs
#[serde(rename = "@context")]
pub context: Vec<String>,
/// DID identifier
pub id: String,
/// Controller DID (typically self-controlled)
pub controller: String,
/// Verification methods (Ed25519 signing + X25519 key agreement)
pub verification_method: Vec<VerificationMethod>,
/// Authentication method references
pub authentication: Vec<String>,
/// Key agreement methods (X25519 for DH key exchange)
pub key_agreement: Vec<VerificationMethod>,
/// Service endpoints
pub service: Vec<ServiceEndpoint>,
/// Created timestamp
pub created: String,
/// Updated timestamp
pub updated: String,
}
/// Verification method (public key entry)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationMethod {
/// Method ID (e.g., "did:nexa:abc#key-1")
pub id: String,
/// Key type (e.g., "Ed25519VerificationKey2020")
#[serde(rename = "type")]
pub key_type: String,
/// Controller DID
pub controller: String,
/// Public key in multibase format (z-prefix + base64)
#[serde(rename = "publicKeyMultibase")]
pub public_key_multibase: String,
}
/// Service endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceEndpoint {
/// Service ID (e.g., "did:nexa:abc#nexa-proxy")
pub id: String,
/// Service type
#[serde(rename = "type")]
pub service_type: String,
/// Service endpoint URL
pub service_endpoint: String,
}
impl DidDocument {
/// Create a new DID Document from a DID and Ed25519 public key
///
/// This creates a minimal DID Document with only the Ed25519 signing key.
/// Use `from_identity_keys()` for the full document with key agreement.
pub fn new(did: &Did, public_key: &VerifyingKey) -> Self {
let now = chrono::Utc::now().to_rfc3339();
let key_id = format!("{}#key-1", did.as_str());
Self {
context: vec![
"https://www.w3.org/ns/did/v1".to_string(),
"https://w3id.org/security/suites/ed25519-2020/v1".to_string(),
"https://nexa-net.org/ns/did/v1".to_string(),
],
id: did.as_str().to_string(),
controller: did.as_str().to_string(),
verification_method: vec![VerificationMethod {
id: key_id.clone(),
key_type: "Ed25519VerificationKey2020".to_string(),
controller: did.as_str().to_string(),
public_key_multibase: multibase_encode_ed25519(public_key.to_bytes()),
}],
authentication: vec![key_id],
key_agreement: vec![],
service: vec![],
created: now.clone(),
updated: now,
}
}
/// Create a full DID Document from IdentityKeys
///
/// This includes both the Ed25519 signing key and the X25519
/// key agreement key, as specified in IDENTITY_LAYER.md.
pub fn from_identity_keys(did: &Did, keys: &IdentityKeys) -> Self {
let now = chrono::Utc::now().to_rfc3339();
let did_str = did.as_str();
let signing_key_id = format!("{}#key-1", did_str);
let key_agreement_id = format!("{}#key-agreement-1", did_str);
Self {
context: vec![
"https://www.w3.org/ns/did/v1".to_string(),
"https://w3id.org/security/suites/ed25519-2020/v1".to_string(),
"https://nexa-net.org/ns/did/v1".to_string(),
],
id: did_str.to_string(),
controller: did_str.to_string(),
verification_method: vec![
VerificationMethod {
id: signing_key_id.clone(),
key_type: "Ed25519VerificationKey2020".to_string(),
controller: did_str.to_string(),
public_key_multibase: multibase_encode_ed25519(
keys.signing.public_key().to_bytes(),
),
},
VerificationMethod {
id: key_agreement_id.clone(),
key_type: "X25519KeyAgreementKey2020".to_string(),
controller: did_str.to_string(),
public_key_multibase: multibase_encode_x25519(*keys.key_agreement.public_key()),
},
],
authentication: vec![signing_key_id],
key_agreement: vec![VerificationMethod {
id: key_agreement_id,
key_type: "X25519KeyAgreementKey2020".to_string(),
controller: did_str.to_string(),
public_key_multibase: multibase_encode_x25519(*keys.key_agreement.public_key()),
}],
service: vec![],
created: now.clone(),
updated: now,
}
}
/// Add a NexaProxyEndpoint service
pub fn add_nexa_proxy_service(&mut self, endpoint: &str) {
let service_id = format!("{}#nexa-proxy", self.id);
self.service.push(ServiceEndpoint {
id: service_id,
service_type: "NexaProxyEndpoint".to_string(),
service_endpoint: endpoint.to_string(),
});
self.updated = chrono::Utc::now().to_rfc3339();
}
/// Add a generic service endpoint
pub fn add_service(&mut self, service_type: &str, endpoint: &str) {
let service_id = format!("{}#service-{}", self.id, self.service.len() + 1);
self.service.push(ServiceEndpoint {
id: service_id,
service_type: service_type.to_string(),
service_endpoint: endpoint.to_string(),
});
self.updated = chrono::Utc::now().to_rfc3339();
}
/// Get the Ed25519 verification method
pub fn signing_key_method(&self) -> Option<&VerificationMethod> {
self.verification_method
.iter()
.find(|m| m.key_type == "Ed25519VerificationKey2020")
}
/// Get the X25519 key agreement method
pub fn key_agreement_method(&self) -> Option<&VerificationMethod> {
self.key_agreement.first()
}
/// Serialize to JSON
pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string_pretty(self)?)
}
/// Deserialize from JSON
pub fn from_json(json: &str) -> Result<Self> {
Ok(serde_json::from_str(json)?)
}
}
/// Encode Ed25519 public key bytes as multibase (z-prefix + base64)
fn multibase_encode_ed25519(key_bytes: [u8; 32]) -> String {
format!(
"z{}",
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, key_bytes)
)
}
/// Encode X25519 public key bytes as multibase (z-prefix + base64)
fn multibase_encode_x25519(key_bytes: [u8; 32]) -> String {
format!(
"z{}",
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, key_bytes)
)
}
#[cfg(test)]
mod tests {
use super::*;
use ed25519_dalek::SigningKey;
use rand::rngs::OsRng;
#[test]
fn test_did_document_creation() {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = signing_key.verifying_key();
let did = Did::from_public_key(&verifying_key);
let doc = DidDocument::new(&did, &verifying_key);
assert_eq!(doc.id, did.as_str());
assert_eq!(doc.controller, did.as_str());
assert!(!doc.verification_method.is_empty());
assert!(!doc.authentication.is_empty());
assert_eq!(doc.context.len(), 3);
}
#[test]
fn test_did_document_from_identity_keys() {
let keys = IdentityKeys::generate().unwrap();
let did = Did::from_public_key(keys.signing.public_key().inner());
let doc = DidDocument::from_identity_keys(&did, &keys);
assert_eq!(doc.id, did.as_str());
assert_eq!(doc.controller, did.as_str());
// Should have 2 verification methods: Ed25519 + X25519
assert_eq!(doc.verification_method.len(), 2);
assert!(!doc.authentication.is_empty());
assert!(!doc.key_agreement.is_empty());
// Check signing key method
let signing_method = doc.signing_key_method().unwrap();
assert_eq!(signing_method.key_type, "Ed25519VerificationKey2020");
// Check key agreement method
let ka_method = doc.key_agreement_method().unwrap();
assert_eq!(ka_method.key_type, "X25519KeyAgreementKey2020");
}
#[test]
fn test_did_document_json_roundtrip() {
let keys = IdentityKeys::generate().unwrap();
let did = Did::from_public_key(keys.signing.public_key().inner());
let doc = DidDocument::from_identity_keys(&did, &keys);
let json = doc.to_json().unwrap();
let parsed = DidDocument::from_json(&json).unwrap();
assert_eq!(parsed.id, doc.id);
assert_eq!(parsed.controller, doc.controller);
assert_eq!(
parsed.verification_method.len(),
doc.verification_method.len()
);
assert_eq!(parsed.key_agreement.len(), doc.key_agreement.len());
}
#[test]
fn test_add_nexa_proxy_service() {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = signing_key.verifying_key();
let did = Did::from_public_key(&verifying_key);
let mut doc = DidDocument::new(&did, &verifying_key);
doc.add_nexa_proxy_service("https://proxy.nexa.net:7070");
assert_eq!(doc.service.len(), 1);
assert_eq!(doc.service[0].service_type, "NexaProxyEndpoint");
assert_eq!(
doc.service[0].service_endpoint,
"https://proxy.nexa.net:7070"
);
}
#[test]
fn test_multibase_encoding() {
let test_bytes: [u8; 32] = [
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,
];
let encoded = multibase_encode_ed25519(test_bytes);
assert!(encoded.starts_with("z"));
// The encoded part after 'z' should be valid base64
let decoded =
base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &encoded[1..])
.unwrap();
assert_eq!(decoded.len(), 32);
}
// ========== Boundary/Error Tests ==========
#[test]
fn test_did_document_minimal_has_no_key_agreement() {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = signing_key.verifying_key();
let did = Did::from_public_key(&verifying_key);
// DidDocument::new (minimal) has no key agreement
let doc = DidDocument::new(&did, &verifying_key);
assert!(doc.key_agreement.is_empty());
assert!(doc.key_agreement_method().is_none());
}
#[test]
fn test_did_document_empty_services_initially() {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = signing_key.verifying_key();
let did = Did::from_public_key(&verifying_key);
let doc = DidDocument::new(&did, &verifying_key);
assert!(doc.service.is_empty());
}
#[test]
fn test_add_generic_service() {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = signing_key.verifying_key();
let did = Did::from_public_key(&verifying_key);
let mut doc = DidDocument::new(&did, &verifying_key);
doc.add_service("MessagingService", "https://msg.nexa.net");
assert_eq!(doc.service.len(), 1);
assert_eq!(doc.service[0].service_type, "MessagingService");
assert_eq!(doc.service[0].service_endpoint, "https://msg.nexa.net");
}
#[test]
fn test_did_document_json_roundtrip_preserves_services() {
let keys = IdentityKeys::generate().unwrap();
let did = Did::from_public_key(keys.signing.public_key().inner());
let mut doc = DidDocument::from_identity_keys(&did, &keys);
doc.add_nexa_proxy_service("https://proxy.nexa.net:7070");
doc.add_service("MessagingService", "https://msg.nexa.net");
let json = doc.to_json().unwrap();
let parsed = DidDocument::from_json(&json).unwrap();
assert_eq!(parsed.service.len(), 2);
assert_eq!(parsed.service[0].service_type, "NexaProxyEndpoint");
assert_eq!(parsed.service[1].service_type, "MessagingService");
}
#[test]
fn test_did_document_invalid_json_deserialization() {
let result = DidDocument::from_json("{invalid json");
assert!(result.is_err());
}
#[test]
fn test_did_document_empty_json_deserialization() {
let result = DidDocument::from_json("{}");
// Empty JSON should still deserialize with defaults/nulls
// Required fields are missing, so this should fail
assert!(result.is_err() || result.unwrap().id.is_empty());
}
// ========== Proptest Tests ==========
use proptest::prelude::*;
proptest! {
/// DID Document JSON round-trip preserves id and controller
#[test]
fn proptest_did_document_json_roundtrip(
method_id in "[a-zA-Z0-9]{10,40}",
service_endpoint in "[a-zA-Z0-9:/._-]{5,50}",
) {
let keys = IdentityKeys::generate().unwrap();
let did = Did::from_public_key(keys.signing.public_key().inner());
let mut doc = DidDocument::from_identity_keys(&did, &keys);
doc.add_nexa_proxy_service(&service_endpoint);
let json = doc.to_json().unwrap();
let parsed = DidDocument::from_json(&json).unwrap();
assert_eq!(parsed.id, doc.id);
assert_eq!(parsed.controller, doc.controller);
assert_eq!(parsed.verification_method.len(), doc.verification_method.len());
assert_eq!(parsed.key_agreement.len(), doc.key_agreement.len());
assert_eq!(parsed.service.len(), doc.service.len());
}
}
}