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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
//! DID resolution and generation functionality for the TAP Agent.
//!
//! This module provides a multi-resolver for Decentralized Identifiers (DIDs)
//! that integrates with the didcomm library's DID resolution system. The multi-resolver
//! currently supports the did:key method, with the architecture allowing for additional
//! methods to be added in the future.
//!
//! It also provides functionality to generate new DIDs with different cryptographic curves.
use crate::key_manager::{Secret, SecretMaterial, SecretType};
#[cfg(not(target_arch = "wasm32"))]
use async_trait::async_trait;
use base64::Engine;
#[cfg(feature = "crypto-ed25519")]
use curve25519_dalek::edwards::CompressedEdwardsY;
#[cfg(feature = "crypto-ed25519")]
use ed25519_dalek::{SigningKey as Ed25519SigningKey, VerifyingKey as Ed25519VerifyingKey};
#[cfg(feature = "crypto-secp256k1")]
use k256::ecdsa::SigningKey as Secp256k1SigningKey;
use multibase::{decode, encode, Base};
#[cfg(feature = "crypto-p256")]
use p256::ecdsa::SigningKey as P256SigningKey;
#[cfg(any(
feature = "crypto-ed25519",
feature = "crypto-p256",
feature = "crypto-secp256k1"
))]
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::fmt::Debug;
#[cfg(not(target_arch = "wasm32"))]
use std::sync::{Arc, RwLock};
use tracing::debug;
#[cfg(not(target_arch = "wasm32"))]
use tracing::warn;
use crate::error::{Error, Result};
/// DID Document
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DIDDoc {
/// DID that this document describes
pub id: String,
/// List of verification methods
pub verification_method: Vec<VerificationMethod>,
/// List of authentication verification method references (id strings)
pub authentication: Vec<String>,
/// List of key agreement verification method references (id strings)
pub key_agreement: Vec<String>,
/// List of assertion method verification method references (id strings)
#[serde(default)]
pub assertion_method: Vec<String>,
/// List of capability invocation verification method references (id strings)
#[serde(default)]
pub capability_invocation: Vec<String>,
/// List of capability delegation verification method references (id strings)
#[serde(default)]
pub capability_delegation: Vec<String>,
/// List of services
pub service: Vec<Service>,
}
/// Service definition in a DID Document
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Service {
/// Service ID
pub id: String,
/// Service type
#[serde(rename = "type")]
pub type_: String,
/// Service endpoint URL
pub service_endpoint: String,
/// Additional properties
#[serde(flatten)]
pub properties: HashMap<String, Value>,
}
/// Verification method in a DID Document
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VerificationMethod {
/// Verification method ID
pub id: String,
/// Verification method type
#[serde(rename = "type")]
pub type_: VerificationMethodType,
/// Controller DID
pub controller: String,
/// Verification material
#[serde(flatten)]
pub verification_material: VerificationMaterial,
}
/// Verification method type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum VerificationMethodType {
/// Ed25519 Verification Key 2018
Ed25519VerificationKey2018,
/// X25519 Key Agreement Key 2019
X25519KeyAgreementKey2019,
/// ECDSA Secp256k1 Verification Key 2019
EcdsaSecp256k1VerificationKey2019,
/// JSON Web Key 2020
JsonWebKey2020,
}
/// Verification material
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum VerificationMaterial {
/// Base58 encoded public key
Base58 {
/// Public key encoded in base58
public_key_base58: String,
},
/// Multibase encoded public key
Multibase {
/// Public key encoded in multibase
public_key_multibase: String,
},
/// JSON Web Key
JWK {
/// Public key in JWK format
public_key_jwk: Value,
},
}
/// Key types supported for DID generation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyType {
/// Ed25519 key type (EdDSA)
#[cfg(feature = "crypto-ed25519")]
Ed25519,
/// P-256 key type (ECDSA secp256r1)
#[cfg(feature = "crypto-p256")]
P256,
/// Secp256k1 key type (ECDSA secp256k1)
#[cfg(feature = "crypto-secp256k1")]
Secp256k1,
}
/// Generated key information
#[derive(Debug, Clone)]
pub struct GeneratedKey {
/// The key type
pub key_type: KeyType,
/// The generated DID
pub did: String,
/// The public key in binary form
pub public_key: Vec<u8>,
/// The private key in binary form
pub private_key: Vec<u8>,
/// The DID document
pub did_doc: DIDDoc,
}
/// Options for generating a DID
#[derive(Debug, Clone)]
pub struct DIDGenerationOptions {
/// Key type to use
pub key_type: KeyType,
}
impl Default for DIDGenerationOptions {
fn default() -> Self {
Self {
#[cfg(feature = "crypto-ed25519")]
key_type: KeyType::Ed25519,
#[cfg(not(feature = "crypto-ed25519"))]
key_type: KeyType::P256, // Fallback if ed25519 is not enabled
}
}
}
/// A trait for resolving DIDs to DID documents that is Send+Sync.
/// This trait is only available in native builds.
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
pub trait SyncDIDResolver: Send + Sync + Debug {
/// Resolve a DID to a DID document.
///
/// # Parameters
/// * `did` - The DID to resolve
///
/// # Returns
/// The DID document as an Option
async fn resolve(&self, did: &str) -> Result<Option<DIDDoc>>;
}
/// A resolver for a specific DID method.
/// This trait is only available in native builds.
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
#[cfg(not(target_arch = "wasm32"))]
pub trait DIDMethodResolver: Send + Sync + Debug {
/// Returns the method name this resolver handles (e.g., "key", "web", "pkh").
fn method(&self) -> &str;
/// Resolve a DID to a DID document.
///
/// # Parameters
/// * `did` - The DID to resolve
///
/// # Returns
/// The DID document as an Option
async fn resolve_method(&self, did: &str) -> Result<Option<DIDDoc>>;
}
/// A simplified DID resolver for WebAssembly with no async or Send/Sync requirements.
#[cfg(target_arch = "wasm32")]
pub trait WasmDIDResolver: Debug {
/// Resolves a DID synchronously, returning the DID document.
fn resolve(&self, did: &str) -> Result<Option<DIDDoc>>;
}
/// A simplified method-specific DID resolver for WebAssembly.
#[cfg(target_arch = "wasm32")]
pub trait WasmDIDMethodResolver: Debug {
/// Returns the method name this resolver handles.
fn method(&self) -> &str;
/// Resolves a DID synchronously, returning the DID document.
fn resolve_method(&self, did: &str) -> Result<Option<DIDDoc>>;
/// Get this resolver as Any for downcasting
fn as_any(&self) -> &dyn std::any::Any;
}
/// A resolver for the did:key method.
#[derive(Debug, Default)]
pub struct KeyResolver;
impl KeyResolver {
/// Create a new KeyResolver
pub fn new() -> Self {
Self
}
/// Convert an Ed25519 public key to an X25519 public key
///
/// This follows the conversion process described in RFC 7748
/// https://datatracker.ietf.org/doc/html/rfc7748#section-5
#[cfg(feature = "crypto-ed25519")]
fn ed25519_to_x25519(ed25519_pubkey: &[u8]) -> Option<[u8; 32]> {
// The Ed25519 public key should be 32 bytes
if ed25519_pubkey.len() != 32 {
return None;
}
// Add debugging
debug!("Ed25519 pubkey: {:?}", ed25519_pubkey);
// Try to create a CompressedEdwardsY from the bytes
let edwards_y = match CompressedEdwardsY::try_from(ed25519_pubkey) {
Ok(point) => point,
Err(e) => {
debug!("Error converting to Compressed EdwardsY: {:?}", e);
return None;
}
};
// Try to decompress to get the Edwards point
let edwards_point = match edwards_y.decompress() {
Some(point) => point,
None => {
debug!("Failed to decompress Edwards point");
return None;
}
};
// Convert to Montgomery form
let montgomery_point = edwards_point.to_montgomery();
// Get the raw bytes representation of the X25519 key
Some(montgomery_point.to_bytes())
}
}
#[cfg(target_arch = "wasm32")]
impl WasmDIDMethodResolver for KeyResolver {
fn method(&self) -> &str {
"key"
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn resolve_method(&self, did_key: &str) -> Result<Option<DIDDoc>> {
// Same implementation but without async/await
// Validate that this is a did:key
if !did_key.starts_with("did:key:") {
return Ok(None);
}
// Parse the multibase-encoded public key
let key_id = &did_key[8..]; // Skip the "did:key:" prefix
let (_, key_bytes) = match decode(key_id) {
Ok(result) => result,
Err(_) => return Ok(None),
};
// Check the key prefix - for did:key only Ed25519 is supported
if key_bytes.len() < 2 {
return Ok(None);
}
// Verify the key type - 0xED01 for Ed25519
if key_bytes[0] != 0xED || key_bytes[1] != 0x01 {
return Ok(None);
}
// Create the DID Document with the Ed25519 public key
let ed25519_public_key = &key_bytes[2..];
let ed_vm_id = format!("{}#{}", did_key, key_id);
// Create the Ed25519 verification method
let ed_verification_method = VerificationMethod {
id: ed_vm_id.clone(),
type_: VerificationMethodType::Ed25519VerificationKey2018,
controller: did_key.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: key_id.to_string(),
},
};
// Convert the Ed25519 public key to X25519 for key agreement
let mut verification_methods = vec![ed_verification_method.clone()];
let mut key_agreement = Vec::new();
#[cfg(feature = "crypto-ed25519")]
if let Some(x25519_key) = Self::ed25519_to_x25519(ed25519_public_key) {
// Encode the X25519 public key in multibase format
let mut x25519_bytes = vec![0xEC, 0x01]; // Prefix for X25519
x25519_bytes.extend_from_slice(&x25519_key);
let x25519_multibase = encode(Base::Base58Btc, x25519_bytes);
// Create the X25519 verification method ID
let x25519_vm_id = format!("{}#{}", did_key, x25519_multibase);
// Create the X25519 verification method
let x25519_verification_method = VerificationMethod {
id: x25519_vm_id.clone(),
type_: VerificationMethodType::X25519KeyAgreementKey2019,
controller: did_key.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: x25519_multibase,
},
};
// Add the X25519 key agreement method
verification_methods.push(x25519_verification_method);
key_agreement.push(x25519_vm_id);
}
// Create the DID document
let did_doc = DIDDoc {
id: did_key.to_string(),
verification_method: verification_methods,
authentication: vec![ed_vm_id],
key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: Vec::new(),
};
Ok(Some(did_doc))
}
}
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl DIDMethodResolver for KeyResolver {
fn method(&self) -> &str {
"key"
}
async fn resolve_method(&self, did_key: &str) -> Result<Option<DIDDoc>> {
// Validate that this is a did:key
if !did_key.starts_with("did:key:") {
return Ok(None);
}
// Parse the multibase-encoded public key
let key_id = &did_key[8..]; // Skip the "did:key:" prefix
let (_, key_bytes) = match decode(key_id) {
Ok(result) => result,
Err(_) => return Ok(None),
};
// Check the key prefix - for did:key only Ed25519 is supported
if key_bytes.len() < 2 {
return Ok(None);
}
// Verify the key type - 0xED01 for Ed25519
if key_bytes[0] != 0xED || key_bytes[1] != 0x01 {
return Ok(None);
}
// Create the DID Document with the Ed25519 public key
let ed25519_public_key = &key_bytes[2..];
let ed_vm_id = format!("{}#{}", did_key, key_id);
// Create the Ed25519 verification method
let ed_verification_method = VerificationMethod {
id: ed_vm_id.clone(),
type_: VerificationMethodType::Ed25519VerificationKey2018,
controller: did_key.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: key_id.to_string(),
},
};
// Convert the Ed25519 public key to X25519 for key agreement
let mut verification_methods = vec![ed_verification_method.clone()];
let mut key_agreement = Vec::new();
#[cfg(feature = "crypto-ed25519")]
if let Some(x25519_key) = Self::ed25519_to_x25519(ed25519_public_key) {
debug!("Successfully converted Ed25519 to X25519!");
// Encode the X25519 public key in multibase format
let mut x25519_bytes = vec![0xEC, 0x01]; // Prefix for X25519
x25519_bytes.extend_from_slice(&x25519_key);
let x25519_multibase = encode(Base::Base58Btc, x25519_bytes);
// Create the X25519 verification method ID
let x25519_vm_id = format!("{}#{}", did_key, x25519_multibase);
// Create the X25519 verification method
let x25519_verification_method = VerificationMethod {
id: x25519_vm_id.clone(),
type_: VerificationMethodType::X25519KeyAgreementKey2019,
controller: did_key.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: x25519_multibase,
},
};
// Add the X25519 key agreement method
verification_methods.push(x25519_verification_method);
key_agreement.push(x25519_vm_id);
} else {
debug!("Failed to convert Ed25519 to X25519!");
}
// Create the DID document
let did_doc = DIDDoc {
id: did_key.to_string(),
verification_method: verification_methods,
authentication: vec![ed_vm_id],
key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: Vec::new(),
};
Ok(Some(did_doc))
}
}
/// A multi-resolver for DID methods. This resolver manages multiple
/// method-specific resolver. New resolvers can be added at runtime.
#[derive(Debug)]
#[cfg(not(target_arch = "wasm32"))]
pub struct MultiResolver {
resolvers: RwLock<HashMap<String, Arc<dyn DIDMethodResolver>>>,
}
#[cfg(not(target_arch = "wasm32"))]
unsafe impl Send for MultiResolver {}
#[cfg(not(target_arch = "wasm32"))]
unsafe impl Sync for MultiResolver {}
#[cfg(not(target_arch = "wasm32"))]
impl MultiResolver {
/// Create a new empty MultiResolver
pub fn new() -> Self {
Self {
resolvers: RwLock::new(HashMap::new()),
}
}
/// Create a new MultiResolver with a list of resolvers
pub fn new_with_resolvers(resolvers: Vec<Arc<dyn DIDMethodResolver>>) -> Self {
let resolver = Self::new();
// Add each resolver to the map if we can acquire the write lock
if let Ok(mut resolver_map) = resolver.resolvers.write() {
for r in resolvers {
let method = r.method().to_string();
resolver_map.insert(method, r);
}
}
resolver
}
/// Register a new resolver for a specific DID method
pub fn register_method<R>(&mut self, method: &str, resolver: R) -> &mut Self
where
R: DIDMethodResolver + Send + Sync + 'static,
{
if let Ok(mut resolvers) = self.resolvers.write() {
resolvers.insert(method.to_string(), Arc::new(resolver));
}
self
}
}
/// DID Web Resolver for resolving did:web identifiers
#[derive(Debug, Default)]
pub struct WebResolver;
impl WebResolver {
/// Create a new WebResolver
pub fn new() -> Self {
Self {}
}
}
#[cfg(target_arch = "wasm32")]
impl WasmDIDMethodResolver for WebResolver {
fn method(&self) -> &str {
"web"
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn resolve_method(&self, did: &str) -> Result<Option<DIDDoc>> {
// For WASM, return a simple placeholder DID document without actual resolution
// Because we lack the proper web-fetch capabilities at the moment
let parts: Vec<&str> = did.split(':').collect();
if parts.len() < 3 || parts[0] != "did" || parts[1] != "web" {
return Err(Error::InvalidDID);
}
// Create a minimal DID document for did:web
let verification_method = VerificationMethod {
id: format!("{}#keys-1", did),
type_: VerificationMethodType::Ed25519VerificationKey2018,
controller: did.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: "zMockPublicKey".to_string(),
},
};
let did_doc = DIDDoc {
id: did.to_string(),
verification_method: vec![verification_method.clone()],
authentication: vec![verification_method.id.clone()],
key_agreement: Vec::new(),
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: Vec::new(),
};
Ok(Some(did_doc))
}
}
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl DIDMethodResolver for WebResolver {
fn method(&self) -> &str {
"web"
}
async fn resolve_method(&self, did: &str) -> Result<Option<DIDDoc>> {
// Extract domain from did:web format
let parts: Vec<&str> = did.split(':').collect();
if parts.len() < 3 || parts[0] != "did" || parts[1] != "web" {
return Err(Error::InvalidDID);
}
// Extract the domain (and path if present)
let domain_path = parts[2..].join(":");
let domain_path = domain_path.replace("%3A", ":");
// Construct the URL to fetch the DID document
// did:web:example.com -> https://example.com/.well-known/did.json
// did:web:example.com:path:to:resource -> https://example.com/path/to/resource/did.json
let url = if domain_path.contains(":") {
// Convert additional colons to slashes for path components
let path_segments: Vec<&str> = domain_path.split(':').collect();
let domain = path_segments[0];
let path = path_segments[1..].join("/");
format!("https://{}/{}/did.json", domain, path)
} else {
// Standard case: did:web:example.com
format!("https://{}/.well-known/did.json", domain_path)
};
// Attempt to fetch and parse the DID document
#[cfg(feature = "native")]
{
use reqwest::Client;
let client = Client::new();
match client.get(&url).send().await {
Ok(response) => {
if response.status().is_success() {
match response.text().await {
Ok(text) => {
// First try normal parsing
let parse_result = serde_json::from_str::<DIDDoc>(&text);
match parse_result {
Ok(doc) => {
// Validate that the document ID matches the requested DID
if doc.id != did {
return Err(Error::DIDResolution(format!(
"DID Document ID ({}) does not match requested DID ({})",
doc.id, did
)));
}
Ok(Some(doc))
}
Err(parse_error) => {
// If normal parsing fails, try to parse as a generic JSON Value
// and manually construct a DIDDoc with the essential fields
match serde_json::from_str::<serde_json::Value>(&text) {
Ok(json_value) => {
let doc_id = match json_value.get("id") {
Some(id) => match id.as_str() {
Some(id_str) => id_str.to_string(),
None => return Err(Error::DIDResolution(
"DID Document has invalid 'id' field"
.to_string(),
)),
},
None => {
return Err(Error::DIDResolution(
"DID Document missing 'id' field"
.to_string(),
))
}
};
// Validate ID
if doc_id != did {
return Err(Error::DIDResolution(format!(
"DID Document ID ({}) does not match requested DID ({})",
doc_id, did
)));
}
// Try to extract verification methods and other fields
warn!("Using partial DID document parsing due to format issues");
warn!("Original parse error: {}", parse_error);
// Extract verification methods if present
// Create a longer-lived empty vec to handle the None case
let empty_vec = Vec::new();
let vm_array = json_value
.get("verificationMethod")
.and_then(|v| v.as_array())
.unwrap_or(&empty_vec);
// Attempt to parse each verification method
let mut verification_methods = Vec::new();
for vm_value in vm_array {
if let Ok(vm) = serde_json::from_value::<
VerificationMethod,
>(
vm_value.clone()
) {
verification_methods.push(vm);
}
}
// Extract authentication references
let authentication = json_value
.get("authentication")
.and_then(|v| v.as_array())
.unwrap_or(&empty_vec)
.iter()
.filter_map(|v| {
v.as_str().map(|s| s.to_string())
})
.collect();
// Extract key agreement references
let key_agreement = json_value
.get("keyAgreement")
.and_then(|v| v.as_array())
.unwrap_or(&empty_vec)
.iter()
.filter_map(|v| {
v.as_str().map(|s| s.to_string())
})
.collect();
// We'll create an empty services list for the DIDDoc
// But save service information separately for display purposes
let services = Vec::new();
// Extract raw service information for display
if let Some(svc_array) = json_value
.get("service")
.and_then(|v| v.as_array())
{
debug!("\nService endpoints (extracted from JSON):");
for (i, svc_value) in
svc_array.iter().enumerate()
{
if let (Some(id), Some(endpoint)) = (
svc_value
.get("id")
.and_then(|v| v.as_str()),
svc_value
.get("serviceEndpoint")
.and_then(|v| v.as_str()),
) {
let type_value = svc_value
.get("type")
.and_then(|v| v.as_str())
.unwrap_or("Unknown");
debug!(" [{}] ID: {}", i + 1, id);
debug!(" Type: {}", type_value);
debug!(" Endpoint: {}", endpoint);
}
}
}
// Create a simplified DID document with whatever we could extract
let simplified_doc = DIDDoc {
id: doc_id,
verification_method: verification_methods,
authentication,
key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: services,
};
Ok(Some(simplified_doc))
}
Err(_) => Err(Error::DIDResolution(format!(
"Failed to parse DID document from {}: {}",
url, parse_error
))),
}
}
}
}
Err(e) => Err(Error::DIDResolution(format!(
"Failed to read response body from {}: {}",
url, e
))),
}
} else if response.status().as_u16() == 404 {
// Not found is a valid response, just return None
Ok(None)
} else {
Err(Error::DIDResolution(format!(
"HTTP error fetching DID document from {}: {}",
url,
response.status()
)))
}
}
Err(e) => Err(Error::DIDResolution(format!(
"Failed to fetch DID document from {}: {}",
url, e
))),
}
}
#[cfg(target_arch = "wasm32")]
{
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
use web_sys::{Headers, Request, RequestInit, RequestMode, Response};
// Create request options
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
// Create the request
let request = match Request::new_with_str_and_init(&url, &opts) {
Ok(req) => req,
Err(e) => {
return Err(Error::DIDResolution(format!(
"Failed to create request for {}: {:?}",
url, e
)));
}
};
// Add Accept header
let headers = match Headers::new() {
Ok(h) => h,
Err(e) => {
return Err(Error::DIDResolution(format!(
"Failed to create headers: {:?}",
e
)));
}
};
if let Err(e) = headers.set("Accept", "application/json") {
return Err(Error::DIDResolution(format!(
"Failed to set Accept header: {:?}",
e
)));
}
if let Err(e) = request.headers().set("Accept", "application/json") {
return Err(Error::DIDResolution(format!(
"Failed to set Accept header: {:?}",
e
)));
}
// Get the window object
let window = match web_sys::window() {
Some(w) => w,
None => {
return Err(Error::DIDResolution(
"No window object available".to_string(),
));
}
};
// Send the request
let resp_value = match JsFuture::from(window.fetch_with_request(&request)).await {
Ok(response) => response,
Err(e) => {
return Err(Error::DIDResolution(format!(
"Failed to fetch DID document from {}: {:?}",
url, e
)));
}
};
// Convert response to Response object
let resp: Response = match resp_value.dyn_into() {
Ok(r) => r,
Err(_) => {
return Err(Error::DIDResolution(
"Failed to convert response".to_string(),
));
}
};
// Check if successful
if resp.ok() {
// Get the text content
let text_promise = match resp.text() {
Ok(t) => t,
Err(e) => {
return Err(Error::DIDResolution(format!(
"Failed to get text from response: {:?}",
e
)));
}
};
let text_jsval = match JsFuture::from(text_promise).await {
Ok(t) => t,
Err(e) => {
return Err(Error::DIDResolution(format!(
"Failed to await text promise: {:?}",
e
)));
}
};
let text = match text_jsval.as_string() {
Some(t) => t,
None => {
return Err(Error::DIDResolution("Response is not a string".to_string()));
}
};
// Parse the DID document
match serde_json::from_str::<DIDDoc>(&text) {
Ok(doc) => {
// Validate that the document ID matches the requested DID
if doc.id != did {
return Err(Error::DIDResolution(format!(
"DID Document ID ({}) does not match requested DID ({})",
doc.id, did
)));
}
Ok(Some(doc))
}
Err(parse_error) => {
// If normal parsing fails, try to parse as a generic JSON Value
// and manually construct a DIDDoc with the essential fields
match serde_json::from_str::<serde_json::Value>(&text) {
Ok(json_value) => {
let doc_id = match json_value.get("id") {
Some(id) => match id.as_str() {
Some(id_str) => id_str.to_string(),
None => {
return Err(Error::DIDResolution(
"DID Document has invalid 'id' field".to_string(),
))
}
},
None => {
return Err(Error::DIDResolution(
"DID Document missing 'id' field".to_string(),
))
}
};
// Validate ID
if doc_id != did {
return Err(Error::DIDResolution(format!(
"DID Document ID ({}) does not match requested DID ({})",
doc_id, did
)));
}
// Try to extract verification methods and other fields
web_sys::console::log_1(&JsValue::from_str(
&format!("WARNING: Using partial DID document parsing due to format issues\nOriginal parse error: {}", parse_error)
));
// Extract verification methods if present
// Create a longer-lived empty vec to handle the None case
let empty_vec = Vec::new();
let vm_array = json_value
.get("verificationMethod")
.and_then(|v| v.as_array())
.unwrap_or(&empty_vec);
// Attempt to parse each verification method
let mut verification_methods = Vec::new();
for vm_value in vm_array {
if let Ok(vm) = serde_json::from_value::<VerificationMethod>(
vm_value.clone(),
) {
verification_methods.push(vm);
}
}
// Extract authentication references
let authentication = json_value
.get("authentication")
.and_then(|v| v.as_array())
.unwrap_or(&empty_vec)
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
// Extract key agreement references
let key_agreement = json_value
.get("keyAgreement")
.and_then(|v| v.as_array())
.unwrap_or(&empty_vec)
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
// Create an empty services list for the DIDDoc
let services = Vec::new();
// Extract raw service information for console logging
if let Some(svc_array) =
json_value.get("service").and_then(|v| v.as_array())
{
web_sys::console::log_1(&JsValue::from_str(
"Service endpoints (extracted from JSON):",
));
for (i, svc_value) in svc_array.iter().enumerate() {
if let (Some(id), Some(endpoint)) = (
svc_value.get("id").and_then(|v| v.as_str()),
svc_value
.get("serviceEndpoint")
.and_then(|v| v.as_str()),
) {
let type_value = svc_value
.get("type")
.and_then(|v| v.as_str())
.unwrap_or("Unknown");
web_sys::console::log_1(&JsValue::from_str(&format!(
"[{}] ID: {}
Type: {}
Endpoint: {}",
i + 1,
id,
type_value,
endpoint
)));
}
}
}
// Create a simplified DID document with whatever we could extract
let simplified_doc = DIDDoc {
id: doc_id,
verification_method: verification_methods,
authentication,
key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: services,
};
Ok(Some(simplified_doc))
}
Err(_) => Err(Error::DIDResolution(format!(
"Failed to parse DID document from {}: {}",
url, parse_error
))),
}
}
}
} else if resp.status() == 404 {
// Not found is a valid response, just return None
Ok(None)
} else {
Err(Error::DIDResolution(format!(
"HTTP error fetching DID document from {}: {}",
url,
resp.status()
)))
}
}
#[cfg(all(not(target_arch = "wasm32"), not(feature = "native")))]
{
Err(Error::DIDResolution(
"Web DID resolution requires the 'native' feature or WASM".to_string(),
))
}
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Default for MultiResolver {
fn default() -> Self {
let mut resolver = Self::new();
resolver.register_method("key", KeyResolver::new());
resolver.register_method("web", WebResolver::new());
resolver
}
}
/// DID Key Generator for creating DIDs with different key types
#[derive(Debug, Default, Clone)]
pub struct DIDKeyGenerator;
impl DIDKeyGenerator {
/// Create a new DID key generator
pub fn new() -> Self {
Self
}
/// Create a Secret from a GeneratedKey for a DID
pub fn create_secret_from_key(&self, key: &GeneratedKey) -> Secret {
// Determine the proper key ID based on DID method
let kid = if key.did.starts_with("did:key:") {
// For did:key, the key ID is the DID + fragment matching the multibase
// Get the first verification method's ID which has the proper format
key.did_doc
.verification_method
.first()
.map(|vm| vm.id.clone())
.unwrap_or_else(|| {
// Fallback: extract the multibase part and construct the ID
let multibase = key.did.strip_prefix("did:key:").unwrap_or("");
format!("{}#{}", key.did, multibase)
})
} else if key.did.starts_with("did:web:") {
// For did:web, use #keys-1
format!("{}#keys-1", key.did)
} else {
// For other DID methods, use a generic pattern
format!("{}#key-1", key.did)
};
match key.key_type {
#[cfg(feature = "crypto-ed25519")]
KeyType::Ed25519 => Secret {
id: key.did.clone(),
type_: SecretType::JsonWebKey2020,
secret_material: SecretMaterial::JWK {
private_key_jwk: serde_json::json!({
"kty": "OKP",
"kid": kid,
"crv": "Ed25519",
"x": base64::engine::general_purpose::STANDARD.encode(&key.public_key),
"d": base64::engine::general_purpose::STANDARD.encode(&key.private_key)
}),
},
},
#[cfg(feature = "crypto-p256")]
KeyType::P256 => Secret {
id: key.did.clone(),
type_: SecretType::JsonWebKey2020,
secret_material: SecretMaterial::JWK {
// public_key is uncompressed [0x04 || x(32) || y(32)], skip the 0x04 prefix
private_key_jwk: serde_json::json!({
"kty": "EC",
"kid": kid,
"crv": "P-256",
"x": base64::engine::general_purpose::STANDARD.encode(&key.public_key[1..33]),
"y": base64::engine::general_purpose::STANDARD.encode(&key.public_key[33..65]),
"d": base64::engine::general_purpose::STANDARD.encode(&key.private_key)
}),
},
},
#[cfg(feature = "crypto-secp256k1")]
KeyType::Secp256k1 => Secret {
id: key.did.clone(),
type_: SecretType::JsonWebKey2020,
secret_material: SecretMaterial::JWK {
// public_key is uncompressed [0x04 || x(32) || y(32)], skip the 0x04 prefix
private_key_jwk: serde_json::json!({
"kty": "EC",
"kid": kid,
"crv": "secp256k1",
"x": base64::engine::general_purpose::STANDARD.encode(&key.public_key[1..33]),
"y": base64::engine::general_purpose::STANDARD.encode(&key.public_key[33..65]),
"d": base64::engine::general_purpose::STANDARD.encode(&key.private_key)
}),
},
},
}
}
/// Generate a did:key identifier with the specified key type
pub fn generate_did(&self, options: DIDGenerationOptions) -> Result<GeneratedKey> {
match options.key_type {
#[cfg(feature = "crypto-ed25519")]
KeyType::Ed25519 => self.generate_ed25519_did(),
#[cfg(feature = "crypto-p256")]
KeyType::P256 => self.generate_p256_did(),
#[cfg(feature = "crypto-secp256k1")]
KeyType::Secp256k1 => self.generate_secp256k1_did(),
}
}
/// Generate a did:key identifier with an Ed25519 key
#[cfg(feature = "crypto-ed25519")]
pub fn generate_ed25519_did(&self) -> Result<GeneratedKey> {
// Generate a new Ed25519 keypair
let mut csprng = OsRng;
let signing_key = Ed25519SigningKey::generate(&mut csprng);
let verifying_key = Ed25519VerifyingKey::from(&signing_key);
// Extract public and private keys
let public_key = verifying_key.to_bytes().to_vec();
let private_key = signing_key.to_bytes().to_vec();
// Create did:key identifier
// Multicodec prefix for Ed25519: 0xed01
let mut prefixed_key = vec![0xed, 0x01];
prefixed_key.extend_from_slice(&public_key);
// Encode the key with multibase (base58btc with 'z' prefix)
let multibase_encoded = encode(Base::Base58Btc, &prefixed_key);
let did = format!("did:key:{}", multibase_encoded);
// Create the DID document
let doc = self.create_did_doc(&did, &prefixed_key, KeyType::Ed25519)?;
// Return the generated key information
Ok(GeneratedKey {
key_type: KeyType::Ed25519,
did,
public_key,
private_key,
did_doc: doc,
})
}
/// Generate a did:key identifier with a P-256 key
#[cfg(feature = "crypto-p256")]
pub fn generate_p256_did(&self) -> Result<GeneratedKey> {
// Generate a new P-256 keypair
let mut rng = OsRng;
let signing_key = P256SigningKey::random(&mut rng);
// Extract public and private keys
let private_key = signing_key.to_bytes().to_vec();
let public_key = signing_key
.verifying_key()
.to_encoded_point(false)
.to_bytes()
.to_vec();
// Create did:key identifier
// Multicodec prefix for P-256: 0x1200
let mut prefixed_key = vec![0x12, 0x00];
prefixed_key.extend_from_slice(&public_key);
// Encode the key with multibase (base58btc with 'z' prefix)
let multibase_encoded = encode(Base::Base58Btc, &prefixed_key);
let did = format!("did:key:{}", multibase_encoded);
// Create the DID document
let doc = self.create_did_doc(&did, &prefixed_key, KeyType::P256)?;
// Return the generated key information
Ok(GeneratedKey {
key_type: KeyType::P256,
did,
public_key,
private_key,
did_doc: doc,
})
}
/// Generate a did:key identifier with a Secp256k1 key
#[cfg(feature = "crypto-secp256k1")]
pub fn generate_secp256k1_did(&self) -> Result<GeneratedKey> {
// Generate a new Secp256k1 keypair
let mut rng = OsRng;
let signing_key = Secp256k1SigningKey::random(&mut rng);
// Extract public and private keys
let private_key = signing_key.to_bytes().to_vec();
let public_key = signing_key
.verifying_key()
.to_encoded_point(false)
.to_bytes()
.to_vec();
// Create did:key identifier
// Multicodec prefix for Secp256k1: 0xe701
let mut prefixed_key = vec![0xe7, 0x01];
prefixed_key.extend_from_slice(&public_key);
// Encode the key with multibase (base58btc with 'z' prefix)
let multibase_encoded = encode(Base::Base58Btc, &prefixed_key);
let did = format!("did:key:{}", multibase_encoded);
// Create the DID document
let doc = self.create_did_doc(&did, &prefixed_key, KeyType::Secp256k1)?;
// Return the generated key information
Ok(GeneratedKey {
key_type: KeyType::Secp256k1,
did,
public_key,
private_key,
did_doc: doc,
})
}
/// Generate a did:web identifier with the given domain and key type
pub fn generate_web_did(
&self,
domain: &str,
options: DIDGenerationOptions,
) -> Result<GeneratedKey> {
// First, generate a key DID of the appropriate type
let key_did = self.generate_did(options)?;
// Format the did:web identifier
let did = format!("did:web:{}", domain);
// Create a new DID document based on the key DID document but with the web DID
let verification_methods: Vec<VerificationMethod> = key_did
.did_doc
.verification_method
.iter()
.map(|vm| {
let id = format!("{}#keys-1", did);
VerificationMethod {
id: id.clone(),
type_: vm.type_.clone(),
controller: did.clone(),
verification_material: vm.verification_material.clone(),
}
})
.collect();
let did_doc = DIDDoc {
id: did.clone(),
verification_method: verification_methods.clone(),
authentication: verification_methods
.iter()
.map(|vm| vm.id.clone())
.collect(),
key_agreement: key_did.did_doc.key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: vec![],
};
// Return the generated key information with the web DID
Ok(GeneratedKey {
key_type: key_did.key_type,
did,
public_key: key_did.public_key,
private_key: key_did.private_key,
did_doc,
})
}
/// Create a DID document for a did:key
fn create_did_doc(
&self,
did: &str,
prefixed_public_key: &[u8],
key_type: KeyType,
) -> Result<DIDDoc> {
// Determine verification method type based on key type
let verification_method_type = match key_type {
#[cfg(feature = "crypto-ed25519")]
KeyType::Ed25519 => VerificationMethodType::Ed25519VerificationKey2018,
#[cfg(feature = "crypto-p256")]
KeyType::P256 => VerificationMethodType::EcdsaSecp256k1VerificationKey2019, // Using Secp256k1 type as P256 isn't available
#[cfg(feature = "crypto-secp256k1")]
KeyType::Secp256k1 => VerificationMethodType::EcdsaSecp256k1VerificationKey2019,
};
// Encode the prefixed public key with multibase
let multibase_encoded = encode(Base::Base58Btc, prefixed_public_key);
// Create the verification method ID
let vm_id = format!("{}#{}", did, multibase_encoded);
// Create the verification method
let verification_method = VerificationMethod {
id: vm_id.clone(),
type_: verification_method_type.clone(),
controller: did.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: multibase_encoded.clone(),
},
};
// For Ed25519, also generate an X25519 verification method for key agreement
let mut verification_methods = vec![verification_method.clone()];
let mut key_agreement = Vec::new();
#[cfg(feature = "crypto-ed25519")]
if key_type == KeyType::Ed25519 {
// Only Ed25519 keys have an X25519 key agreement method
if let Some(x25519_bytes) = self.ed25519_to_x25519(&prefixed_public_key[2..]) {
// Prefix for X25519: 0xEC01
let mut x25519_prefixed = vec![0xEC, 0x01];
x25519_prefixed.extend_from_slice(&x25519_bytes);
// Encode the prefixed X25519 key with multibase
let x25519_multibase = encode(Base::Base58Btc, &x25519_prefixed);
// Create the X25519 verification method ID
let x25519_vm_id = format!("{}#{}", did, x25519_multibase);
// Create the X25519 verification method
let x25519_verification_method = VerificationMethod {
id: x25519_vm_id.clone(),
type_: VerificationMethodType::X25519KeyAgreementKey2019,
controller: did.to_string(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: x25519_multibase,
},
};
// Add the X25519 verification method and key agreement method
verification_methods.push(x25519_verification_method);
key_agreement.push(x25519_vm_id);
}
}
// Create the DID document
let did_doc = DIDDoc {
id: did.to_string(),
verification_method: verification_methods,
authentication: vec![vm_id.clone()],
key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: vec![],
};
Ok(did_doc)
}
/// Convert an Ed25519 public key to an X25519 public key
///
/// This follows the conversion process described in RFC 7748
/// https://datatracker.ietf.org/doc/html/rfc7748#section-5
#[cfg(feature = "crypto-ed25519")]
fn ed25519_to_x25519(&self, ed25519_pubkey: &[u8]) -> Option<[u8; 32]> {
// The Ed25519 public key should be 32 bytes
if ed25519_pubkey.len() != 32 {
return None;
}
// Try to create a CompressedEdwardsY from the bytes
let edwards_y = match CompressedEdwardsY::from_slice(ed25519_pubkey) {
Ok(point) => point,
Err(_) => return None,
};
// Try to decompress to get the Edwards point
let edwards_point = edwards_y.decompress()?;
// Convert to Montgomery form
let montgomery_point = edwards_point.to_montgomery();
// Get the raw bytes representation of the X25519 key
Some(montgomery_point.to_bytes())
}
// The create_secret_from_key method has been moved up
}
#[derive(Debug)]
#[cfg(target_arch = "wasm32")]
pub struct MultiResolver {
// WASM-specific implementation with no thread-safety requirements
resolvers: HashMap<String, Box<dyn WasmDIDMethodResolver>>,
}
#[cfg(target_arch = "wasm32")]
impl MultiResolver {
pub fn new() -> Self {
Self {
resolvers: HashMap::new(),
}
}
pub fn default() -> Self {
let mut resolver = Self::new();
// Add default resolvers
resolver.add_resolver(Box::new(KeyResolver::new()));
resolver
}
pub fn add_resolver(&mut self, resolver: Box<dyn WasmDIDMethodResolver>) {
self.resolvers
.insert(resolver.method().to_string(), resolver);
}
}
#[cfg(target_arch = "wasm32")]
impl WasmDIDResolver for MultiResolver {
fn resolve(&self, did: &str) -> Result<Option<DIDDoc>> {
// Extract the DID method
let parts: Vec<&str> = did.split(':').collect();
if parts.len() < 3 {
return Err(Error::InvalidDID);
}
let method = parts[1];
// Get the resolver from the map
if let Some(resolver) = self.resolvers.get(method) {
resolver.resolve_method(did)
} else {
Err(Error::UnsupportedDIDMethod(format!(
"Method {} is not a WasmDIDMethodResolver",
method
)))
}
}
}
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
#[cfg(not(target_arch = "wasm32"))]
impl SyncDIDResolver for MultiResolver {
async fn resolve(&self, did: &str) -> Result<Option<DIDDoc>> {
// Extract the DID method
let parts: Vec<&str> = did.split(':').collect();
if parts.len() < 3 {
return Err(Error::InvalidDID);
}
let method = parts[1];
// Get the resolver from the map first
let resolver = {
let resolver_guard = self
.resolvers
.read()
.map_err(|_| Error::FailedToAcquireResolverReadLock)?;
if let Some(resolver) = resolver_guard.get(method) {
resolver.clone()
} else {
return Err(Error::UnsupportedDIDMethod(method.to_string()));
}
// Lock is dropped here when resolver_guard goes out of scope
};
// Now use the resolver without holding the lock
resolver.resolve_method(did).await
}
}
// DIDResolver trait from didcomm is no longer needed since we've removed the didcomm dependency
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use js_sys::Function;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "Promise<string>")]
pub type JsPromiseString;
#[wasm_bindgen(typescript_type = "Promise<string | null>")]
pub type JsPromiseStringOrNull;
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub struct JsDIDResolver {
resolve_fn: Function,
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl JsDIDResolver {
#[wasm_bindgen(constructor)]
pub fn new(resolve_fn: Function) -> Self {
Self { resolve_fn }
}
#[wasm_bindgen]
pub fn method(&self) -> String {
// The JS resolver should return its method
let this = JsValue::null();
let method = self
.resolve_fn
.call1(&this, &JsValue::from_str("method"))
.unwrap_or_else(|_| JsValue::from_str("unknown"));
method.as_string().unwrap_or_else(|| "unknown".to_string())
}
}
// This is a duplicate trait that conflicts with the one defined above
// So we're removing it here to avoid the conflict
/// A wrapper for JavaScript DID resolvers.
#[cfg(target_arch = "wasm32")]
#[derive(Debug)]
pub struct JsDIDMethodResolver {
method: String,
#[allow(dead_code)] // Reserved for future async JS resolver implementation
resolve_fn: Function,
}
#[cfg(target_arch = "wasm32")]
impl JsDIDMethodResolver {
/// Create a new JavaScript DID method resolver from a function in the global context
pub fn new(method: &str, resolve_fn: Function) -> Self {
Self {
method: method.to_string(),
resolve_fn,
}
}
}
#[cfg(target_arch = "wasm32")]
impl WasmDIDMethodResolver for JsDIDMethodResolver {
fn method(&self) -> &str {
&self.method
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn resolve_method(&self, did: &str) -> Result<Option<DIDDoc>> {
// Ensure the DID is for the method that this resolver is for
let parts: Vec<&str> = did.split(':').collect();
if parts.len() < 3 || parts[1] != self.method {
return Err(Error::InvalidDID);
}
// In WASM target mode, we can't use async/await in this interface
// This implementation is a simplified version that just returns None
// The proper implementation would be in the JavaScript binding
Err(Error::NotImplemented(
"JS resolver not supported in this context".to_string(),
))
}
#[cfg(not(feature = "wasm"))]
async fn resolve_method(&self, _did: &str) -> Result<Option<DIDDoc>> {
Err(Error::NotImplemented(
"JavaScript DID Method resolver is only available with the 'wasm' feature".to_string(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "native")]
#[tokio::test]
async fn test_key_resolver() {
let resolver = KeyResolver::new();
// Test a valid did:key for Ed25519
let did = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
let result = resolver.resolve_method(did).await.unwrap();
assert!(result.is_some());
let doc = result.unwrap();
assert_eq!(doc.id, did);
assert_eq!(doc.verification_method.len(), 2); // Should have both Ed25519 and X25519 methods
// Verify Ed25519 verification method is present
let ed25519_method = doc
.verification_method
.iter()
.find(|vm| matches!(vm.type_, VerificationMethodType::Ed25519VerificationKey2018))
.expect("Should have an Ed25519 verification method");
// Verify X25519 verification method
let x25519_method = doc
.verification_method
.iter()
.find(|vm| matches!(vm.type_, VerificationMethodType::X25519KeyAgreementKey2019))
.expect("Should have an X25519 key agreement method");
// Check that authentication uses the Ed25519 key
assert!(doc.authentication.contains(&ed25519_method.id));
// Check that key agreement uses the X25519 key
assert!(doc.key_agreement.contains(&x25519_method.id));
}
#[cfg(feature = "native")]
#[tokio::test]
async fn test_multi_resolver() {
let resolver = MultiResolver::default();
// Test resolving a valid did:key
let did = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
let result = <MultiResolver as SyncDIDResolver>::resolve(&resolver, did).await;
assert!(result.is_ok());
let doc_option = result.unwrap();
assert!(doc_option.is_some());
let doc = doc_option.unwrap();
assert_eq!(doc.id, did);
assert_eq!(doc.verification_method.len(), 2); // Should have both Ed25519 and X25519 methods
// Test resolving an unsupported DID method
let did = "did:unsupported:123";
let result = <MultiResolver as SyncDIDResolver>::resolve(&resolver, did).await;
// This should return an error since it's an unsupported method
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("Unsupported DID method"));
}
#[test]
fn test_did_key_generator_ed25519() {
let generator = DIDKeyGenerator::new();
// Generate an Ed25519 DID
let options = DIDGenerationOptions {
key_type: KeyType::Ed25519,
};
let key_result = generator.generate_did(options);
assert!(key_result.is_ok());
let key = key_result.unwrap();
// Check the DID format
assert!(key.did.starts_with("did:key:z"));
// Check that public and private keys have the correct length
assert_eq!(key.public_key.len(), 32); // Ed25519 public key is 32 bytes
assert_eq!(key.private_key.len(), 32); // Ed25519 private key is 32 bytes
// Check the DID document
assert_eq!(key.did_doc.id, key.did);
assert_eq!(key.did_doc.verification_method.len(), 2); // Should have both Ed25519 and X25519
// Verify Ed25519 verification method is present
let ed25519_method = key
.did_doc
.verification_method
.iter()
.find(|vm| matches!(vm.type_, VerificationMethodType::Ed25519VerificationKey2018))
.expect("Should have an Ed25519 verification method");
// Verify X25519 verification method
let x25519_method = key
.did_doc
.verification_method
.iter()
.find(|vm| matches!(vm.type_, VerificationMethodType::X25519KeyAgreementKey2019))
.expect("Should have an X25519 key agreement method");
// Check that authentication uses the Ed25519 key
assert!(key.did_doc.authentication.contains(&ed25519_method.id));
// Check that key agreement uses the X25519 key
assert!(key.did_doc.key_agreement.contains(&x25519_method.id));
// Create a secret from the key
let secret = generator.create_secret_from_key(&key);
assert_eq!(secret.id, key.did);
assert!(matches!(secret.type_, SecretType::JsonWebKey2020));
}
#[test]
#[cfg(feature = "crypto-p256")]
fn test_did_key_generator_p256() {
let generator = DIDKeyGenerator::new();
// Generate a P-256 DID
let options = DIDGenerationOptions {
key_type: KeyType::P256,
};
let key_result = generator.generate_did(options);
assert!(key_result.is_ok());
let key = key_result.unwrap();
// Check the DID format
assert!(key.did.starts_with("did:key:z"));
// Check the DID document
assert_eq!(key.did_doc.id, key.did);
assert_eq!(key.did_doc.verification_method.len(), 1); // P-256 has no key agreement
// Verify P-256 verification method is present
let p256_method = key
.did_doc
.verification_method
.iter()
.find(|vm| {
matches!(
vm.type_,
VerificationMethodType::EcdsaSecp256k1VerificationKey2019
)
}) // Use available type
.expect("Should have a P-256 verification method");
// Check that authentication uses the P-256 key
assert!(key.did_doc.authentication.contains(&p256_method.id));
// Create a secret from the key
let secret = generator.create_secret_from_key(&key);
assert_eq!(secret.id, key.did);
assert!(matches!(secret.type_, SecretType::JsonWebKey2020));
}
#[test]
#[cfg(feature = "crypto-secp256k1")]
fn test_did_key_generator_secp256k1() {
let generator = DIDKeyGenerator::new();
// Generate a Secp256k1 DID
let options = DIDGenerationOptions {
key_type: KeyType::Secp256k1,
};
let key_result = generator.generate_did(options);
assert!(key_result.is_ok());
let key = key_result.unwrap();
// Check the DID format
assert!(key.did.starts_with("did:key:z"));
// Check the DID document
assert_eq!(key.did_doc.id, key.did);
assert_eq!(key.did_doc.verification_method.len(), 1); // Secp256k1 has no key agreement
// Verify Secp256k1 verification method is present
let secp256k1_method = key
.did_doc
.verification_method
.iter()
.find(|vm| {
matches!(
vm.type_,
VerificationMethodType::EcdsaSecp256k1VerificationKey2019
)
})
.expect("Should have a Secp256k1 verification method");
// Check that authentication uses the Secp256k1 key
assert!(key.did_doc.authentication.contains(&secp256k1_method.id));
// Create a secret from the key
let secret = generator.create_secret_from_key(&key);
assert_eq!(secret.id, key.did);
assert!(matches!(secret.type_, SecretType::JsonWebKey2020));
}
#[test]
fn test_did_web_generator() {
let generator = DIDKeyGenerator::new();
// Generate a did:web
let domain = "example.com";
let options = DIDGenerationOptions {
key_type: KeyType::Ed25519,
};
let key_result = generator.generate_web_did(domain, options);
assert!(key_result.is_ok());
let key = key_result.unwrap();
// Check the DID format
assert_eq!(key.did, format!("did:web:{}", domain));
// Check the DID document
assert_eq!(key.did_doc.id, key.did);
assert!(!key.did_doc.verification_method.is_empty());
// Verify that all verification methods have the correct controller
for vm in &key.did_doc.verification_method {
assert_eq!(vm.controller, key.did);
assert!(vm.id.starts_with(&key.did));
}
// Create a secret from the key
let secret = generator.create_secret_from_key(&key);
assert_eq!(secret.id, key.did);
assert!(matches!(secret.type_, SecretType::JsonWebKey2020));
}
}