@enbox/dids 0.0.2 → 0.0.4
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/README.md +41 -1
- package/dist/browser.mjs +6 -10
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/bearer-did.js +5 -5
- package/dist/esm/bearer-did.js.map +1 -1
- package/dist/esm/did.js +13 -6
- package/dist/esm/did.js.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/methods/did-dht.js +34 -25
- package/dist/esm/methods/did-dht.js.map +1 -1
- package/dist/esm/methods/did-ion.js +9 -7
- package/dist/esm/methods/did-ion.js.map +1 -1
- package/dist/esm/methods/did-jwk.js +3 -3
- package/dist/esm/methods/did-jwk.js.map +1 -1
- package/dist/esm/methods/did-key.js +12 -240
- package/dist/esm/methods/did-key.js.map +1 -1
- package/dist/esm/methods/did-web.js +3 -2
- package/dist/esm/methods/did-web.js.map +1 -1
- package/dist/esm/resolver/resolver-cache-level.js +1 -1
- package/dist/esm/resolver/resolver-cache-level.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/resolver/resolver-cache-noop.js +10 -10
- package/dist/esm/resolver/resolver-cache-noop.js.map +1 -1
- package/dist/esm/resolver/universal-resolver.js +3 -3
- package/dist/esm/resolver/universal-resolver.js.map +1 -1
- package/dist/esm/utils.js +62 -21
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/bearer-did.d.ts +6 -5
- package/dist/types/bearer-did.d.ts.map +1 -1
- package/dist/types/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.d.ts +6 -6
- package/dist/types/methods/did-dht.d.ts.map +1 -1
- package/dist/types/methods/did-ion.d.ts +6 -5
- package/dist/types/methods/did-ion.d.ts.map +1 -1
- package/dist/types/methods/did-jwk.d.ts +5 -5
- package/dist/types/methods/did-jwk.d.ts.map +1 -1
- package/dist/types/methods/did-key.d.ts +6 -55
- package/dist/types/methods/did-key.d.ts.map +1 -1
- package/dist/types/methods/did-method.d.ts +5 -5
- package/dist/types/methods/did-method.d.ts.map +1 -1
- package/dist/types/resolver/resolver-cache-level.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/resolver/universal-resolver.d.ts +2 -2
- package/dist/types/resolver/universal-resolver.d.ts.map +1 -1
- package/dist/types/types/did-core.d.ts +1 -1
- package/dist/types/types/did-core.d.ts.map +1 -1
- package/dist/types/utils.d.ts +27 -5
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -5
- package/dist/utils.js.map +4 -4
- package/package.json +35 -47
- package/src/bearer-did.ts +15 -14
- package/src/did.ts +8 -6
- package/src/index.ts +2 -0
- package/src/methods/did-dht.ts +37 -37
- package/src/methods/did-ion.ts +23 -15
- package/src/methods/did-jwk.ts +9 -9
- package/src/methods/did-key.ts +29 -310
- package/src/methods/did-method.ts +6 -6
- package/src/methods/did-web.ts +2 -2
- package/src/resolver/resolver-cache-level.ts +4 -4
- package/src/resolver/resolver-cache-memory.ts +84 -0
- package/src/resolver/resolver-cache-noop.ts +10 -10
- package/src/resolver/universal-resolver.ts +5 -5
- package/src/types/did-core.ts +3 -3
- package/src/utils.ts +55 -26
- package/dist/browser.js +0 -77
- package/dist/browser.js.map +0 -7
- package/dist/cjs/index.js +0 -6303
- package/dist/cjs/index.js.map +0 -7
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/utils.js +0 -245
- package/dist/cjs/utils.js.map +0 -7
package/src/methods/did-key.ts
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
-
import type { MulticodecCode, MulticodecDefinition, RequireOnly } from '@enbox/common';
|
|
2
1
|
import type {
|
|
2
|
+
AsymmetricKeyConverter,
|
|
3
|
+
InferKeyGeneratorAlgorithm,
|
|
3
4
|
Jwk,
|
|
4
|
-
CryptoApi,
|
|
5
5
|
KeyCompressor,
|
|
6
6
|
KeyIdentifier,
|
|
7
|
+
KeyImporterExporter,
|
|
8
|
+
KeyManager,
|
|
7
9
|
KmsExportKeyParams,
|
|
8
10
|
KmsImportKeyParams,
|
|
9
|
-
KeyImporterExporter,
|
|
10
|
-
AsymmetricKeyConverter,
|
|
11
|
-
InferKeyGeneratorAlgorithm,
|
|
12
11
|
} from '@enbox/crypto';
|
|
12
|
+
import type { MulticodecCode, MulticodecDefinition } from '@enbox/common';
|
|
13
13
|
|
|
14
|
-
import { Multicodec, universalTypeOf } from '@enbox/common';
|
|
15
14
|
import {
|
|
16
|
-
X25519,
|
|
17
15
|
Ed25519,
|
|
16
|
+
LocalKeyManager,
|
|
18
17
|
Secp256k1,
|
|
19
18
|
Secp256r1,
|
|
20
|
-
LocalKeyManager,
|
|
21
19
|
} from '@enbox/crypto';
|
|
20
|
+
import { Multicodec, universalTypeOf } from '@enbox/common';
|
|
22
21
|
|
|
23
22
|
import type { PortableDid } from '../types/portable-did.js';
|
|
24
23
|
import type { DidCreateOptions, DidCreateVerificationMethod } from './did-method.js';
|
|
@@ -29,12 +28,11 @@ import type {
|
|
|
29
28
|
DidVerificationMethod,
|
|
30
29
|
} from '../types/did-core.js';
|
|
31
30
|
|
|
31
|
+
import { BearerDid } from '../bearer-did.js';
|
|
32
32
|
import { Did } from '../did.js';
|
|
33
33
|
import { DidMethod } from './did-method.js';
|
|
34
|
-
import { BearerDid } from '../bearer-did.js';
|
|
35
|
-
import { DidError, DidErrorCode } from '../did-error.js';
|
|
36
|
-
import { KeyWithMulticodec } from '../types/multibase.js';
|
|
37
34
|
import { EMPTY_DID_RESOLUTION_RESULT } from '../types/did-resolution.js';
|
|
35
|
+
import { DidError, DidErrorCode } from '../did-error.js';
|
|
38
36
|
import { getVerificationMethodTypes, keyBytesToMultibaseId, multibaseIdToKeyBytes } from '../utils.js';
|
|
39
37
|
|
|
40
38
|
/**
|
|
@@ -90,7 +88,7 @@ export interface DidKeyCreateOptions<TKms> extends DidCreateOptions<TKms> {
|
|
|
90
88
|
/**
|
|
91
89
|
* Optionally specify the algorithm to be used for key generation.
|
|
92
90
|
*/
|
|
93
|
-
algorithm?: TKms extends
|
|
91
|
+
algorithm?: TKms extends KeyManager
|
|
94
92
|
? InferKeyGeneratorAlgorithm<TKms>
|
|
95
93
|
: InferKeyGeneratorAlgorithm<LocalKeyManager>;
|
|
96
94
|
|
|
@@ -103,23 +101,6 @@ export interface DidKeyCreateOptions<TKms> extends DidCreateOptions<TKms> {
|
|
|
103
101
|
*/
|
|
104
102
|
defaultContext?: string;
|
|
105
103
|
|
|
106
|
-
/**
|
|
107
|
-
* Optionally enable encryption key derivation during DID creation.
|
|
108
|
-
*
|
|
109
|
-
* By default, this option is set to `false`, which means encryption key derivation is not
|
|
110
|
-
* performed unless explicitly enabled.
|
|
111
|
-
*
|
|
112
|
-
* When set to `true`, an `X25519` key will be derived from the `Ed25519` public key used to
|
|
113
|
-
* create the DID. This feature enables the same DID to be used for encrypted communication, in
|
|
114
|
-
* addition to signature verification.
|
|
115
|
-
*
|
|
116
|
-
* Notes:
|
|
117
|
-
* - This option is ONLY applicable when the `algorithm` of the DID's public key is `Ed25519`.
|
|
118
|
-
* - Enabling this introduces specific cryptographic considerations that should be understood
|
|
119
|
-
* before using the same key pair for digital signatures and encrypted communication. See the following for more information:
|
|
120
|
-
*/
|
|
121
|
-
enableEncryptionKeyDerivation?: boolean;
|
|
122
|
-
|
|
123
104
|
/**
|
|
124
105
|
* Optionally enable experimental public key types during DID creation.
|
|
125
106
|
* By default, this option is set to `false`, which means experimental public key types are not
|
|
@@ -167,11 +148,6 @@ export enum DidKeyRegisteredKeyType {
|
|
|
167
148
|
* and is widely supported in various cryptographic libraries and standards.
|
|
168
149
|
*/
|
|
169
150
|
secp256r1 = 'secp256r1',
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* X25519: A Diffie-Hellman key exchange algorithm using Curve25519.
|
|
173
|
-
*/
|
|
174
|
-
X25519 = 'X25519'
|
|
175
151
|
}
|
|
176
152
|
|
|
177
153
|
/**
|
|
@@ -187,9 +163,6 @@ export const DidKeyVerificationMethodType = {
|
|
|
187
163
|
|
|
188
164
|
/** Represents a JSON Web Key (JWK) used for digital signatures and key agreement protocols. */
|
|
189
165
|
JsonWebKey2020: 'https://w3id.org/security/suites/jws-2020/v1',
|
|
190
|
-
|
|
191
|
-
/** Represents an X25519 public key used for key agreement protocols. */
|
|
192
|
-
X25519KeyAgreementKey2020: 'https://w3id.org/security/suites/x25519-2020/v1',
|
|
193
166
|
} as const;
|
|
194
167
|
|
|
195
168
|
/**
|
|
@@ -203,7 +176,6 @@ const AlgorithmToKeyTypeMap = {
|
|
|
203
176
|
'P-256' : DidKeyRegisteredKeyType.secp256r1,
|
|
204
177
|
secp256k1 : DidKeyRegisteredKeyType.secp256k1,
|
|
205
178
|
secp256r1 : DidKeyRegisteredKeyType.secp256r1,
|
|
206
|
-
X25519 : DidKeyRegisteredKeyType.X25519
|
|
207
179
|
} as const;
|
|
208
180
|
|
|
209
181
|
/**
|
|
@@ -230,13 +202,6 @@ const AlgorithmToKeyTypeMap = {
|
|
|
230
202
|
* for the public key type and the raw public key bytes. To form the DID URI, the method-specific
|
|
231
203
|
* identifier is prefixed with the string 'did:key:'.
|
|
232
204
|
*
|
|
233
|
-
* This method can optionally derive an encryption key from the public key used to create the DID
|
|
234
|
-
* if and only if the public key algorithm is `Ed25519`. This feature enables the same DID to be
|
|
235
|
-
* used for encrypted communication, in addition to signature verification. To enable this
|
|
236
|
-
* feature when calling {@link DidKey.create | `DidKey.create()`}, first specify an `algorithm` of
|
|
237
|
-
* `Ed25519` or provide a `keySet` referencing an `Ed25519` key and then set the
|
|
238
|
-
* `enableEncryptionKeyDerivation` option to `true`.
|
|
239
|
-
*
|
|
240
205
|
* Note:
|
|
241
206
|
* - The authors of the DID Key specification have indicated that use of this method for long-lived
|
|
242
207
|
* use cases is only recommended when accompanied with high confidence that private keys are
|
|
@@ -310,12 +275,6 @@ export class DidKey extends DidMethod {
|
|
|
310
275
|
* {@link https://github.com/multiformats/multicodec/blob/master/README.md | Multicodec}-encoded
|
|
311
276
|
* public key and prefixing with `did:key:`.
|
|
312
277
|
*
|
|
313
|
-
* This method can optionally derive an encryption key from the public key used to create the DID
|
|
314
|
-
* if and only if the public key algorithm is `Ed25519`. This feature enables the same DID to be
|
|
315
|
-
* used for encrypted communication, in addition to signature verification. To enable this
|
|
316
|
-
* feature, specify an `algorithm` of `Ed25519` as either a top-level option or in a
|
|
317
|
-
* `verificationMethod` and set the `enableEncryptionKeyDerivation` option to `true`.
|
|
318
|
-
*
|
|
319
278
|
* Notes:
|
|
320
279
|
* - If no `options` are given, by default a new Ed25519 key will be generated.
|
|
321
280
|
* - The `algorithm` and `verificationMethods` options are mutually exclusive. If both are given,
|
|
@@ -336,7 +295,7 @@ export class DidKey extends DidMethod {
|
|
|
336
295
|
* @param params.options - Optional parameters that can be specified when creating a new DID.
|
|
337
296
|
* @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
|
|
338
297
|
*/
|
|
339
|
-
public static async create<TKms extends
|
|
298
|
+
public static async create<TKms extends KeyManager | undefined = undefined>({
|
|
340
299
|
keyManager = new LocalKeyManager(),
|
|
341
300
|
options = {}
|
|
342
301
|
}: {
|
|
@@ -449,7 +408,7 @@ export class DidKey extends DidMethod {
|
|
|
449
408
|
* @throws An error if the DID document does not contain exactly one verification method.
|
|
450
409
|
*/
|
|
451
410
|
public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
|
|
452
|
-
keyManager?:
|
|
411
|
+
keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
|
|
453
412
|
portableDid: PortableDid;
|
|
454
413
|
}): Promise<BearerDid> {
|
|
455
414
|
// Verify the DID method is supported.
|
|
@@ -491,7 +450,7 @@ export class DidKey extends DidMethod {
|
|
|
491
450
|
|
|
492
451
|
} catch (error: any) {
|
|
493
452
|
// Rethrow any unexpected errors that are not a `DidError`.
|
|
494
|
-
if (!(error instanceof DidError)) throw new Error(error);
|
|
453
|
+
if (!(error instanceof DidError)) {throw new Error(error);}
|
|
495
454
|
|
|
496
455
|
// Return a DID Resolution Result with the appropriate error code.
|
|
497
456
|
return {
|
|
@@ -512,13 +471,12 @@ export class DidKey extends DidMethod {
|
|
|
512
471
|
* @param options
|
|
513
472
|
* @returns - A DID dodcument.
|
|
514
473
|
*/
|
|
515
|
-
private static async createDocument({ didUri, options = {}}: {
|
|
474
|
+
private static async createDocument({ didUri, options = {} }: {
|
|
516
475
|
didUri: string;
|
|
517
|
-
options?: Exclude<DidKeyCreateOptions<
|
|
476
|
+
options?: Exclude<DidKeyCreateOptions<KeyManager>, 'algorithm' | 'verificationMethods'> | DidResolutionOptions;
|
|
518
477
|
}): Promise<DidDocument> {
|
|
519
478
|
const {
|
|
520
479
|
defaultContext = 'https://www.w3.org/ns/did/v1',
|
|
521
|
-
enableEncryptionKeyDerivation = false,
|
|
522
480
|
enableExperimentalPublicKeyTypes = false,
|
|
523
481
|
publicKeyFormat = 'JsonWebKey2020'
|
|
524
482
|
} = options;
|
|
@@ -592,51 +550,7 @@ export class DidKey extends DidMethod {
|
|
|
592
550
|
didDocument.capabilityDelegation = [signatureVerificationMethod.id];
|
|
593
551
|
|
|
594
552
|
/**
|
|
595
|
-
* 8.
|
|
596
|
-
* Add the encryptionVerificationMethod value to the verificationMethod
|
|
597
|
-
* array. Initialize the keyAgreement property in document to an array
|
|
598
|
-
* where the first item is the value of the id property in
|
|
599
|
-
* encryptionVerificationMethod.
|
|
600
|
-
*/
|
|
601
|
-
if (enableEncryptionKeyDerivation === true) {
|
|
602
|
-
/**
|
|
603
|
-
* Although not covered by the did:key method specification, a sensible
|
|
604
|
-
* default will be taken to use the 'X25519KeyAgreementKey2020'
|
|
605
|
-
* verification method type if the given publicKeyFormat is
|
|
606
|
-
* 'Ed25519VerificationKey2020' and 'JsonWebKey2020' otherwise.
|
|
607
|
-
*/
|
|
608
|
-
const encryptionPublicKeyFormat =
|
|
609
|
-
(publicKeyFormat === 'Ed25519VerificationKey2020')
|
|
610
|
-
? 'X25519KeyAgreementKey2020'
|
|
611
|
-
: 'JsonWebKey2020';
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
* 8.1 Initialize the encryptionVerificationMethod to the result of
|
|
615
|
-
* passing identifier, multibaseValue, and options to an
|
|
616
|
-
* {@link https://w3c-ccg.github.io/did-method-key/#encryption-method-creation-algorithm | Encryption Method Creation Algorithm}.
|
|
617
|
-
*/
|
|
618
|
-
const encryptionVerificationMethod = await this.createEncryptionMethod({
|
|
619
|
-
didUri,
|
|
620
|
-
multibaseValue,
|
|
621
|
-
options: { enableExperimentalPublicKeyTypes, publicKeyFormat: encryptionPublicKeyFormat }
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
* 8.2 Add the encryptionVerificationMethod value to the
|
|
626
|
-
* verificationMethod array.
|
|
627
|
-
*/
|
|
628
|
-
didDocument.verificationMethod.push(encryptionVerificationMethod);
|
|
629
|
-
|
|
630
|
-
/**
|
|
631
|
-
* 8.3. Initialize the keyAgreement property in document to an array
|
|
632
|
-
* where the first item is the value of the id property in
|
|
633
|
-
* encryptionVerificationMethod.
|
|
634
|
-
*/
|
|
635
|
-
didDocument.keyAgreement = [encryptionVerificationMethod.id];
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
/**
|
|
639
|
-
* 9. Initialize the @context property in document to the result of passing document and options to the Context
|
|
553
|
+
* 8. Initialize the @context property in document to the result of passing document and options to the Context
|
|
640
554
|
* Creation algorithm.
|
|
641
555
|
*/
|
|
642
556
|
// Set contextArray to an array that is initialized to options.defaultContext.
|
|
@@ -659,125 +573,6 @@ export class DidKey extends DidMethod {
|
|
|
659
573
|
return didDocument;
|
|
660
574
|
}
|
|
661
575
|
|
|
662
|
-
/**
|
|
663
|
-
* Decoding a multibase-encoded multicodec value into a verification method
|
|
664
|
-
* that is suitable for verifying that encrypted information will be
|
|
665
|
-
* received by the intended recipient.
|
|
666
|
-
*/
|
|
667
|
-
private static async createEncryptionMethod({ didUri, multibaseValue, options }: {
|
|
668
|
-
didUri: string;
|
|
669
|
-
multibaseValue: string;
|
|
670
|
-
options: Required<Pick<DidKeyCreateOptions<CryptoApi>, 'enableExperimentalPublicKeyTypes' | 'publicKeyFormat'>>;
|
|
671
|
-
}): Promise<DidVerificationMethod> {
|
|
672
|
-
const { enableExperimentalPublicKeyTypes, publicKeyFormat } = options;
|
|
673
|
-
|
|
674
|
-
/**
|
|
675
|
-
* 1. Initialize verificationMethod to an empty object.
|
|
676
|
-
*/
|
|
677
|
-
const verificationMethod: DidVerificationMethod = { id: '', type: '', controller: '' };
|
|
678
|
-
|
|
679
|
-
/**
|
|
680
|
-
* 2. Set multicodecValue and raw publicKeyBytes to the result of passing multibaseValue and
|
|
681
|
-
* options to a Derive Encryption Key algorithm.
|
|
682
|
-
*/
|
|
683
|
-
const {
|
|
684
|
-
keyBytes: publicKeyBytes,
|
|
685
|
-
multicodecCode: multicodecValue,
|
|
686
|
-
} = await DidKey.deriveEncryptionKey({ multibaseValue });
|
|
687
|
-
|
|
688
|
-
/**
|
|
689
|
-
* 3. Ensure the proper key length of raw publicKeyBytes based on the multicodecValue table
|
|
690
|
-
* provided below:
|
|
691
|
-
*
|
|
692
|
-
* Multicodec hexadecimal value: 0xec
|
|
693
|
-
*
|
|
694
|
-
* If the byte length of raw publicKeyBytes does not match the expected public key length for
|
|
695
|
-
* the associated multicodecValue, an invalidPublicKeyLength error MUST be raised.
|
|
696
|
-
*/
|
|
697
|
-
const actualLength = publicKeyBytes.byteLength;
|
|
698
|
-
const expectedLength = DidKeyUtils.MULTICODEC_PUBLIC_KEY_LENGTH[multicodecValue];
|
|
699
|
-
if (actualLength !== expectedLength) {
|
|
700
|
-
throw new DidError(DidErrorCode.InvalidPublicKeyLength, `Expected ${actualLength} bytes. Actual: ${expectedLength}`);
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
/**
|
|
704
|
-
* 4. Create the multibaseValue by concatenating the letter 'z' and the
|
|
705
|
-
* base58-btc encoding of the concatenation of the multicodecValue and
|
|
706
|
-
* the raw publicKeyBytes.
|
|
707
|
-
*/
|
|
708
|
-
const kemMultibaseValue = keyBytesToMultibaseId({
|
|
709
|
-
keyBytes : publicKeyBytes,
|
|
710
|
-
multicodecCode : multicodecValue
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
/**
|
|
714
|
-
* 5. Set the verificationMethod.id value by concatenating identifier,
|
|
715
|
-
* a hash character (#), and the multibaseValue. If verificationMethod.id
|
|
716
|
-
* is not a valid DID URL, an invalidDidUrl error MUST be raised.
|
|
717
|
-
*/
|
|
718
|
-
verificationMethod.id = `${didUri}#${kemMultibaseValue}`;
|
|
719
|
-
try {
|
|
720
|
-
new URL(verificationMethod.id);
|
|
721
|
-
} catch (error: any) {
|
|
722
|
-
throw new DidError(DidErrorCode.InvalidDidUrl, 'Verification Method ID is not a valid DID URL.');
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* 6. Set the publicKeyFormat value to the options.publicKeyFormat value.
|
|
727
|
-
* 7. If publicKeyFormat is not known to the implementation, an
|
|
728
|
-
* unsupportedPublicKeyType error MUST be raised.
|
|
729
|
-
*/
|
|
730
|
-
if (!(publicKeyFormat in DidKeyVerificationMethodType)) {
|
|
731
|
-
throw new DidError(DidErrorCode.UnsupportedPublicKeyType, `Unsupported format: ${publicKeyFormat}`);
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
/**
|
|
735
|
-
* 8. If options.enableExperimentalPublicKeyTypes is set to false and publicKeyFormat is not
|
|
736
|
-
* Multikey, JsonWebKey2020, or X25519KeyAgreementKey2020, an invalidPublicKeyType error MUST be
|
|
737
|
-
* raised.
|
|
738
|
-
*/
|
|
739
|
-
const StandardPublicKeyTypes = ['Multikey', 'JsonWebKey2020', 'X25519KeyAgreementKey2020'];
|
|
740
|
-
if (enableExperimentalPublicKeyTypes === false
|
|
741
|
-
&& !(StandardPublicKeyTypes.includes(publicKeyFormat))) {
|
|
742
|
-
throw new DidError(DidErrorCode.InvalidPublicKeyType, `Specified '${publicKeyFormat}' without setting enableExperimentalPublicKeyTypes to true.`);
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
/**
|
|
746
|
-
* 9. Set verificationMethod.type to the publicKeyFormat value.
|
|
747
|
-
*/
|
|
748
|
-
verificationMethod.type = publicKeyFormat;
|
|
749
|
-
|
|
750
|
-
/**
|
|
751
|
-
* 10. Set verificationMethod.controller to the identifier value.
|
|
752
|
-
*/
|
|
753
|
-
verificationMethod.controller = didUri;
|
|
754
|
-
|
|
755
|
-
/**
|
|
756
|
-
* 11. If publicKeyFormat is Multikey or X25519KeyAgreementKey2020, set the verificationMethod.publicKeyMultibase
|
|
757
|
-
* value to multibaseValue.
|
|
758
|
-
*
|
|
759
|
-
* Note: This implementation does not currently support the Multikey
|
|
760
|
-
* format.
|
|
761
|
-
*/
|
|
762
|
-
if (publicKeyFormat === 'X25519KeyAgreementKey2020') {
|
|
763
|
-
verificationMethod.publicKeyMultibase = kemMultibaseValue;
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* 12. If publicKeyFormat is JsonWebKey2020, set the verificationMethod.publicKeyJwk value to
|
|
768
|
-
* the result of passing multicodecValue and rawPublicKeyBytes to a JWK encoding algorithm.
|
|
769
|
-
*/
|
|
770
|
-
if (publicKeyFormat === 'JsonWebKey2020') {
|
|
771
|
-
const { crv } = await DidKeyUtils.multicodecToJwk({ code: multicodecValue });
|
|
772
|
-
verificationMethod.publicKeyJwk = await DidKeyUtils.keyConverter(crv!).bytesToPublicKey({ publicKeyBytes });
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
/**
|
|
776
|
-
* 13. Return verificationMethod.
|
|
777
|
-
*/
|
|
778
|
-
return verificationMethod;
|
|
779
|
-
}
|
|
780
|
-
|
|
781
576
|
/**
|
|
782
577
|
* Decodes a multibase-encoded multicodec value into a verification method
|
|
783
578
|
* that is suitable for verifying digital signatures.
|
|
@@ -787,7 +582,7 @@ export class DidKey extends DidMethod {
|
|
|
787
582
|
private static async createSignatureMethod({ didUri, multibaseValue, options }: {
|
|
788
583
|
didUri: string;
|
|
789
584
|
multibaseValue: string;
|
|
790
|
-
options: Required<Pick<DidKeyCreateOptions<
|
|
585
|
+
options: Required<Pick<DidKeyCreateOptions<KeyManager>, 'enableExperimentalPublicKeyTypes' | 'publicKeyFormat'>>
|
|
791
586
|
}): Promise<DidVerificationMethod> {
|
|
792
587
|
const { enableExperimentalPublicKeyTypes, publicKeyFormat } = options;
|
|
793
588
|
|
|
@@ -831,11 +626,6 @@ export class DidKey extends DidMethod {
|
|
|
831
626
|
case 'ed25519-pub':
|
|
832
627
|
isValid = await Ed25519.validatePublicKey({ publicKeyBytes });
|
|
833
628
|
break;
|
|
834
|
-
case 'x25519-pub':
|
|
835
|
-
// TODO: Validate key once/if X25519.validatePublicKey() is implemented.
|
|
836
|
-
// isValid = X25519.validatePublicKey({ key: rawPublicKeyBytes})
|
|
837
|
-
isValid = true;
|
|
838
|
-
break;
|
|
839
629
|
}
|
|
840
630
|
if (!isValid) {
|
|
841
631
|
throw new DidError(DidErrorCode.InvalidPublicKey, 'Invalid public key detected.');
|
|
@@ -849,7 +639,7 @@ export class DidKey extends DidMethod {
|
|
|
849
639
|
verificationMethod.id = `${didUri}#${multibaseValue}`;
|
|
850
640
|
try {
|
|
851
641
|
new URL(verificationMethod.id);
|
|
852
|
-
} catch
|
|
642
|
+
} catch {
|
|
853
643
|
throw new DidError(DidErrorCode.InvalidDidUrl, 'Verification Method ID is not a valid DID URL.');
|
|
854
644
|
}
|
|
855
645
|
|
|
@@ -870,7 +660,10 @@ export class DidKey extends DidMethod {
|
|
|
870
660
|
const StandardPublicKeyTypes = ['Multikey', 'JsonWebKey2020', 'Ed25519VerificationKey2020'];
|
|
871
661
|
if (enableExperimentalPublicKeyTypes === false
|
|
872
662
|
&& !(StandardPublicKeyTypes.includes(publicKeyFormat))) {
|
|
873
|
-
throw new DidError(
|
|
663
|
+
throw new DidError(
|
|
664
|
+
DidErrorCode.InvalidPublicKeyType,
|
|
665
|
+
`Specified '${publicKeyFormat}' without setting enableExperimentalPublicKeyTypes to true.`
|
|
666
|
+
);
|
|
874
667
|
}
|
|
875
668
|
|
|
876
669
|
/**
|
|
@@ -900,7 +693,7 @@ export class DidKey extends DidMethod {
|
|
|
900
693
|
*/
|
|
901
694
|
if (publicKeyFormat === 'JsonWebKey2020') {
|
|
902
695
|
const { crv } = await DidKeyUtils.multicodecToJwk({ code: multicodecValue });
|
|
903
|
-
verificationMethod.publicKeyJwk = await DidKeyUtils.keyConverter(crv!).bytesToPublicKey({ publicKeyBytes});
|
|
696
|
+
verificationMethod.publicKeyJwk = await DidKeyUtils.keyConverter(crv!).bytesToPublicKey({ publicKeyBytes });
|
|
904
697
|
}
|
|
905
698
|
|
|
906
699
|
/**
|
|
@@ -910,72 +703,6 @@ export class DidKey extends DidMethod {
|
|
|
910
703
|
}
|
|
911
704
|
|
|
912
705
|
|
|
913
|
-
/**
|
|
914
|
-
* Transform a multibase-encoded multicodec value to public encryption key
|
|
915
|
-
* components that are suitable for encrypting messages to a receiver. A
|
|
916
|
-
* mathematical proof elaborating on the safety of performing this operation
|
|
917
|
-
* is available in:
|
|
918
|
-
* {@link https://eprint.iacr.org/2021/509.pdf | On using the same key pair for Ed25519 and an X25519 based KEM}
|
|
919
|
-
*/
|
|
920
|
-
private static async deriveEncryptionKey({ multibaseValue }: {
|
|
921
|
-
multibaseValue: string
|
|
922
|
-
}): Promise<RequireOnly<KeyWithMulticodec, 'keyBytes' | 'multicodecCode'>> {
|
|
923
|
-
/**
|
|
924
|
-
* 1. Set publicEncryptionKey to an empty object.
|
|
925
|
-
*/
|
|
926
|
-
let publicEncryptionKey: RequireOnly<KeyWithMulticodec, 'keyBytes' | 'multicodecCode'> = {
|
|
927
|
-
keyBytes : new Uint8Array(),
|
|
928
|
-
multicodecCode : 0
|
|
929
|
-
};
|
|
930
|
-
|
|
931
|
-
/**
|
|
932
|
-
* 2. Decode multibaseValue using the base58-btc multibase alphabet and
|
|
933
|
-
* set multicodecValue to the multicodec header for the decoded value.
|
|
934
|
-
* Implementers are cautioned to ensure that the multicodecValue is set
|
|
935
|
-
* to the result after performing varint decoding.
|
|
936
|
-
*
|
|
937
|
-
* 3. Set the rawPublicKeyBytes to the bytes remaining after the multicodec
|
|
938
|
-
* header.
|
|
939
|
-
*/
|
|
940
|
-
const {
|
|
941
|
-
keyBytes: publicKeyBytes,
|
|
942
|
-
multicodecCode: multicodecValue
|
|
943
|
-
} = multibaseIdToKeyBytes({ multibaseKeyId: multibaseValue });
|
|
944
|
-
|
|
945
|
-
/**
|
|
946
|
-
* 4. If the multicodecValue is 0xed (Ed25519 public key), derive a public X25519 encryption key
|
|
947
|
-
* by using the raw publicKeyBytes and the algorithm defined in
|
|
948
|
-
* {@link https://datatracker.ietf.org/doc/html/draft-ietf-core-oscore-groupcomm | Group OSCORE - Secure Group Communication for CoAP}
|
|
949
|
-
* for Curve25519 in Section 2.4.2: ECDH with Montgomery Coordinates and set
|
|
950
|
-
* generatedPublicEncryptionKeyBytes to the result.
|
|
951
|
-
*/
|
|
952
|
-
if (multicodecValue === 0xed) {
|
|
953
|
-
const ed25519PublicKey = await DidKeyUtils.keyConverter('Ed25519').bytesToPublicKey({
|
|
954
|
-
publicKeyBytes
|
|
955
|
-
});
|
|
956
|
-
const generatedPublicEncryptionKey = await Ed25519.convertPublicKeyToX25519({
|
|
957
|
-
publicKey: ed25519PublicKey
|
|
958
|
-
});
|
|
959
|
-
const generatedPublicEncryptionKeyBytes = await DidKeyUtils.keyConverter('Ed25519').publicKeyToBytes({
|
|
960
|
-
publicKey: generatedPublicEncryptionKey
|
|
961
|
-
});
|
|
962
|
-
|
|
963
|
-
/**
|
|
964
|
-
* 5. Set multicodecValue to 0xec.
|
|
965
|
-
* 6. Set raw public keyBytes to generatedPublicEncryptionKeyBytes.
|
|
966
|
-
*/
|
|
967
|
-
publicEncryptionKey = {
|
|
968
|
-
keyBytes : generatedPublicEncryptionKeyBytes,
|
|
969
|
-
multicodecCode : 0xec
|
|
970
|
-
};
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
/**
|
|
974
|
-
* 7. Return publicEncryptionKey.
|
|
975
|
-
*/
|
|
976
|
-
return publicEncryptionKey;
|
|
977
|
-
}
|
|
978
|
-
|
|
979
706
|
/**
|
|
980
707
|
* Validates the structure and components of a DID URI against the `did:key` method specification.
|
|
981
708
|
*
|
|
@@ -1031,8 +758,6 @@ export class DidKeyUtils {
|
|
|
1031
758
|
'Ed25519:private' : 'ed25519-priv',
|
|
1032
759
|
'secp256k1:public' : 'secp256k1-pub',
|
|
1033
760
|
'secp256k1:private' : 'secp256k1-priv',
|
|
1034
|
-
'X25519:public' : 'x25519-pub',
|
|
1035
|
-
'X25519:private' : 'x25519-priv',
|
|
1036
761
|
};
|
|
1037
762
|
|
|
1038
763
|
/**
|
|
@@ -1043,9 +768,6 @@ export class DidKeyUtils {
|
|
|
1043
768
|
// secp256k1-pub - Secp256k1 public key (compressed) - 33 bytes
|
|
1044
769
|
0xe7: 33,
|
|
1045
770
|
|
|
1046
|
-
// x25519-pub - Curve25519 public key - 32 bytes
|
|
1047
|
-
0xec: 32,
|
|
1048
|
-
|
|
1049
771
|
// ed25519-pub - Ed25519 public key - 32 bytes
|
|
1050
772
|
0xed: 32
|
|
1051
773
|
};
|
|
@@ -1066,12 +788,10 @@ export class DidKeyUtils {
|
|
|
1066
788
|
* ```
|
|
1067
789
|
*/
|
|
1068
790
|
private static MULTICODEC_TO_JWK: { [key: string]: Jwk } = {
|
|
1069
|
-
'ed25519-pub' : { crv: 'Ed25519',
|
|
1070
|
-
'ed25519-priv' : { crv: 'Ed25519',
|
|
1071
|
-
'secp256k1-pub' : { crv: 'secp256k1', kty: 'EC',
|
|
1072
|
-
'secp256k1-priv' : { crv: 'secp256k1', kty: 'EC',
|
|
1073
|
-
'x25519-pub' : { crv: 'X25519', kty: 'OKP', x: '' },
|
|
1074
|
-
'x25519-priv' : { crv: 'X25519', kty: 'OKP', x: '', d: '' },
|
|
791
|
+
'ed25519-pub' : { crv: 'Ed25519', kty: 'OKP', x: '' },
|
|
792
|
+
'ed25519-priv' : { crv: 'Ed25519', kty: 'OKP', x: '', d: '' },
|
|
793
|
+
'secp256k1-pub' : { crv: 'secp256k1', kty: 'EC', x: '', y: '' },
|
|
794
|
+
'secp256k1-priv' : { crv: 'secp256k1', kty: 'EC', x: '', y: '', d: '' },
|
|
1075
795
|
};
|
|
1076
796
|
|
|
1077
797
|
/**
|
|
@@ -1130,7 +850,7 @@ export class DidKeyUtils {
|
|
|
1130
850
|
|
|
1131
851
|
const compressor = compressors[curve];
|
|
1132
852
|
|
|
1133
|
-
if (!compressor) throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);
|
|
853
|
+
if (!compressor) {throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);}
|
|
1134
854
|
|
|
1135
855
|
return compressor;
|
|
1136
856
|
}
|
|
@@ -1146,12 +866,11 @@ export class DidKeyUtils {
|
|
|
1146
866
|
'Ed25519' : Ed25519,
|
|
1147
867
|
'P-256' : Secp256r1,
|
|
1148
868
|
'secp256k1' : Secp256k1,
|
|
1149
|
-
'X25519' : X25519
|
|
1150
869
|
};
|
|
1151
870
|
|
|
1152
871
|
const converter = converters[curve];
|
|
1153
872
|
|
|
1154
|
-
if (!converter) throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);
|
|
873
|
+
if (!converter) {throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);}
|
|
1155
874
|
|
|
1156
875
|
return converter;
|
|
1157
876
|
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
CryptoApi,
|
|
3
|
-
LocalKeyManager,
|
|
4
2
|
InferKeyGeneratorAlgorithm,
|
|
3
|
+
KeyManager,
|
|
4
|
+
LocalKeyManager,
|
|
5
5
|
} from '@enbox/crypto';
|
|
6
6
|
|
|
7
7
|
import type { BearerDid } from '../bearer-did.js';
|
|
8
8
|
import type { DidMetadata } from '../types/portable-did.js';
|
|
9
9
|
import type {
|
|
10
10
|
DidDocument,
|
|
11
|
-
DidResolutionResult,
|
|
12
11
|
DidResolutionOptions,
|
|
12
|
+
DidResolutionResult,
|
|
13
13
|
DidVerificationMethod,
|
|
14
14
|
} from '../types/did-core.js';
|
|
15
15
|
|
|
16
|
-
import { DidVerificationRelationship } from '../types/did-core.js';
|
|
16
|
+
import type { DidVerificationRelationship } from '../types/did-core.js';
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Represents options during the creation of a Decentralized Identifier (DID).
|
|
@@ -47,7 +47,7 @@ export interface DidCreateVerificationMethod<TKms> extends Pick<Partial<DidVerif
|
|
|
47
47
|
* };
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
|
-
algorithm: TKms extends
|
|
50
|
+
algorithm: TKms extends KeyManager
|
|
51
51
|
? InferKeyGeneratorAlgorithm<TKms>
|
|
52
52
|
: InferKeyGeneratorAlgorithm<LocalKeyManager>;
|
|
53
53
|
|
|
@@ -82,7 +82,7 @@ export interface DidCreateVerificationMethod<TKms> extends Pick<Partial<DidVerif
|
|
|
82
82
|
* @typeparam O - The type of the options used for creating the DID.
|
|
83
83
|
*/
|
|
84
84
|
export interface DidMethodApi<
|
|
85
|
-
TKms extends
|
|
85
|
+
TKms extends KeyManager | undefined = KeyManager,
|
|
86
86
|
TDid extends BearerDid = BearerDid,
|
|
87
87
|
TOptions extends DidCreateOptions<TKms> = DidCreateOptions<TKms>
|
|
88
88
|
> extends DidMethodResolver {
|
package/src/methods/did-web.ts
CHANGED
|
@@ -75,7 +75,7 @@ export class DidWeb extends DidMethod {
|
|
|
75
75
|
const response = await fetch(didDocumentUrl);
|
|
76
76
|
|
|
77
77
|
// If the response status code is not 200, return an error.
|
|
78
|
-
if (!response.ok) throw new Error('HTTP error status code returned');
|
|
78
|
+
if (!response.ok) {throw new Error('HTTP error status code returned');}
|
|
79
79
|
|
|
80
80
|
// Parse the DID document.
|
|
81
81
|
const didDocument = await response.json() as DidDocument;
|
|
@@ -85,7 +85,7 @@ export class DidWeb extends DidMethod {
|
|
|
85
85
|
didDocument,
|
|
86
86
|
};
|
|
87
87
|
|
|
88
|
-
} catch
|
|
88
|
+
} catch {
|
|
89
89
|
// If the DID document could not be retrieved, return an error.
|
|
90
90
|
return {
|
|
91
91
|
...EMPTY_DID_RESOLUTION_RESULT,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AbstractLevel } from 'abstract-level';
|
|
2
2
|
|
|
3
|
-
import ms from 'ms';
|
|
4
3
|
import { Level } from 'level';
|
|
4
|
+
import ms from 'ms';
|
|
5
5
|
|
|
6
6
|
import type { DidResolutionResult } from '../types/did-core.js';
|
|
7
7
|
import type { DidResolverCache } from '../types/did-resolution.js';
|
|
@@ -31,7 +31,7 @@ export type DidResolverCacheLevelParams = {
|
|
|
31
31
|
* to '15m' if not specified.
|
|
32
32
|
*/
|
|
33
33
|
ttl?: string;
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Encapsulates a DID resolution result along with its expiration information for caching purposes.
|
|
@@ -54,7 +54,7 @@ type CachedDidResolutionResult = {
|
|
|
54
54
|
* This object contains the resolved DID document and associated metadata.
|
|
55
55
|
*/
|
|
56
56
|
value: DidResolutionResult;
|
|
57
|
-
}
|
|
57
|
+
};
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
60
|
* A Level-based cache implementation for storing and retrieving DID resolution results.
|
|
@@ -109,7 +109,7 @@ export class DidResolverCacheLevel implements DidResolverCache {
|
|
|
109
109
|
return cachedDidResolutionResult.value;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
} catch(error: any) {
|
|
112
|
+
} catch (error: any) {
|
|
113
113
|
// Don't throw when a key wasn't found.
|
|
114
114
|
if (error.notFound) {
|
|
115
115
|
return;
|