@enbox/dids 0.0.1
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/LICENSE +201 -0
- package/README.md +1 -0
- package/dist/browser.js +77 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +77 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/index.js +6303 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/utils.js +245 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/esm/bearer-did.js +201 -0
- package/dist/esm/bearer-did.js.map +1 -0
- package/dist/esm/did-error.js +62 -0
- package/dist/esm/did-error.js.map +1 -0
- package/dist/esm/did.js +114 -0
- package/dist/esm/did.js.map +1 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/methods/did-dht.js +1241 -0
- package/dist/esm/methods/did-dht.js.map +1 -0
- package/dist/esm/methods/did-ion.js +570 -0
- package/dist/esm/methods/did-ion.js.map +1 -0
- package/dist/esm/methods/did-jwk.js +298 -0
- package/dist/esm/methods/did-jwk.js.map +1 -0
- package/dist/esm/methods/did-key.js +983 -0
- package/dist/esm/methods/did-key.js.map +1 -0
- package/dist/esm/methods/did-method.js +53 -0
- package/dist/esm/methods/did-method.js.map +1 -0
- package/dist/esm/methods/did-web.js +83 -0
- package/dist/esm/methods/did-web.js.map +1 -0
- package/dist/esm/resolver/resolver-cache-level.js +101 -0
- package/dist/esm/resolver/resolver-cache-level.js.map +1 -0
- package/dist/esm/resolver/resolver-cache-noop.js +24 -0
- package/dist/esm/resolver/resolver-cache-noop.js.map +1 -0
- package/dist/esm/resolver/universal-resolver.js +187 -0
- package/dist/esm/resolver/universal-resolver.js.map +1 -0
- package/dist/esm/types/did-core.js +51 -0
- package/dist/esm/types/did-core.js.map +1 -0
- package/dist/esm/types/did-resolution.js +12 -0
- package/dist/esm/types/did-resolution.js.map +1 -0
- package/dist/esm/types/multibase.js +2 -0
- package/dist/esm/types/multibase.js.map +1 -0
- package/dist/esm/types/portable-did.js +2 -0
- package/dist/esm/types/portable-did.js.map +1 -0
- package/dist/esm/utils.js +458 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/bearer-did.d.ts +143 -0
- package/dist/types/bearer-did.d.ts.map +1 -0
- package/dist/types/did-error.d.ts +50 -0
- package/dist/types/did-error.d.ts.map +1 -0
- package/dist/types/did.d.ts +125 -0
- package/dist/types/did.d.ts.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/methods/did-dht.d.ts +682 -0
- package/dist/types/methods/did-dht.d.ts.map +1 -0
- package/dist/types/methods/did-ion.d.ts +492 -0
- package/dist/types/methods/did-ion.d.ts.map +1 -0
- package/dist/types/methods/did-jwk.d.ts +236 -0
- package/dist/types/methods/did-jwk.d.ts.map +1 -0
- package/dist/types/methods/did-key.d.ts +499 -0
- package/dist/types/methods/did-key.d.ts.map +1 -0
- package/dist/types/methods/did-method.d.ts +238 -0
- package/dist/types/methods/did-method.d.ts.map +1 -0
- package/dist/types/methods/did-web.d.ts +37 -0
- package/dist/types/methods/did-web.d.ts.map +1 -0
- package/dist/types/resolver/resolver-cache-level.d.ts +86 -0
- package/dist/types/resolver/resolver-cache-level.d.ts.map +1 -0
- package/dist/types/resolver/resolver-cache-noop.d.ts +9 -0
- package/dist/types/resolver/resolver-cache-noop.d.ts.map +1 -0
- package/dist/types/resolver/universal-resolver.d.ts +109 -0
- package/dist/types/resolver/universal-resolver.d.ts.map +1 -0
- package/dist/types/types/did-core.d.ts +523 -0
- package/dist/types/types/did-core.d.ts.map +1 -0
- package/dist/types/types/did-resolution.d.ts +85 -0
- package/dist/types/types/did-resolution.d.ts.map +1 -0
- package/dist/types/types/multibase.d.ts +28 -0
- package/dist/types/types/multibase.d.ts.map +1 -0
- package/dist/types/types/portable-did.d.ts +59 -0
- package/dist/types/types/portable-did.d.ts.map +1 -0
- package/dist/types/utils.d.ts +378 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +28 -0
- package/dist/utils.js.map +7 -0
- package/package.json +116 -0
- package/src/bearer-did.ts +287 -0
- package/src/did-error.ts +75 -0
- package/src/did.ts +186 -0
- package/src/index.ts +21 -0
- package/src/methods/did-dht.ts +1637 -0
- package/src/methods/did-ion.ts +887 -0
- package/src/methods/did-jwk.ts +410 -0
- package/src/methods/did-key.ts +1248 -0
- package/src/methods/did-method.ts +276 -0
- package/src/methods/did-web.ts +96 -0
- package/src/resolver/resolver-cache-level.ts +163 -0
- package/src/resolver/resolver-cache-noop.ts +26 -0
- package/src/resolver/universal-resolver.ts +238 -0
- package/src/types/did-core.ts +580 -0
- package/src/types/did-resolution.ts +93 -0
- package/src/types/multibase.ts +29 -0
- package/src/types/portable-did.ts +64 -0
- package/src/utils.ts +532 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import type { MulticodecCode, MulticodecDefinition } from '@enbox/common';
|
|
2
|
+
import type { Jwk, CryptoApi, KeyCompressor, KeyIdentifier, KmsExportKeyParams, KmsImportKeyParams, KeyImporterExporter, AsymmetricKeyConverter, InferKeyGeneratorAlgorithm } from '@enbox/crypto';
|
|
3
|
+
import { LocalKeyManager } from '@enbox/crypto';
|
|
4
|
+
import type { PortableDid } from '../types/portable-did.js';
|
|
5
|
+
import type { DidCreateOptions, DidCreateVerificationMethod } from './did-method.js';
|
|
6
|
+
import type { DidDocument, DidResolutionOptions, DidResolutionResult, DidVerificationMethod } from '../types/did-core.js';
|
|
7
|
+
import { DidMethod } from './did-method.js';
|
|
8
|
+
import { BearerDid } from '../bearer-did.js';
|
|
9
|
+
/**
|
|
10
|
+
* Defines the set of options available when creating a new Decentralized Identifier (DID) with the
|
|
11
|
+
* 'did:key' method.
|
|
12
|
+
*
|
|
13
|
+
* Either the `algorithm` or `verificationMethods` option can be specified, but not both.
|
|
14
|
+
* - A new key will be generated using the algorithm identifier specified in either the `algorithm`
|
|
15
|
+
* property or the `verificationMethods` object's `algorithm` property.
|
|
16
|
+
* - If `verificationMethods` is given, it must contain exactly one entry since DID Key only
|
|
17
|
+
* supports a single verification method.
|
|
18
|
+
* - If neither is given, the default is to generate a new Ed25519 key.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* // By default, when no options are given, a new Ed25519 key will be generated.
|
|
23
|
+
* const did = await DidKey.create();
|
|
24
|
+
*
|
|
25
|
+
* // The algorithm to use for key generation can be specified as a top-level option.
|
|
26
|
+
* const did = await DidKey.create({
|
|
27
|
+
* options: { algorithm = 'secp256k1' }
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Or, alternatively as a property of the verification method.
|
|
31
|
+
* const did = await DidKey.create({
|
|
32
|
+
* options: {
|
|
33
|
+
* verificationMethods: [{ algorithm = 'secp256k1' }]
|
|
34
|
+
* }
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // DID Creation with a KMS
|
|
38
|
+
* const keyManager = new LocalKeyManager();
|
|
39
|
+
* const did = await DidKey.create({ keyManager });
|
|
40
|
+
*
|
|
41
|
+
* // DID Resolution
|
|
42
|
+
* const resolutionResult = await DidKey.resolve({ did: did.uri });
|
|
43
|
+
*
|
|
44
|
+
* // Signature Operations
|
|
45
|
+
* const signer = await did.getSigner();
|
|
46
|
+
* const signature = await signer.sign({ data: new TextEncoder().encode('Message') });
|
|
47
|
+
* const isValid = await signer.verify({ data: new TextEncoder().encode('Message'), signature });
|
|
48
|
+
*
|
|
49
|
+
* // Import / Export
|
|
50
|
+
*
|
|
51
|
+
* // Export a BearerDid object to the PortableDid format.
|
|
52
|
+
* const portableDid = await did.export();
|
|
53
|
+
*
|
|
54
|
+
* // Reconstruct a BearerDid object from a PortableDid
|
|
55
|
+
* const did = await DidKey.import(portableDid);
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export interface DidKeyCreateOptions<TKms> extends DidCreateOptions<TKms> {
|
|
59
|
+
/**
|
|
60
|
+
* Optionally specify the algorithm to be used for key generation.
|
|
61
|
+
*/
|
|
62
|
+
algorithm?: TKms extends CryptoApi ? InferKeyGeneratorAlgorithm<TKms> : InferKeyGeneratorAlgorithm<LocalKeyManager>;
|
|
63
|
+
/**
|
|
64
|
+
* Optionally specify an array of JSON-LD context links for the @context property of the DID
|
|
65
|
+
* document.
|
|
66
|
+
*
|
|
67
|
+
* The @context property provides a JSON-LD processor with the information necessary to interpret
|
|
68
|
+
* the DID document JSON. The default context URL is 'https://www.w3.org/ns/did/v1'.
|
|
69
|
+
*/
|
|
70
|
+
defaultContext?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Optionally enable encryption key derivation during DID creation.
|
|
73
|
+
*
|
|
74
|
+
* By default, this option is set to `false`, which means encryption key derivation is not
|
|
75
|
+
* performed unless explicitly enabled.
|
|
76
|
+
*
|
|
77
|
+
* When set to `true`, an `X25519` key will be derived from the `Ed25519` public key used to
|
|
78
|
+
* create the DID. This feature enables the same DID to be used for encrypted communication, in
|
|
79
|
+
* addition to signature verification.
|
|
80
|
+
*
|
|
81
|
+
* Notes:
|
|
82
|
+
* - This option is ONLY applicable when the `algorithm` of the DID's public key is `Ed25519`.
|
|
83
|
+
* - Enabling this introduces specific cryptographic considerations that should be understood
|
|
84
|
+
* before using the same key pair for digital signatures and encrypted communication. See the following for more information:
|
|
85
|
+
*/
|
|
86
|
+
enableEncryptionKeyDerivation?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Optionally enable experimental public key types during DID creation.
|
|
89
|
+
* By default, this option is set to `false`, which means experimental public key types are not
|
|
90
|
+
* supported.
|
|
91
|
+
*
|
|
92
|
+
* Note: This implementation of the DID Key method does not support any experimental public key
|
|
93
|
+
* types.
|
|
94
|
+
*/
|
|
95
|
+
enableExperimentalPublicKeyTypes?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Optionally specify the format of the public key to be used for DID creation.
|
|
98
|
+
*/
|
|
99
|
+
publicKeyFormat?: keyof typeof DidKeyVerificationMethodType;
|
|
100
|
+
/**
|
|
101
|
+
* Alternatively, specify the algorithm to be used for key generation of the single verification
|
|
102
|
+
* method in the DID Document.
|
|
103
|
+
*/
|
|
104
|
+
verificationMethods?: DidCreateVerificationMethod<TKms>[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Enumerates the types of keys that can be used in a DID Key document.
|
|
108
|
+
*
|
|
109
|
+
* The DID Key method supports various cryptographic key types. These key types are essential for
|
|
110
|
+
* the creation and management of DIDs and their associated cryptographic operations like signing
|
|
111
|
+
* and encryption.
|
|
112
|
+
*/
|
|
113
|
+
export declare enum DidKeyRegisteredKeyType {
|
|
114
|
+
/**
|
|
115
|
+
* Ed25519: A public-key signature system using the EdDSA (Edwards-curve Digital Signature
|
|
116
|
+
* Algorithm) and Curve25519.
|
|
117
|
+
*/
|
|
118
|
+
Ed25519 = "Ed25519",
|
|
119
|
+
/**
|
|
120
|
+
* secp256k1: A cryptographic curve used for digital signatures in a range of decentralized
|
|
121
|
+
* systems.
|
|
122
|
+
*/
|
|
123
|
+
secp256k1 = "secp256k1",
|
|
124
|
+
/**
|
|
125
|
+
* secp256r1: Also known as P-256 or prime256v1, this curve is used for cryptographic operations
|
|
126
|
+
* and is widely supported in various cryptographic libraries and standards.
|
|
127
|
+
*/
|
|
128
|
+
secp256r1 = "secp256r1",
|
|
129
|
+
/**
|
|
130
|
+
* X25519: A Diffie-Hellman key exchange algorithm using Curve25519.
|
|
131
|
+
*/
|
|
132
|
+
X25519 = "X25519"
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Enumerates the verification method types supported by the DID Key method.
|
|
136
|
+
*
|
|
137
|
+
* This enum defines the URIs associated with common verification methods used in DID Documents.
|
|
138
|
+
* These URIs represent cryptographic suites or key types standardized for use across decentralized
|
|
139
|
+
* identifiers (DIDs).
|
|
140
|
+
*/
|
|
141
|
+
export declare const DidKeyVerificationMethodType: {
|
|
142
|
+
/** Represents an Ed25519 public key used for digital signatures. */
|
|
143
|
+
readonly Ed25519VerificationKey2020: "https://w3id.org/security/suites/ed25519-2020/v1";
|
|
144
|
+
/** Represents a JSON Web Key (JWK) used for digital signatures and key agreement protocols. */
|
|
145
|
+
readonly JsonWebKey2020: "https://w3id.org/security/suites/jws-2020/v1";
|
|
146
|
+
/** Represents an X25519 public key used for key agreement protocols. */
|
|
147
|
+
readonly X25519KeyAgreementKey2020: "https://w3id.org/security/suites/x25519-2020/v1";
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* The `DidKey` class provides an implementation of the 'did:key' DID method.
|
|
151
|
+
*
|
|
152
|
+
* Features:
|
|
153
|
+
* - DID Creation: Create new `did:key` DIDs.
|
|
154
|
+
* - DID Key Management: Instantiate a DID object from an existing verification method key set or
|
|
155
|
+
* or a key in a Key Management System (KMS). If supported by the KMS, a DID's
|
|
156
|
+
* key can be exported to a portable DID format.
|
|
157
|
+
* - DID Resolution: Resolve a `did:key` to its corresponding DID Document.
|
|
158
|
+
* - Signature Operations: Sign and verify messages using keys associated with a DID.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
* The `did:key` DID method uses a single public key to generate a DID and does not rely
|
|
162
|
+
* on any external system such as a blockchain or centralized database. This characteristic makes
|
|
163
|
+
* it suitable for use cases where a assertions about a DID Subject can be self-verifiable by
|
|
164
|
+
* third parties.
|
|
165
|
+
*
|
|
166
|
+
* The method-specific identifier is formed by
|
|
167
|
+
* {@link https://datatracker.ietf.org/doc/html/draft-multiformats-multibase#name-base-58-bitcoin-encoding | Multibase base58-btc}
|
|
168
|
+
* encoding the concatenation of the
|
|
169
|
+
* {@link https://github.com/multiformats/multicodec/blob/master/README.md | Multicodec} identifier
|
|
170
|
+
* for the public key type and the raw public key bytes. To form the DID URI, the method-specific
|
|
171
|
+
* identifier is prefixed with the string 'did:key:'.
|
|
172
|
+
*
|
|
173
|
+
* This method can optionally derive an encryption key from the public key used to create the DID
|
|
174
|
+
* if and only if the public key algorithm is `Ed25519`. This feature enables the same DID to be
|
|
175
|
+
* used for encrypted communication, in addition to signature verification. To enable this
|
|
176
|
+
* feature when calling {@link DidKey.create | `DidKey.create()`}, first specify an `algorithm` of
|
|
177
|
+
* `Ed25519` or provide a `keySet` referencing an `Ed25519` key and then set the
|
|
178
|
+
* `enableEncryptionKeyDerivation` option to `true`.
|
|
179
|
+
*
|
|
180
|
+
* Note:
|
|
181
|
+
* - The authors of the DID Key specification have indicated that use of this method for long-lived
|
|
182
|
+
* use cases is only recommended when accompanied with high confidence that private keys are
|
|
183
|
+
* securely protected by software or hardware isolation.
|
|
184
|
+
*
|
|
185
|
+
* @see {@link https://w3c-ccg.github.io/did-method-key/ | DID Key Specification}
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* // DID Creation
|
|
190
|
+
* const did = await DidKey.create();
|
|
191
|
+
*
|
|
192
|
+
* // DID Creation with a KMS
|
|
193
|
+
* const keyManager = new LocalKeyManager();
|
|
194
|
+
* const did = await DidKey.create({ keyManager });
|
|
195
|
+
*
|
|
196
|
+
* // DID Resolution
|
|
197
|
+
* const resolutionResult = await DidKey.resolve({ did: did.uri });
|
|
198
|
+
*
|
|
199
|
+
* // Signature Operations
|
|
200
|
+
* const signer = await did.getSigner();
|
|
201
|
+
* const signature = await signer.sign({ data: new TextEncoder().encode('Message') });
|
|
202
|
+
* const isValid = await signer.verify({ data: new TextEncoder().encode('Message'), signature });
|
|
203
|
+
*
|
|
204
|
+
* // Key Management
|
|
205
|
+
*
|
|
206
|
+
* // Instantiate a DID object from an existing key in a KMS
|
|
207
|
+
* const did = await DidKey.fromKeyManager({
|
|
208
|
+
* didUri: 'did:key:z6MkpUzNmYVTGpqhStxK8yRKXWCRNm1bGYz8geAg2zmjYHKX',
|
|
209
|
+
* keyManager
|
|
210
|
+
* });
|
|
211
|
+
*
|
|
212
|
+
* // Instantiate a DID object from an existing verification method key
|
|
213
|
+
* const did = await DidKey.fromKeys({
|
|
214
|
+
* verificationMethods: [{
|
|
215
|
+
* publicKeyJwk: {
|
|
216
|
+
* kty: 'OKP',
|
|
217
|
+
* crv: 'Ed25519',
|
|
218
|
+
* x: 'cHs7YMLQ3gCWjkacMURBsnEJBcEsvlsE5DfnsfTNDP4'
|
|
219
|
+
* },
|
|
220
|
+
* privateKeyJwk: {
|
|
221
|
+
* kty: 'OKP',
|
|
222
|
+
* crv: 'Ed25519',
|
|
223
|
+
* x: 'cHs7YMLQ3gCWjkacMURBsnEJBcEsvlsE5DfnsfTNDP4',
|
|
224
|
+
* d: 'bdcGE4KzEaekOwoa-ee3gAm1a991WvNj_Eq3WKyqTnE'
|
|
225
|
+
* }
|
|
226
|
+
* }]
|
|
227
|
+
* });
|
|
228
|
+
*
|
|
229
|
+
* // Convert a DID object to a portable format
|
|
230
|
+
* const portableDid = await DidKey.toKeys({ did });
|
|
231
|
+
*
|
|
232
|
+
* // Reconstruct a DID object from a portable format
|
|
233
|
+
* const did = await DidKey.fromKeys(portableDid);
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export declare class DidKey extends DidMethod {
|
|
237
|
+
/**
|
|
238
|
+
* Name of the DID method, as defined in the DID Key specification.
|
|
239
|
+
*/
|
|
240
|
+
static methodName: string;
|
|
241
|
+
/**
|
|
242
|
+
* Creates a new DID using the `did:key` method formed from a newly generated key.
|
|
243
|
+
*
|
|
244
|
+
* @remarks
|
|
245
|
+
* The DID URI is formed by
|
|
246
|
+
* {@link https://datatracker.ietf.org/doc/html/draft-multiformats-multibase#name-base-58-bitcoin-encoding | Multibase base58-btc}
|
|
247
|
+
* encoding the
|
|
248
|
+
* {@link https://github.com/multiformats/multicodec/blob/master/README.md | Multicodec}-encoded
|
|
249
|
+
* public key and prefixing with `did:key:`.
|
|
250
|
+
*
|
|
251
|
+
* This method can optionally derive an encryption key from the public key used to create the DID
|
|
252
|
+
* if and only if the public key algorithm is `Ed25519`. This feature enables the same DID to be
|
|
253
|
+
* used for encrypted communication, in addition to signature verification. To enable this
|
|
254
|
+
* feature, specify an `algorithm` of `Ed25519` as either a top-level option or in a
|
|
255
|
+
* `verificationMethod` and set the `enableEncryptionKeyDerivation` option to `true`.
|
|
256
|
+
*
|
|
257
|
+
* Notes:
|
|
258
|
+
* - If no `options` are given, by default a new Ed25519 key will be generated.
|
|
259
|
+
* - The `algorithm` and `verificationMethods` options are mutually exclusive. If both are given,
|
|
260
|
+
* an error will be thrown.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* // DID Creation
|
|
265
|
+
* const did = await DidKey.create();
|
|
266
|
+
*
|
|
267
|
+
* // DID Creation with a KMS
|
|
268
|
+
* const keyManager = new LocalKeyManager();
|
|
269
|
+
* const did = await DidKey.create({ keyManager });
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @param params - The parameters for the create operation.
|
|
273
|
+
* @param params.keyManager - Key Management System (KMS) used to generate keys and sign data.
|
|
274
|
+
* @param params.options - Optional parameters that can be specified when creating a new DID.
|
|
275
|
+
* @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
|
|
276
|
+
*/
|
|
277
|
+
static create<TKms extends CryptoApi | undefined = undefined>({ keyManager, options }?: {
|
|
278
|
+
keyManager?: TKms;
|
|
279
|
+
options?: DidKeyCreateOptions<TKms>;
|
|
280
|
+
}): Promise<BearerDid>;
|
|
281
|
+
/**
|
|
282
|
+
* Given the W3C DID Document of a `did:key` DID, return the verification method that will be used
|
|
283
|
+
* for signing messages and credentials. With DID Key, the first verification method in the
|
|
284
|
+
* authentication property in the DID Document is used.
|
|
285
|
+
*
|
|
286
|
+
* Note that for DID Key, only one verification method intended for signing can exist so
|
|
287
|
+
* specifying `methodId` could be considered redundant or unnecessary. The option is provided for
|
|
288
|
+
* consistency with other DID method implementations.
|
|
289
|
+
*
|
|
290
|
+
* @param params - The parameters for the `getSigningMethod` operation.
|
|
291
|
+
* @param params.didDocument - DID Document to get the verification method from.
|
|
292
|
+
* @param params.methodId - ID of the verification method to use for signing.
|
|
293
|
+
* @returns Verification method to use for signing.
|
|
294
|
+
*/
|
|
295
|
+
static getSigningMethod({ didDocument }: {
|
|
296
|
+
didDocument: DidDocument;
|
|
297
|
+
methodId?: string;
|
|
298
|
+
}): Promise<DidVerificationMethod>;
|
|
299
|
+
/**
|
|
300
|
+
* Instantiates a {@link BearerDid} object for the DID Key method from a given {@link PortableDid}.
|
|
301
|
+
*
|
|
302
|
+
* This method allows for the creation of a `BearerDid` object using a previously created DID's
|
|
303
|
+
* key material, DID document, and metadata.
|
|
304
|
+
*
|
|
305
|
+
* @remarks
|
|
306
|
+
* The `verificationMethod` array of the DID document must contain exactly one key since the
|
|
307
|
+
* `did:key` method only supports a single verification method.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* // Export an existing BearerDid to PortableDid format.
|
|
312
|
+
* const portableDid = await did.export();
|
|
313
|
+
* // Reconstruct a BearerDid object from the PortableDid.
|
|
314
|
+
* const did = await DidKey.import({ portableDid });
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* @param params - The parameters for the import operation.
|
|
318
|
+
* @param params.portableDid - The PortableDid object to import.
|
|
319
|
+
* @param params.keyManager - Optionally specify an external Key Management System (KMS) used to
|
|
320
|
+
* generate keys and sign data. If not given, a new
|
|
321
|
+
* {@link LocalKeyManager} instance will be created and
|
|
322
|
+
* used.
|
|
323
|
+
* @returns A Promise resolving to a `BearerDid` object representing the DID formed from the provided keys.
|
|
324
|
+
* @throws An error if the DID document does not contain exactly one verification method.
|
|
325
|
+
*/
|
|
326
|
+
static import({ portableDid, keyManager }: {
|
|
327
|
+
keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
|
|
328
|
+
portableDid: PortableDid;
|
|
329
|
+
}): Promise<BearerDid>;
|
|
330
|
+
/**
|
|
331
|
+
* Resolves a `did:key` identifier to a DID Document.
|
|
332
|
+
*
|
|
333
|
+
* @param didUri - The DID to be resolved.
|
|
334
|
+
* @param options - Optional parameters for resolving the DID.
|
|
335
|
+
* @returns A Promise resolving to a {@link DidResolutionResult} object representing the result of the resolution.
|
|
336
|
+
*/
|
|
337
|
+
static resolve(didUri: string, options?: DidResolutionOptions): Promise<DidResolutionResult>;
|
|
338
|
+
/**
|
|
339
|
+
* Expands a did:key identifier to a DID Document.
|
|
340
|
+
*
|
|
341
|
+
* Reference: https://w3c-ccg.github.io/did-method-key/#document-creation-algorithm
|
|
342
|
+
*
|
|
343
|
+
* @param options
|
|
344
|
+
* @returns - A DID dodcument.
|
|
345
|
+
*/
|
|
346
|
+
private static createDocument;
|
|
347
|
+
/**
|
|
348
|
+
* Decoding a multibase-encoded multicodec value into a verification method
|
|
349
|
+
* that is suitable for verifying that encrypted information will be
|
|
350
|
+
* received by the intended recipient.
|
|
351
|
+
*/
|
|
352
|
+
private static createEncryptionMethod;
|
|
353
|
+
/**
|
|
354
|
+
* Decodes a multibase-encoded multicodec value into a verification method
|
|
355
|
+
* that is suitable for verifying digital signatures.
|
|
356
|
+
* @param options - Signature method creation algorithm inputs.
|
|
357
|
+
* @returns - A verification method.
|
|
358
|
+
*/
|
|
359
|
+
private static createSignatureMethod;
|
|
360
|
+
/**
|
|
361
|
+
* Transform a multibase-encoded multicodec value to public encryption key
|
|
362
|
+
* components that are suitable for encrypting messages to a receiver. A
|
|
363
|
+
* mathematical proof elaborating on the safety of performing this operation
|
|
364
|
+
* is available in:
|
|
365
|
+
* {@link https://eprint.iacr.org/2021/509.pdf | On using the same key pair for Ed25519 and an X25519 based KEM}
|
|
366
|
+
*/
|
|
367
|
+
private static deriveEncryptionKey;
|
|
368
|
+
/**
|
|
369
|
+
* Validates the structure and components of a DID URI against the `did:key` method specification.
|
|
370
|
+
*
|
|
371
|
+
* @param parsedDid - An object representing the parsed components of a DID URI, including the
|
|
372
|
+
* scheme, method, and method-specific identifier.
|
|
373
|
+
* @returns `true` if the DID URI meets the `did:key` method's structural requirements, `false` otherwise.
|
|
374
|
+
*
|
|
375
|
+
*/
|
|
376
|
+
private static validateIdentifier;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* The `DidKeyUtils` class provides utility functions to support operations in the DID Key method.
|
|
380
|
+
*/
|
|
381
|
+
export declare class DidKeyUtils {
|
|
382
|
+
/**
|
|
383
|
+
* A mapping from JSON Web Key (JWK) property descriptors to multicodec names.
|
|
384
|
+
*
|
|
385
|
+
* This mapping is used to convert keys in JWK (JSON Web Key) format to multicodec format.
|
|
386
|
+
*
|
|
387
|
+
* @remarks
|
|
388
|
+
* The keys of this object are strings that describe the JOSE key type and usage,
|
|
389
|
+
* such as 'Ed25519:public', 'Ed25519:private', etc. The values are the corresponding multicodec
|
|
390
|
+
* names used to represent these key types.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* const multicodecName = JWK_TO_MULTICODEC['Ed25519:public'];
|
|
395
|
+
* // Returns 'ed25519-pub', the multicodec name for an Ed25519 public key
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
private static JWK_TO_MULTICODEC;
|
|
399
|
+
/**
|
|
400
|
+
* Defines the expected byte lengths for public keys associated with different cryptographic
|
|
401
|
+
* algorithms, indexed by their multicodec code values.
|
|
402
|
+
*/
|
|
403
|
+
static MULTICODEC_PUBLIC_KEY_LENGTH: Record<number, number>;
|
|
404
|
+
/**
|
|
405
|
+
* A mapping from multicodec names to their corresponding JOSE (JSON Object Signing and Encryption)
|
|
406
|
+
* representations. This mapping facilitates the conversion of multicodec key formats to
|
|
407
|
+
* JWK (JSON Web Key) formats.
|
|
408
|
+
*
|
|
409
|
+
* @remarks
|
|
410
|
+
* The keys of this object are multicodec names, such as 'ed25519-pub', 'ed25519-priv', etc.
|
|
411
|
+
* The values are objects representing the corresponding JWK properties for that key type.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* ```ts
|
|
415
|
+
* const joseKey = MULTICODEC_TO_JWK['ed25519-pub'];
|
|
416
|
+
* // Returns a partial JWK for an Ed25519 public key
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
private static MULTICODEC_TO_JWK;
|
|
420
|
+
/**
|
|
421
|
+
* Converts a JWK (JSON Web Key) to a Multicodec code and name.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* const jwk: Jwk = { crv: 'Ed25519', kty: 'OKP', x: '...' };
|
|
426
|
+
* const { code, name } = await DidKeyUtils.jwkToMulticodec({ jwk });
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* @param params - The parameters for the conversion.
|
|
430
|
+
* @param params.jwk - The JSON Web Key to be converted.
|
|
431
|
+
* @returns A promise that resolves to a Multicodec definition.
|
|
432
|
+
*/
|
|
433
|
+
static jwkToMulticodec({ jwk }: {
|
|
434
|
+
jwk: Jwk;
|
|
435
|
+
}): Promise<MulticodecDefinition<MulticodecCode>>;
|
|
436
|
+
/**
|
|
437
|
+
* Returns the appropriate public key compressor for the specified cryptographic curve.
|
|
438
|
+
*
|
|
439
|
+
* @param curve - The cryptographic curve to use for the key conversion.
|
|
440
|
+
* @returns A public key compressor for the specified curve.
|
|
441
|
+
*/
|
|
442
|
+
static keyCompressor(curve: string): KeyCompressor['compressPublicKey'];
|
|
443
|
+
/**
|
|
444
|
+
* Returns the appropriate key converter for the specified cryptographic curve.
|
|
445
|
+
*
|
|
446
|
+
* @param curve - The cryptographic curve to use for the key conversion.
|
|
447
|
+
* @returns An `AsymmetricKeyConverter` for the specified curve.
|
|
448
|
+
*/
|
|
449
|
+
static keyConverter(curve: string): AsymmetricKeyConverter;
|
|
450
|
+
/**
|
|
451
|
+
* Converts a Multicodec code or name to parial JWK (JSON Web Key).
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```ts
|
|
455
|
+
* const partialJwk = await DidKeyUtils.multicodecToJwk({ name: 'ed25519-pub' });
|
|
456
|
+
* ```
|
|
457
|
+
*
|
|
458
|
+
* @param params - The parameters for the conversion.
|
|
459
|
+
* @param params.code - Optional Multicodec code to convert.
|
|
460
|
+
* @param params.name - Optional Multicodec name to convert.
|
|
461
|
+
* @returns A promise that resolves to a JOSE format key.
|
|
462
|
+
*/
|
|
463
|
+
static multicodecToJwk({ code, name }: {
|
|
464
|
+
code?: MulticodecCode;
|
|
465
|
+
name?: string;
|
|
466
|
+
}): Promise<Jwk>;
|
|
467
|
+
/**
|
|
468
|
+
* Converts a public key in JWK (JSON Web Key) format to a multibase identifier.
|
|
469
|
+
*
|
|
470
|
+
* @remarks
|
|
471
|
+
* Note: All secp public keys are converted to compressed point encoding
|
|
472
|
+
* before the multibase identifier is computed.
|
|
473
|
+
*
|
|
474
|
+
* Per {@link https://github.com/multiformats/multicodec/blob/master/table.csv | Multicodec table}:
|
|
475
|
+
* Public keys for Elliptic Curve cryptography algorithms (e.g., secp256k1,
|
|
476
|
+
* secp256k1r1, secp384r1, etc.) are always represented with compressed point
|
|
477
|
+
* encoding (e.g., secp256k1-pub, p256-pub, p384-pub, etc.).
|
|
478
|
+
*
|
|
479
|
+
* Per {@link https://datatracker.ietf.org/doc/html/rfc8812#name-jose-and-cose-secp256k1-cur | RFC 8812}:
|
|
480
|
+
* "As a compressed point encoding representation is not defined for JWK
|
|
481
|
+
* elliptic curve points, the uncompressed point encoding defined there
|
|
482
|
+
* MUST be used. The x and y values represented MUST both be exactly
|
|
483
|
+
* 256 bits, with any leading zeros preserved."
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```ts
|
|
487
|
+
* const publicKey = { crv: 'Ed25519', kty: 'OKP', x: '...' };
|
|
488
|
+
* const multibaseId = await DidKeyUtils.publicKeyToMultibaseId({ publicKey });
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* @param params - The parameters for the conversion.
|
|
492
|
+
* @param params.publicKey - The public key in JWK format.
|
|
493
|
+
* @returns A promise that resolves to the multibase identifier.
|
|
494
|
+
*/
|
|
495
|
+
static publicKeyToMultibaseId({ publicKey }: {
|
|
496
|
+
publicKey: Jwk;
|
|
497
|
+
}): Promise<string>;
|
|
498
|
+
}
|
|
499
|
+
//# sourceMappingURL=did-key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did-key.d.ts","sourceRoot":"","sources":["../../../src/methods/did-key.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAe,MAAM,eAAe,CAAC;AACvF,OAAO,KAAK,EACV,GAAG,EACH,SAAS,EACT,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,EACtB,0BAA0B,EAC3B,MAAM,eAAe,CAAC;AAGvB,OAAO,EAKL,eAAe,EAChB,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AACrF,OAAO,KAAK,EACV,WAAW,EACX,oBAAoB,EACpB,mBAAmB,EACnB,qBAAqB,EACtB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAM7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,MAAM,WAAW,mBAAmB,CAAC,IAAI,CAAE,SAAQ,gBAAgB,CAAC,IAAI,CAAC;IACvE;;OAEG;IACH,SAAS,CAAC,EAAE,IAAI,SAAS,SAAS,GAC9B,0BAA0B,CAAC,IAAI,CAAC,GAChC,0BAA0B,CAAC,eAAe,CAAC,CAAC;IAEhD;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;;;;;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