@twin.org/standards-w3c-did 0.0.1-next.4 → 0.0.1-next.41
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/dist/cjs/index.cjs +492 -0
- package/dist/esm/index.mjs +485 -1
- package/dist/types/index.d.ts +16 -2
- package/dist/types/models/{IDidProof.d.ts → IDataIntegrityProof.d.ts} +18 -6
- package/dist/types/models/IDidCredentialSchema.d.ts +13 -0
- package/dist/types/models/IDidDocument.d.ts +6 -0
- package/dist/types/models/IDidDocumentVerificationMethod.d.ts +3 -2
- package/dist/types/models/IDidLabel.d.ts +17 -0
- package/dist/types/models/IDidVerifiableCredential.d.ts +41 -9
- package/dist/types/models/IDidVerifiablePresentation.d.ts +9 -7
- package/dist/types/models/IJsonWebSignature2020Proof.d.ts +33 -0
- package/dist/types/models/IMultikey.d.ts +41 -0
- package/dist/types/models/IProof.d.ts +6 -0
- package/dist/types/models/IProofSignerVerifier.d.ts +31 -0
- package/dist/types/models/didContexts.d.ts +41 -0
- package/dist/types/models/didCryptoSuites.d.ts +19 -0
- package/dist/types/models/didTypes.d.ts +33 -0
- package/dist/types/models/proofTypes.d.ts +17 -0
- package/dist/types/signerVerifiers/dataIntegrityProofSignerVerifier.d.ts +38 -0
- package/dist/types/signerVerifiers/jsonWebSignature2020SignerVerifier.d.ts +36 -0
- package/dist/types/utils/multikeyHelper.d.ts +37 -0
- package/dist/types/utils/proofHelper.d.ts +47 -0
- package/docs/changelog.md +57 -1
- package/docs/reference/classes/DataIntegrityProofSignerVerifier.md +134 -0
- package/docs/reference/classes/JsonWebSignature2020SignerVerifier.md +133 -0
- package/docs/reference/classes/MultikeyHelper.md +119 -0
- package/docs/reference/classes/ProofHelper.md +159 -0
- package/docs/reference/index.md +22 -1
- package/docs/reference/interfaces/{IDidProof.md → IDataIntegrityProof.md} +23 -7
- package/docs/reference/interfaces/IDidCredentialSchema.md +19 -0
- package/docs/reference/interfaces/IDidCredentialStatus.md +3 -1
- package/docs/reference/interfaces/IDidDocument.md +8 -0
- package/docs/reference/interfaces/IDidDocumentVerificationMethod.md +9 -1
- package/docs/reference/interfaces/IDidLabel.md +27 -0
- package/docs/reference/interfaces/IDidVerifiableCredential.md +57 -12
- package/docs/reference/interfaces/IDidVerifiablePresentation.md +6 -6
- package/docs/reference/interfaces/IJsonWebSignature2020Proof.md +52 -0
- package/docs/reference/interfaces/IMultikey.md +68 -0
- package/docs/reference/interfaces/IProofSignerVerifier.md +99 -0
- package/docs/reference/type-aliases/DidContexts.md +5 -0
- package/docs/reference/type-aliases/DidCryptoSuites.md +5 -0
- package/docs/reference/type-aliases/DidTypes.md +5 -0
- package/docs/reference/type-aliases/DidVerificationMethodType.md +1 -1
- package/docs/reference/type-aliases/IProof.md +5 -0
- package/docs/reference/type-aliases/ProofTypes.md +5 -0
- package/docs/reference/variables/DidContexts.md +55 -0
- package/docs/reference/variables/DidCryptoSuites.md +21 -0
- package/docs/reference/variables/DidTypes.md +43 -0
- package/docs/reference/variables/DidVerificationMethodType.md +1 -1
- package/docs/reference/variables/ProofTypes.md +19 -0
- package/locales/en.json +27 -1
- package/package.json +7 -4
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,5 +1,103 @@
|
|
|
1
1
|
'use strict';
|
|
2
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
|
+
|
|
3
101
|
/**
|
|
4
102
|
* The types of verification method.
|
|
5
103
|
*/
|
|
@@ -31,4 +129,398 @@ const DidVerificationMethodType = {
|
|
|
31
129
|
CapabilityDelegation: "capabilityDelegation"
|
|
32
130
|
};
|
|
33
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
|
+
exports.DataIntegrityProofSignerVerifier = DataIntegrityProofSignerVerifier;
|
|
519
|
+
exports.DidContexts = DidContexts;
|
|
520
|
+
exports.DidCryptoSuites = DidCryptoSuites;
|
|
521
|
+
exports.DidTypes = DidTypes;
|
|
34
522
|
exports.DidVerificationMethodType = DidVerificationMethodType;
|
|
523
|
+
exports.JsonWebSignature2020SignerVerifier = JsonWebSignature2020SignerVerifier;
|
|
524
|
+
exports.MultikeyHelper = MultikeyHelper;
|
|
525
|
+
exports.ProofHelper = ProofHelper;
|
|
526
|
+
exports.ProofTypes = ProofTypes;
|