@twin.org/identity-models 0.0.1-next.28 → 0.0.1-next.29

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.
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@twin.org/core');
4
+ var standardsW3cDid = require('@twin.org/standards-w3c-did');
4
5
  var web = require('@twin.org/web');
5
6
 
6
7
  // Copyright 2024 IOTA Stiftung.
@@ -33,6 +34,10 @@ const IdentityResolverConnectorFactory = core.Factory.createFactory("identity-re
33
34
  * Helper methods for documents.
34
35
  */
35
36
  class DocumentHelper {
37
+ /**
38
+ * Runtime name for the class.
39
+ */
40
+ static CLASS_NAME = "DocumentHelper";
36
41
  /**
37
42
  * Parse the document id into its parts.
38
43
  * @param documentId The full document id.
@@ -75,43 +80,116 @@ class DocumentHelper {
75
80
  }
76
81
  return fullId;
77
82
  }
83
+ /**
84
+ * Get a verification method from a DID document.
85
+ * @param didDocument The DID Document to get the method from.
86
+ * @param methodName The name of the method to get the JWK from.
87
+ * @param methodType The type of the method, defaults to verificationMethod.
88
+ * @returns The verification method if found.
89
+ * @throws Error if the method is not found.
90
+ */
91
+ static getVerificationMethod(didDocument, methodName, methodType) {
92
+ const verificationMethod = didDocument[methodType ?? standardsW3cDid.DidVerificationMethodType.VerificationMethod]?.find(vm => core.Is.object(vm) && vm.id === methodName);
93
+ if (core.Is.object(verificationMethod)) {
94
+ return verificationMethod;
95
+ }
96
+ throw new core.GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodNotFound", {
97
+ methodName,
98
+ methodType
99
+ });
100
+ }
101
+ /**
102
+ * Gets a JWK from a DID document verification method.
103
+ * @param didDocument The DID Document to get the method from.
104
+ * @param methodName The name of the method to get the JWK from.
105
+ * @param methodType The type of the method, defaults to verificationMethod.
106
+ * @returns The JWK if found.
107
+ * @throws Error if the method is not found.
108
+ */
109
+ static getJwk(didDocument, methodName, methodType) {
110
+ const verificationMethod = DocumentHelper.getVerificationMethod(didDocument, methodName, methodType);
111
+ if (core.Is.object(verificationMethod) && core.Is.object(verificationMethod.publicKeyJwk)) {
112
+ return verificationMethod.publicKeyJwk;
113
+ }
114
+ throw new core.GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodJwkNotFound", {
115
+ methodName,
116
+ methodType
117
+ });
118
+ }
78
119
  }
79
120
 
80
121
  // Copyright 2024 IOTA Stiftung.
81
122
  // SPDX-License-Identifier: Apache-2.0.
82
123
  /**
83
- * Helper methods for JSON Web Tokens.
124
+ * Helper methods for verification.
84
125
  */
