@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.
- package/dist/es/index.js +33 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IDataIntegrityProof.js +2 -0
- package/dist/es/models/IDataIntegrityProof.js.map +1 -0
- package/dist/es/models/IDidCredentialSchema.js +4 -0
- package/dist/es/models/IDidCredentialSchema.js.map +1 -0
- package/dist/es/models/IDidCredentialStatus.js +4 -0
- package/dist/es/models/IDidCredentialStatus.js.map +1 -0
- package/dist/es/models/IDidDocument.js +2 -0
- package/dist/es/models/IDidDocument.js.map +1 -0
- package/dist/es/models/IDidDocumentVerificationMethod.js +2 -0
- package/dist/es/models/IDidDocumentVerificationMethod.js.map +1 -0
- package/dist/es/models/IDidLabel.js +4 -0
- package/dist/es/models/IDidLabel.js.map +1 -0
- package/dist/es/models/IDidPresentationVerification.js +2 -0
- package/dist/es/models/IDidPresentationVerification.js.map +1 -0
- package/dist/es/models/IDidService.js +4 -0
- package/dist/es/models/IDidService.js.map +1 -0
- package/dist/es/models/IDidVerifiableCredential.js +2 -0
- package/dist/es/models/IDidVerifiableCredential.js.map +1 -0
- package/dist/es/models/IDidVerifiableCredentialCommon.js +2 -0
- package/dist/es/models/IDidVerifiableCredentialCommon.js.map +1 -0
- package/dist/es/models/IDidVerifiableCredentialV1.js +2 -0
- package/dist/es/models/IDidVerifiableCredentialV1.js.map +1 -0
- package/dist/es/models/IDidVerifiableCredentialV2.js +2 -0
- package/dist/es/models/IDidVerifiableCredentialV2.js.map +1 -0
- package/dist/es/models/IDidVerifiablePresentation.js +2 -0
- package/dist/es/models/IDidVerifiablePresentation.js.map +1 -0
- package/dist/es/models/IDidVerifiablePresentationCommon.js +2 -0
- package/dist/es/models/IDidVerifiablePresentationCommon.js.map +1 -0
- package/dist/es/models/IDidVerifiablePresentationV1.js +2 -0
- package/dist/es/models/IDidVerifiablePresentationV1.js.map +1 -0
- package/dist/es/models/IDidVerifiablePresentationV2.js +2 -0
- package/dist/es/models/IDidVerifiablePresentationV2.js.map +1 -0
- package/dist/es/models/IJsonWebSignature2020Proof.js +2 -0
- package/dist/es/models/IJsonWebSignature2020Proof.js.map +1 -0
- package/dist/es/models/IMultikey.js +2 -0
- package/dist/es/models/IMultikey.js.map +1 -0
- package/dist/es/models/IProof.js +2 -0
- package/dist/es/models/IProof.js.map +1 -0
- package/dist/es/models/IProofSignerVerifier.js +2 -0
- package/dist/es/models/IProofSignerVerifier.js.map +1 -0
- package/dist/es/models/didContexts.js +41 -0
- package/dist/es/models/didContexts.js.map +1 -0
- package/dist/es/models/didCryptoSuites.js +19 -0
- package/dist/es/models/didCryptoSuites.js.map +1 -0
- package/dist/es/models/didTypes.js +33 -0
- package/dist/es/models/didTypes.js.map +1 -0
- package/dist/es/models/didVerificationMethodType.js +31 -0
- package/dist/es/models/didVerificationMethodType.js.map +1 -0
- package/dist/es/models/proofTypes.js +17 -0
- package/dist/es/models/proofTypes.js.map +1 -0
- package/dist/es/signerVerifiers/dataIntegrityProofSignerVerifier.js +90 -0
- package/dist/es/signerVerifiers/dataIntegrityProofSignerVerifier.js.map +1 -0
- package/dist/es/signerVerifiers/jsonWebSignature2020SignerVerifier.js +79 -0
- package/dist/es/signerVerifiers/jsonWebSignature2020SignerVerifier.js.map +1 -0
- package/dist/es/utils/multikeyHelper.js +114 -0
- package/dist/es/utils/multikeyHelper.js.map +1 -0
- package/dist/es/utils/proofHelper.js +106 -0
- package/dist/es/utils/proofHelper.js.map +1 -0
- package/dist/es/utils/verifiableCredentialHelper.js +31 -0
- package/dist/es/utils/verifiableCredentialHelper.js.map +1 -0
- package/dist/types/index.d.ts +30 -30
- package/dist/types/models/IDataIntegrityProof.d.ts +3 -3
- package/dist/types/models/IDidDocument.d.ts +3 -3
- package/dist/types/models/IDidPresentationVerification.d.ts +1 -1
- package/dist/types/models/IDidVerifiableCredential.d.ts +2 -2
- package/dist/types/models/IDidVerifiableCredentialCommon.d.ts +4 -4
- package/dist/types/models/IDidVerifiableCredentialV1.d.ts +2 -2
- package/dist/types/models/IDidVerifiableCredentialV2.d.ts +2 -2
- package/dist/types/models/IDidVerifiablePresentation.d.ts +2 -2
- package/dist/types/models/IDidVerifiablePresentationCommon.d.ts +1 -1
- package/dist/types/models/IDidVerifiablePresentationV1.d.ts +3 -3
- package/dist/types/models/IDidVerifiablePresentationV2.d.ts +3 -3
- package/dist/types/models/IJsonWebSignature2020Proof.d.ts +2 -2
- package/dist/types/models/IMultikey.d.ts +2 -2
- package/dist/types/models/IProof.d.ts +2 -2
- package/dist/types/models/IProofSignerVerifier.d.ts +1 -1
- package/dist/types/models/didVerificationMethodType.d.ts +1 -1
- package/dist/types/signerVerifiers/dataIntegrityProofSignerVerifier.d.ts +4 -4
- package/dist/types/signerVerifiers/jsonWebSignature2020SignerVerifier.d.ts +3 -3
- package/dist/types/utils/multikeyHelper.d.ts +1 -1
- package/dist/types/utils/proofHelper.d.ts +3 -3
- package/dist/types/utils/verifiableCredentialHelper.d.ts +1 -1
- package/docs/changelog.md +76 -0
- package/docs/reference/classes/DataIntegrityProofSignerVerifier.md +3 -3
- package/docs/reference/classes/JsonWebSignature2020SignerVerifier.md +3 -3
- package/docs/reference/classes/MultikeyHelper.md +3 -3
- package/docs/reference/classes/ProofHelper.md +2 -2
- package/docs/reference/classes/VerifiableCredentialHelper.md +4 -4
- package/docs/reference/interfaces/IDidDocumentVerificationMethod.md +2 -2
- package/docs/reference/interfaces/IProofSignerVerifier.md +2 -2
- package/locales/en.json +4 -8
- package/package.json +20 -8
- package/dist/cjs/index.cjs +0 -557
- package/dist/esm/index.mjs +0 -546
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { Converter, GeneralError, Guards, Is, ObjectHelper, Uint8ArrayHelper } from "@twin.org/core";
|
|
4
|
+
import { Sha256 } from "@twin.org/crypto";
|
|
5
|
+
import { JsonLdProcessor } from "@twin.org/data-json-ld";
|
|
6
|
+
import { Jwk, Jws } from "@twin.org/web";
|
|
7
|
+
import { DidContexts } from "../models/didContexts.js";
|
|
8
|
+
/**
|
|
9
|
+
* Helper methods for creating and verifying proofs.
|
|
10
|
+
*/
|
|
11
|
+
export class JsonWebSignature2020SignerVerifier {
|
|
12
|
+
/**
|
|
13
|
+
* Runtime name for the class.
|
|
14
|
+
*/
|
|
15
|
+
static CLASS_NAME = "JsonWebSignature2020SignerVerifier";
|
|
16
|
+
/**
|
|
17
|
+
* Create a proof for the given data.
|
|
18
|
+
* @param unsecuredDocument The data to create the proof for.
|
|
19
|
+
* @param unsignedProof The proof options.
|
|
20
|
+
* @param signKey The key to sign the proof with.
|
|
21
|
+
* @returns The created proof.
|
|
22
|
+
*/
|
|
23
|
+
async createProof(unsecuredDocument, unsignedProof, signKey) {
|
|
24
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
|
|
25
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "unsignedProof", unsignedProof);
|
|
26
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "signKey", signKey);
|
|
27
|
+
const unsecuredDocumentClone = ObjectHelper.clone(unsecuredDocument);
|
|
28
|
+
unsecuredDocumentClone["@context"] = JsonLdProcessor.combineContexts(unsecuredDocumentClone["@context"], DidContexts.ContextSecurityJws2020);
|
|
29
|
+
const hash = await this.createHash(unsecuredDocument, unsignedProof);
|
|
30
|
+
const cryptoKey = await Jwk.toCryptoKey(signKey);
|
|
31
|
+
const signature = await Jws.create(cryptoKey, hash, signKey.alg);
|
|
32
|
+
const signedProof = ObjectHelper.clone(unsignedProof);
|
|
33
|
+
signedProof["@context"] = unsecuredDocumentClone["@context"];
|
|
34
|
+
signedProof.jws = signature;
|
|
35
|
+
return signedProof;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Verify a proof for the given data in format.
|
|
39
|
+
* @param securedDocument The credential to verify.
|
|
40
|
+
* @param signedProof The proof to verify.
|
|
41
|
+
* @param verifyKey The public key to verify the proof with.
|
|
42
|
+
* @returns True if the credential was verified.
|
|
43
|
+
*/
|
|
44
|
+
async verifyProof(securedDocument, signedProof, verifyKey) {
|
|
45
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "securedDocument", securedDocument);
|
|
46
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "signedProof", signedProof);
|
|
47
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "verifyKey", verifyKey);
|
|
48
|
+
const jws = signedProof.jws;
|
|
49
|
+
if (!Is.stringValue(jws)) {
|
|
50
|
+
throw new GeneralError(JsonWebSignature2020SignerVerifier.CLASS_NAME, "missingJws");
|
|
51
|
+
}
|
|
52
|
+
const hash = await this.createHash(securedDocument, signedProof);
|
|
53
|
+
const cryptoKey = await Jwk.toCryptoKey(verifyKey);
|
|
54
|
+
return Jws.verify(jws, cryptoKey, hash);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create a hash for the given data.
|
|
58
|
+
* @param unsecuredDocument The data to create the proof for.
|
|
59
|
+
* @param unsignedProof The unsigned proof.
|
|
60
|
+
* @returns The created hash.
|
|
61
|
+
*/
|
|
62
|
+
async createHash(unsecuredDocument, unsignedProof) {
|
|
63
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
|
|
64
|
+
Guards.object(JsonWebSignature2020SignerVerifier.CLASS_NAME, "unsignedProof", unsignedProof);
|
|
65
|
+
Guards.stringValue(JsonWebSignature2020SignerVerifier.CLASS_NAME, "unsignedProof.verificationMethod", unsignedProof.verificationMethod);
|
|
66
|
+
const unsecuredDocumentClone = ObjectHelper.clone(unsecuredDocument);
|
|
67
|
+
const proofOptionsClone = ObjectHelper.clone(unsignedProof);
|
|
68
|
+
unsecuredDocumentClone["@context"] = JsonLdProcessor.combineContexts(unsecuredDocumentClone["@context"], DidContexts.ContextSecurityJws2020);
|
|
69
|
+
proofOptionsClone["@context"] = unsecuredDocumentClone["@context"];
|
|
70
|
+
delete unsecuredDocumentClone.proof;
|
|
71
|
+
delete proofOptionsClone.jws;
|
|
72
|
+
const canonizedData = await JsonLdProcessor.canonize(unsecuredDocumentClone);
|
|
73
|
+
const canonizedProof = await JsonLdProcessor.canonize(proofOptionsClone);
|
|
74
|
+
const hashedProof = Sha256.sum256(Converter.utf8ToBytes(canonizedProof));
|
|
75
|
+
const hashedData = Sha256.sum256(Converter.utf8ToBytes(canonizedData));
|
|
76
|
+
return Uint8ArrayHelper.concat([hashedProof, hashedData]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=jsonWebSignature2020SignerVerifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonWebSignature2020SignerVerifier.js","sourceRoot":"","sources":["../../../src/signerVerifiers/jsonWebSignature2020SignerVerifier.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,SAAS,EACT,YAAY,EACZ,MAAM,EACN,EAAE,EACF,YAAY,EACZ,gBAAgB,EAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,eAAe,EAA0B,MAAM,wBAAwB,CAAC;AAEjF,OAAO,EAAE,GAAG,EAAE,GAAG,EAAa,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAIvD;;GAEG;AACH,MAAM,OAAO,kCAAkC;IAC9C;;OAEG;IACI,MAAM,CAAU,UAAU,wCAAwD;IAEzF;;;;;;OAMG;IACI,KAAK,CAAC,WAAW,CACvB,iBAAoC,EACpC,aAAyC,EACzC,OAAa;QAEb,MAA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ACrF,MAAM,cAAc,GAAW,MAAM,eAAe,CAAC,QAAQ,CAC5D,iBAAiD,CACjD,CAAC;QAEF,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC;QACzE,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC,CAAC;QAEvE,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAC;IAC3D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tConverter,\n\tGeneralError,\n\tGuards,\n\tIs,\n\tObjectHelper,\n\tUint8ArrayHelper\n} from \"@twin.org/core\";\nimport { Sha256 } from \"@twin.org/crypto\";\nimport { JsonLdProcessor, type IJsonLdNodeObject } from \"@twin.org/data-json-ld\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { Jwk, Jws, type IJwk } from \"@twin.org/web\";\nimport { DidContexts } from \"../models/didContexts.js\";\nimport type { IJsonWebSignature2020Proof } from \"../models/IJsonWebSignature2020Proof.js\";\nimport type { IProofSignerVerifier } from \"../models/IProofSignerVerifier.js\";\n\n/**\n * Helper methods for creating and verifying proofs.\n */\nexport class JsonWebSignature2020SignerVerifier implements IProofSignerVerifier {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<JsonWebSignature2020SignerVerifier>();\n\n\t/**\n\t * Create a proof for the given data.\n\t * @param unsecuredDocument The data to create the proof for.\n\t * @param unsignedProof The proof options.\n\t * @param signKey The key to sign the proof with.\n\t * @returns The created proof.\n\t */\n\tpublic async createProof(\n\t\tunsecuredDocument: IJsonLdNodeObject,\n\t\tunsignedProof: IJsonWebSignature2020Proof,\n\t\tsignKey: IJwk\n\t): Promise<IJsonWebSignature2020Proof> {\n\t\tGuards.object<IJsonLdNodeObject>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(unsecuredDocument),\n\t\t\tunsecuredDocument\n\t\t);\n\t\tGuards.object<IJsonWebSignature2020Proof>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(unsignedProof),\n\t\t\tunsignedProof\n\t\t);\n\t\tGuards.object<IJwk>(JsonWebSignature2020SignerVerifier.CLASS_NAME, nameof(signKey), signKey);\n\n\t\tconst unsecuredDocumentClone = ObjectHelper.clone(unsecuredDocument);\n\n\t\tunsecuredDocumentClone[\"@context\"] = JsonLdProcessor.combineContexts(\n\t\t\tunsecuredDocumentClone[\"@context\"],\n\t\t\tDidContexts.ContextSecurityJws2020\n\t\t);\n\n\t\tconst hash = await this.createHash(unsecuredDocument, unsignedProof);\n\n\t\tconst cryptoKey = await Jwk.toCryptoKey(signKey);\n\n\t\tconst signature = await Jws.create(cryptoKey, hash, signKey.alg);\n\n\t\tconst signedProof = ObjectHelper.clone(unsignedProof);\n\n\t\tsignedProof[\"@context\"] = unsecuredDocumentClone[\n\t\t\t\"@context\"\n\t\t] as IJsonWebSignature2020Proof[\"@context\"];\n\n\t\tsignedProof.jws = signature;\n\n\t\treturn signedProof;\n\t}\n\n\t/**\n\t * Verify a proof for the given data in format.\n\t * @param securedDocument The credential to verify.\n\t * @param signedProof The proof to verify.\n\t * @param verifyKey The public key to verify the proof with.\n\t * @returns True if the credential was verified.\n\t */\n\tpublic async verifyProof(\n\t\tsecuredDocument: IJsonLdNodeObject,\n\t\tsignedProof: IJsonWebSignature2020Proof,\n\t\tverifyKey: IJwk\n\t): Promise<boolean> {\n\t\tGuards.object<IJsonLdNodeObject>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(securedDocument),\n\t\t\tsecuredDocument\n\t\t);\n\t\tGuards.object<IJsonWebSignature2020Proof>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(signedProof),\n\t\t\tsignedProof\n\t\t);\n\t\tGuards.object<IJwk>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(verifyKey),\n\t\t\tverifyKey\n\t\t);\n\t\tconst jws = signedProof.jws;\n\t\tif (!Is.stringValue(jws)) {\n\t\t\tthrow new GeneralError(JsonWebSignature2020SignerVerifier.CLASS_NAME, \"missingJws\");\n\t\t}\n\n\t\tconst hash = await this.createHash(securedDocument, signedProof);\n\n\t\tconst cryptoKey = await Jwk.toCryptoKey(verifyKey);\n\n\t\treturn Jws.verify(jws, cryptoKey, hash);\n\t}\n\n\t/**\n\t * Create a hash for the given data.\n\t * @param unsecuredDocument The data to create the proof for.\n\t * @param unsignedProof The unsigned proof.\n\t * @returns The created hash.\n\t */\n\tpublic async createHash(\n\t\tunsecuredDocument: IJsonLdNodeObject,\n\t\tunsignedProof: IJsonWebSignature2020Proof\n\t): Promise<Uint8Array> {\n\t\tGuards.object<IJsonLdNodeObject>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(unsecuredDocument),\n\t\t\tunsecuredDocument\n\t\t);\n\t\tGuards.object<IJsonWebSignature2020Proof>(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(unsignedProof),\n\t\t\tunsignedProof\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tJsonWebSignature2020SignerVerifier.CLASS_NAME,\n\t\t\tnameof(unsignedProof.verificationMethod),\n\t\t\tunsignedProof.verificationMethod\n\t\t);\n\t\tconst unsecuredDocumentClone = ObjectHelper.clone(unsecuredDocument);\n\t\tconst proofOptionsClone = ObjectHelper.clone(unsignedProof);\n\n\t\tunsecuredDocumentClone[\"@context\"] = JsonLdProcessor.combineContexts(\n\t\t\tunsecuredDocumentClone[\"@context\"],\n\t\t\tDidContexts.ContextSecurityJws2020\n\t\t);\n\n\t\tproofOptionsClone[\"@context\"] = unsecuredDocumentClone[\n\t\t\t\"@context\"\n\t\t] as IJsonWebSignature2020Proof[\"@context\"];\n\n\t\tdelete unsecuredDocumentClone.proof;\n\t\tdelete proofOptionsClone.jws;\n\n\t\tconst canonizedData: string = await JsonLdProcessor.canonize(unsecuredDocumentClone);\n\t\tconst canonizedProof: string = await JsonLdProcessor.canonize(\n\t\t\tproofOptionsClone as unknown as IJsonLdNodeObject\n\t\t);\n\n\t\tconst hashedProof = Sha256.sum256(Converter.utf8ToBytes(canonizedProof));\n\t\tconst hashedData = Sha256.sum256(Converter.utf8ToBytes(canonizedData));\n\n\t\treturn Uint8ArrayHelper.concat([hashedProof, hashedData]);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { Converter, GeneralError, Guards, Is } from "@twin.org/core";
|
|
4
|
+
import { DidContexts } from "../models/didContexts.js";
|
|
5
|
+
import { DidTypes } from "../models/didTypes.js";
|
|
6
|
+
/**
|
|
7
|
+
* Helper methods for multikey.
|
|
8
|
+
*/
|
|
9
|
+
export class MultikeyHelper {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static CLASS_NAME = "MultikeyHelper";
|
|
14
|
+
/**
|
|
15
|
+
* Convert a multikey to a JWK.
|
|
16
|
+
* @param multikey The multikey to convert.
|
|
17
|
+
* @returns The JWK.
|
|
18
|
+
* @throws GeneralError if the multikey is invalid.
|
|
19
|
+
*/
|
|
20
|
+
static toJwk(multikey) {
|
|
21
|
+
Guards.object(MultikeyHelper.CLASS_NAME, "multikey", multikey);
|
|
22
|
+
const { publicKey, privateKey } = MultikeyHelper.toRaw(multikey);
|
|
23
|
+
return {
|
|
24
|
+
kty: "OKP",
|
|
25
|
+
crv: "Ed25519",
|
|
26
|
+
alg: "EdDSA",
|
|
27
|
+
x: Is.uint8Array(publicKey) ? Converter.bytesToBase64Url(publicKey) : undefined,
|
|
28
|
+
d: Is.uint8Array(privateKey) ? Converter.bytesToBase64Url(privateKey) : undefined
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Convert a JWK to a Multikey.
|
|
33
|
+
* @param controller The controller of the multikey.
|
|
34
|
+
* @param id The id of the multikey.
|
|
35
|
+
* @param jwk The jwk to convert.
|
|
36
|
+
* @returns The multikey.
|
|
37
|
+
* @throws GeneralError if the jwk is invalid.
|
|
38
|
+
*/
|
|
39
|
+
static fromJwk(controller, id, jwk) {
|
|
40
|
+
Guards.stringValue(MultikeyHelper.CLASS_NAME, "controller", controller);
|
|
41
|
+
Guards.stringValue(MultikeyHelper.CLASS_NAME, "id", id);
|
|
42
|
+
Guards.object(MultikeyHelper.CLASS_NAME, "jwk", jwk);
|
|
43
|
+
Guards.stringValue(MultikeyHelper.CLASS_NAME, "jwk.x", jwk.x);
|
|
44
|
+
if (jwk.kty !== "OKP") {
|
|
45
|
+
throw new GeneralError(MultikeyHelper.CLASS_NAME, "unsupportedKty", { kty: jwk.kty });
|
|
46
|
+
}
|
|
47
|
+
if (jwk.crv !== "Ed25519") {
|
|
48
|
+
throw new GeneralError(MultikeyHelper.CLASS_NAME, "unsupportedCrv", { crv: jwk.crv });
|
|
49
|
+
}
|
|
50
|
+
const publicRaw = Converter.base64UrlToBytes(jwk.x);
|
|
51
|
+
const publicKey = new Uint8Array(2 + publicRaw.length);
|
|
52
|
+
publicKey[0] = 0xed;
|
|
53
|
+
publicKey[1] = 0x01;
|
|
54
|
+
publicKey.set(publicRaw, 2);
|
|
55
|
+
const multikey = {
|
|
56
|
+
"@context": DidContexts.ContextControllerIdentifiers,
|
|
57
|
+
type: DidTypes.Multikey,
|
|
58
|
+
controller,
|
|
59
|
+
id,
|
|
60
|
+
publicKeyMultibase: `z${Converter.bytesToBase58(publicKey)}`
|
|
61
|
+
};
|
|
62
|
+
if (Is.stringValue(jwk.d)) {
|
|
63
|
+
const privateRaw = Converter.base64UrlToBytes(jwk.d);
|
|
64
|
+
const secretKey = new Uint8Array(2 + privateRaw.length);
|
|
65
|
+
secretKey[0] = 0x80;
|
|
66
|
+
secretKey[1] = 0x26;
|
|
67
|
+
secretKey.set(privateRaw, 2);
|
|
68
|
+
multikey.secretKeyMultibase = `z${Converter.bytesToBase58(secretKey)}`;
|
|
69
|
+
}
|
|
70
|
+
return multikey;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Convert a multikey to raw keys.
|
|
74
|
+
* @param multikey The multikey to convert.
|
|
75
|
+
* @returns The JWK.
|
|
76
|
+
* @throws GeneralError if the multikey is invalid.
|
|
77
|
+
*/
|
|
78
|
+
static toRaw(multikey) {
|
|
79
|
+
Guards.object(MultikeyHelper.CLASS_NAME, "multikey", multikey);
|
|
80
|
+
let publicKeyRaw;
|
|
81
|
+
let secretKeyRaw;
|
|
82
|
+
if (Is.stringValue(multikey.publicKeyMultibase)) {
|
|
83
|
+
if (!multikey.publicKeyMultibase.startsWith("z")) {
|
|
84
|
+
throw new GeneralError(MultikeyHelper.CLASS_NAME, "invalidPublicKeyMultibase", {
|
|
85
|
+
publicKeyMultibase: multikey.publicKeyMultibase
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
publicKeyRaw = Converter.base58ToBytes(multikey.publicKeyMultibase.slice(1));
|
|
89
|
+
if (publicKeyRaw[0] !== 0xed || publicKeyRaw[1] !== 0x01) {
|
|
90
|
+
throw new GeneralError(MultikeyHelper.CLASS_NAME, "publicKeyMultibaseMissingHeader", {
|
|
91
|
+
publicKeyMultibase: multikey.publicKeyMultibase
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (Is.stringValue(multikey.secretKeyMultibase)) {
|
|
96
|
+
if (!multikey.secretKeyMultibase.startsWith("z")) {
|
|
97
|
+
throw new GeneralError(MultikeyHelper.CLASS_NAME, "invalidSecretKeyMultibase", {
|
|
98
|
+
secretKeyMultibase: multikey.secretKeyMultibase
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
secretKeyRaw = Converter.base58ToBytes(multikey.secretKeyMultibase.slice(1));
|
|
102
|
+
if (secretKeyRaw[0] !== 0x80 || secretKeyRaw[1] !== 0x26) {
|
|
103
|
+
throw new GeneralError(MultikeyHelper.CLASS_NAME, "secretKeyMultibaseMissingHeader", {
|
|
104
|
+
secretKeyMultibase: multikey.secretKeyMultibase
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
publicKey: publicKeyRaw?.slice(2) ?? new Uint8Array(),
|
|
110
|
+
privateKey: secretKeyRaw?.slice(2, 34) ?? new Uint8Array()
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=multikeyHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multikeyHelper.js","sourceRoot":"","sources":["../../../src/utils/multikeyHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAGrE,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAGjD;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;OAEG;IACI,MAAM,CAAU,UAAU,oBAAoC;IAErE;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,QAAmB;QACtC,MAAM,CAAC,MAAM,CAAY,cAAc,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QAEhF,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAEjE,OAAO;YACN,GAAG,EAAE,KAAK;YACV,GAAG,EAAE,SAAS;YACd,GAAG,EAAE,OAAO;Y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AC,IAAI,IAAI,UAAU,EAAE;YACrD,UAAU,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,UAAU,EAAE;SAC1D,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Converter, GeneralError, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IJwk } from \"@twin.org/web\";\nimport { DidContexts } from \"../models/didContexts.js\";\nimport { DidTypes } from \"../models/didTypes.js\";\nimport type { IMultikey } from \"../models/IMultikey.js\";\n\n/**\n * Helper methods for multikey.\n */\nexport class MultikeyHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MultikeyHelper>();\n\n\t/**\n\t * Convert a multikey to a JWK.\n\t * @param multikey The multikey to convert.\n\t * @returns The JWK.\n\t * @throws GeneralError if the multikey is invalid.\n\t */\n\tpublic static toJwk(multikey: IMultikey): IJwk {\n\t\tGuards.object<IMultikey>(MultikeyHelper.CLASS_NAME, nameof(multikey), multikey);\n\n\t\tconst { publicKey, privateKey } = MultikeyHelper.toRaw(multikey);\n\n\t\treturn {\n\t\t\tkty: \"OKP\",\n\t\t\tcrv: \"Ed25519\",\n\t\t\talg: \"EdDSA\",\n\t\t\tx: Is.uint8Array(publicKey) ? Converter.bytesToBase64Url(publicKey) : undefined,\n\t\t\td: Is.uint8Array(privateKey) ? Converter.bytesToBase64Url(privateKey) : undefined\n\t\t};\n\t}\n\n\t/**\n\t * Convert a JWK to a Multikey.\n\t * @param controller The controller of the multikey.\n\t * @param id The id of the multikey.\n\t * @param jwk The jwk to convert.\n\t * @returns The multikey.\n\t * @throws GeneralError if the jwk is invalid.\n\t */\n\tpublic static fromJwk(controller: string, id: string, jwk: IJwk): IMultikey {\n\t\tGuards.stringValue(MultikeyHelper.CLASS_NAME, nameof(controller), controller);\n\t\tGuards.stringValue(MultikeyHelper.CLASS_NAME, nameof(id), id);\n\t\tGuards.object<IJwk>(MultikeyHelper.CLASS_NAME, nameof(jwk), jwk);\n\t\tGuards.stringValue(MultikeyHelper.CLASS_NAME, nameof(jwk.x), jwk.x);\n\n\t\tif (jwk.kty !== \"OKP\") {\n\t\t\tthrow new GeneralError(MultikeyHelper.CLASS_NAME, \"unsupportedKty\", { kty: jwk.kty });\n\t\t}\n\n\t\tif (jwk.crv !== \"Ed25519\") {\n\t\t\tthrow new GeneralError(MultikeyHelper.CLASS_NAME, \"unsupportedCrv\", { crv: jwk.crv });\n\t\t}\n\n\t\tconst publicRaw = Converter.base64UrlToBytes(jwk.x);\n\t\tconst publicKey = new Uint8Array(2 + publicRaw.length);\n\t\tpublicKey[0] = 0xed;\n\t\tpublicKey[1] = 0x01;\n\t\tpublicKey.set(publicRaw, 2);\n\n\t\tconst multikey: IMultikey = {\n\t\t\t\"@context\": DidContexts.ContextControllerIdentifiers,\n\t\t\ttype: DidTypes.Multikey,\n\t\t\tcontroller,\n\t\t\tid,\n\t\t\tpublicKeyMultibase: `z${Converter.bytesToBase58(publicKey)}`\n\t\t};\n\n\t\tif (Is.stringValue(jwk.d)) {\n\t\t\tconst privateRaw = Converter.base64UrlToBytes(jwk.d);\n\t\t\tconst secretKey = new Uint8Array(2 + privateRaw.length);\n\t\t\tsecretKey[0] = 0x80;\n\t\t\tsecretKey[1] = 0x26;\n\t\t\tsecretKey.set(privateRaw, 2);\n\t\t\tmultikey.secretKeyMultibase = `z${Converter.bytesToBase58(secretKey)}`;\n\t\t}\n\n\t\treturn multikey;\n\t}\n\n\t/**\n\t * Convert a multikey to raw keys.\n\t * @param multikey The multikey to convert.\n\t * @returns The JWK.\n\t * @throws GeneralError if the multikey is invalid.\n\t */\n\tpublic static toRaw(multikey: IMultikey): {\n\t\tpublicKey: Uint8Array;\n\t\tprivateKey: Uint8Array;\n\t} {\n\t\tGuards.object<IMultikey>(MultikeyHelper.CLASS_NAME, nameof(multikey), multikey);\n\n\t\tlet publicKeyRaw: Uint8Array | undefined;\n\t\tlet secretKeyRaw: Uint8Array | undefined;\n\n\t\tif (Is.stringValue(multikey.publicKeyMultibase)) {\n\t\t\tif (!multikey.publicKeyMultibase.startsWith(\"z\")) {\n\t\t\t\tthrow new GeneralError(MultikeyHelper.CLASS_NAME, \"invalidPublicKeyMultibase\", {\n\t\t\t\t\tpublicKeyMultibase: multikey.publicKeyMultibase\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tpublicKeyRaw = Converter.base58ToBytes(multikey.publicKeyMultibase.slice(1));\n\t\t\tif (publicKeyRaw[0] !== 0xed || publicKeyRaw[1] !== 0x01) {\n\t\t\t\tthrow new GeneralError(MultikeyHelper.CLASS_NAME, \"publicKeyMultibaseMissingHeader\", {\n\t\t\t\t\tpublicKeyMultibase: multikey.publicKeyMultibase\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (Is.stringValue(multikey.secretKeyMultibase)) {\n\t\t\tif (!multikey.secretKeyMultibase.startsWith(\"z\")) {\n\t\t\t\tthrow new GeneralError(MultikeyHelper.CLASS_NAME, \"invalidSecretKeyMultibase\", {\n\t\t\t\t\tsecretKeyMultibase: multikey.secretKeyMultibase\n\t\t\t\t});\n\t\t\t}\n\t\t\tsecretKeyRaw = Converter.base58ToBytes(multikey.secretKeyMultibase.slice(1));\n\t\t\tif (secretKeyRaw[0] !== 0x80 || secretKeyRaw[1] !== 0x26) {\n\t\t\t\tthrow new GeneralError(MultikeyHelper.CLASS_NAME, \"secretKeyMultibaseMissingHeader\", {\n\t\t\t\t\tsecretKeyMultibase: multikey.secretKeyMultibase\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tpublicKey: publicKeyRaw?.slice(2) ?? new Uint8Array(),\n\t\t\tprivateKey: secretKeyRaw?.slice(2, 34) ?? new Uint8Array()\n\t\t};\n\t}\n}\n"]}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { GeneralError, Guards, Is } from "@twin.org/core";
|
|
4
|
+
import { DidContexts } from "../models/didContexts.js";
|
|
5
|
+
import { DidCryptoSuites } from "../models/didCryptoSuites.js";
|
|
6
|
+
import { ProofTypes } from "../models/proofTypes.js";
|
|
7
|
+
import { DataIntegrityProofSignerVerifier } from "../signerVerifiers/dataIntegrityProofSignerVerifier.js";
|
|
8
|
+
import { JsonWebSignature2020SignerVerifier } from "../signerVerifiers/jsonWebSignature2020SignerVerifier.js";
|
|
9
|
+
/**
|
|
10
|
+
* Helper methods for creating and verifying proofs.
|
|
11
|
+
*/
|
|
12
|
+
export class ProofHelper {
|
|
13
|
+
/**
|
|
14
|
+
* Runtime name for the class.
|
|
15
|
+
*/
|
|
16
|
+
static CLASS_NAME = "ProofHelper";
|
|
17
|
+
/**
|
|
18
|
+
* Create a signer verifier.
|
|
19
|
+
* @param proofType The type of proof to create.
|
|
20
|
+
* @returns The created signer verifier.
|
|
21
|
+
* @throws GeneralError if the proof type is not supported.
|
|
22
|
+
*/
|
|
23
|
+
static createSignerVerifier(proofType) {
|
|
24
|
+
Guards.arrayOneOf(ProofHelper.CLASS_NAME, "proofType", proofType, Object.values(ProofTypes));
|
|
25
|
+
let signerVerifier;
|
|
26
|
+
if (proofType === ProofTypes.DataIntegrityProof) {
|
|
27
|
+
signerVerifier = new DataIntegrityProofSignerVerifier();
|
|
28
|
+
}
|
|
29
|
+
else if (proofType === ProofTypes.JsonWebSignature2020) {
|
|
30
|
+
signerVerifier = new JsonWebSignature2020SignerVerifier();
|
|
31
|
+
}
|
|
32
|
+
if (Is.empty(signerVerifier)) {
|
|
33
|
+
throw new GeneralError(ProofHelper.CLASS_NAME, "unsupportedProofType", { proofType });
|
|
34
|
+
}
|
|
35
|
+
return signerVerifier;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a proof for the given data.
|
|
39
|
+
* @param proofType The type of proof to create.
|
|
40
|
+
* @param unsecuredDocument The data to create the proof for.
|
|
41
|
+
* @param unsignedProof The proof options.
|
|
42
|
+
* @param signKey The key to sign the proof with.
|
|
43
|
+
* @returns The created proof.
|
|
44
|
+
*/
|
|
45
|
+
static async createProof(proofType, unsecuredDocument, unsignedProof, signKey) {
|
|
46
|
+
Guards.arrayOneOf(ProofHelper.CLASS_NAME, "proofType", proofType, Object.values(ProofTypes));
|
|
47
|
+
Guards.object(ProofHelper.CLASS_NAME, "unsecuredDocument", unsecuredDocument);
|
|
48
|
+
Guards.object(ProofHelper.CLASS_NAME, "unsignedProof", unsignedProof);
|
|
49
|
+
Guards.object(ProofHelper.CLASS_NAME, "signKey", signKey);
|
|
50
|
+
return ProofHelper.createSignerVerifier(proofType).createProof(unsecuredDocument, unsignedProof, signKey);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Verify a proof for the given data.
|
|
54
|
+
* @param securedDocument The credential to verify.
|
|
55
|
+
* @param signedProof The proof to verify.
|
|
56
|
+
* @param verifyKey The public key to verify the proof with.
|
|
57
|
+
* @returns True if the credential was verified.
|
|
58
|
+
*/
|
|
59
|
+
static async verifyProof(securedDocument, signedProof, verifyKey) {
|
|
60
|
+
Guards.object(ProofHelper.CLASS_NAME, "securedDocument", securedDocument);
|
|
61
|
+
Guards.object(ProofHelper.CLASS_NAME, "signedProof", signedProof);
|
|
62
|
+
Guards.stringValue(ProofHelper.CLASS_NAME, "signedProof.type", signedProof.type);
|
|
63
|
+
Guards.object(ProofHelper.CLASS_NAME, "verifyKey", verifyKey);
|
|
64
|
+
const signerVerifier = ProofHelper.createSignerVerifier(signedProof.type);
|
|
65
|
+
return signerVerifier.verifyProof(securedDocument, signedProof, verifyKey);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create an unsigned proof.
|
|
69
|
+
* @param proofType The type of proof to create.
|
|
70
|
+
* @param verificationMethodId The verification method id.
|
|
71
|
+
* @param otherParams Other parameters for the proof.
|
|
72
|
+
* @returns The created proof.
|
|
73
|
+
* @throws GeneralError if the proof type is not supported.
|
|
74
|
+
*/
|
|
75
|
+
static createUnsignedProof(proofType, verificationMethodId,
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
+
otherParams) {
|
|
78
|
+
let proof;
|
|
79
|
+
if (proofType === ProofTypes.DataIntegrityProof) {
|
|
80
|
+
proof = {
|
|
81
|
+
"@context": DidContexts.ContextDataIntegrity,
|
|
82
|
+
type: ProofTypes.DataIntegrityProof,
|
|
83
|
+
cryptosuite: DidCryptoSuites.EdDSAJcs2022,
|
|
84
|
+
created: new Date(Date.now()).toISOString(),
|
|
85
|
+
verificationMethod: verificationMethodId,
|
|
86
|
+
proofPurpose: "assertionMethod",
|
|
87
|
+
...otherParams
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
else if (proofType === ProofTypes.JsonWebSignature2020) {
|
|
91
|
+
proof = {
|
|
92
|
+
"@context": DidContexts.ContextSecurityJws2020,
|
|
93
|
+
type: ProofTypes.JsonWebSignature2020,
|
|
94
|
+
created: new Date(Date.now()).toISOString(),
|
|
95
|
+
verificationMethod: verificationMethodId,
|
|
96
|
+
proofPurpose: "assertionMethod",
|
|
97
|
+
...otherParams
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (Is.empty(proof)) {
|
|
101
|
+
throw new GeneralError(ProofHelper.CLASS_NAME, "unsupportedProofType", { proofType });
|
|
102
|
+
}
|
|
103
|
+
return proof;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=proofHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proofHelper.js","sourceRoot":"","sources":["../../../src/utils/proofHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAI1D,OAAO,EAAE,WAAW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AG,EAAE,CAAC,CAAC,WAAW,EAAE;gBAC3C,kBAAkB,EAAE,oBAAoB;gBACxC,YAAY,EAAE,iBAAiB;gBAC/B,GAAG,WAAW;aACd,CAAC;QACH,CAAC;aAAM,IAAI,SAAS,KAAK,UAAU,CAAC,oBAAoB,EAAE,CAAC;YAC1D,KAAK,GAAG;gBACP,UAAU,EAAE,WAAW,CAAC,sBAAsB;gBAC9C,IAAI,EAAE,UAAU,CAAC,oBAAoB;gBACrC,OAAO,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,WAAW,EAAE;gBAC3C,kBAAkB,EAAE,oBAAoB;gBACxC,YAAY,EAAE,iBAAiB;gBAC/B,GAAG,WAAW;aACd,CAAC;QACH,CAAC;QACD,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACrB,MAAM,IAAI,YAAY,CAAC,WAAW,CAAC,UAAU,EAAE,sBAAsB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;QACvF,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is } from \"@twin.org/core\";\nimport type { IJsonLdNodeObject } from \"@twin.org/data-json-ld\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IJwk } from \"@twin.org/web\";\nimport { DidContexts } from \"../models/didContexts.js\";\nimport { DidCryptoSuites } from \"../models/didCryptoSuites.js\";\nimport type { IProof } from \"../models/IProof.js\";\nimport type { IProofSignerVerifier } from \"../models/IProofSignerVerifier.js\";\nimport { ProofTypes } from \"../models/proofTypes.js\";\nimport { DataIntegrityProofSignerVerifier } from \"../signerVerifiers/dataIntegrityProofSignerVerifier.js\";\nimport { JsonWebSignature2020SignerVerifier } from \"../signerVerifiers/jsonWebSignature2020SignerVerifier.js\";\n\n/**\n * Helper methods for creating and verifying proofs.\n */\nexport class ProofHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ProofHelper>();\n\n\t/**\n\t * Create a signer verifier.\n\t * @param proofType The type of proof to create.\n\t * @returns The created signer verifier.\n\t * @throws GeneralError if the proof type is not supported.\n\t */\n\tpublic static createSignerVerifier(proofType: ProofTypes): IProofSignerVerifier {\n\t\tGuards.arrayOneOf(\n\t\t\tProofHelper.CLASS_NAME,\n\t\t\tnameof(proofType),\n\t\t\tproofType,\n\t\t\tObject.values(ProofTypes)\n\t\t);\n\n\t\tlet signerVerifier: IProofSignerVerifier | undefined;\n\t\tif (proofType === ProofTypes.DataIntegrityProof) {\n\t\t\tsignerVerifier = new DataIntegrityProofSignerVerifier();\n\t\t} else if (proofType === ProofTypes.JsonWebSignature2020) {\n\t\t\tsignerVerifier = new JsonWebSignature2020SignerVerifier();\n\t\t}\n\n\t\tif (Is.empty(signerVerifier)) {\n\t\t\tthrow new GeneralError(ProofHelper.CLASS_NAME, \"unsupportedProofType\", { proofType });\n\t\t}\n\t\treturn signerVerifier;\n\t}\n\n\t/**\n\t * Create a proof for the given data.\n\t * @param proofType The type of proof to create.\n\t * @param unsecuredDocument The data to create the proof for.\n\t * @param unsignedProof The proof options.\n\t * @param signKey The key to sign the proof with.\n\t * @returns The created proof.\n\t */\n\tpublic static async createProof(\n\t\tproofType: ProofTypes,\n\t\tunsecuredDocument: IJsonLdNodeObject,\n\t\tunsignedProof: IProof,\n\t\tsignKey: IJwk\n\t): Promise<IProof> {\n\t\tGuards.arrayOneOf(\n\t\t\tProofHelper.CLASS_NAME,\n\t\t\tnameof(proofType),\n\t\t\tproofType,\n\t\t\tObject.values(ProofTypes)\n\t\t);\n\t\tGuards.object<IJsonLdNodeObject>(\n\t\t\tProofHelper.CLASS_NAME,\n\t\t\tnameof(unsecuredDocument),\n\t\t\tunsecuredDocument\n\t\t);\n\t\tGuards.object<IJsonLdNodeObject>(ProofHelper.CLASS_NAME, nameof(unsignedProof), unsignedProof);\n\t\tGuards.object<IJwk>(ProofHelper.CLASS_NAME, nameof(signKey), signKey);\n\t\treturn ProofHelper.createSignerVerifier(proofType).createProof(\n\t\t\tunsecuredDocument as unknown as IJsonLdNodeObject,\n\t\t\tunsignedProof,\n\t\t\tsignKey\n\t\t);\n\t}\n\n\t/**\n\t * Verify a proof for the given data.\n\t * @param securedDocument The credential to verify.\n\t * @param signedProof The proof to verify.\n\t * @param verifyKey The public key to verify the proof with.\n\t * @returns True if the credential was verified.\n\t */\n\tpublic static async verifyProof(\n\t\tsecuredDocument: IJsonLdNodeObject,\n\t\tsignedProof: IProof,\n\t\tverifyKey: IJwk\n\t): Promise<boolean> {\n\t\tGuards.object<IJsonLdNodeObject>(\n\t\t\tProofHelper.CLASS_NAME,\n\t\t\tnameof(securedDocument),\n\t\t\tsecuredDocument\n\t\t);\n\t\tGuards.object<IJsonLdNodeObject>(ProofHelper.CLASS_NAME, nameof(signedProof), signedProof);\n\t\tGuards.stringValue(ProofHelper.CLASS_NAME, nameof(signedProof.type), signedProof.type);\n\t\tGuards.object<IJwk>(ProofHelper.CLASS_NAME, nameof(verifyKey), verifyKey);\n\n\t\tconst signerVerifier = ProofHelper.createSignerVerifier(signedProof.type as ProofTypes);\n\n\t\treturn signerVerifier.verifyProof(securedDocument, signedProof, verifyKey);\n\t}\n\n\t/**\n\t * Create an unsigned proof.\n\t * @param proofType The type of proof to create.\n\t * @param verificationMethodId The verification method id.\n\t * @param otherParams Other parameters for the proof.\n\t * @returns The created proof.\n\t * @throws GeneralError if the proof type is not supported.\n\t */\n\tpublic static createUnsignedProof(\n\t\tproofType: ProofTypes,\n\t\tverificationMethodId: string,\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\totherParams?: any\n\t): IProof {\n\t\tlet proof: IProof | undefined;\n\t\tif (proofType === ProofTypes.DataIntegrityProof) {\n\t\t\tproof = {\n\t\t\t\t\"@context\": DidContexts.ContextDataIntegrity,\n\t\t\t\ttype: ProofTypes.DataIntegrityProof,\n\t\t\t\tcryptosuite: DidCryptoSuites.EdDSAJcs2022,\n\t\t\t\tcreated: new Date(Date.now()).toISOString(),\n\t\t\t\tverificationMethod: verificationMethodId,\n\t\t\t\tproofPurpose: \"assertionMethod\",\n\t\t\t\t...otherParams\n\t\t\t};\n\t\t} else if (proofType === ProofTypes.JsonWebSignature2020) {\n\t\t\tproof = {\n\t\t\t\t\"@context\": DidContexts.ContextSecurityJws2020,\n\t\t\t\ttype: ProofTypes.JsonWebSignature2020,\n\t\t\t\tcreated: new Date(Date.now()).toISOString(),\n\t\t\t\tverificationMethod: verificationMethodId,\n\t\t\t\tproofPurpose: \"assertionMethod\",\n\t\t\t\t...otherParams\n\t\t\t};\n\t\t}\n\t\tif (Is.empty(proof)) {\n\t\t\tthrow new GeneralError(ProofHelper.CLASS_NAME, \"unsupportedProofType\", { proofType });\n\t\t}\n\t\treturn proof;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { ObjectHelper } from "@twin.org/core";
|
|
4
|
+
/**
|
|
5
|
+
* Helper methods for creating and verifying proofs.
|
|
6
|
+
*/
|
|
7
|
+
export class VerifiableCredentialHelper {
|
|
8
|
+
/**
|
|
9
|
+
* Runtime name for the class.
|
|
10
|
+
*/
|
|
11
|
+
static CLASS_NAME = "VerifiableCredentialHelper";
|
|
12
|
+
/**
|
|
13
|
+
* Get the valid until date from a verifiable credential.
|
|
14
|
+
* @param verifiableCredential The verifiable credential to extract the expiration date from.
|
|
15
|
+
* @returns The expiration date, if available.
|
|
16
|
+
*/
|
|
17
|
+
static getValidUntil(verifiableCredential) {
|
|
18
|
+
return (ObjectHelper.propertyGet(verifiableCredential, "validUntil") ??
|
|
19
|
+
ObjectHelper.propertyGet(verifiableCredential, "expirationDate"));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the valid from from a verifiable credential.
|
|
23
|
+
* @param verifiableCredential The verifiable credential to extract the issuance date from.
|
|
24
|
+
* @returns The issuance date, if available.
|
|
25
|
+
*/
|
|
26
|
+
static getValidFrom(verifiableCredential) {
|
|
27
|
+
return (ObjectHelper.propertyGet(verifiableCredential, "validFrom") ??
|
|
28
|
+
ObjectHelper.propertyGet(verifiableCredential, "issuanceDate"));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=verifiableCredentialHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verifiableCredentialHelper.js","sourceRoot":"","sources":["../../../src/utils/verifiableCredentialHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAI9C;;GAEG;AACH,MAAM,OAAO,0BAA0B;IACtC;;OAEG;IACI,MAAM,CAAU,UAAU,gCAAgD;IAEjF;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,oBAA8C;QACzE,OAAO,CACN,YAAY,CAAC,WAAW,CAAC,oBAAoB,EAAE,YAAY,CAAC;YAC5D,YAAY,CAAC,WAAW,CAAC,oBAAoB,EAAE,gBAAgB,CAAC,CAChE,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,oBAA8C;QACxE,OAAO,CACN,YAAY,CAAC,WAAW,CAAC,oBAAoB,EAAE,WAAW,CAAC;YAC3D,YAAY,CAAC,WAAW,CAAC,oBAAoB,EAAE,cAAc,CAAC,CAC9D,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ObjectHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IDidVerifiableCredential } from \"../models/IDidVerifiableCredential.js\";\n\n/**\n * Helper methods for creating and verifying proofs.\n */\nexport class VerifiableCredentialHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<VerifiableCredentialHelper>();\n\n\t/**\n\t * Get the valid until date from a verifiable credential.\n\t * @param verifiableCredential The verifiable credential to extract the expiration date from.\n\t * @returns The expiration date, if available.\n\t */\n\tpublic static getValidUntil(verifiableCredential: IDidVerifiableCredential): string | undefined {\n\t\treturn (\n\t\t\tObjectHelper.propertyGet(verifiableCredential, \"validUntil\") ??\n\t\t\tObjectHelper.propertyGet(verifiableCredential, \"expirationDate\")\n\t\t);\n\t}\n\n\t/**\n\t * Get the valid from from a verifiable credential.\n\t * @param verifiableCredential The verifiable credential to extract the issuance date from.\n\t * @returns The issuance date, if available.\n\t */\n\tpublic static getValidFrom(verifiableCredential: IDidVerifiableCredential): string | undefined {\n\t\treturn (\n\t\t\tObjectHelper.propertyGet(verifiableCredential, \"validFrom\") ??\n\t\t\tObjectHelper.propertyGet(verifiableCredential, \"issuanceDate\")\n\t\t);\n\t}\n}\n"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
export * from "./models/didContexts";
|
|
2
|
-
export * from "./models/didCryptoSuites";
|
|
3
|
-
export * from "./models/didTypes";
|
|
4
|
-
export * from "./models/didVerificationMethodType";
|
|
5
|
-
export * from "./models/IDataIntegrityProof";
|
|
6
|
-
export * from "./models/IDidCredentialSchema";
|
|
7
|
-
export * from "./models/IDidCredentialStatus";
|
|
8
|
-
export * from "./models/IDidDocument";
|
|
9
|
-
export * from "./models/IDidDocumentVerificationMethod";
|
|
10
|
-
export * from "./models/IDidLabel";
|
|
11
|
-
export * from "./models/IDidPresentationVerification";
|
|
12
|
-
export * from "./models/IDidService";
|
|
13
|
-
export * from "./models/IDidVerifiableCredential";
|
|
14
|
-
export * from "./models/IDidVerifiableCredentialCommon";
|
|
15
|
-
export * from "./models/IDidVerifiableCredentialV1";
|
|
16
|
-
export * from "./models/IDidVerifiableCredentialV2";
|
|
17
|
-
export * from "./models/IDidVerifiablePresentation";
|
|
18
|
-
export * from "./models/IDidVerifiablePresentationCommon";
|
|
19
|
-
export * from "./models/IDidVerifiablePresentationV1";
|
|
20
|
-
export * from "./models/IDidVerifiablePresentationV2";
|
|
21
|
-
export * from "./models/IJsonWebSignature2020Proof";
|
|
22
|
-
export * from "./models/IMultikey";
|
|
23
|
-
export * from "./models/IProof";
|
|
24
|
-
export * from "./models/IProofSignerVerifier";
|
|
25
|
-
export * from "./models/proofTypes";
|
|
26
|
-
export * from "./signerVerifiers/dataIntegrityProofSignerVerifier";
|
|
27
|
-
export * from "./signerVerifiers/jsonWebSignature2020SignerVerifier";
|
|
28
|
-
export * from "./utils/multikeyHelper";
|
|
29
|
-
export * from "./utils/proofHelper";
|
|
30
|
-
export * from "./utils/verifiableCredentialHelper";
|
|
1
|
+
export * from "./models/didContexts.js";
|
|
2
|
+
export * from "./models/didCryptoSuites.js";
|
|
3
|
+
export * from "./models/didTypes.js";
|
|
4
|
+
export * from "./models/didVerificationMethodType.js";
|
|
5
|
+
export * from "./models/IDataIntegrityProof.js";
|
|
6
|
+
export * from "./models/IDidCredentialSchema.js";
|
|
7
|
+
export * from "./models/IDidCredentialStatus.js";
|
|
8
|
+
export * from "./models/IDidDocument.js";
|
|
9
|
+
export * from "./models/IDidDocumentVerificationMethod.js";
|
|
10
|
+
export * from "./models/IDidLabel.js";
|
|
11
|
+
export * from "./models/IDidPresentationVerification.js";
|
|
12
|
+
export * from "./models/IDidService.js";
|
|
13
|
+
export * from "./models/IDidVerifiableCredential.js";
|
|
14
|
+
export * from "./models/IDidVerifiableCredentialCommon.js";
|
|
15
|
+
export * from "./models/IDidVerifiableCredentialV1.js";
|
|
16
|
+
export * from "./models/IDidVerifiableCredentialV2.js";
|
|
17
|
+
export * from "./models/IDidVerifiablePresentation.js";
|
|
18
|
+
export * from "./models/IDidVerifiablePresentationCommon.js";
|
|
19
|
+
export * from "./models/IDidVerifiablePresentationV1.js";
|
|
20
|
+
export * from "./models/IDidVerifiablePresentationV2.js";
|
|
21
|
+
export * from "./models/IJsonWebSignature2020Proof.js";
|
|
22
|
+
export * from "./models/IMultikey.js";
|
|
23
|
+
export * from "./models/IProof.js";
|
|
24
|
+
export * from "./models/IProofSignerVerifier.js";
|
|
25
|
+
export * from "./models/proofTypes.js";
|
|
26
|
+
export * from "./signerVerifiers/dataIntegrityProofSignerVerifier.js";
|
|
27
|
+
export * from "./signerVerifiers/jsonWebSignature2020SignerVerifier.js";
|
|
28
|
+
export * from "./utils/multikeyHelper.js";
|
|
29
|
+
export * from "./utils/proofHelper.js";
|
|
30
|
+
export * from "./utils/verifiableCredentialHelper.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { DidCryptoSuites } from "./didCryptoSuites";
|
|
4
|
-
import type { ProofTypes } from "./proofTypes";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { DidCryptoSuites } from "./didCryptoSuites.js";
|
|
4
|
+
import type { ProofTypes } from "./proofTypes.js";
|
|
5
5
|
/**
|
|
6
6
|
* Interface describing a did proof.
|
|
7
7
|
* https://www.w3.org/TR/vc-data-integrity/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { IDidDocumentVerificationMethod } from "./IDidDocumentVerificationMethod";
|
|
4
|
-
import type { IDidService } from "./IDidService";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { IDidDocumentVerificationMethod } from "./IDidDocumentVerificationMethod.js";
|
|
4
|
+
import type { IDidService } from "./IDidService.js";
|
|
5
5
|
/**
|
|
6
6
|
* Interface describing a DID Document.
|
|
7
7
|
* Spec https://www.w3.org/TR/did-core/#did-document-properties.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { IDidVerifiableCredentialV1 } from "./IDidVerifiableCredentialV1";
|
|
2
|
-
import type { IDidVerifiableCredentialV2 } from "./IDidVerifiableCredentialV2";
|
|
1
|
+
import type { IDidVerifiableCredentialV1 } from "./IDidVerifiableCredentialV1.js";
|
|
2
|
+
import type { IDidVerifiableCredentialV2 } from "./IDidVerifiableCredentialV2.js";
|
|
3
3
|
/**
|
|
4
4
|
* Interface describing a verifiable credential.
|
|
5
5
|
*/
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { IJsonLdNodeObject } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { IDidCredentialSchema } from "./IDidCredentialSchema";
|
|
3
|
-
import type { IDidCredentialStatus } from "./IDidCredentialStatus";
|
|
4
|
-
import type { IDidLabel } from "./IDidLabel";
|
|
5
|
-
import type { IProof } from "./IProof";
|
|
2
|
+
import type { IDidCredentialSchema } from "./IDidCredentialSchema.js";
|
|
3
|
+
import type { IDidCredentialStatus } from "./IDidCredentialStatus.js";
|
|
4
|
+
import type { IDidLabel } from "./IDidLabel.js";
|
|
5
|
+
import type { IProof } from "./IProof.js";
|
|
6
6
|
/**
|
|
7
7
|
* Interface describing a verifiable credential.
|
|
8
8
|
* https://www.w3.org/TR/vc-data-model-2.0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { IDidVerifiableCredentialCommon } from "./IDidVerifiableCredentialCommon";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { IDidVerifiableCredentialCommon } from "./IDidVerifiableCredentialCommon.js";
|
|
4
4
|
/**
|
|
5
5
|
* Interface describing a verifiable credential.
|
|
6
6
|
* https://www.w3.org/TR/vc-data-model-1.1
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { IDidVerifiableCredentialCommon } from "./IDidVerifiableCredentialCommon";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { IDidVerifiableCredentialCommon } from "./IDidVerifiableCredentialCommon.js";
|
|
4
4
|
/**
|
|
5
5
|
* Interface describing a verifiable credential.
|
|
6
6
|
* https://www.w3.org/TR/vc-data-model-2.0
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { IDidVerifiablePresentationV1 } from "./IDidVerifiablePresentationV1";
|
|
2
|
-
import type { IDidVerifiablePresentationV2 } from "./IDidVerifiablePresentationV2";
|
|
1
|
+
import type { IDidVerifiablePresentationV1 } from "./IDidVerifiablePresentationV1.js";
|
|
2
|
+
import type { IDidVerifiablePresentationV2 } from "./IDidVerifiablePresentationV2.js";
|
|
3
3
|
/**
|
|
4
4
|
* Interface describing a verifiable presentation.
|
|
5
5
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { IDidVerifiableCredentialV1 } from "./IDidVerifiableCredentialV1";
|
|
4
|
-
import type { IDidVerifiablePresentationCommon } from "./IDidVerifiablePresentationCommon";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { IDidVerifiableCredentialV1 } from "./IDidVerifiableCredentialV1.js";
|
|
4
|
+
import type { IDidVerifiablePresentationCommon } from "./IDidVerifiablePresentationCommon.js";
|
|
5
5
|
/**
|
|
6
6
|
* Interface describing a verifiable presentation.
|
|
7
7
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { IDidVerifiableCredentialV2 } from "./IDidVerifiableCredentialV2";
|
|
4
|
-
import type { IDidVerifiablePresentationCommon } from "./IDidVerifiablePresentationCommon";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { IDidVerifiableCredentialV2 } from "./IDidVerifiableCredentialV2.js";
|
|
4
|
+
import type { IDidVerifiablePresentationCommon } from "./IDidVerifiablePresentationCommon.js";
|
|
5
5
|
/**
|
|
6
6
|
* Interface describing a verifiable presentation.
|
|
7
7
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { ProofTypes } from "./proofTypes";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { ProofTypes } from "./proofTypes.js";
|
|
4
4
|
/**
|
|
5
5
|
* Interface describing a did proof in JSON Web Signature 2020 Format.
|
|
6
6
|
* https://www.w3.org/TR/vc-jws-2020/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IJsonLdContextDefinitionElement } from "@twin.org/data-json-ld";
|
|
2
|
-
import type { DidContexts } from "./didContexts";
|
|
3
|
-
import type { DidTypes } from "./didTypes";
|
|
2
|
+
import type { DidContexts } from "./didContexts.js";
|
|
3
|
+
import type { DidTypes } from "./didTypes.js";
|
|
4
4
|
/**
|
|
5
5
|
* Interface describing a Multikey.
|
|
6
6
|
* https://www.w3.org/TR/cid-1.0/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { IDataIntegrityProof } from "./IDataIntegrityProof";
|
|
2
|
-
import type { IJsonWebSignature2020Proof } from "./IJsonWebSignature2020Proof";
|
|
1
|
+
import type { IDataIntegrityProof } from "./IDataIntegrityProof.js";
|
|
2
|
+
import type { IJsonWebSignature2020Proof } from "./IJsonWebSignature2020Proof.js";
|
|
3
3
|
/**
|
|
4
4
|
* Interface describing a proof.
|
|
5
5
|
*/
|