@enbox/dids 0.0.4 → 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.
Files changed (48) hide show
  1. package/dist/browser.mjs +1 -1
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/methods/did-dht-dns.js +455 -0
  4. package/dist/esm/methods/did-dht-dns.js.map +1 -0
  5. package/dist/esm/methods/did-dht-pkarr.js +168 -0
  6. package/dist/esm/methods/did-dht-pkarr.js.map +1 -0
  7. package/dist/esm/methods/did-dht-types.js +116 -0
  8. package/dist/esm/methods/did-dht-types.js.map +1 -0
  9. package/dist/esm/methods/did-dht-utils.js +143 -0
  10. package/dist/esm/methods/did-dht-utils.js.map +1 -0
  11. package/dist/esm/methods/did-dht.js +65 -842
  12. package/dist/esm/methods/did-dht.js.map +1 -1
  13. package/dist/esm/methods/did-ion-utils.js +161 -0
  14. package/dist/esm/methods/did-ion-utils.js.map +1 -0
  15. package/dist/esm/methods/did-ion.js +4 -151
  16. package/dist/esm/methods/did-ion.js.map +1 -1
  17. package/dist/esm/methods/did-key-utils.js +235 -0
  18. package/dist/esm/methods/did-key-utils.js.map +1 -0
  19. package/dist/esm/methods/did-key.js +6 -222
  20. package/dist/esm/methods/did-key.js.map +1 -1
  21. package/dist/types/methods/did-dht-dns.d.ts +114 -0
  22. package/dist/types/methods/did-dht-dns.d.ts.map +1 -0
  23. package/dist/types/methods/did-dht-pkarr.d.ts +56 -0
  24. package/dist/types/methods/did-dht-pkarr.d.ts.map +1 -0
  25. package/dist/types/methods/did-dht-types.d.ts +286 -0
  26. package/dist/types/methods/did-dht-types.d.ts.map +1 -0
  27. package/dist/types/methods/did-dht-utils.d.ts +54 -0
  28. package/dist/types/methods/did-dht-utils.d.ts.map +1 -0
  29. package/dist/types/methods/did-dht.d.ts +42 -457
  30. package/dist/types/methods/did-dht.d.ts.map +1 -1
  31. package/dist/types/methods/did-ion-utils.d.ts +86 -0
  32. package/dist/types/methods/did-ion-utils.d.ts.map +1 -0
  33. package/dist/types/methods/did-ion.d.ts +2 -82
  34. package/dist/types/methods/did-ion.d.ts.map +1 -1
  35. package/dist/types/methods/did-key-utils.d.ts +138 -0
  36. package/dist/types/methods/did-key-utils.d.ts.map +1 -0
  37. package/dist/types/methods/did-key.d.ts +3 -124
  38. package/dist/types/methods/did-key.d.ts.map +1 -1
  39. package/package.json +1 -1
  40. package/src/methods/did-dht-dns.ts +516 -0
  41. package/src/methods/did-dht-pkarr.ts +192 -0
  42. package/src/methods/did-dht-types.ts +316 -0
  43. package/src/methods/did-dht-utils.ts +157 -0
  44. package/src/methods/did-dht.ts +122 -1128
  45. package/src/methods/did-ion-utils.ts +186 -0
  46. package/src/methods/did-ion.ts +14 -190
  47. package/src/methods/did-key-utils.ts +258 -0
  48. package/src/methods/did-key.ts +14 -266
@@ -1,39 +1,30 @@
1
+ import type { PortableDid } from '../types/portable-did.js';
2
+ import type { DidCreateOptions, DidCreateVerificationMethod } from './did-method.js';
3
+ import type {
4
+ DidDocument,
5
+ DidResolutionOptions,
6
+ DidResolutionResult,
7
+ DidVerificationMethod,
8
+ } from '../types/did-core.js';
1
9
  import type {
2
- AsymmetricKeyConverter,
3
10
  InferKeyGeneratorAlgorithm,
4
- Jwk,
5
- KeyCompressor,
6
11
  KeyIdentifier,
7
12
  KeyImporterExporter,
8
13
  KeyManager,
9
14
  KmsExportKeyParams,
10
15
  KmsImportKeyParams,
11
16
  } from '@enbox/crypto';
