@twin.org/standards-w3c-did 0.0.2-next.9 → 0.0.3-next.2

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 (96) hide show
  1. package/dist/es/index.js +33 -0
  2. package/dist/es/index.js.map +1 -0
  3. package/dist/es/models/IDataIntegrityProof.js +2 -0
  4. package/dist/es/models/IDataIntegrityProof.js.map +1 -0
  5. package/dist/es/models/IDidCredentialSchema.js +4 -0
  6. package/dist/es/models/IDidCredentialSchema.js.map +1 -0
  7. package/dist/es/models/IDidCredentialStatus.js +4 -0
  8. package/dist/es/models/IDidCredentialStatus.js.map +1 -0
  9. package/dist/es/models/IDidDocument.js +2 -0
  10. package/dist/es/models/IDidDocument.js.map +1 -0
  11. package/dist/es/models/IDidDocumentVerificationMethod.js +2 -0
  12. package/dist/es/models/IDidDocumentVerificationMethod.js.map +1 -0
  13. package/dist/es/models/IDidLabel.js +4 -0
  14. package/dist/es/models/IDidLabel.js.map +1 -0
  15. package/dist/es/models/IDidPresentationVerification.js +2 -0
  16. package/dist/es/models/IDidPresentationVerification.js.map +1 -0
  17. package/dist/es/models/IDidService.js +4 -0
  18. package/dist/es/models/IDidService.js.map +1 -0
  19. package/dist/es/models/IDidVerifiableCredential.js +2 -0
  20. package/dist/es/models/IDidVerifiableCredential.js.map +1 -0
  21. package/dist/es/models/IDidVerifiableCredentialCommon.js +2 -0
  22. package/dist/es/models/IDidVerifiableCredentialCommon.js.map +1 -0
  23. package/dist/es/models/IDidVerifiableCredentialV1.js +2 -0
  24. package/dist/es/models/IDidVerifiableCredentialV1.js.map +1 -0
  25. package/dist/es/models/IDidVerifiableCredentialV2.js +2 -0
  26. package/dist/es/models/IDidVerifiableCredentialV2.js.map +1 -0
  27. package/dist/es/models/IDidVerifiablePresentation.js +2 -0
  28. package/dist/es/models/IDidVerifiablePresentation.js.map +1 -0
  29. package/dist/es/models/IDidVerifiablePresentationCommon.js +2 -0
  30. package/dist/es/models/IDidVerifiablePresentationCommon.js.map +1 -0
  31. package/dist/es/models/IDidVerifiablePresentationV1.js +2 -0
  32. package/dist/es/models/IDidVerifiablePresentationV1.js.map +1 -0
  33. package/dist/es/models/IDidVerifiablePresentationV2.js +2 -0
  34. package/dist/es/models/IDidVerifiablePresentationV2.js.map +1 -0
  35. package/dist/es/models/IJsonWebSignature2020Proof.js +2 -0
  36. package/dist/es/models/IJsonWebSignature2020Proof.js.map +1 -0
  37. package/dist/es/models/IMultikey.js +2 -0
  38. package/dist/es/models/IMultikey.js.map +1 -0
  39. package/dist/es/models/IProof.js +2 -0
  40. package/dist/es/models/IProof.js.map +1 -0
  41. package/dist/es/models/IProofSignerVerifier.js +2 -0
  42. package/dist/es/models/IProofSignerVerifier.js.map +1 -0
  43. package/dist/es/models/didContexts.js +41 -0
  44. package/dist/es/models/didContexts.js.map +1 -0
  45. package/dist/es/models/didCryptoSuites.js +19 -0
  46. package/dist/es/models/didCryptoSuites.js.map +1 -0
  47. package/dist/es/models/didTypes.js +33 -0
  48. package/dist/es/models/didTypes.js.map +1 -0
  49. package/dist/es/models/didVerificationMethodType.js +31 -0
  50. package/dist/es/models/didVerificationMethodType.js.map +1 -0
  51. package/dist/es/models/proofTypes.js +17 -0
  52. package/dist/es/models/proofTypes.js.map +1 -0
  53. package/dist/es/signerVerifiers/dataIntegrityProofSignerVerifier.js +90 -0
  54. package/dist/es/signerVerifiers/dataIntegrityProofSignerVerifier.js.map +1 -0
  55. package/dist/es/signerVerifiers/jsonWebSignature2020SignerVerifier.js +79 -0
  56. package/dist/es/signerVerifiers/jsonWebSignature2020SignerVerifier.js.map +1 -0
  57. package/dist/es/utils/multikeyHelper.js +114 -0
  58. package/dist/es/utils/multikeyHelper.js.map +1 -0
  59. package/dist/es/utils/proofHelper.js +106 -0
  60. package/dist/es/utils/proofHelper.js.map +1 -0
  61. package/dist/es/utils/verifiableCredentialHelper.js +31 -0
  62. package/dist/es/utils/verifiableCredentialHelper.js.map +1 -0
  63. package/dist/types/index.d.ts +30 -30
  64. package/dist/types/models/IDataIntegrityProof.d.ts +3 -3
  65. package/dist/types/models/IDidDocument.d.ts +3 -3
  66. package/dist/types/models/IDidPresentationVerification.d.ts +1 -1
  67. package/dist/types/models/IDidVerifiableCredential.d.ts +2 -2
  68. package/dist/types/models/IDidVerifiableCredentialCommon.d.ts +4 -4
  69. package/dist/types/models/IDidVerifiableCredentialV1.d.ts +2 -2
  70. package/dist/types/models/IDidVerifiableCredentialV2.d.ts +2 -2
  71. package/dist/types/models/IDidVerifiablePresentation.d.ts +2 -2
  72. package/dist/types/models/IDidVerifiablePresentationCommon.d.ts +1 -1
  73. package/dist/types/models/IDidVerifiablePresentationV1.d.ts +3 -3
  74. package/dist/types/models/IDidVerifiablePresentationV2.d.ts +3 -3
  75. package/dist/types/models/IJsonWebSignature2020Proof.d.ts +2 -2
  76. package/dist/types/models/IMultikey.d.ts +2 -2
  77. package/dist/types/models/IProof.d.ts +2 -2
  78. package/dist/types/models/IProofSignerVerifier.d.ts +1 -1
  79. package/dist/types/models/didVerificationMethodType.d.ts +1 -1
  80. package/dist/types/signerVerifiers/dataIntegrityProofSignerVerifier.d.ts +4 -4
  81. package/dist/types/signerVerifiers/jsonWebSignature2020SignerVerifier.d.ts +3 -3
  82. package/dist/types/utils/multikeyHelper.d.ts +1 -1
  83. package/dist/types/utils/proofHelper.d.ts +3 -3
  84. package/dist/types/utils/verifiableCredentialHelper.d.ts +1 -1
  85. package/docs/changelog.md +76 -0
  86. package/docs/reference/classes/DataIntegrityProofSignerVerifier.md +3 -3
  87. package/docs/reference/classes/JsonWebSignature2020SignerVerifier.md +3 -3
  88. package/docs/reference/classes/MultikeyHelper.md +3 -3
  89. package/docs/reference/classes/ProofHelper.md +2 -2
  90. package/docs/reference/classes/VerifiableCredentialHelper.md +4 -4
  91. package/docs/reference/interfaces/IDidDocumentVerificationMethod.md +2 -2
  92. package/docs/reference/interfaces/IProofSignerVerifier.md +2 -2
  93. package/locales/en.json +4 -8
  94. package/package.json +20 -8
  95. package/dist/cjs/index.cjs +0 -557
  96. package/dist/esm/index.mjs +0 -546
