@enbox/dids 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.mjs +1 -1
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/methods/did-dht-dns.js +455 -0
- package/dist/esm/methods/did-dht-dns.js.map +1 -0
- package/dist/esm/methods/did-dht-pkarr.js +168 -0
- package/dist/esm/methods/did-dht-pkarr.js.map +1 -0
- package/dist/esm/methods/did-dht-types.js +116 -0
- package/dist/esm/methods/did-dht-types.js.map +1 -0
- package/dist/esm/methods/did-dht-utils.js +143 -0
- package/dist/esm/methods/did-dht-utils.js.map +1 -0
- package/dist/esm/methods/did-dht.js +65 -842
- package/dist/esm/methods/did-dht.js.map +1 -1
- package/dist/esm/methods/did-ion-utils.js +161 -0
- package/dist/esm/methods/did-ion-utils.js.map +1 -0
- package/dist/esm/methods/did-ion.js +4 -151
- package/dist/esm/methods/did-ion.js.map +1 -1
- package/dist/esm/methods/did-jwk.js.map +1 -1
- package/dist/esm/methods/did-key-utils.js +235 -0
- package/dist/esm/methods/did-key-utils.js.map +1 -0
- package/dist/esm/methods/did-key.js +6 -222
- package/dist/esm/methods/did-key.js.map +1 -1
- package/dist/esm/resolver/resolver-cache-memory.js +77 -0
- package/dist/esm/resolver/resolver-cache-memory.js.map +1 -0
- package/dist/esm/utils.js +25 -0
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/bearer-did.d.ts +4 -4
- package/dist/types/bearer-did.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/methods/did-dht-dns.d.ts +114 -0
- package/dist/types/methods/did-dht-dns.d.ts.map +1 -0
- package/dist/types/methods/did-dht-pkarr.d.ts +56 -0
- package/dist/types/methods/did-dht-pkarr.d.ts.map +1 -0
- package/dist/types/methods/did-dht-types.d.ts +286 -0
- package/dist/types/methods/did-dht-types.d.ts.map +1 -0
- package/dist/types/methods/did-dht-utils.d.ts +54 -0
- package/dist/types/methods/did-dht-utils.d.ts.map +1 -0
- package/dist/types/methods/did-dht.d.ts +45 -460
- package/dist/types/methods/did-dht.d.ts.map +1 -1
- package/dist/types/methods/did-ion-utils.d.ts +86 -0
- package/dist/types/methods/did-ion-utils.d.ts.map +1 -0
- package/dist/types/methods/did-ion.d.ts +4 -84
- package/dist/types/methods/did-ion.d.ts.map +1 -1
- package/dist/types/methods/did-jwk.d.ts +4 -4
- package/dist/types/methods/did-jwk.d.ts.map +1 -1
- package/dist/types/methods/did-key-utils.d.ts +138 -0
- package/dist/types/methods/did-key-utils.d.ts.map +1 -0
- package/dist/types/methods/did-key.d.ts +6 -127
- package/dist/types/methods/did-key.d.ts.map +1 -1
- package/dist/types/methods/did-method.d.ts +3 -3
- package/dist/types/methods/did-method.d.ts.map +1 -1
- package/dist/types/resolver/resolver-cache-memory.d.ts +58 -0
- package/dist/types/resolver/resolver-cache-memory.d.ts.map +1 -0
- package/dist/types/utils.d.ts +19 -0
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +4 -4
- package/package.json +11 -13
- package/src/bearer-did.ts +4 -4
- package/src/index.ts +2 -0
- package/src/methods/did-dht-dns.ts +516 -0
- package/src/methods/did-dht-pkarr.ts +192 -0
- package/src/methods/did-dht-types.ts +316 -0
- package/src/methods/did-dht-utils.ts +157 -0
- package/src/methods/did-dht.ts +125 -1131
- package/src/methods/did-ion-utils.ts +186 -0
- package/src/methods/did-ion.ts +16 -185
- package/src/methods/did-jwk.ts +4 -4
- package/src/methods/did-key-utils.ts +258 -0
- package/src/methods/did-key.ts +20 -272
- package/src/methods/did-method.ts +3 -3
- package/src/resolver/resolver-cache-memory.ts +84 -0
- package/src/utils.ts +27 -0
- package/dist/browser.js +0 -73
- package/dist/browser.js.map +0 -7
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { Jwk } from '@enbox/crypto';
|
|
2
|
+
import type {
|
|
3
|
+
IonDocumentModel,
|
|
4
|
+
IonPublicKeyModel,
|
|
5
|
+
IonPublicKeyPurpose,
|
|
6
|
+
JwkEs256k,
|
|
7
|
+
} from '@decentralized-identity/ion-sdk';
|
|
8
|
+
|
|
9
|
+
import type { DidService } from '../types/did-core.js';
|
|
10
|
+
import type { DidIonCreateRequest, DidIonVerificationMethod } from './did-ion.js';
|
|
11
|
+
|
|
12
|
+
import { computeJwkThumbprint } from '@enbox/crypto';
|
|
13
|
+
import { IonDid, IonRequest } from '@decentralized-identity/ion-sdk';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The `DidIonUtils` class provides utility functions to support operations in the DID ION method.
|
|
17
|
+
*/
|
|
18
|
+
export class DidIonUtils {
|
|
19
|
+
/**
|
|
20
|
+
* Appends a specified path to a base URL, ensuring proper formatting of the resulting URL.
|
|
21
|
+
*
|
|
22
|
+
* This method is useful for constructing URLs for accessing various endpoints, such as Sidetree
|
|
23
|
+
* nodes in the ION network. It handles the nuances of URL path concatenation, including the
|
|
24
|
+
* addition or removal of leading/trailing slashes, to create a well-formed URL.
|
|
25
|
+
*
|
|
26
|
+
* @param params - The parameters for URL construction.
|
|
27
|
+
* @param params.baseUrl - The base URL to which the path will be appended.
|
|
28
|
+
* @param params.path - The path to append to the base URL.
|
|
29
|
+
* @returns The fully constructed URL string with the path appended to the base URL.
|
|
30
|
+
*/
|
|
31
|
+
public static appendPathToUrl({ baseUrl, path }: {
|
|
32
|
+
baseUrl: string;
|
|
33
|
+
path: string;
|
|
34
|
+
}): string {
|
|
35
|
+
const url = new URL(baseUrl);
|
|
36
|
+
url.pathname = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/';
|
|
37
|
+
url.pathname += path.startsWith('/') ? path.substring(1) : path;
|
|
38
|
+
|
|
39
|
+
return url.toString();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Computes the Long Form DID URI given an ION DID's recovery key, update key, services, and
|
|
44
|
+
* verification methods.
|
|
45
|
+
*
|
|
46
|
+
* @param params - The parameters for computing the Long Form DID URI.
|
|
47
|
+
* @param params.recoveryKey - The ION Recovery Key.
|
|
48
|
+
* @param params.updateKey - The ION Update Key.
|
|
49
|
+
* @param params.services - An array of services associated with the DID.
|
|
50
|
+
* @param params.verificationMethods - An array of verification methods associated with the DID.
|
|
51
|
+
* @returns A Promise resolving to the Long Form DID URI.
|
|
52
|
+
*/
|
|
53
|
+
public static async computeLongFormDidUri({ recoveryKey, updateKey, services, verificationMethods }: {
|
|
54
|
+
recoveryKey: Jwk;
|
|
55
|
+
updateKey: Jwk;
|
|
56
|
+
services: DidService[];
|
|
57
|
+
verificationMethods: DidIonVerificationMethod[];
|
|
58
|
+
}): Promise<string> {
|
|
59
|
+
// Create the ION document.
|
|
60
|
+
const ionDocument = await DidIonUtils.createIonDocument({ services, verificationMethods });
|
|
61
|
+
|
|
62
|
+
// Normalize JWK to onnly include specific members and in lexicographic order.
|
|
63
|
+
const normalizedRecoveryKey = DidIonUtils.normalizeJwk(recoveryKey);
|
|
64
|
+
const normalizedUpdateKey = DidIonUtils.normalizeJwk(updateKey);
|
|
65
|
+
|
|
66
|
+
// Compute the Long Form DID URI.
|
|
67
|
+
const longFormDidUri = await IonDid.createLongFormDid({
|
|
68
|
+
document : ionDocument,
|
|
69
|
+
recoveryKey : normalizedRecoveryKey as JwkEs256k,
|
|
70
|
+
updateKey : normalizedUpdateKey as JwkEs256k
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return longFormDidUri;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Constructs a Sidetree Create Operation request for a DID document within the ION network.
|
|
78
|
+
*
|
|
79
|
+
* This method prepares the necessary payload for submitting a Create Operation to a Sidetree
|
|
80
|
+
* node, encapsulating the details of the DID document, recovery key, and update key.
|
|
81
|
+
*
|
|
82
|
+
* @param params - Parameters required to construct the Create Operation request.
|
|
83
|
+
* @param params.ionDocument - The DID document model containing public keys and service endpoints.
|
|
84
|
+
* @param params.recoveryKey - The recovery public key in JWK format.
|
|
85
|
+
* @param params.updateKey - The update public key in JWK format.
|
|
86
|
+
* @returns A promise resolving to the ION Create Operation request model, ready for submission to a Sidetree node.
|
|
87
|
+
*/
|
|
88
|
+
public static async constructCreateRequest({ ionDocument, recoveryKey, updateKey }: {
|
|
89
|
+
ionDocument: IonDocumentModel,
|
|
90
|
+
recoveryKey: Jwk,
|
|
91
|
+
updateKey: Jwk
|
|
92
|
+
}): Promise<DidIonCreateRequest> {
|
|
93
|
+
// Create an ION DID create request operation.
|
|
94
|
+
const createRequest = await IonRequest.createCreateRequest({
|
|
95
|
+
document : ionDocument,
|
|
96
|
+
recoveryKey : DidIonUtils.normalizeJwk(recoveryKey) as JwkEs256k,
|
|
97
|
+
updateKey : DidIonUtils.normalizeJwk(updateKey) as JwkEs256k
|
|
98
|
+
}) as DidIonCreateRequest;
|
|
99
|
+
|
|
100
|
+
return createRequest;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Assembles an ION document model from provided services and verification methods
|
|
105
|
+
*
|
|
106
|
+
* This model serves as the foundation for a DID document in the ION network, facilitating the
|
|
107
|
+
* creation and management of decentralized identities. It translates service endpoints and
|
|
108
|
+
* public keys into a format compatible with the Sidetree protocol, ensuring the resulting DID
|
|
109
|
+
* document adheres to the required specifications for ION DIDs. This method is essential for
|
|
110
|
+
* constructing the payload needed to register or update DIDs within the ION network.
|
|
111
|
+
*
|
|
112
|
+
* @param params - The parameters containing the services and verification methods to include in the ION document.
|
|
113
|
+
* @param params.services - A list of service endpoints to be included in the DID document, specifying ways to interact with the DID subject.
|
|
114
|
+
* @param params.verificationMethods - A list of verification methods to be included, detailing the
|
|
115
|
+
* cryptographic keys and their intended uses within the DID document.
|
|
116
|
+
* @returns A Promise resolving to an `IonDocumentModel`, ready for use in Sidetree operations like DID creation and updates.
|
|
117
|
+
*/
|
|
118
|
+
public static async createIonDocument({ services, verificationMethods }: {
|
|
119
|
+
services: DidService[];
|
|
120
|
+
verificationMethods: DidIonVerificationMethod[]
|
|
121
|
+
}): Promise<IonDocumentModel> {
|
|
122
|
+
/**
|
|
123
|
+
* STEP 1: Convert verification methods to ION SDK format.
|
|
124
|
+
*/
|
|
125
|
+
const ionPublicKeys: IonPublicKeyModel[] = [];
|
|
126
|
+
|
|
127
|
+
for (const vm of verificationMethods) {
|
|
128
|
+
// Use the given ID, the key's ID, or the key's thumbprint as the verification method ID.
|
|
129
|
+
let methodId = vm.id ?? vm.publicKeyJwk.kid ?? await computeJwkThumbprint({ jwk: vm.publicKeyJwk });
|
|
130
|
+
methodId = `${methodId.split('#').pop()}`; // Remove fragment prefix, if any.
|
|
131
|
+
|
|
132
|
+
// Convert public key JWK to ION format.
|
|
133
|
+
const publicKey: IonPublicKeyModel = {
|
|
134
|
+
id : methodId,
|
|
135
|
+
publicKeyJwk : DidIonUtils.normalizeJwk(vm.publicKeyJwk),
|
|
136
|
+
purposes : vm.purposes as IonPublicKeyPurpose[],
|
|
137
|
+
type : 'JsonWebKey2020'
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
ionPublicKeys.push(publicKey);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* STEP 2: Convert service entries, if any, to ION SDK format.
|
|
145
|
+
*/
|
|
146
|
+
const ionServices = services.map(service => ({
|
|
147
|
+
...service,
|
|
148
|
+
id: `${service.id.split('#').pop()}` // Remove fragment prefix, if any.
|
|
149
|
+
}));
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* STEP 3: Format as ION document.
|
|
153
|
+
*/
|
|
154
|
+
const ionDocumentModel: IonDocumentModel = {
|
|
155
|
+
publicKeys : ionPublicKeys,
|
|
156
|
+
services : ionServices
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return ionDocumentModel;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Normalize the given JWK to include only specific members and in lexicographic order.
|
|
164
|
+
*
|
|
165
|
+
* @param jwk - The JWK to normalize.
|
|
166
|
+
* @returns The normalized JWK.
|
|
167
|
+
*/
|
|
168
|
+
private static normalizeJwk(jwk: Jwk): Jwk {
|
|
169
|
+
const keyType = jwk.kty;
|
|
170
|
+
let normalizedJwk: Jwk;
|
|
171
|
+
|
|
172
|
+
if (keyType === 'EC') {
|
|
173
|
+
normalizedJwk = { crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y };
|
|
174
|
+
} else if (keyType === 'oct') {
|
|
175
|
+
normalizedJwk = { k: jwk.k, kty: jwk.kty };
|
|
176
|
+
} else if (keyType === 'OKP') {
|
|
177
|
+
normalizedJwk = { crv: jwk.crv, kty: jwk.kty, x: jwk.x };
|
|
178
|
+
} else if (keyType === 'RSA') {
|
|
179
|
+
normalizedJwk = { e: jwk.e, kty: jwk.kty, n: jwk.n };
|
|
180
|
+
} else {
|
|
181
|
+
throw new Error(`Unsupported key type: ${keyType}`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return normalizedJwk;
|
|
185
|
+
}
|
|
186
|
+
}
|
package/src/methods/did-ion.ts
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
IonDocumentModel,
|
|
4
|
-
IonPublicKeyModel,
|
|
5
|
-
IonPublicKeyPurpose,
|
|
6
|
-
JwkEs256k,
|
|
7
|
-
} from '@decentralized-identity/ion-sdk';
|
|
8
|
-
|
|
9
|
-
import { computeJwkThumbprint, LocalKeyManager } from '@enbox/crypto';
|
|
10
|
-
import { IonDid, IonRequest } from '@decentralized-identity/ion-sdk';
|
|
11
|
-
|
|
1
|
+
import type { IonDocumentModel } from '@decentralized-identity/ion-sdk';
|
|
12
2
|
import type { PortableDid } from '../types/portable-did.js';
|
|
13
3
|
import type { DidCreateOptions, DidCreateVerificationMethod, DidRegistrationResult } from '../methods/did-method.js';
|
|
14
4
|
import type {
|
|
@@ -19,9 +9,20 @@ import type {
|
|
|
19
9
|
DidVerificationMethod,
|
|
20
10
|
DidVerificationRelationship,
|
|
21
11
|
} from '../types/did-core.js';
|
|
12
|
+
import type {
|
|
13
|
+
Jwk,
|
|
14
|
+
KeyIdentifier,
|
|
15
|
+
KeyImporterExporter,
|
|
16
|
+
KeyManager,
|
|
17
|
+
KmsExportKeyParams,
|
|
18
|
+
KmsImportKeyParams,
|
|
19
|
+
} from '@enbox/crypto';
|
|
20
|
+
|
|
21
|
+
import { LocalKeyManager } from '@enbox/crypto';
|
|
22
22
|
|
|
23
23
|
import { BearerDid } from '../bearer-did.js';
|
|
24
24
|
import { Did } from '../did.js';
|
|
25
|
+
import { DidIonUtils } from './did-ion-utils.js';
|
|
25
26
|
import { DidMethod } from '../methods/did-method.js';
|
|
26
27
|
import { EMPTY_DID_RESOLUTION_RESULT } from '../types/did-resolution.js';
|
|
27
28
|
import { getVerificationRelationshipsById } from '../utils.js';
|
|
@@ -364,7 +365,7 @@ export class DidIon extends DidMethod {
|
|
|
364
365
|
* @param params.options - Optional parameters that can be specified when creating a new DID.
|
|
365
366
|
* @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
|
|
366
367
|
*/
|
|
367
|
-
public static async create<TKms extends
|
|
368
|
+
public static async create<TKms extends KeyManager | undefined = undefined>({
|
|
368
369
|
keyManager = new LocalKeyManager(),
|
|
369
370
|
options = {}
|
|
370
371
|
}: {
|
|
@@ -517,7 +518,7 @@ export class DidIon extends DidMethod {
|
|
|
517
518
|
* any verification method are missing in the key manager.
|
|
518
519
|
*/
|
|
519
520
|
public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
|
|
520
|
-
keyManager?:
|
|
521
|
+
keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
|
|
521
522
|
portableDid: PortableDid;
|
|
522
523
|
}): Promise<BearerDid> {
|
|
523
524
|
// Verify the DID method is supported.
|
|
@@ -714,175 +715,5 @@ export class DidIon extends DidMethod {
|
|
|
714
715
|
}
|
|
715
716
|
}
|
|
716
717
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
*/
|
|
720
|
-
export class DidIonUtils {
|
|
721
|
-
/**
|
|
722
|
-
* Appends a specified path to a base URL, ensuring proper formatting of the resulting URL.
|
|
723
|
-
*
|
|
724
|
-
* This method is useful for constructing URLs for accessing various endpoints, such as Sidetree
|
|
725
|
-
* nodes in the ION network. It handles the nuances of URL path concatenation, including the
|
|
726
|
-
* addition or removal of leading/trailing slashes, to create a well-formed URL.
|
|
727
|
-
*
|
|
728
|
-
* @param params - The parameters for URL construction.
|
|
729
|
-
* @param params.baseUrl - The base URL to which the path will be appended.
|
|
730
|
-
* @param params.path - The path to append to the base URL.
|
|
731
|
-
* @returns The fully constructed URL string with the path appended to the base URL.
|
|
732
|
-
*/
|
|
733
|
-
public static appendPathToUrl({ baseUrl, path }: {
|
|
734
|
-
baseUrl: string;
|
|
735
|
-
path: string;
|
|
736
|
-
}): string {
|
|
737
|
-
const url = new URL(baseUrl);
|
|
738
|
-
url.pathname = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/';
|
|
739
|
-
url.pathname += path.startsWith('/') ? path.substring(1) : path;
|
|
740
|
-
|
|
741
|
-
return url.toString();
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/**
|
|
745
|
-
* Computes the Long Form DID URI given an ION DID's recovery key, update key, services, and
|
|
746
|
-
* verification methods.
|
|
747
|
-
*
|
|
748
|
-
* @param params - The parameters for computing the Long Form DID URI.
|
|
749
|
-
* @param params.recoveryKey - The ION Recovery Key.
|
|
750
|
-
* @param params.updateKey - The ION Update Key.
|
|
751
|
-
* @param params.services - An array of services associated with the DID.
|
|
752
|
-
* @param params.verificationMethods - An array of verification methods associated with the DID.
|
|
753
|
-
* @returns A Promise resolving to the Long Form DID URI.
|
|
754
|
-
*/
|
|
755
|
-
public static async computeLongFormDidUri({ recoveryKey, updateKey, services, verificationMethods }: {
|
|
756
|
-
recoveryKey: Jwk;
|
|
757
|
-
updateKey: Jwk;
|
|
758
|
-
services: DidService[];
|
|
759
|
-
verificationMethods: DidIonVerificationMethod[];
|
|
760
|
-
}): Promise<string> {
|
|
761
|
-
// Create the ION document.
|
|
762
|
-
const ionDocument = await DidIonUtils.createIonDocument({ services, verificationMethods });
|
|
763
|
-
|
|
764
|
-
// Normalize JWK to onnly include specific members and in lexicographic order.
|
|
765
|
-
const normalizedRecoveryKey = DidIonUtils.normalizeJwk(recoveryKey);
|
|
766
|
-
const normalizedUpdateKey = DidIonUtils.normalizeJwk(updateKey);
|
|
767
|
-
|
|
768
|
-
// Compute the Long Form DID URI.
|
|
769
|
-
const longFormDidUri = await IonDid.createLongFormDid({
|
|
770
|
-
document : ionDocument,
|
|
771
|
-
recoveryKey : normalizedRecoveryKey as JwkEs256k,
|
|
772
|
-
updateKey : normalizedUpdateKey as JwkEs256k
|
|
773
|
-
});
|
|
774
|
-
|
|
775
|
-
return longFormDidUri;
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
/**
|
|
779
|
-
* Constructs a Sidetree Create Operation request for a DID document within the ION network.
|
|
780
|
-
*
|
|
781
|
-
* This method prepares the necessary payload for submitting a Create Operation to a Sidetree
|
|
782
|
-
* node, encapsulating the details of the DID document, recovery key, and update key.
|
|
783
|
-
*
|
|
784
|
-
* @param params - Parameters required to construct the Create Operation request.
|
|
785
|
-
* @param params.ionDocument - The DID document model containing public keys and service endpoints.
|
|
786
|
-
* @param params.recoveryKey - The recovery public key in JWK format.
|
|
787
|
-
* @param params.updateKey - The update public key in JWK format.
|
|
788
|
-
* @returns A promise resolving to the ION Create Operation request model, ready for submission to a Sidetree node.
|
|
789
|
-
*/
|
|
790
|
-
public static async constructCreateRequest({ ionDocument, recoveryKey, updateKey }: {
|
|
791
|
-
ionDocument: IonDocumentModel,
|
|
792
|
-
recoveryKey: Jwk,
|
|
793
|
-
updateKey: Jwk
|
|
794
|
-
}): Promise<DidIonCreateRequest> {
|
|
795
|
-
// Create an ION DID create request operation.
|
|
796
|
-
const createRequest = await IonRequest.createCreateRequest({
|
|
797
|
-
document : ionDocument,
|
|
798
|
-
recoveryKey : DidIonUtils.normalizeJwk(recoveryKey) as JwkEs256k,
|
|
799
|
-
updateKey : DidIonUtils.normalizeJwk(updateKey) as JwkEs256k
|
|
800
|
-
}) as DidIonCreateRequest;
|
|
801
|
-
|
|
802
|
-
return createRequest;
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
/**
|
|
806
|
-
* Assembles an ION document model from provided services and verification methods
|
|
807
|
-
*
|
|
808
|
-
* This model serves as the foundation for a DID document in the ION network, facilitating the
|
|
809
|
-
* creation and management of decentralized identities. It translates service endpoints and
|
|
810
|
-
* public keys into a format compatible with the Sidetree protocol, ensuring the resulting DID
|
|
811
|
-
* document adheres to the required specifications for ION DIDs. This method is essential for
|
|
812
|
-
* constructing the payload needed to register or update DIDs within the ION network.
|
|
813
|
-
*
|
|
814
|
-
* @param params - The parameters containing the services and verification methods to include in the ION document.
|
|
815
|
-
* @param params.services - A list of service endpoints to be included in the DID document, specifying ways to interact with the DID subject.
|
|
816
|
-
* @param params.verificationMethods - A list of verification methods to be included, detailing the
|
|
817
|
-
* cryptographic keys and their intended uses within the DID document.
|
|
818
|
-
* @returns A Promise resolving to an `IonDocumentModel`, ready for use in Sidetree operations like DID creation and updates.
|
|
819
|
-
*/
|
|
820
|
-
public static async createIonDocument({ services, verificationMethods }: {
|
|
821
|
-
services: DidService[];
|
|
822
|
-
verificationMethods: DidIonVerificationMethod[]
|
|
823
|
-
}): Promise<IonDocumentModel> {
|
|
824
|
-
/**
|
|
825
|
-
* STEP 1: Convert verification methods to ION SDK format.
|
|
826
|
-
*/
|
|
827
|
-
const ionPublicKeys: IonPublicKeyModel[] = [];
|
|
828
|
-
|
|
829
|
-
for (const vm of verificationMethods) {
|
|
830
|
-
// Use the given ID, the key's ID, or the key's thumbprint as the verification method ID.
|
|
831
|
-
let methodId = vm.id ?? vm.publicKeyJwk.kid ?? await computeJwkThumbprint({ jwk: vm.publicKeyJwk });
|
|
832
|
-
methodId = `${methodId.split('#').pop()}`; // Remove fragment prefix, if any.
|
|
833
|
-
|
|
834
|
-
// Convert public key JWK to ION format.
|
|
835
|
-
const publicKey: IonPublicKeyModel = {
|
|
836
|
-
id : methodId,
|
|
837
|
-
publicKeyJwk : DidIonUtils.normalizeJwk(vm.publicKeyJwk),
|
|
838
|
-
purposes : vm.purposes as IonPublicKeyPurpose[],
|
|
839
|
-
type : 'JsonWebKey2020'
|
|
840
|
-
};
|
|
841
|
-
|
|
842
|
-
ionPublicKeys.push(publicKey);
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
/**
|
|
846
|
-
* STEP 2: Convert service entries, if any, to ION SDK format.
|
|
847
|
-
*/
|
|
848
|
-
const ionServices = services.map(service => ({
|
|
849
|
-
...service,
|
|
850
|
-
id: `${service.id.split('#').pop()}` // Remove fragment prefix, if any.
|
|
851
|
-
}));
|
|
852
|
-
|
|
853
|
-
/**
|
|
854
|
-
* STEP 3: Format as ION document.
|
|
855
|
-
*/
|
|
856
|
-
const ionDocumentModel: IonDocumentModel = {
|
|
857
|
-
publicKeys : ionPublicKeys,
|
|
858
|
-
services : ionServices
|
|
859
|
-
};
|
|
860
|
-
|
|
861
|
-
return ionDocumentModel;
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* Normalize the given JWK to include only specific members and in lexicographic order.
|
|
866
|
-
*
|
|
867
|
-
* @param jwk - The JWK to normalize.
|
|
868
|
-
* @returns The normalized JWK.
|
|
869
|
-
*/
|
|
870
|
-
private static normalizeJwk(jwk: Jwk): Jwk {
|
|
871
|
-
const keyType = jwk.kty;
|
|
872
|
-
let normalizedJwk: Jwk;
|
|
873
|
-
|
|
874
|
-
if (keyType === 'EC') {
|
|
875
|
-
normalizedJwk = { crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y };
|
|
876
|
-
} else if (keyType === 'oct') {
|
|
877
|
-
normalizedJwk = { k: jwk.k, kty: jwk.kty };
|
|
878
|
-
} else if (keyType === 'OKP') {
|
|
879
|
-
normalizedJwk = { crv: jwk.crv, kty: jwk.kty, x: jwk.x };
|
|
880
|
-
} else if (keyType === 'RSA') {
|
|
881
|
-
normalizedJwk = { e: jwk.e, kty: jwk.kty, n: jwk.n };
|
|
882
|
-
} else {
|
|
883
|
-
throw new Error(`Unsupported key type: ${keyType}`);
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
return normalizedJwk;
|
|
887
|
-
}
|
|
888
|
-
}
|
|
718
|
+
// Re-export DidIonUtils from its dedicated module for backward compatibility.
|
|
719
|
+
export { DidIonUtils } from './did-ion-utils.js';
|
package/src/methods/did-jwk.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
CryptoApi,
|
|
3
2
|
InferKeyGeneratorAlgorithm,
|
|
4
3
|
Jwk,
|
|
5
4
|
KeyIdentifier,
|
|
6
5
|
KeyImporterExporter,
|
|
6
|
+
KeyManager,
|
|
7
7
|
KmsExportKeyParams,
|
|
8
8
|
KmsImportKeyParams,
|
|
9
9
|
} from '@enbox/crypto';
|
|
@@ -76,7 +76,7 @@ export interface DidJwkCreateOptions<TKms> extends DidCreateOptions<TKms> {
|
|
|
76
76
|
/**
|
|
77
77
|
* Optionally specify the algorithm to be used for key generation.
|
|
78
78
|
*/
|
|
79
|
-
algorithm?: TKms extends
|
|
79
|
+
algorithm?: TKms extends KeyManager
|
|
80
80
|
? InferKeyGeneratorAlgorithm<TKms>
|
|
81
81
|
: InferKeyGeneratorAlgorithm<LocalKeyManager>;
|
|
82
82
|
|
|
@@ -193,7 +193,7 @@ export class DidJwk extends DidMethod {
|
|
|
193
193
|
* @param params.options - Optional parameters that can be specified when creating a new DID.
|
|
194
194
|
* @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
|
|
195
195
|
*/
|
|
196
|
-
public static async create<TKms extends
|
|
196
|
+
public static async create<TKms extends KeyManager | undefined = undefined>({
|
|
197
197
|
keyManager = new LocalKeyManager(),
|
|
198
198
|
options = {}
|
|
199
199
|
}: {
|
|
@@ -305,7 +305,7 @@ export class DidJwk extends DidMethod {
|
|
|
305
305
|
* @throws An error if the DID document does not contain exactly one verification method.
|
|
306
306
|
*/
|
|
307
307
|
public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
|
|
308
|
-
keyManager?:
|
|
308
|
+
keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
|
|
309
309
|
portableDid: PortableDid;
|
|
310
310
|
}): Promise<BearerDid> {
|
|
311
311
|
// Verify the DID method is supported.
|