12
- import type { MulticodecCode, MulticodecDefinition } from '@enbox/common';
13
17
 
14
- import {
15
- Ed25519,
16
- LocalKeyManager,
17
- Secp256k1,
18
- Secp256r1,
19
- } from '@enbox/crypto';
20
- import { Multicodec, universalTypeOf } from '@enbox/common';
21
-
22
- import type { PortableDid } from '../types/portable-did.js';
23
- import type { DidCreateOptions, DidCreateVerificationMethod } from './did-method.js';
24
- import type {
25
- DidDocument,
26
- DidResolutionOptions,
27
- DidResolutionResult,
28
- DidVerificationMethod,
29
- } from '../types/did-core.js';
18
+ import { universalTypeOf } from '@enbox/common';
19
+ import { Ed25519, LocalKeyManager, Secp256k1 } from '@enbox/crypto';
30
20
 
31
21
  import { BearerDid } from '../bearer-did.js';
32
22
  import { Did } from '../did.js';
23
+ import { DidKeyUtils } from './did-key-utils.js';
33
24
  import { DidMethod } from './did-method.js';
34
25
  import { EMPTY_DID_RESOLUTION_RESULT } from '../types/did-resolution.js';
35
26
  import { DidError, DidErrorCode } from '../did-error.js';
36
- import { getVerificationMethodTypes, keyBytesToMultibaseId, multibaseIdToKeyBytes } from '../utils.js';
27
+ import { getVerificationMethodTypes, multibaseIdToKeyBytes } from '../utils.js';
37
28
 