85
- class JwtHelper {
126
+ class VerificationHelper {
86
127
  /**
87
128
  * Runtime name for the class.
88
129
  */
89
- static CLASS_NAME = "JwtHelper";
130
+ static CLASS_NAME = "VerificationHelper";
90
131
  /**
91
- * Parse the token and check that the properties are valid.
92
- * @param jwt The token top validate.
93
- * @param paramsToCheck Parameters to check they exist.
94
- * @returns The token components.
95
- * @throws Error if the token is invalid.
132
+ * Verified the JWT.
133
+ * @param resolver The resolver to use for finding the document.
134
+ * @param jwt The token to verify.
135
+ * @returns The decoded payload.
96
136
  */
97
- static async parse(jwt, paramsToCheck) {
137
+ async verifyJwt(resolver, jwt) {
138
+ core.Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
139
+ core.Guards.string(VerificationHelper.CLASS_NAME, "jwt", jwt);
98
140
  const jwtDecoded = await web.Jwt.decode(jwt);
99
141
  const jwtHeader = jwtDecoded.header;
100
142
  const jwtPayload = jwtDecoded.payload;
101
143
  const jwtSignature = jwtDecoded.signature;
102
- if (core.Is.undefined(jwtHeader) || core.Is.undefined(jwtPayload) || core.Is.undefined(jwtSignature)) {
103
- throw new core.GeneralError(JwtHelper.CLASS_NAME, "jwtDecodeFailed");
144
+ if (!core.Is.object(jwtHeader) || !core.Is.object(jwtPayload) || !core.Is.uint8Array(jwtSignature)) {
145
+ throw new core.GeneralError(VerificationHelper.CLASS_NAME, "jwtDecodeFailed");
104
146
  }
105
- if (core.Is.arrayValue(paramsToCheck)) {
106
- for (const param of paramsToCheck) {
107
- if (!core.Is.stringValue(jwtPayload[param])) {
108
- throw new core.GeneralError(JwtHelper.CLASS_NAME, "jwtPayloadMissingParam", {
109
- param
110
- });
111
- }
147
+ const iss = jwtPayload?.iss;
148
+ const kid = jwtPayload?.kid;
149
+ core.Guards.stringValue(VerificationHelper.CLASS_NAME, "iss", iss);
150
+ core.Guards.stringValue(VerificationHelper.CLASS_NAME, "kid", kid);
151
+ const didDocument = await resolver.resolveDocument(iss);
152
+ const jwk = DocumentHelper.getJwk(didDocument, kid);
153
+ const publicKey = await web.Jwk.toCryptoKey(jwk);
154
+ return web.Jwt.verify(jwt, publicKey);
155
+ }
156
+ /**
157
+ * Verified the proof for the document e.g. verifiable credential.
158
+ * @param resolver The resolver to use for finding the document.
159
+ * @param secureDocument The secure document to verify.
160
+ * @returns True if the verification is successful.
161
+ */
162
+ async verifyProof(resolver, secureDocument) {
163
+ core.Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
164
+ core.Guards.object(VerificationHelper.CLASS_NAME, "secureDocument", secureDocument);
165
+ core.Guards.object(VerificationHelper.CLASS_NAME, "secureDocument.proof", secureDocument.proof);
166
+ const proofList = core.Is.array(secureDocument.proof)
167
+ ? secureDocument.proof
168
+ : [secureDocument.proof];
169
+ const documentCache = {};
170
+ for (const proof of proofList) {
171
+ if (!core.Is.stringValue(proof?.verificationMethod)) {
172
+ throw new core.GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
173
+ }
174
+ const proofVerificationMethod = DocumentHelper.parseId(proof.verificationMethod);
175
+ if (!core.Is.stringValue(proofVerificationMethod.fragment)) {
176
+ throw new core.GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
177
+ }
178
+ let document;
179
+ if (documentCache[proofVerificationMethod.id]) {
180
+ document = documentCache[proofVerificationMethod.id];
181
+ }
182
+ else {
183
+ document = await resolver.resolveDocument(proofVerificationMethod.id);
184
+ documentCache[proofVerificationMethod.id] = document;
185
+ }
186
+ const verificationJwk = await DocumentHelper.getJwk(document, proofVerificationMethod.id);
187
+ const verified = standardsW3cDid.ProofHelper.verifyProof(secureDocument, proof, verificationJwk);
188
+ if (!verified) {
189
+ return false;
112
190
  }
113
191
  }
114
- return jwtDecoded;
192
+ return true;
115
193
  }
116
194
  }
117
195
 
@@ -119,4 +197,4 @@ exports.DocumentHelper = DocumentHelper;
119
197
  exports.IdentityConnectorFactory = IdentityConnectorFactory;
120
198
  exports.IdentityProfileConnectorFactory = IdentityProfileConnectorFactory;
121
199
  exports.IdentityResolverConnectorFactory = IdentityResolverConnectorFactory;
122
- exports.JwtHelper = JwtHelper;
200
+ exports.VerificationHelper = VerificationHelper;
@@ -1,5 +1,6 @@
1
- import { Factory, Is, GeneralError } from '@twin.org/core';
2
- import { Jwt } from '@twin.org/web';
1
+ import { Factory, Is, GeneralError, Guards } from '@twin.org/core';
2
+ import { DidVerificationMethodType, ProofHelper } from '@twin.org/standards-w3c-did';
3
+ import { Jwt, Jwk } from '@twin.org/web';
3
4
 
4
5
  // Copyright 2024 IOTA Stiftung.
5
6
  // SPDX-License-Identifier: Apache-2.0.
@@ -31,6 +32,10 @@ const IdentityResolverConnectorFactory = Factory.createFactory("identity-resolve
31
32
  * Helper methods for documents.
32
33
  */
33
34
  class DocumentHelper {
35
+ /**
36
+ * Runtime name for the class.
37
+ */
38
+ static CLASS_NAME = "DocumentHelper";
34
39
  /**
35
40
  * Parse the document id into its parts.
36
41
  * @param documentId The full document id.
@@ -73,44 +78,117 @@ class DocumentHelper {
73
78
  }
74
79
  return fullId;
75
80
  }
81
+ /**
82
+ * Get a verification method from a DID document.
83
+ * @param didDocument The DID Document to get the method from.
84
+ * @param methodName The name of the method to get the JWK from.
85
+ * @param methodType The type of the method, defaults to verificationMethod.
86
+ * @returns The verification method if found.
87
+ * @throws Error if the method is not found.
88
+ */
89
+ static getVerificationMethod(didDocument, methodName, methodType) {
90
+ const verificationMethod = didDocument[methodType ?? DidVerificationMethodType.VerificationMethod]?.find(vm => Is.object(vm) && vm.id === methodName);
91
+ if (Is.object(verificationMethod)) {
92
+ return verificationMethod;
93
+ }
94
+ throw new GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodNotFound", {
95
+ methodName,
96
+ methodType
97
+ });
98
+ }
99
+ /**
100
+ * Gets a JWK from a DID document verification method.
101
+ * @param didDocument The DID Document to get the method from.
102
+ * @param methodName The name of the method to get the JWK from.
103
+ * @param methodType The type of the method, defaults to verificationMethod.
104
+ * @returns The JWK if found.
105
+ * @throws Error if the method is not found.
106
+ */
107
+ static getJwk(didDocument, methodName, methodType) {
108
+ const verificationMethod = DocumentHelper.getVerificationMethod(didDocument, methodName, methodType);
109
+ if (Is.object(verificationMethod) && Is.object(verificationMethod.publicKeyJwk)) {
110
+ return verificationMethod.publicKeyJwk;
111
+ }
112
+ throw new GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodJwkNotFound", {
113
+ methodName,
114
+ methodType
115
+ });
116
+ }
76
117
  }
77
118
 
78
119
  // Copyright 2024 IOTA Stiftung.
79
120
  // SPDX-License-Identifier: Apache-2.0.
80
121
  /**
81
- * Helper methods for JSON Web Tokens.
122
+ * Helper methods for verification.
82
123
  */
83
- class JwtHelper {
124
+ class VerificationHelper {
84
125
  /**
85
126
  * Runtime name for the class.
86
127
  */
87
- static CLASS_NAME = "JwtHelper";
128
+ static CLASS_NAME = "VerificationHelper";
88
129
  /**
89
- * Parse the token and check that the properties are valid.
90
- * @param jwt The token top validate.
91
- * @param paramsToCheck Parameters to check they exist.
92
- * @returns The token components.
93
- * @throws Error if the token is invalid.
130
+ * Verified the JWT.
131
+ * @param resolver The resolver to use for finding the document.
132
+ * @param jwt The token to verify.
133
+ * @returns The decoded payload.
94
134
  */
95
- static async parse(jwt, paramsToCheck) {
135
+ async verifyJwt(resolver, jwt) {
136
+ Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
137
+ Guards.string(VerificationHelper.CLASS_NAME, "jwt", jwt);
96
138
  const jwtDecoded = await Jwt.decode(jwt);
97
139
  const jwtHeader = jwtDecoded.header;
98
140
  const jwtPayload = jwtDecoded.payload;
99
141
  const jwtSignature = jwtDecoded.signature;
100
- if (Is.undefined(jwtHeader) || Is.undefined(jwtPayload) || Is.undefined(jwtSignature)) {
101
- throw new GeneralError(JwtHelper.CLASS_NAME, "jwtDecodeFailed");
142
+ if (!Is.object(jwtHeader) || !Is.object(jwtPayload) || !Is.uint8Array(jwtSignature)) {
143
+ throw new GeneralError(VerificationHelper.CLASS_NAME, "jwtDecodeFailed");
102
144
  }
103
- if (Is.arrayValue(paramsToCheck)) {
104
- for (const param of paramsToCheck) {
105
- if (!Is.stringValue(jwtPayload[param])) {
106
- throw new GeneralError(JwtHelper.CLASS_NAME, "jwtPayloadMissingParam", {
107
- param
108
- });
109
- }
145
+ const iss = jwtPayload?.iss;
146
+ const kid = jwtPayload?.kid;
147
+ Guards.stringValue(VerificationHelper.CLASS_NAME, "iss", iss);
148
+ Guards.stringValue(VerificationHelper.CLASS_NAME, "kid", kid);
149
+ const didDocument = await resolver.resolveDocument(iss);
150
+ const jwk = DocumentHelper.getJwk(didDocument, kid);
151
+ const publicKey = await Jwk.toCryptoKey(jwk);
152
+ return Jwt.verify(jwt, publicKey);
153
+ }
154
+ /**
155
+ * Verified the proof for the document e.g. verifiable credential.
156
+ * @param resolver The resolver to use for finding the document.
157
+ * @param secureDocument The secure document to verify.
158
+ * @returns True if the verification is successful.
159
+ */
160
+ async verifyProof(resolver, secureDocument) {
161
+ Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
162
+ Guards.object(VerificationHelper.CLASS_NAME, "secureDocument", secureDocument);
163
+ Guards.object(VerificationHelper.CLASS_NAME, "secureDocument.proof", secureDocument.proof);
164
+ const proofList = Is.array(secureDocument.proof)
165
+ ? secureDocument.proof
166
+ : [secureDocument.proof];
167
+ const documentCache = {};
168
+ for (const proof of proofList) {
169
+ if (!Is.stringValue(proof?.verificationMethod)) {
170
+ throw new GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
171
+ }
172
+ const proofVerificationMethod = DocumentHelper.parseId(proof.verificationMethod);
173
+ if (!Is.stringValue(proofVerificationMethod.fragment)) {
174
+ throw new GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
175
+ }
176
+ let document;
177
+ if (documentCache[proofVerificationMethod.id]) {
178
+ document = documentCache[proofVerificationMethod.id];
179
+ }
180
+ else {
181
+ document = await resolver.resolveDocument(proofVerificationMethod.id);
182
+ documentCache[proofVerificationMethod.id] = document;
183
+ }
184
+ const verificationJwk = await DocumentHelper.getJwk(document, proofVerificationMethod.id);
185
+ const verified = ProofHelper.verifyProof(secureDocument, proof, verificationJwk);
186
+ if (!verified) {
187
+ return false;
110
188
  }
111
189
  }
112
- return jwtDecoded;
190
+ return true;
113
191
  }
114
192
  }
115
193
 
116
- export { DocumentHelper, IdentityConnectorFactory, IdentityProfileConnectorFactory, IdentityResolverConnectorFactory, JwtHelper };
194
+ export { DocumentHelper, IdentityConnectorFactory, IdentityProfileConnectorFactory, IdentityResolverConnectorFactory, VerificationHelper };
@@ -40,4 +40,4 @@ export * from "./models/IIdentityProfileConnector";
40
40
  export * from "./models/IIdentityResolverComponent";
41
41
  export * from "./models/IIdentityResolverConnector";
42
42
  export * from "./utils/documentHelper";
43
- export * from "./utils/jwtHelper";
43
+ export * from "./utils/verificationHelper";
@@ -1,6 +1,6 @@
1
1
  import type { IComponent } from "@twin.org/core";
2
2
  import type { IJsonLdContextDefinitionRoot, IJsonLdNodeObject } from "@twin.org/data-json-ld";
3
- import type { DidVerificationMethodType, IDidDocument, IDidDocumentVerificationMethod, IDidProof, IDidService, IDidVerifiableCredential, IDidVerifiablePresentation } from "@twin.org/standards-w3c-did";
3
+ import type { DidVerificationMethodType, IDidDocument, IDidDocumentVerificationMethod, IProof, IDidService, IDidVerifiableCredential, IDidVerifiablePresentation, ProofTypes } from "@twin.org/standards-w3c-did";
4
4
  /**
5
5
  * Interface describing a contract which provides identity operations.
6
6
  */
@@ -117,18 +117,19 @@ export interface IIdentityComponent extends IComponent {
117
117
  issuers?: IDidDocument[];
118
118
  }>;
119
119
  /**
120
- * Create a proof for arbitrary data with the specified verification method.
120
+ * Create a proof for a document with the specified verification method.
121
121
  * @param verificationMethodId The verification method id to use.
122
- * @param bytes The data bytes to sign.
122
+ * @param proofType The type of proof to create.
123
+ * @param unsecureDocument The unsecure document to create the proof for.
123
124
  * @param controller The controller of the identity who can make changes.
124
125
  * @returns The proof.
125
126
  */
126
- proofCreate(verificationMethodId: string, bytes: Uint8Array, controller?: string): Promise<IDidProof>;
127
+ proofCreate(verificationMethodId: string, proofType: ProofTypes, unsecureDocument: IJsonLdNodeObject, controller?: string): Promise<IProof>;
127
128
  /**
128
- * Verify proof for arbitrary data with the specified verification method.
129
- * @param bytes The data bytes to verify.
129
+ * Verify proof for a document with the specified verification method.
130
+ * @param document The document to verify.
130
131
  * @param proof The proof to verify.
131
132
  * @returns True if the proof is verified.
132
133
  */
133
- proofVerify(bytes: Uint8Array, proof: IDidProof): Promise<boolean>;
134
+ proofVerify(document: IJsonLdNodeObject, proof: IProof): Promise<boolean>;
134
135
  }
@@ -1,6 +1,6 @@
1
1
  import type { IComponent } from "@twin.org/core";
2
2
  import type { IJsonLdContextDefinitionRoot, IJsonLdNodeObject } from "@twin.org/data-json-ld";
3
- import type { DidVerificationMethodType, IDidDocument, IDidDocumentVerificationMethod, IDidProof, IDidService, IDidVerifiableCredential, IDidVerifiablePresentation } from "@twin.org/standards-w3c-did";
3
+ import type { DidVerificationMethodType, IDidDocument, IDidDocumentVerificationMethod, IProof, IDidService, IDidVerifiableCredential, IDidVerifiablePresentation, ProofTypes } from "@twin.org/standards-w3c-did";
4
4
  /**
5
5
  * Interface describing an identity connector.
6
6
  */
@@ -119,15 +119,16 @@ export interface IIdentityConnector extends IComponent {
119
119
  * Create a proof for arbitrary data with the specified verification method.
120
120
  * @param controller The controller of the identity who can make changes.
121
121
  * @param verificationMethodId The verification method id to use.
122
- * @param bytes The data bytes to sign.
122
+ * @param proofType The type of proof to create.
123
+ * @param unsecureDocument The unsecure document to create the proof for.
123
124
  * @returns The proof.
124
125
  */
125
- createProof(controller: string, verificationMethodId: string, bytes: Uint8Array): Promise<IDidProof>;
126
+ createProof(controller: string, verificationMethodId: string, proofType: ProofTypes, unsecureDocument: IJsonLdNodeObject): Promise<IProof>;
126
127
  /**
127
128
  * Verify proof for arbitrary data with the specified verification method.
128
- * @param bytes The data bytes to verify.
129
+ * @param document The document to verify.
129
130
  * @param proof The proof to verify.
130
131
  * @returns True if the proof is verified.
131
132
  */
132
- verifyProof(bytes: Uint8Array, proof: IDidProof): Promise<boolean>;
133
+ verifyProof(document: IJsonLdNodeObject, proof: IProof): Promise<boolean>;
133
134
  }
@@ -1,3 +1,5 @@
1
+ import type { IJsonLdNodeObject } from "@twin.org/data-json-ld";
2
+ import type { ProofTypes } from "@twin.org/standards-w3c-did";
1
3
  /**
2
4
  * Request to create a proof.
3
5
  */
@@ -20,8 +22,12 @@ export interface IIdentityProofCreateRequest {
20
22
  */
21
23
  body: {
22
24
  /**
23
- * The data bytes base64 encoded.
25
+ * The type of proof to create.
24
26
  */
25
- bytes: string;
27
+ proofType: ProofTypes;
28
+ /**
29
+ * The document to create the proof for.
30
+ */
31
+ document: IJsonLdNodeObject;
26
32
  };
27
33
  }
@@ -1,4 +1,4 @@
1
- import type { IDidProof } from "@twin.org/standards-w3c-did";
1
+ import type { IProof } from "@twin.org/standards-w3c-did";
2
2
  /**
3
3
  * Response to creating a proof.
4
4
  */
@@ -6,5 +6,5 @@ export interface IIdentityProofCreateResponse {
6
6
  /**
7
7
  * The response payload.
8
8
  */
9
- body: IDidProof;
9
+ body: IProof;
10
10
  }
@@ -1,4 +1,5 @@
1
- import type { IDidProof } from "@twin.org/standards-w3c-did";
1
+ import type { IJsonLdNodeObject } from "@twin.org/data-json-ld";
2
+ import type { IProof } from "@twin.org/standards-w3c-did";
2
3
  /**
3
4
  * Request to verify a proof.
4
5
  */
@@ -8,12 +9,12 @@ export interface IIdentityProofVerifyRequest {
8
9
  */
9
10
  body: {
10
11
  /**
11
- * The data bytes base64 encoded.
12
+ * The document to verify the proof for.
12
13
  */
13
- bytes: string;
14
+ document: IJsonLdNodeObject;
14
15
  /**
15
16
  * The proof to verify.
16
17
  */
17
- proof: IDidProof;
18
+ proof: IProof;
18
19
  };
19
20
  }
@@ -1,7 +1,13 @@
1
+ import { DidVerificationMethodType, type IDidDocumentVerificationMethod, type IDidDocument } from "@twin.org/standards-w3c-did";
2
+ import type { IJwk } from "@twin.org/web";
1
3
  /**
2
4
  * Helper methods for documents.
3
5
  */
4
6
  export declare class DocumentHelper {
7
+ /**
8
+ * Runtime name for the class.
9
+ */
10
+ static readonly CLASS_NAME: string;
5
11
  /**
6
12
  * Parse the document id into its parts.
7
13
  * @param documentId The full document id.
@@ -18,4 +24,22 @@ export declare class DocumentHelper {
18
24
  * @returns The full id.
19
25
  */
20
26
  static joinId(documentId: string, fragment?: string): string;
27
+ /**
28
+ * Get a verification method from a DID document.
29
+ * @param didDocument The DID Document to get the method from.
30
+ * @param methodName The name of the method to get the JWK from.
31
+ * @param methodType The type of the method, defaults to verificationMethod.
32
+ * @returns The verification method if found.
33
+ * @throws Error if the method is not found.
34
+ */
35
+ static getVerificationMethod(didDocument: IDidDocument, methodName: string, methodType?: DidVerificationMethodType): IDidDocumentVerificationMethod;
36
+ /**
37
+ * Gets a JWK from a DID document verification method.
38
+ * @param didDocument The DID Document to get the method from.
39
+ * @param methodName The name of the method to get the JWK from.
40
+ * @param methodType The type of the method, defaults to verificationMethod.
41
+ * @returns The JWK if found.
42
+ * @throws Error if the method is not found.
43
+ */
44
+ static getJwk(didDocument: IDidDocument, methodName: string, methodType?: DidVerificationMethodType): IJwk;
21
45
  }
@@ -0,0 +1,29 @@
1
+ import type { IJsonLdNodeObject } from "@twin.org/data-json-ld";
2
+ import { type IJwtHeader, type IJwtPayload } from "@twin.org/web";
3
+ import type { IIdentityResolverConnector } from "../models/IIdentityResolverConnector";
4
+ /**
5
+ * Helper methods for verification.
6
+ */
7
+ export declare class VerificationHelper {
8
+ /**
9
+ * Runtime name for the class.
10
+ */
11
+ static readonly CLASS_NAME: string;
12
+ /**
13
+ * Verified the JWT.
14
+ * @param resolver The resolver to use for finding the document.
15
+ * @param jwt The token to verify.
16
+ * @returns The decoded payload.
17
+ */
18
+ verifyJwt<T extends IJwtHeader, U extends IJwtPayload>(resolver: IIdentityResolverConnector, jwt: string): Promise<{
19
+ header: T;
20
+ payload: U;
21
+ }>;
22
+ /**
23
+ * Verified the proof for the document e.g. verifiable credential.
24
+ * @param resolver The resolver to use for finding the document.
25
+ * @param secureDocument The secure document to verify.
26
+ * @returns True if the verification is successful.
27
+ */
28
+ verifyProof(resolver: IIdentityResolverConnector, secureDocument: IJsonLdNodeObject): Promise<boolean>;
29
+ }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/identity-service-models - Changelog
2
2
 
3
- ## v0.0.1-next.28
3
+ ## v0.0.1-next.29
4
4
 
5
5
  - Initial Release
@@ -12,6 +12,14 @@ Helper methods for documents.
12
12
 
13
13
  [`DocumentHelper`](DocumentHelper.md)
14
14
 
15
+ ## Properties
16
+
17
+ ### CLASS\_NAME
18
+
19
+ > `readonly` `static` **CLASS\_NAME**: `string`
20
+
21
+ Runtime name for the class.
22
+
15
23
  ## Methods
16
24
 
17
25
  ### parseId()
@@ -69,3 +77,79 @@ The fragment part for the identifier.
69
77
  `string`
70
78
 
71
79
  The full id.
80
+
81
+ ***
82
+
83
+ ### getVerificationMethod()
84
+
85
+ > `static` **getVerificationMethod**(`didDocument`, `methodName`, `methodType`?): `IDidDocumentVerificationMethod`
86
+
87
+ Get a verification method from a DID document.
88
+
89
+ #### Parameters
90
+
91
+ ##### didDocument
92
+
93
+ `IDidDocument`
94
+
95
+ The DID Document to get the method from.
96
+
97
+ ##### methodName
98
+
99
+ `string`
100
+
101
+ The name of the method to get the JWK from.
102
+
103
+ ##### methodType?
104
+
105
+ `DidVerificationMethodType`
106
+
107
+ The type of the method, defaults to verificationMethod.
108
+
109
+ #### Returns
110
+
111
+ `IDidDocumentVerificationMethod`
112
+
113
+ The verification method if found.
114
+
115
+ #### Throws
116
+
117
+ Error if the method is not found.
118
+
119
+ ***
120
+
121
+ ### getJwk()
122
+
123
+ > `static` **getJwk**(`didDocument`, `methodName`, `methodType`?): `IJwk`
124
+
125
+ Gets a JWK from a DID document verification method.
126
+
127
+ #### Parameters
128
+
129
+ ##### didDocument
130
+
131
+ `IDidDocument`
132
+
133
+ The DID Document to get the method from.
134
+
135
+ ##### methodName
136
+
137
+ `string`
138
+
139
+ The name of the method to get the JWK from.
140
+
141
+ ##### methodType?
142
+
143
+ `DidVerificationMethodType`
144
+
145
+ The type of the method, defaults to verificationMethod.
146
+
147
+ #### Returns
148
+
149
+ `IJwk`
150
+
151
+ The JWK if found.
152
+
153
+ #### Throws
154
+
155
+ Error if the method is not found.
@@ -0,0 +1,83 @@
1
+ # Class: VerificationHelper
2
+
3
+ Helper methods for verification.
4
+
5
+ ## Constructors
6
+
7
+ ### new VerificationHelper()
8
+
9
+ > **new VerificationHelper**(): [`VerificationHelper`](VerificationHelper.md)
10
+
11
+ #### Returns
12
+
13
+ [`VerificationHelper`](VerificationHelper.md)
14
+
15
+ ## Properties
16
+
17
+ ### CLASS\_NAME
18
+
19
+ > `readonly` `static` **CLASS\_NAME**: `string`
20
+
21
+ Runtime name for the class.
22
+
23
+ ## Methods
24
+
25
+ ### verifyJwt()
26
+
27
+ > **verifyJwt**\<`T`, `U`\>(`resolver`, `jwt`): `Promise`\<\{ `header`: `T`; `payload`: `U`; \}\>
28
+
29
+ Verified the JWT.
30
+
31
+ #### Type Parameters
32
+
33
+ • **T** *extends* `IJwtHeader`
34
+
35
+ • **U** *extends* `IJwtPayload`
36
+
37
+ #### Parameters
38
+
39
+ ##### resolver
40
+
41
+ [`IIdentityResolverConnector`](../interfaces/IIdentityResolverConnector.md)
42
+
43
+ The resolver to use for finding the document.
44
+
45
+ ##### jwt
46
+
47
+ `string`
48
+
49
+ The token to verify.
50
+
51
+ #### Returns
52
+
53
+ `Promise`\<\{ `header`: `T`; `payload`: `U`; \}\>
54
+
55
+ The decoded payload.
56
+
57
+ ***
58
+
59
+ ### verifyProof()
60
+
61
+ > **verifyProof**(`resolver`, `secureDocument`): `Promise`\<`boolean`\>
62
+
63
+ Verified the proof for the document e.g. verifiable credential.
64
+
65
+ #### Parameters
66
+
67
+ ##### resolver
68
+
69
+ [`IIdentityResolverConnector`](../interfaces/IIdentityResolverConnector.md)
70
+
71
+ The resolver to use for finding the document.
72
+
73
+ ##### secureDocument
74
+
75
+ `IJsonLdNodeObject`
76
+
77
+ The secure document to verify.
78
+
79
+ #### Returns
80
+
81
+ `Promise`\<`boolean`\>
82
+
83
+ True if the verification is successful.
@@ -3,7 +3,7 @@
3
3
  ## Classes
4
4
 
5
5
  - [DocumentHelper](classes/DocumentHelper.md)
6
- - [JwtHelper](classes/JwtHelper.md)
6
+ - [VerificationHelper](classes/VerificationHelper.md)
7
7
 
8
8
  ## Interfaces
9
9
 
@@ -428,9 +428,9 @@ The presentation stored in the jwt and the revocation status.
428
428
 
429
429
  ### proofCreate()
430
430
 
431
- > **proofCreate**(`verificationMethodId`, `bytes`, `controller`?): `Promise`\<`IDidProof`\>
431
+ > **proofCreate**(`verificationMethodId`, `proofType`, `unsecureDocument`, `controller`?): `Promise`\<`IProof`\>
432
432
 
433
- Create a proof for arbitrary data with the specified verification method.
433
+ Create a proof for a document with the specified verification method.
434
434
 
435
435
  #### Parameters
436
436
 
@@ -440,11 +440,17 @@ Create a proof for arbitrary data with the specified verification method.
440
440
 
441
441
  The verification method id to use.
442
442
 
443
- ##### bytes
443
+ ##### proofType
444
444
 
445
- `Uint8Array`
445
+ `ProofTypes`
446
446
 
447
- The data bytes to sign.
447
+ The type of proof to create.
448
+
449
+ ##### unsecureDocument
450
+
451
+ `IJsonLdNodeObject`
452
+
453
+ The unsecure document to create the proof for.
448
454
 
449
455
  ##### controller?
450
456
 
@@ -454,7 +460,7 @@ The controller of the identity who can make changes.
454
460
 
455
461
  #### Returns
456
462
 
457
- `Promise`\<`IDidProof`\>
463
+ `Promise`\<`IProof`\>
458
464
 
459
465
  The proof.
460
466
 
@@ -462,21 +468,21 @@ The proof.
462
468
 
463
469
  ### proofVerify()
464
470
 
465
- > **proofVerify**(`bytes`, `proof`): `Promise`\<`boolean`\>
471
+ > **proofVerify**(`document`, `proof`): `Promise`\<`boolean`\>
466
472
 
467
- Verify proof for arbitrary data with the specified verification method.
473
+ Verify proof for a document with the specified verification method.
468
474
 
469
475
  #### Parameters
470
476
 
471
- ##### bytes
477
+ ##### document
472
478
 
473
- `Uint8Array`
479
+ `IJsonLdNodeObject`
474
480
 
475
- The data bytes to verify.
481
+ The document to verify.
476
482
 
477
483
  ##### proof
478
484
 
479
- `IDidProof`
485
+ `IProof`
480
486
 
481
487
  The proof to verify.
482
488
 
@@ -422,7 +422,7 @@ The presentation stored in the jwt and the revocation status.
422
422
 
423
423
  ### createProof()
424
424
 
425
- > **createProof**(`controller`, `verificationMethodId`, `bytes`): `Promise`\<`IDidProof`\>
425
+ > **createProof**(`controller`, `verificationMethodId`, `proofType`, `unsecureDocument`): `Promise`\<`IProof`\>
426
426
 
427
427
  Create a proof for arbitrary data with the specified verification method.
428
428
 
@@ -440,15 +440,21 @@ The controller of the identity who can make changes.
440
440
 
441
441
  The verification method id to use.
442
442
 
443
- ##### bytes
443
+ ##### proofType
444
444
 
445
- `Uint8Array`
445
+ `ProofTypes`
446
446
 
447
- The data bytes to sign.
447
+ The type of proof to create.
448
+
449
+ ##### unsecureDocument
450
+
451
+ `IJsonLdNodeObject`
452
+
453
+ The unsecure document to create the proof for.
448
454
 
449
455
  #### Returns
450
456
 
451
- `Promise`\<`IDidProof`\>
457
+ `Promise`\<`IProof`\>
452
458
 
453
459
  The proof.
454
460
 
@@ -456,21 +462,21 @@ The proof.
456
462
 
457
463
  ### verifyProof()
458
464
 
459
- > **verifyProof**(`bytes`, `proof`): `Promise`\<`boolean`\>
465
+ > **verifyProof**(`document`, `proof`): `Promise`\<`boolean`\>
460
466
 
461
467
  Verify proof for arbitrary data with the specified verification method.
462
468
 
463
469
  #### Parameters
464
470
 
465
- ##### bytes
471
+ ##### document
466
472
 
467
- `Uint8Array`
473
+ `IJsonLdNodeObject`
468
474
 
469
- The data bytes to verify.
475
+ The document to verify.
470
476
 
471
477
  ##### proof
472
478
 
473
- `IDidProof`
479
+ `IProof`
474
480
 
475
481
  The proof to verify.
476
482
 
@@ -30,8 +30,14 @@ The verification method id to use.
30
30
 
31
31
  The data for the request.
32
32
 
33
- #### bytes
33
+ #### proofType
34
34
 
35
- > **bytes**: `string`
35
+ > **proofType**: `ProofTypes`
36
36
 
37
- The data bytes base64 encoded.
37
+ The type of proof to create.
38
+
39
+ #### document
40
+
41
+ > **document**: `IJsonLdNodeObject`
42
+
43
+ The document to create the proof for.
@@ -6,6 +6,6 @@ Response to creating a proof.
6
6
 
7
7
  ### body
8
8
 
9
- > **body**: `IDidProof`
9
+ > **body**: `IProof`
10
10
 
11
11
  The response payload.
@@ -10,14 +10,14 @@ Request to verify a proof.
10
10
 
11
11
  The data for the request.
12
12
 
13
- #### bytes
13
+ #### document
14
14
 
15
- > **bytes**: `string`
15
+ > **document**: `IJsonLdNodeObject`
16
16
 
17
- The data bytes base64 encoded.
17
+ The document to verify the proof for.
18
18
 
19
19
  #### proof
20
20
 
21
- > **proof**: `IDidProof`
21
+ > **proof**: `IProof`
22
22
 
23
23
  The proof to verify.
package/locales/en.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "error": {
3
- "jwtHelper": {
3
+ "verificationHelper": {
4
4
  "jwtDecodeFailed": "Decoding the JWT failed",
5
- "jwtPayloadMissingParam": "The JWT is missing the required parameter \"{param}\""
5
+ "proofTypeNotSupported": "The proof type \"{proofType}\" is not supported",
6
+ "proofMissingVerificationMethod": "The proof is missing the verification method"
7
+ },
8
+ "documentHelper": {
9
+ "verificationMethodNotFound": "The verification method \"{methodName}\" of type \"{methodType}\" could not be found",
10
+ "verificationMethodJwkNotFound": "The verification method \"{methodName}\" of type \"{methodType}\" is missing the JWK"
6
11
  }
7
12
  },
8
13
  "verifiableCredentialStates": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/identity-models",
3
- "version": "0.0.1-next.28",
3
+ "version": "0.0.1-next.29",
4
4
  "description": "Models which define the structure of the contracts and connectors",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,22 +0,0 @@
1
- import { type IJwtHeader, type IJwtPayload } from "@twin.org/web";
2
- /**
3
- * Helper methods for JSON Web Tokens.
4
- */
5
- export declare class JwtHelper {
6
- /**
7
- * Runtime name for the class.
8
- */
9
- static readonly CLASS_NAME: string;
10
- /**
11
- * Parse the token and check that the properties are valid.
12
- * @param jwt The token top validate.
13
- * @param paramsToCheck Parameters to check they exist.
14
- * @returns The token components.
15
- * @throws Error if the token is invalid.
16
- */
17
- static parse<U extends IJwtHeader, T extends IJwtPayload>(jwt: string, paramsToCheck?: string[]): Promise<{
18
- header?: U;
19
- payload?: T;
20
- signature?: Uint8Array;
21
- }>;
22
- }
@@ -1,59 +0,0 @@
1
- # Class: JwtHelper
2
-
3
- Helper methods for JSON Web Tokens.
4
-
5
- ## Constructors
6
-
7
- ### new JwtHelper()
8
-
9
- > **new JwtHelper**(): [`JwtHelper`](JwtHelper.md)
10
-
11
- #### Returns
12
-
13
- [`JwtHelper`](JwtHelper.md)
14
-
15
- ## Properties
16
-
17
- ### CLASS\_NAME
18
-
19
- > `readonly` `static` **CLASS\_NAME**: `string`
20
-
21
- Runtime name for the class.
22
-
23
- ## Methods
24
-
25
- ### parse()
26
-
27
- > `static` **parse**\<`U`, `T`\>(`jwt`, `paramsToCheck`?): `Promise`\<\{ `header`: `U`; `payload`: `T`; `signature`: `Uint8Array`\<`ArrayBufferLike`\>; \}\>
28
-
29
- Parse the token and check that the properties are valid.
30
-
31
- #### Type Parameters
32
-
33
- • **U** *extends* `IJwtHeader`
34
-
35
- • **T** *extends* `IJwtPayload`
36
-
37
- #### Parameters
38
-
39
- ##### jwt
40
-
41
- `string`
42
-
43
- The token top validate.
44
-
45
- ##### paramsToCheck?
46
-
47
- `string`[]
48
-
49
- Parameters to check they exist.
50
-
51
- #### Returns
52
-
53
- `Promise`\<\{ `header`: `U`; `payload`: `T`; `signature`: `Uint8Array`\<`ArrayBufferLike`\>; \}\>
54
-
55
- The token components.
56
-
57
- #### Throws
58
-
59
- Error if the token is invalid.