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