@@ -1,557 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@twin.org/core');
4
- var crypto = require('@twin.org/crypto');
5
- var dataJsonLd = require('@twin.org/data-json-ld');
6
- var web = require('@twin.org/web');
7
-
8
- // Copyright 2024 IOTA Stiftung.
9
- // SPDX-License-Identifier: Apache-2.0.
10
- /**
11
- * The contexts for DIDs.
12
- */
13
- // eslint-disable-next-line @typescript-eslint/naming-convention
14
- const DidContexts = {
15
- /**
16
- * The context root for DID.
17
- */
18
- Context: "https://www.w3.org/ns/did/v1",
19
- /**
20
- * The context root for DID VC v1.
21
- */
22
- ContextVCv1: "https://www.w3.org/2018/credentials/v1",
23
- /**
24
- * The context root for DID VC v2.
25
- */
26
- ContextVCv2: "https://www.w3.org/ns/credentials/v2",
27
- /**
28
- * The context root for security ed25519 suites.
29
- */
30
- ContextSecurityEd25519: "https://w3id.org/security/suites/ed25519-2020/v1",
31
- /**
32
- * The context root for security jws-2020 suites.
33
- */
34
- ContextSecurityJws2020: "https://w3id.org/security/suites/jws-2020/v1",
35
- /**
36
- * The context root for VC Data Integrity.
37
- */
38
- ContextDataIntegrity: "https://www.w3.org/ns/credentials/v2",
39
- /**
40
- * The context root for VC Data Integrity.
41
- */
42
- ContextControllerIdentifiers: "https://www.w3.org/ns/cid/v1",
43
- /**
44
- * The context root for security multikey suites.
45
- */
46
- ContextSecurityMultikey: "https://w3id.org/security/multikey/v1"
47
- };
48
-
49
- // Copyright 2024 IOTA Stiftung.
50
- // SPDX-License-Identifier: Apache-2.0.
51
- /**
52
- * The types for DID Proof crypto suites.
53
- */
54
- // eslint-disable-next-line @typescript-eslint/naming-convention
55
- const DidCryptoSuites = {
56
- /**
57
- * The type for EdDSA crypto suite for JSON Canonicalization Scheme [RFC8785].
58
- * https://www.w3.org/TR/vc-di-eddsa/#eddsa-jcs-2022
59
- */
60
- EdDSAJcs2022: "eddsa-jcs-2022",
61
- /**
62
- * The type for EdDSA crypto suite for RDF Dataset Canonicalization.
63
- * https://www.w3.org/TR/vc-di-eddsa/#eddsa-rdfc-2022
64
- */
65
- EdDSARdfc2022: "eddsa-rdfc-2022"
66
- };
67
-
68
- // Copyright 2024 IOTA Stiftung.
69
- // SPDX-License-Identifier: Apache-2.0.
70
- /**
71
- * The types for DIDs.
72
- */
73
- // eslint-disable-next-line @typescript-eslint/naming-convention
74
- const DidTypes = {
75
- /**
76
- * The type for Verifiable Credential.
77
- */
78
- VerifiableCredential: "VerifiableCredential",
79
- /**
80
- * The type for Verifiable Presentation.
81
- */
82
- VerifiablePresentation: "VerifiablePresentation",
83
- /**
84
- * The type for Ed25519VerificationKey2020.
85
- */
86
- Ed25519VerificationKey2020: "Ed25519VerificationKey2020",
87
- /**
88
- * The type for JsonWebKey2020.
89
- */
90
- JsonWebKey2020: "JsonWebKey2020",
91
- /**
92
- * The type for LinkedDomains.
93
- */
94
- LinkedDomains: "LinkedDomains",
95
- /**
96
- * The type for Multikey.
97
- */
98
- Multikey: "Multikey"
99
- };
100
-
101
- /**
102
- * The types of verification method.
103
- */
104
- // eslint-disable-next-line @typescript-eslint/naming-convention
105
- const DidVerificationMethodType = {
106
- /**
107
- * VerificationMethod.
108
- */
109
- VerificationMethod: "verificationMethod",
110
- /**
111
- * Authentication.
112
- */
113
- Authentication: "authentication",
114
- /**
115
- * Assertion Method.
116
- */
117
- AssertionMethod: "assertionMethod",
118
- /**
119
- * Key Agreement.
120
- */
121
- KeyAgreement: "keyAgreement",
122
- /**
123
- * Capability Invocation.
124
- */
125
- CapabilityInvocation: "capabilityInvocation",
126
- /**
127
- * Capability Delegation.
128
- */
129
- CapabilityDelegation: "capabilityDelegation"
130
- };
131
-
132
- // Copyright 2024 IOTA Stiftung.
133
- // SPDX-License-Identifier: Apache-2.0.
134
- /**
135
- * The types for proofs.
136
- */
137
- // eslint-disable-next-line @typescript-eslint/naming-convention
138
- const ProofTypes = {
139
- /**
140
- * The type for Data Integrity Proof.
141
- */
142
- DataIntegrityProof: "DataIntegrityProof",
143
- /**
144
- * The type for Json Web Signature 2020.
145
- */
146
- JsonWebSignature2020: "JsonWebSignature2020"
147
- };
148
-
149
- // Copyright 2024 IOTA Stiftung.
150
- // SPDX-License-Identifier: Apache-2.0.
151
- /**
152
- * Helper methods for creating and verifying proofs.
153
- * https://www.w3.org/TR/vc-di-eddsa/#eddsa-jcs-2022
154
- */
155
- class DataIntegrityProofSignerVerifier {
156
- /**
157
- * Runtime name for the class.
158
- */
159
- CLASS_NAME = "DataIntegrityProofSignerVerifier";
160
- /**
161
- * Create a proof for the given data.
162
- * @param unsecuredDocument The data to create the proof for.
163
- * @param unsignedProof The proof options.
164
- * @param signKey The key to sign the proof with.
165
- * @returns The created proof.
166
- */
167
- async createProof(unsecuredDocument, unsignedProof, signKey) {
168
- core.Guards.object(this.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
169
- core.Guards.object(this.CLASS_NAME, "unsignedProof", unsignedProof);
170
- core.Guards.object(this.CLASS_NAME, "signKey", signKey);
171
- const rawKeys = await web.Jwk.toRaw(signKey);
172
- if (!core.Is.uint8Array(rawKeys.privateKey)) {
173
- throw new core.GeneralError(this.CLASS_NAME, "missingPrivateKey");
174
- }
175
- const unsecuredDocumentClone = core.ObjectHelper.clone(unsecuredDocument);
176
- const signedProof = core.ObjectHelper.clone(unsignedProof);
177
- unsecuredDocumentClone["@context"] = dataJsonLd.JsonLdProcessor.combineContexts(unsecuredDocumentClone["@context"], DidContexts.ContextDataIntegrity);
178
- signedProof["@context"] = unsecuredDocumentClone["@context"];
179
- const combinedHash = await this.createHash(unsecuredDocument, unsignedProof);
180
- const signature = crypto.Ed25519.sign(rawKeys.privateKey, combinedHash);
181
- signedProof.proofValue = `z${core.Converter.bytesToBase58(signature)}`;
182
- return signedProof;
183
- }
184
- /**
185
- * Verify a proof for the given data in format.
186
- * @param securedDocument The credential to verify.
187
- * @param signedProof The proof to verify.
188
- * @param verifyKey The public key to verify the proof with.
189
- * @returns True if the credential was verified.
190
- */
191
- async verifyProof(securedDocument, signedProof, verifyKey) {
192
- core.Guards.object(this.CLASS_NAME, "securedDocument", securedDocument);
193
- core.Guards.object(this.CLASS_NAME, "signedProof", signedProof);
194
- core.Guards.stringValue(this.CLASS_NAME, "signedProof.proofValue", signedProof.proofValue);
195
- core.Guards.object(this.CLASS_NAME, "verifyKey", verifyKey);
196
- const rawKeys = await web.Jwk.toRaw(verifyKey);
197
- if (!core.Is.uint8Array(rawKeys.publicKey)) {
198
- throw new core.GeneralError(this.CLASS_NAME, "missingPublicKey");
199
- }
200
- const combinedHash = await this.createHash(securedDocument, signedProof);
201
- return crypto.Ed25519.verify(rawKeys.publicKey, combinedHash, core.Converter.base58ToBytes(signedProof.proofValue.slice(1)));
202
- }
203
- /**
204
- * Create a hash for the given data.
205
- * @param unsecuredDocument The data to create the proof for.
206
- * @param unsignedProof The unsigned proof.
207
- * @returns The created hash.
208
- */
209
- async createHash(unsecuredDocument, unsignedProof) {
210
- core.Guards.object(this.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
211
- core.Guards.object(this.CLASS_NAME, "unsignedProof", unsignedProof);
212
- core.Guards.stringValue(this.CLASS_NAME, "unsignedProof.cryptosuite", unsignedProof.cryptosuite);
213
- core.Guards.stringValue(this.CLASS_NAME, "unsignedProof.verificationMethod", unsignedProof.verificationMethod);
214
- const unsecuredDocumentClone = core.ObjectHelper.clone(unsecuredDocument);
215
- const proofOptionsClone = core.ObjectHelper.clone(unsignedProof);
216
- delete unsecuredDocumentClone.proof;
217
- delete proofOptionsClone.proofValue;
218
- if (proofOptionsClone.cryptosuite !== DidCryptoSuites.EdDSAJcs2022) {
219
- throw new core.GeneralError(this.CLASS_NAME, "cryptosuiteNotSupported", {
220
- cryptoSuite: proofOptionsClone.cryptosuite
221
- });
222
- }
223
- unsecuredDocumentClone["@context"] = dataJsonLd.JsonLdProcessor.combineContexts(unsecuredDocumentClone["@context"], DidContexts.ContextDataIntegrity);
224
- proofOptionsClone["@context"] = unsecuredDocumentClone["@context"];
225
- const transformedDocument = core.JsonHelper.canonicalize(unsecuredDocumentClone);
226
- const transformedDocumentHash = crypto.Sha256.sum256(core.Converter.utf8ToBytes(transformedDocument));
227
- const transformedProofOptions = core.JsonHelper.canonicalize(proofOptionsClone);
228
- const proofOptionsHash = crypto.Sha256.sum256(core.Converter.utf8ToBytes(transformedProofOptions));
229
- return core.Uint8ArrayHelper.concat([proofOptionsHash, transformedDocumentHash]);
230
- }
231
- }
232
-
233
- // Copyright 2024 IOTA Stiftung.
234
- // SPDX-License-Identifier: Apache-2.0.
235
- /**
236
- * Helper methods for creating and verifying proofs.
237
- */
238
- class JsonWebSignature2020SignerVerifier {
239
- /**
240
- * Runtime name for the class.
241
- */
242
- CLASS_NAME = "JsonWebSignature2020SignerVerifier";
243
- /**
244
- * Create a proof for the given data.
245
- * @param unsecuredDocument The data to create the proof for.
246
- * @param unsignedProof The proof options.
247
- * @param signKey The key to sign the proof with.
248
- * @returns The created proof.
249
- */
250
- async createProof(unsecuredDocument, unsignedProof, signKey) {
251
- core.Guards.object(this.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
252
- core.Guards.object(this.CLASS_NAME, "unsignedProof", unsignedProof);
253
- core.Guards.object(this.CLASS_NAME, "signKey", signKey);
254
- const unsecuredDocumentClone = core.ObjectHelper.clone(unsecuredDocument);
255
- unsecuredDocumentClone["@context"] = dataJsonLd.JsonLdProcessor.combineContexts(unsecuredDocumentClone["@context"], DidContexts.ContextSecurityJws2020);
256
- const hash = await this.createHash(unsecuredDocument, unsignedProof);
257
- const cryptoKey = await web.Jwk.toCryptoKey(signKey);
258
- const signature = await web.Jws.create(cryptoKey, hash, signKey.alg);
259
- const signedProof = core.ObjectHelper.clone(unsignedProof);
260
- signedProof["@context"] = unsecuredDocumentClone["@context"];
261
- signedProof.jws = signature;
262
- return signedProof;
263
- }
264
- /**
265
- * Verify a proof for the given data in format.
266
- * @param securedDocument The credential to verify.
267
- * @param signedProof The proof to verify.
268
- * @param verifyKey The public key to verify the proof with.
269
- * @returns True if the credential was verified.
270
- */
271
- async verifyProof(securedDocument, signedProof, verifyKey) {
272
- core.Guards.object(this.CLASS_NAME, "securedDocument", securedDocument);
273
- core.Guards.object(this.CLASS_NAME, "signedProof", signedProof);
274
- core.Guards.object(this.CLASS_NAME, "verifyKey", verifyKey);
275
- const jws = signedProof.jws;
276
- if (!core.Is.stringValue(jws)) {
277
- throw new core.GeneralError(this.CLASS_NAME, "jwsMissing");
278
- }
279
- const hash = await this.createHash(securedDocument, signedProof);
280
- const cryptoKey = await web.Jwk.toCryptoKey(verifyKey);
281
- return web.Jws.verify(jws, cryptoKey, hash);
282
- }
283
- /**
284
- * Create a hash for the given data.
285
- * @param unsecuredDocument The data to create the proof for.
286
- * @param unsignedProof The unsigned proof.
287
- * @returns The created hash.
288
- */
289
- async createHash(unsecuredDocument, unsignedProof) {
290
- core.Guards.object(this.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
291
- core.Guards.object(this.CLASS_NAME, "unsignedProof", unsignedProof);
292
- core.Guards.stringValue(this.CLASS_NAME, "unsignedProof.verificationMethod", unsignedProof.verificationMethod);
293
- const unsecuredDocumentClone = core.ObjectHelper.clone(unsecuredDocument);
294
- const proofOptionsClone = core.ObjectHelper.clone(unsignedProof);
295
- unsecuredDocumentClone["@context"] = dataJsonLd.JsonLdProcessor.combineContexts(unsecuredDocumentClone["@context"], DidContexts.ContextSecurityJws2020);
296
- proofOptionsClone["@context"] = unsecuredDocumentClone["@context"];
297
- delete unsecuredDocumentClone.proof;
298
- delete proofOptionsClone.jws;
299
- const canonizedData = await dataJsonLd.JsonLdProcessor.canonize(unsecuredDocumentClone);
300
- const canonizedProof = await dataJsonLd.JsonLdProcessor.canonize(proofOptionsClone);
301
- const hashedProof = crypto.Sha256.sum256(core.Converter.utf8ToBytes(canonizedProof));
302
- const hashedData = crypto.Sha256.sum256(core.Converter.utf8ToBytes(canonizedData));
303
- return core.Uint8ArrayHelper.concat([hashedProof, hashedData]);
304
- }
305
- }
306
-
307
- // Copyright 2024 IOTA Stiftung.
308
- // SPDX-License-Identifier: Apache-2.0.
309
- /**
310
- * Helper methods for multikey.
311
- */
312
- class MultikeyHelper {
313
- /**
314
- * Runtime name for the class.
315
- */
316
- static CLASS_NAME = "MultikeyHelper";
317
- /**
318
- * Convert a multikey to a JWK.
319
- * @param multikey The multikey to convert.
320
- * @returns The JWK.
321
- * @throws GeneralError if the multikey is invalid.
322
- */
323
- static toJwk(multikey) {
324
- core.Guards.object(MultikeyHelper.CLASS_NAME, "multikey", multikey);
325
- const { publicKey, privateKey } = MultikeyHelper.toRaw(multikey);
326
- return {
327
- kty: "OKP",
328
- crv: "Ed25519",
329
- alg: "EdDSA",
330
- x: core.Is.uint8Array(publicKey) ? core.Converter.bytesToBase64Url(publicKey) : undefined,
331
- d: core.Is.uint8Array(privateKey) ? core.Converter.bytesToBase64Url(privateKey) : undefined
332
- };
333
- }
334
- /**
335
- * Convert a JWK to a Multikey.
336
- * @param controller The controller of the multikey.
337
- * @param id The id of the multikey.
338
- * @param jwk The jwk to convert.
339
- * @returns The multikey.
340
- * @throws GeneralError if the jwk is invalid.
341
- */
342
- static fromJwk(controller, id, jwk) {
343
- core.Guards.stringValue(MultikeyHelper.CLASS_NAME, "controller", controller);
344
- core.Guards.stringValue(MultikeyHelper.CLASS_NAME, "id", id);
345
- core.Guards.object(MultikeyHelper.CLASS_NAME, "jwk", jwk);
346
- core.Guards.stringValue(MultikeyHelper.CLASS_NAME, "jwk.x", jwk.x);
347
- if (jwk.kty !== "OKP") {
348
- throw new core.GeneralError(MultikeyHelper.CLASS_NAME, "unsupportedKty", { kty: jwk.kty });
349
- }
350
- if (jwk.crv !== "Ed25519") {
351
- throw new core.GeneralError(MultikeyHelper.CLASS_NAME, "unsupportedCrv", { crv: jwk.crv });
352
- }
353
- const publicRaw = core.Converter.base64UrlToBytes(jwk.x);
354
- const publicKey = new Uint8Array(2 + publicRaw.length);
355
- publicKey[0] = 0xed;
356
- publicKey[1] = 0x01;
357
- publicKey.set(publicRaw, 2);
358
- const multikey = {
359
- "@context": DidContexts.ContextControllerIdentifiers,
360
- type: DidTypes.Multikey,
361
- controller,
362
- id,
363
- publicKeyMultibase: `z${core.Converter.bytesToBase58(publicKey)}`
364
- };
365
- if (core.Is.stringValue(jwk.d)) {
366
- const privateRaw = core.Converter.base64UrlToBytes(jwk.d);
367
- const secretKey = new Uint8Array(2 + privateRaw.length);
368
- secretKey[0] = 0x80;
369
- secretKey[1] = 0x26;
370
- secretKey.set(privateRaw, 2);
371
- multikey.secretKeyMultibase = `z${core.Converter.bytesToBase58(secretKey)}`;
372
- }
373
- return multikey;
374
- }
375
- /**
376
- * Convert a multikey to raw keys.
377
- * @param multikey The multikey to convert.
378
- * @returns The JWK.
379
- * @throws GeneralError if the multikey is invalid.
380
- */
381
- static toRaw(multikey) {
382
- core.Guards.object(MultikeyHelper.CLASS_NAME, "multikey", multikey);
383
- let publicKeyRaw;
384
- let secretKeyRaw;
385
- if (core.Is.stringValue(multikey.publicKeyMultibase)) {
386
- if (!multikey.publicKeyMultibase.startsWith("z")) {
387
- throw new core.GeneralError(MultikeyHelper.CLASS_NAME, "invalidPublicKeyMultibase", {
388
- publicKeyMultibase: multikey.publicKeyMultibase
389
- });
390
- }
391
- publicKeyRaw = core.Converter.base58ToBytes(multikey.publicKeyMultibase.slice(1));
392
- if (publicKeyRaw[0] !== 0xed || publicKeyRaw[1] !== 0x01) {
393
- throw new core.GeneralError(MultikeyHelper.CLASS_NAME, "publicKeyMultibaseMissingHeader", {
394
- publicKeyMultibase: multikey.publicKeyMultibase
395
- });
396
- }
397
- }
398
- if (core.Is.stringValue(multikey.secretKeyMultibase)) {
399
- if (!multikey.secretKeyMultibase.startsWith("z")) {
400
- throw new core.GeneralError(MultikeyHelper.CLASS_NAME, "invalidSecretKeyMultibase", {
401
- secretKeyMultibase: multikey.secretKeyMultibase
402
- });
403
- }
404
- secretKeyRaw = core.Converter.base58ToBytes(multikey.secretKeyMultibase.slice(1));
405
- if (secretKeyRaw[0] !== 0x80 || secretKeyRaw[1] !== 0x26) {
406
- throw new core.GeneralError(MultikeyHelper.CLASS_NAME, "publicKeyMultibaseMissingHeader", {
407
- publicKeyMultibase: multikey.publicKeyMultibase
408
- });
409
- }
410
- }
411
- return {
412
- publicKey: publicKeyRaw?.slice(2) ?? new Uint8Array(),
413
- privateKey: secretKeyRaw?.slice(2, 34) ?? new Uint8Array()
414
- };
415
- }
416
- }
417
-
418
- // Copyright 2024 IOTA Stiftung.
419
- // SPDX-License-Identifier: Apache-2.0.
420
- /**
421
- * Helper methods for creating and verifying proofs.
422
- */
423
- class ProofHelper {
424
- /**
425
- * Runtime name for the class.
426
- */
427
- static CLASS_NAME = "ProofHelper";
428
- /**
429
- * Create a signer verifier.
430
- * @param proofType The type of proof to create.
431
- * @returns The created signer verifier.
432
- * @throws GeneralError if the proof type is not supported.
433
- */
434
- static createSignerVerifier(proofType) {
435
- core.Guards.arrayOneOf(this.CLASS_NAME, "proofType", proofType, Object.values(ProofTypes));
436
- let signerVerifier;
437
- if (proofType === ProofTypes.DataIntegrityProof) {
438
- signerVerifier = new DataIntegrityProofSignerVerifier();
439
- }
440
- else if (proofType === ProofTypes.JsonWebSignature2020) {
441
- signerVerifier = new JsonWebSignature2020SignerVerifier();
442
- }
443
- if (core.Is.empty(signerVerifier)) {
444
- throw new core.GeneralError(ProofHelper.CLASS_NAME, "unsupportedProofType", { proofType });
445
- }
446
- return signerVerifier;
447
- }
448
- /**
449
- * Create a proof for the given data.
450
- * @param proofType The type of proof to create.
451
- * @param unsecuredDocument The data to create the proof for.
452
- * @param unsignedProof The proof options.
453
- * @param signKey The key to sign the proof with.
454
- * @returns The created proof.
455
- */
456
- static async createProof(proofType, unsecuredDocument, unsignedProof, signKey) {
457
- core.Guards.arrayOneOf(this.CLASS_NAME, "proofType", proofType, Object.values(ProofTypes));
458
- core.Guards.object(this.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
459
- core.Guards.object(this.CLASS_NAME, "unsignedProof", unsignedProof);
460
- core.Guards.object(this.CLASS_NAME, "signKey", signKey);
461
- return ProofHelper.createSignerVerifier(proofType).createProof(unsecuredDocument, unsignedProof, signKey);
462
- }
463
- /**
464
- * Verify a proof for the given data.
465
- * @param securedDocument The credential to verify.
466
- * @param signedProof The proof to verify.
467
- * @param verifyKey The public key to verify the proof with.
468
- * @returns True if the credential was verified.
469
- */
470
- static async verifyProof(securedDocument, signedProof, verifyKey) {
471
- core.Guards.object(this.CLASS_NAME, "securedDocument", securedDocument);
472
- core.Guards.object(this.CLASS_NAME, "signedProof", signedProof);
473
- core.Guards.stringValue(this.CLASS_NAME, "signedProof.type", signedProof.type);
474
- core.Guards.object(this.CLASS_NAME, "verifyKey", verifyKey);
475
- const signerVerifier = ProofHelper.createSignerVerifier(signedProof.type);
476
- return signerVerifier.verifyProof(securedDocument, signedProof, verifyKey);
477
- }
478
- /**
479
- * Create an unsigned proof.
480
- * @param proofType The type of proof to create.
481
- * @param verificationMethodId The verification method id.
482
- * @param otherParams Other parameters for the proof.
483
- * @returns The created proof.
484
- * @throws GeneralError if the proof type is not supported.
485
- */
486
- static createUnsignedProof(proofType, verificationMethodId,
487
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
488
- otherParams) {
489
- let proof;
490
- if (proofType === ProofTypes.DataIntegrityProof) {
491
- proof = {
492
- "@context": DidContexts.ContextDataIntegrity,
493
- type: ProofTypes.DataIntegrityProof,
494
- cryptosuite: DidCryptoSuites.EdDSAJcs2022,
495
- created: new Date(Date.now()).toISOString(),
496
- verificationMethod: verificationMethodId,
497
- proofPurpose: "assertionMethod",
498
- ...otherParams
499
- };
500
- }
501
- else if (proofType === ProofTypes.JsonWebSignature2020) {
502
- proof = {
503
- "@context": DidContexts.ContextSecurityJws2020,
504
- type: ProofTypes.JsonWebSignature2020,
505
- created: new Date(Date.now()).toISOString(),
506
- verificationMethod: verificationMethodId,
507
- proofPurpose: "assertionMethod",
508
- ...otherParams
509
- };
510
- }
511
- if (core.Is.empty(proof)) {
512
- throw new core.GeneralError(ProofHelper.CLASS_NAME, "unsupportedProofType", { proofType });
513
- }
514
- return proof;
515
- }
516
- }
517
-
518
- // Copyright 2024 IOTA Stiftung.
519
- // SPDX-License-Identifier: Apache-2.0.
520
- /**
521
- * Helper methods for creating and verifying proofs.
522
- */
523
- class VerifiableCredentialHelper {
524
- /**
525
- * Runtime name for the class.
526
- */
527
- static CLASS_NAME = "VerifiableCredentialHelper";
528
- /**
529
- * Get the valid until date from a verifiable credential.
530
- * @param verifiableCredential The verifiable credential to extract the expiration date from.
531
- * @returns The expiration date, if available.
532
- */
533
- static getValidUntil(verifiableCredential) {
534
- return (core.ObjectHelper.propertyGet(verifiableCredential, "validUntil") ??
535
- core.ObjectHelper.propertyGet(verifiableCredential, "expirationDate"));
536
- }
537
- /**
538
- * Get the valid from from a verifiable credential.
539
- * @param verifiableCredential The verifiable credential to extract the issuance date from.
540
- * @returns The issuance date, if available.
541
- */
542
- static getValidFrom(verifiableCredential) {
543
- return (core.ObjectHelper.propertyGet(verifiableCredential, "validFrom") ??
544
- core.ObjectHelper.propertyGet(verifiableCredential, "issuanceDate"));
545
- }
546
- }
547
-
548
- exports.DataIntegrityProofSignerVerifier = DataIntegrityProofSignerVerifier;
549
- exports.DidContexts = DidContexts;
550
- exports.DidCryptoSuites = DidCryptoSuites;
551
- exports.DidTypes = DidTypes;
552
- exports.DidVerificationMethodType = DidVerificationMethodType;
553
- exports.JsonWebSignature2020SignerVerifier = JsonWebSignature2020SignerVerifier;
554
- exports.MultikeyHelper = MultikeyHelper;
555
- exports.ProofHelper = ProofHelper;
556
- exports.ProofTypes = ProofTypes;
557
- exports.VerifiableCredentialHelper = VerifiableCredentialHelper;