@dwn-protocol/id-sdk 0.2.5 → 0.2.6
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/package.json +2 -3
- package/src/agent/app-data-store.ts +0 -365
- package/src/agent/did-manager.ts +0 -393
- package/src/agent/dwn-manager.ts +0 -548
- package/src/agent/identity-manager.ts +0 -165
- package/src/agent/index.ts +0 -19
- package/src/agent/json-rpc.ts +0 -107
- package/src/agent/key-manager.ts +0 -302
- package/src/agent/kms-local.ts +0 -412
- package/src/agent/outbox.ts +0 -128
- package/src/agent/rpc-client.ts +0 -223
- package/src/agent/store-managed-did.ts +0 -295
- package/src/agent/store-managed-identity.ts +0 -243
- package/src/agent/store-managed-key.ts +0 -754
- package/src/agent/sync-manager.ts +0 -631
- package/src/agent/test-managed-agent.ts +0 -299
- package/src/agent/types/agent.ts +0 -145
- package/src/agent/types/managed-key.ts +0 -442
- package/src/agent/utils.ts +0 -190
- package/src/common/convert.ts +0 -424
- package/src/common/index.ts +0 -9
- package/src/common/multicodec.ts +0 -176
- package/src/common/object.ts +0 -43
- package/src/common/stores.ts +0 -125
- package/src/common/stream-node.ts +0 -381
- package/src/common/stream.ts +0 -406
- package/src/common/type-utils.ts +0 -117
- package/src/common/types.ts +0 -48
- package/src/credentials/credential-bbs.ts +0 -419
- package/src/credentials/credential.ts +0 -324
- package/src/credentials/index.ts +0 -5
- package/src/credentials/presentation.ts +0 -182
- package/src/credentials/status-list.ts +0 -365
- package/src/credentials/utils.ts +0 -58
- package/src/credentials/validators.ts +0 -52
- package/src/crypto/algorithms-api/aes/base.ts +0 -49
- package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
- package/src/crypto/algorithms-api/aes/index.ts +0 -2
- package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
- package/src/crypto/algorithms-api/crypto-key.ts +0 -56
- package/src/crypto/algorithms-api/ec/base.ts +0 -39
- package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
- package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
- package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
- package/src/crypto/algorithms-api/ec/index.ts +0 -4
- package/src/crypto/algorithms-api/errors.ts +0 -29
- package/src/crypto/algorithms-api/index.ts +0 -6
- package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
- package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
- package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
- package/src/crypto/crypto-algorithms/bbs.ts +0 -110
- package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
- package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
- package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
- package/src/crypto/crypto-algorithms/index.ts +0 -6
- package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
- package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
- package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
- package/src/crypto/crypto-primitives/bbs.ts +0 -183
- package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
- package/src/crypto/crypto-primitives/ed25519.ts +0 -201
- package/src/crypto/crypto-primitives/index.ts +0 -10
- package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
- package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
- package/src/crypto/crypto-primitives/x25519.ts +0 -101
- package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
- package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
- package/src/crypto/index.ts +0 -8
- package/src/crypto/jose.ts +0 -948
- package/src/crypto/types/crypto-key.ts +0 -4
- package/src/crypto/types/iddwn-crypto.ts +0 -119
- package/src/crypto/utils.ts +0 -200
- package/src/did-api.ts +0 -72
- package/src/dids/dht.ts +0 -412
- package/src/dids/did-dht.ts +0 -436
- package/src/dids/did-ion.ts +0 -613
- package/src/dids/did-key.ts +0 -791
- package/src/dids/did-resolver.ts +0 -107
- package/src/dids/index.ts +0 -9
- package/src/dids/resolver-cache-level.ts +0 -82
- package/src/dids/resolver-cache-noop.ts +0 -25
- package/src/dids/types.ts +0 -278
- package/src/dids/utils.ts +0 -129
- package/src/dwn-api.ts +0 -584
- package/src/iddwn.ts +0 -241
- package/src/identity-agent/index.ts +0 -270
- package/src/index.ts +0 -26
- package/src/interfaces/metadata.ts +0 -163
- package/src/interfaces/queue.ts +0 -108
- package/src/interfaces/services.ts +0 -122
- package/src/interfaces/transactions.ts +0 -220
- package/src/protocol.ts +0 -68
- package/src/proxy-agent/index.ts +0 -255
- package/src/record.ts +0 -521
- package/src/service-options.ts +0 -62
- package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
- package/src/user-agent/index.ts +0 -295
- package/src/utils.ts +0 -29
- package/src/vc-api.ts +0 -505
package/src/dids/did-dht.ts
DELETED
|
@@ -1,436 +0,0 @@
|
|
|
1
|
-
import type { JwkKeyPair, PublicKeyJwk, IDCrypto } from '../crypto/index.js';
|
|
2
|
-
import type {
|
|
3
|
-
DidMethod,
|
|
4
|
-
DidService,
|
|
5
|
-
DidDocument,
|
|
6
|
-
PortableDid,
|
|
7
|
-
DidResolutionResult,
|
|
8
|
-
DidResolutionOptions,
|
|
9
|
-
VerificationRelationship,
|
|
10
|
-
DidKeySetVerificationMethodKey,
|
|
11
|
-
DwnServiceEndpoint,
|
|
12
|
-
} from './types.js';
|
|
13
|
-
|
|
14
|
-
import { DidDht } from './dht.js';
|
|
15
|
-
import { EcdsaAlgorithm, EdDsaAlgorithm, Jose } from '../crypto/index.js';
|
|
16
|
-
import { parseDid } from './utils.js';
|
|
17
|
-
|
|
18
|
-
// for base32
|
|
19
|
-
import z32 from 'z32';
|
|
20
|
-
|
|
21
|
-
const SupportedCryptoKeyTypes = [
|
|
22
|
-
'Ed25519',
|
|
23
|
-
'secp256k1'
|
|
24
|
-
] as const;
|
|
25
|
-
|
|
26
|
-
export type DidDhtCreateOptions = {
|
|
27
|
-
publish?: boolean;
|
|
28
|
-
relay?: string;
|
|
29
|
-
keySet?: DidDhtKeySet;
|
|
30
|
-
services?: DidService[];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type DidDhtKeySet = {
|
|
34
|
-
verificationMethodKeys?: DidKeySetVerificationMethodKey[];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class DidDhtMethod implements DidMethod {
|
|
38
|
-
|
|
39
|
-
public static methodName = 'dht';
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Creates a new DID Document according to the did:dht spec.
|
|
43
|
-
* @param options The options to use when creating the DID Document, including whether to publish it.
|
|
44
|
-
* @returns A promise that resolves to a PortableDid object.
|
|
45
|
-
*/
|
|
46
|
-
public static async create(options?: DidDhtCreateOptions): Promise<PortableDid> {
|
|
47
|
-
const { publish = false, relay, keySet: initialKeySet, services } = options ?? {};
|
|
48
|
-
|
|
49
|
-
// Generate missing keys, if not provided in the options.
|
|
50
|
-
const keySet = await this.generateKeySet({ keySet: initialKeySet });
|
|
51
|
-
|
|
52
|
-
// Get the identifier and set it.
|
|
53
|
-
const identityKey = keySet.verificationMethodKeys.find(key => key.publicKeyJwk.kid === '0');
|
|
54
|
-
const id = await this.getDidIdentifier({ key: identityKey.publicKeyJwk });
|
|
55
|
-
|
|
56
|
-
// Add all other keys to the verificationMethod and relationship arrays.
|
|
57
|
-
const relationshipsMap: Partial<Record<VerificationRelationship, string[]>> = {};
|
|
58
|
-
const verificationMethods = keySet.verificationMethodKeys.map(key => {
|
|
59
|
-
for (const relationship of key.relationships) {
|
|
60
|
-
if (relationshipsMap[relationship]) {
|
|
61
|
-
relationshipsMap[relationship].push(`#${key.publicKeyJwk.kid}`);
|
|
62
|
-
} else {
|
|
63
|
-
relationshipsMap[relationship] = [`#${key.publicKeyJwk.kid}`];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
id : `${id}#${key.publicKeyJwk.kid}`,
|
|
69
|
-
type : 'JsonWebKey2020',
|
|
70
|
-
controller : id,
|
|
71
|
-
// Keep DID document JWK minimal and schema-safe for DWN validators.
|
|
72
|
-
publicKeyJwk : DidDhtMethod.toDidDocumentPublicJwk(key.publicKeyJwk)
|
|
73
|
-
};
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// Normalize service IDs to absolute DID URL form (e.g., did:dht:...#dwn).
|
|
77
|
-
services?.map(service => {
|
|
78
|
-
if (service.id.startsWith('did:')) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (service.id.startsWith('#')) {
|
|
83
|
-
service.id = `${id}${service.id}`;
|
|
84
|
-
} else {
|
|
85
|
-
service.id = `${id}#${service.id}`;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// Assemble the DID Document.
|
|
90
|
-
const document: DidDocument = {
|
|
91
|
-
id,
|
|
92
|
-
verificationMethod: [...verificationMethods],
|
|
93
|
-
...relationshipsMap,
|
|
94
|
-
...services && { service: services }
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// If the publish flag is set, publish the DID Document to the DHT.
|
|
98
|
-
if (publish) {
|
|
99
|
-
await this.publish({ identityKey, didDocument: document, relay });
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
did : document.id,
|
|
104
|
-
document : document,
|
|
105
|
-
keySet : keySet
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Generates a JWK key pair.
|
|
112
|
-
* @param options The key algorithm and key ID to use.
|
|
113
|
-
* @returns A promise that resolves to a JwkKeyPair object.
|
|
114
|
-
*/
|
|
115
|
-
public static async generateJwkKeyPair(options: {
|
|
116
|
-
keyAlgorithm: typeof SupportedCryptoKeyTypes[number],
|
|
117
|
-
keyId?: string
|
|
118
|
-
}): Promise<JwkKeyPair> {
|
|
119
|
-
const {keyAlgorithm, keyId} = options;
|
|
120
|
-
|
|
121
|
-
let cryptoKeyPair: IDCrypto.CryptoKeyPair;
|
|
122
|
-
|
|
123
|
-
switch (keyAlgorithm) {
|
|
124
|
-
case 'Ed25519': {
|
|
125
|
-
cryptoKeyPair = await new EdDsaAlgorithm().generateKey({
|
|
126
|
-
algorithm : {name: 'EdDSA', namedCurve: 'Ed25519'},
|
|
127
|
-
extractable : true,
|
|
128
|
-
keyUsages : ['sign', 'verify']
|
|
129
|
-
});
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
case 'secp256k1': {
|
|
134
|
-
cryptoKeyPair = await new EcdsaAlgorithm().generateKey({
|
|
135
|
-
algorithm : {name: 'ECDSA', namedCurve: 'secp256k1'},
|
|
136
|
-
extractable : true,
|
|
137
|
-
keyUsages : ['sign', 'verify']
|
|
138
|
-
});
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
default: {
|
|
143
|
-
throw new Error(`Unsupported crypto algorithm: '${keyAlgorithm}'`);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Convert the CryptoKeyPair to JwkKeyPair.
|
|
148
|
-
const jwkKeyPair = await Jose.cryptoKeyToJwkPair({keyPair: cryptoKeyPair});
|
|
149
|
-
|
|
150
|
-
// Set kid values.
|
|
151
|
-
if (keyId) {
|
|
152
|
-
jwkKeyPair.privateKeyJwk.kid = keyId;
|
|
153
|
-
jwkKeyPair.publicKeyJwk.kid = keyId;
|
|
154
|
-
} else {
|
|
155
|
-
// If a key ID is not specified, generate RFC 7638 JWK thumbprint.
|
|
156
|
-
const jwkThumbprint = await Jose.jwkThumbprint({key: jwkKeyPair.publicKeyJwk});
|
|
157
|
-
jwkKeyPair.privateKeyJwk.kid = jwkThumbprint;
|
|
158
|
-
jwkKeyPair.publicKeyJwk.kid = jwkThumbprint;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return jwkKeyPair;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Generates a key set for a DID Document.
|
|
166
|
-
* @param options The key set to use when generating the key set.
|
|
167
|
-
* @returns A promise that resolves to a DidDhtKeySet object.
|
|
168
|
-
*/
|
|
169
|
-
public static async generateKeySet(options?: {
|
|
170
|
-
keySet?: DidDhtKeySet
|
|
171
|
-
}): Promise<DidDhtKeySet> {
|
|
172
|
-
let { keySet = {} } = options ?? {};
|
|
173
|
-
|
|
174
|
-
// If the key set is missing a `verificationMethodKeys` array, create one.
|
|
175
|
-
if (!keySet.verificationMethodKeys) keySet.verificationMethodKeys = [];
|
|
176
|
-
|
|
177
|
-
// If the key set lacks an identity key (`kid: 0`), generate one.
|
|
178
|
-
if (!keySet.verificationMethodKeys.some(key => key.publicKeyJwk.kid === '0')) {
|
|
179
|
-
const identityKey = await this.generateJwkKeyPair({
|
|
180
|
-
keyAlgorithm : 'Ed25519',
|
|
181
|
-
keyId : '0'
|
|
182
|
-
});
|
|
183
|
-
keySet.verificationMethodKeys.push({
|
|
184
|
-
...identityKey,
|
|
185
|
-
relationships: ['authentication', 'assertionMethod', 'capabilityInvocation', 'capabilityDelegation']
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Generate RFC 7638 JWK thumbprints if `kid` is missing from any key.
|
|
190
|
-
for (const key of keySet.verificationMethodKeys) {
|
|
191
|
-
if (key.publicKeyJwk) key.publicKeyJwk.kid ??= await Jose.jwkThumbprint({key: key.publicKeyJwk});
|
|
192
|
-
if (key.privateKeyJwk) key.privateKeyJwk.kid ??= await Jose.jwkThumbprint({key: key.privateKeyJwk});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return keySet;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Gets the identifier fragment from a DID.
|
|
200
|
-
* @param options The key to get the identifier fragment from.
|
|
201
|
-
* @returns A promise that resolves to a string containing the identifier.
|
|
202
|
-
*/
|
|
203
|
-
public static async getDidIdentifier(options: {
|
|
204
|
-
key: PublicKeyJwk
|
|
205
|
-
}): Promise<string> {
|
|
206
|
-
const { key } = options;
|
|
207
|
-
|
|
208
|
-
const cryptoKey = await Jose.jwkToCryptoKey({ key });
|
|
209
|
-
const identifier = z32.encode(cryptoKey.material);
|
|
210
|
-
return 'did:dht:' + identifier;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Gets the identifier fragment from a DID.
|
|
215
|
-
* @param options The key to get the identifier fragment from.
|
|
216
|
-
* @returns A promise that resolves to a string containing the identifier fragment.
|
|
217
|
-
*/
|
|
218
|
-
public static async getDidIdentifierFragment(options: {
|
|
219
|
-
key: PublicKeyJwk
|
|
220
|
-
}): Promise<string> {
|
|
221
|
-
const { key } = options;
|
|
222
|
-
const cryptoKey = await Jose.jwkToCryptoKey({ key });
|
|
223
|
-
return z32.encode(cryptoKey.material);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Publishes a DID Document to the DHT.
|
|
228
|
-
* @param keySet The key set to use to sign the DHT payload.
|
|
229
|
-
* @param didDocument The DID Document to publish.
|
|
230
|
-
* @returns A boolean indicating the success of the publishing operation.
|
|
231
|
-
*/
|
|
232
|
-
public static async publish({ didDocument, identityKey, relay }: {
|
|
233
|
-
didDocument: DidDocument,
|
|
234
|
-
identityKey: DidKeySetVerificationMethodKey,
|
|
235
|
-
relay?: string
|
|
236
|
-
}): Promise<boolean> {
|
|
237
|
-
const publicCryptoKey = await Jose.jwkToCryptoKey({key: identityKey.publicKeyJwk});
|
|
238
|
-
const privateCryptoKey = await Jose.jwkToCryptoKey({key: identityKey.privateKeyJwk});
|
|
239
|
-
|
|
240
|
-
const isPublished = await DidDht.publishDidDocument({
|
|
241
|
-
keyPair: {
|
|
242
|
-
publicKey : publicCryptoKey,
|
|
243
|
-
privateKey : privateCryptoKey
|
|
244
|
-
},
|
|
245
|
-
didDocument,
|
|
246
|
-
relay
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
return isPublished;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Resolves a DID Document based on the specified options.
|
|
254
|
-
*
|
|
255
|
-
* @param options - Configuration for resolving a DID Document.
|
|
256
|
-
* @param options.didUrl - The DID URL to resolve.
|
|
257
|
-
* @param options.resolutionOptions - Optional settings for the DID resolution process as defined in the DID Core specification.
|
|
258
|
-
* @returns A Promise that resolves to a `DidResolutionResult`, containing the resolved DID Document and associated metadata.
|
|
259
|
-
*/
|
|
260
|
-
public static async resolve(options: {
|
|
261
|
-
didUrl: string,
|
|
262
|
-
resolutionOptions?: DidResolutionOptions
|
|
263
|
-
}): Promise<DidResolutionResult> {
|
|
264
|
-
const { didUrl, resolutionOptions } = options;
|
|
265
|
-
// TODO: Implement resolutionOptions as defined in https://www.w3.org/TR/did-core/#did-resolution
|
|
266
|
-
|
|
267
|
-
const parsedDid = parseDid({ didUrl });
|
|
268
|
-
if (!parsedDid) {
|
|
269
|
-
return {
|
|
270
|
-
'@context' : 'https://w3id.org/did-resolution/v1',
|
|
271
|
-
didDocument : null,
|
|
272
|
-
didDocumentMetadata : {},
|
|
273
|
-
didResolutionMetadata : {
|
|
274
|
-
contentType : 'application/did+json',
|
|
275
|
-
error : 'invalidDid',
|
|
276
|
-
errorMessage : `Cannot parse DID: ${didUrl}`
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
if (parsedDid.method !== 'dht') {
|
|
282
|
-
return {
|
|
283
|
-
'@context' : 'https://w3id.org/did-resolution/v1',
|
|
284
|
-
didDocument : null,
|
|
285
|
-
didDocumentMetadata : {},
|
|
286
|
-
didResolutionMetadata : {
|
|
287
|
-
contentType : 'application/did+json',
|
|
288
|
-
error : 'methodNotSupported',
|
|
289
|
-
errorMessage : `Method not supported: ${parsedDid.method}`
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
let didDocument: DidDocument;
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* As of 5 Dec 2023, the `pkarr` library throws an error if the DID is not found. Until a
|
|
298
|
-
* better solution is found, catch the error and return a DID Resolution Result with an
|
|
299
|
-
* error message.
|
|
300
|
-
*/
|
|
301
|
-
try {
|
|
302
|
-
const relay = resolutionOptions?.relay;
|
|
303
|
-
didDocument = await DidDht.getDidDocument({ did: parsedDid.did, relay });
|
|
304
|
-
} catch (error: any) {
|
|
305
|
-
return {
|
|
306
|
-
'@context' : 'https://w3id.org/did-resolution/v1',
|
|
307
|
-
didDocument : null,
|
|
308
|
-
didDocumentMetadata : {},
|
|
309
|
-
didResolutionMetadata : {
|
|
310
|
-
contentType : 'application/did+json',
|
|
311
|
-
error : 'internalError',
|
|
312
|
-
errorMessage : `An unexpected error occurred while resolving DID: ${parsedDid.did}`
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
return {
|
|
318
|
-
'@context' : 'https://w3id.org/did-resolution/v1',
|
|
319
|
-
didDocument,
|
|
320
|
-
didDocumentMetadata : {},
|
|
321
|
-
didResolutionMetadata : {
|
|
322
|
-
contentType : 'application/did+json',
|
|
323
|
-
did : {
|
|
324
|
-
didString : parsedDid.did,
|
|
325
|
-
methodSpecificId : parsedDid.id,
|
|
326
|
-
method : parsedDid.method
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
public static async getDefaultSigningKey(options: {
|
|
333
|
-
didDocument: DidDocument
|
|
334
|
-
}): Promise<string | undefined> {
|
|
335
|
-
const { didDocument } = options;
|
|
336
|
-
|
|
337
|
-
if (didDocument.authentication
|
|
338
|
-
&& Array.isArray(didDocument.authentication)
|
|
339
|
-
&& didDocument.authentication.length > 0
|
|
340
|
-
&& typeof didDocument.authentication[0] === 'string') {
|
|
341
|
-
|
|
342
|
-
const [verificationMethodId] = didDocument.authentication;
|
|
343
|
-
const did = didDocument.id;
|
|
344
|
-
const signingKeyId = verificationMethodId.startsWith('#')
|
|
345
|
-
? `${did}${verificationMethodId}`
|
|
346
|
-
: verificationMethodId;
|
|
347
|
-
|
|
348
|
-
return signingKeyId;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Generates a key set and service configuration for a DWN-enabled DID.
|
|
354
|
-
*
|
|
355
|
-
* @param options - Configuration options for generating DWN options
|
|
356
|
-
* @param options.serviceEndpointNodes - Array of DWN endpoint URLs
|
|
357
|
-
* @param options.serviceId - Service ID for the DWN service (defaults to '#dwn')
|
|
358
|
-
* @param options.signingKeyAlgorithm - Algorithm for signing key (defaults to 'Ed25519')
|
|
359
|
-
* @param options.signingKeyId - Key ID for signing key (defaults to '0')
|
|
360
|
-
* @param options.encryptionKeyId - Key ID for encryption key (defaults to '1')
|
|
361
|
-
* @returns A promise that resolves to DidDhtCreateOptions
|
|
362
|
-
*/
|
|
363
|
-
public static async generateDwnOptions(options: {
|
|
364
|
-
encryptionKeyId?: string,
|
|
365
|
-
serviceEndpointNodes: string[],
|
|
366
|
-
serviceId?: string,
|
|
367
|
-
signingKeyAlgorithm?: typeof SupportedCryptoKeyTypes[number]
|
|
368
|
-
signingKeyId?: string,
|
|
369
|
-
}): Promise<DidDhtCreateOptions> {
|
|
370
|
-
const {
|
|
371
|
-
signingKeyAlgorithm = 'Ed25519', // Generate Ed25519 key pairs, by default.
|
|
372
|
-
serviceId = '#dwn', // Use default ID value, unless overridden.
|
|
373
|
-
signingKeyId = '0', // Use default key ID value for DHT (identity key)
|
|
374
|
-
encryptionKeyId = '1', // Use default key ID value for encryption key
|
|
375
|
-
serviceEndpointNodes } = options;
|
|
376
|
-
|
|
377
|
-
const signingKeyPair = await DidDhtMethod.generateJwkKeyPair({
|
|
378
|
-
keyAlgorithm : signingKeyAlgorithm,
|
|
379
|
-
keyId : signingKeyId
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
/** Currently, `id` has only implemented support for record
|
|
383
|
-
* encryption using the `ECIES-ES256K` crypto algorithm. Until the
|
|
384
|
-
* DWN SDK supports ECIES with EdDSA, the encryption key pair must
|
|
385
|
-
* use secp256k1. */
|
|
386
|
-
const encryptionKeyPair = await DidDhtMethod.generateJwkKeyPair({
|
|
387
|
-
keyAlgorithm : 'secp256k1',
|
|
388
|
-
keyId : encryptionKeyId
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
const keySet: DidDhtKeySet = {
|
|
392
|
-
verificationMethodKeys: [
|
|
393
|
-
{ ...signingKeyPair, relationships: ['authentication', 'assertionMethod', 'capabilityInvocation', 'capabilityDelegation'] },
|
|
394
|
-
{ ...encryptionKeyPair, relationships: ['keyAgreement'] }
|
|
395
|
-
]
|
|
396
|
-
};
|
|
397
|
-
|
|
398
|
-
const serviceEndpoint: DwnServiceEndpoint = {
|
|
399
|
-
encryptionKeys : [`#${encryptionKeyId}`],
|
|
400
|
-
nodes : serviceEndpointNodes,
|
|
401
|
-
signingKeys : [`#${signingKeyId}`]
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
const services: DidService[] = [{
|
|
405
|
-
id : serviceId,
|
|
406
|
-
serviceEndpoint,
|
|
407
|
-
type : 'DecentralizedWebNode',
|
|
408
|
-
}];
|
|
409
|
-
|
|
410
|
-
return { keySet, services };
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Convert a JWK to a DID-document-safe public JWK shape.
|
|
415
|
-
* Excludes private and WebCrypto-only properties that can fail downstream
|
|
416
|
-
* schema validation in some DWN implementations.
|
|
417
|
-
*/
|
|
418
|
-
private static toDidDocumentPublicJwk(publicJwk: PublicKeyJwk): PublicKeyJwk {
|
|
419
|
-
const sanitized: any = {
|
|
420
|
-
alg : publicJwk.alg,
|
|
421
|
-
crv : (publicJwk as any).crv,
|
|
422
|
-
kid : publicJwk.kid,
|
|
423
|
-
kty : publicJwk.kty,
|
|
424
|
-
x : (publicJwk as any).x,
|
|
425
|
-
y : (publicJwk as any).y,
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
// Remove undefined fields and any private key material if present.
|
|
429
|
-
delete sanitized.d;
|
|
430
|
-
Object.keys(sanitized).forEach((key) => {
|
|
431
|
-
if (sanitized[key] === undefined) delete sanitized[key];
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
return sanitized as PublicKeyJwk;
|
|
435
|
-
}
|
|
436
|
-
}
|