38
29
  /**
39
30
  * Defines the set of options available when creating a new Decentralized Identifier (DID) with the
@@ -165,19 +156,6 @@ export const DidKeyVerificationMethodType = {
165
156
  JsonWebKey2020: 'https://w3id.org/security/suites/jws-2020/v1',
166
157
  } as const;
167
158
 
168
- /**
169
- * Private helper that maps algorithm identifiers to their corresponding DID Key
170
- * {@link DidKeyRegisteredKeyType | registered key type}.
171
- */
172
- const AlgorithmToKeyTypeMap = {
173
- Ed25519 : DidKeyRegisteredKeyType.Ed25519,
174
- ES256K : DidKeyRegisteredKeyType.secp256k1,
175
- ES256 : DidKeyRegisteredKeyType.secp256r1,
176
- 'P-256' : DidKeyRegisteredKeyType.secp256r1,
177
- secp256k1 : DidKeyRegisteredKeyType.secp256k1,
178
- secp256r1 : DidKeyRegisteredKeyType.secp256r1,
179
- } as const;
180
-
181
159
  /**
182
160
  * The `DidKey` class provides an implementation of the 'did:key' DID method.
183
161
  *
@@ -733,235 +711,5 @@ export class DidKey extends DidMethod {
733
711
  }
734
712
  }
735
713
 
736
- /**
737
- * The `DidKeyUtils` class provides utility functions to support operations in the DID Key method.
738
- */
739
- export class DidKeyUtils {
740
- /**
741
- * A mapping from JSON Web Key (JWK) property descriptors to multicodec names.
742
- *
743
- * This mapping is used to convert keys in JWK (JSON Web Key) format to multicodec format.
744
- *
745
- * @remarks
746
- * The keys of this object are strings that describe the JOSE key type and usage,
747
- * such as 'Ed25519:public', 'Ed25519:private', etc. The values are the corresponding multicodec
748
- * names used to represent these key types.
749
- *
750
- * @example
751
- * ```ts
752
- * const multicodecName = JWK_TO_MULTICODEC['Ed25519:public'];
753
- * // Returns 'ed25519-pub', the multicodec name for an Ed25519 public key
754
- * ```
755
- */
756
- private static JWK_TO_MULTICODEC: { [key: string]: string } = {
757
- 'Ed25519:public' : 'ed25519-pub',
758
- 'Ed25519:private' : 'ed25519-priv',
759
- 'secp256k1:public' : 'secp256k1-pub',
760
- 'secp256k1:private' : 'secp256k1-priv',
761
- };
762
-
763
- /**
764
- * Defines the expected byte lengths for public keys associated with different cryptographic
765
- * algorithms, indexed by their multicodec code values.
766
- */
767
- public static MULTICODEC_PUBLIC_KEY_LENGTH: Record<number, number> = {
768
- // secp256k1-pub - Secp256k1 public key (compressed) - 33 bytes
769
- 0xe7: 33,
770
-
771
- // ed25519-pub - Ed25519 public key - 32 bytes
772
- 0xed: 32
773
- };
774
-
775
- /**
776
- * A mapping from multicodec names to their corresponding JOSE (JSON Object Signing and Encryption)
777
- * representations. This mapping facilitates the conversion of multicodec key formats to
778
- * JWK (JSON Web Key) formats.
779
- *
780
- * @remarks
781
- * The keys of this object are multicodec names, such as 'ed25519-pub', 'ed25519-priv', etc.
782
- * The values are objects representing the corresponding JWK properties for that key type.
783
- *
784
- * @example
785
- * ```ts
786
- * const joseKey = MULTICODEC_TO_JWK['ed25519-pub'];
787
- * // Returns a partial JWK for an Ed25519 public key
788
- * ```
789
- */
790
- private static MULTICODEC_TO_JWK: { [key: string]: Jwk } = {
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: '' },
795
- };
796
-
797
- /**
798
- * Converts a JWK (JSON Web Key) to a Multicodec code and name.
799
- *
800
- * @example
801
- * ```ts
802
- * const jwk: Jwk = { crv: 'Ed25519', kty: 'OKP', x: '...' };
803
- * const { code, name } = await DidKeyUtils.jwkToMulticodec({ jwk });
804
- * ```
805
- *
806
- * @param params - The parameters for the conversion.
807
- * @param params.jwk - The JSON Web Key to be converted.
808
- * @returns A promise that resolves to a Multicodec definition.
809
- */
810
- public static async jwkToMulticodec({ jwk }: {
811
- jwk: Jwk
812
- }): Promise<MulticodecDefinition<MulticodecCode>> {
813
- const params: string[] = [];
814
-
815
- if (jwk.crv) {
816
- params.push(jwk.crv);
817
- if (jwk.d) {
818
- params.push('private');
819
- } else {
820
- params.push('public');
821
- }
822
- }
823
-
824
- const lookupKey = params.join(':');
825
- const name = DidKeyUtils.JWK_TO_MULTICODEC[lookupKey];
826
-
827
- if (name === undefined) {
828
- throw new Error(`Unsupported JWK to Multicodec conversion: '${lookupKey}'`);
829
- }
830
-
831
- const code = Multicodec.getCodeFromName({ name });
832
-
833
- return { code, name };
834
- }
835
-
836
- /**
837
- * Returns the appropriate public key compressor for the specified cryptographic curve.
838
- *
839
- * @param curve - The cryptographic curve to use for the key conversion.
840
- * @returns A public key compressor for the specified curve.
841
- */
842
- public static keyCompressor(
843
- curve: string
844
- ): KeyCompressor['compressPublicKey'] {
845
- // ): ({ publicKeyBytes }: { publicKeyBytes: Uint8Array }) => Promise<Uint8Array> {
846
- const compressors = {
847
- 'P-256' : Secp256r1.compressPublicKey,
848
- 'secp256k1' : Secp256k1.compressPublicKey
849
- } as Record<string, KeyCompressor['compressPublicKey']>;
850
-
851
- const compressor = compressors[curve];
852
-
853
- if (!compressor) {throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);}
854
-
855
- return compressor;
856
- }
857
-
858
- /**
859
- * Returns the appropriate key converter for the specified cryptographic curve.
860
- *
861
- * @param curve - The cryptographic curve to use for the key conversion.
862
- * @returns An `AsymmetricKeyConverter` for the specified curve.
863
- */
864
- public static keyConverter(curve: string): AsymmetricKeyConverter {
865
- const converters: Record<string, AsymmetricKeyConverter> = {
866
- 'Ed25519' : Ed25519,
867
- 'P-256' : Secp256r1,
868
- 'secp256k1' : Secp256k1,
869
- };
870
-
871
- const converter = converters[curve];
872
-
873
- if (!converter) {throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);}
874
-
875
- return converter;
876
- }
877
-
878
- /**
879
- * Converts a Multicodec code or name to parial JWK (JSON Web Key).
880
- *
881
- * @example
882
- * ```ts
883
- * const partialJwk = await DidKeyUtils.multicodecToJwk({ name: 'ed25519-pub' });
884
- * ```
885
- *
886
- * @param params - The parameters for the conversion.
887
- * @param params.code - Optional Multicodec code to convert.
888
- * @param params.name - Optional Multicodec name to convert.
889
- * @returns A promise that resolves to a JOSE format key.
890
- */
891
- public static async multicodecToJwk({ code, name }: {
892
- code?: MulticodecCode,
893
- name?: string
894
- }): Promise<Jwk> {
895
- // Either code or name must be specified, but not both.
896
- if (!(name ? !code : code)) {
897
- throw new Error(`Either 'name' or 'code' must be defined, but not both.`);
898
- }
899
-
900
- // If name is undefined, lookup by code.
901
- name = (name === undefined ) ? Multicodec.getNameFromCode({ code: code! }) : name;
902
-
903
- const lookupKey = name;
904
- const jose = DidKeyUtils.MULTICODEC_TO_JWK[lookupKey];
905
-
906
- if (jose === undefined) {
907
- throw new Error(`Unsupported Multicodec to JWK conversion`);
908
- }
909
-
910
- return { ...jose };
911
- }
912
-
913
- /**
914
- * Converts a public key in JWK (JSON Web Key) format to a multibase identifier.
915
- *
916
- * @remarks
917
- * Note: All secp public keys are converted to compressed point encoding
918
- * before the multibase identifier is computed.
919
- *
920
- * Per {@link https://github.com/multiformats/multicodec/blob/master/table.csv | Multicodec table}:
921
- * Public keys for Elliptic Curve cryptography algorithms (e.g., secp256k1,
922
- * secp256k1r1, secp384r1, etc.) are always represented with compressed point
923
- * encoding (e.g., secp256k1-pub, p256-pub, p384-pub, etc.).
924
- *
925
- * Per {@link https://datatracker.ietf.org/doc/html/rfc8812#name-jose-and-cose-secp256k1-cur | RFC 8812}:
926
- * "As a compressed point encoding representation is not defined for JWK
927
- * elliptic curve points, the uncompressed point encoding defined there
928
- * MUST be used. The x and y values represented MUST both be exactly
929
- * 256 bits, with any leading zeros preserved."
930
- *
931
- * @example
932
- * ```ts
933
- * const publicKey = { crv: 'Ed25519', kty: 'OKP', x: '...' };
934
- * const multibaseId = await DidKeyUtils.publicKeyToMultibaseId({ publicKey });
935
- * ```
936
- *
937
- * @param params - The parameters for the conversion.
938
- * @param params.publicKey - The public key in JWK format.
939
- * @returns A promise that resolves to the multibase identifier.
940
- */
941
- public static async publicKeyToMultibaseId({ publicKey }: {
942
- publicKey: Jwk
943
- }): Promise<string> {
944
- if (!(publicKey?.crv && publicKey.crv in AlgorithmToKeyTypeMap)) {
945
- throw new DidError(DidErrorCode.InvalidPublicKeyType, `Public key contains an unsupported key type: ${publicKey?.crv ?? 'undefined'}`);
946
- }
947
-
948
- // Convert the public key from JWK format to a byte array.
949
- let publicKeyBytes = await DidKeyUtils.keyConverter(publicKey.crv).publicKeyToBytes({ publicKey });
950
-
951
- // Compress the public key if it is an elliptic curve key.
952
- if (/^(secp256k1|P-256|P-384|P-521)$/.test(publicKey.crv)) {
953
- publicKeyBytes = await DidKeyUtils.keyCompressor(publicKey.crv)({ publicKeyBytes });
954
- }
955
-
956
- // Convert the JSON Web Key (JWK) parameters to a Multicodec name.
957
- const { name: multicodecName } = await DidKeyUtils.jwkToMulticodec({ jwk: publicKey });
958
-
959
- // Compute the multibase identifier based on the provided key.
960
- const multibaseId = keyBytesToMultibaseId({
961
- keyBytes: publicKeyBytes,
962
- multicodecName
963
- });
964
-
965
- return multibaseId;
966
- }
967
- }
714
+ // Re-export DidKeyUtils from its dedicated module for backward compatibility.
715
+ export { DidKeyUtils } from './did-key-utils.js';