@twin.org/identity-models 0.0.2-next.9 → 0.0.3-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/contextIdHandlers/didContextIdHandler.js +34 -0
- package/dist/es/contextIdHandlers/didContextIdHandler.js.map +1 -0
- package/dist/es/factories/identityConnectorFactory.js +9 -0
- package/dist/es/factories/identityConnectorFactory.js.map +1 -0
- package/dist/es/factories/identityProfileConnectorFactory.js +9 -0
- package/dist/es/factories/identityProfileConnectorFactory.js.map +1 -0
- package/dist/es/factories/identityResolverConnectorFactory.js +9 -0
- package/dist/es/factories/identityResolverConnectorFactory.js.map +1 -0
- package/dist/es/index.js +49 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IIdentityComponent.js +2 -0
- package/dist/es/models/IIdentityComponent.js.map +1 -0
- package/dist/es/models/IIdentityConnector.js +2 -0
- package/dist/es/models/IIdentityConnector.js.map +1 -0
- package/dist/es/models/IIdentityProfileComponent.js +2 -0
- package/dist/es/models/IIdentityProfileComponent.js.map +1 -0
- package/dist/es/models/IIdentityProfileConnector.js +2 -0
- package/dist/es/models/IIdentityProfileConnector.js.map +1 -0
- package/dist/es/models/IIdentityResolverComponent.js +2 -0
- package/dist/es/models/IIdentityResolverComponent.js.map +1 -0
- package/dist/es/models/IIdentityResolverConnector.js +2 -0
- package/dist/es/models/IIdentityResolverConnector.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityCreateRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityCreateRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityCreateResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityCreateResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityProofCreateRequest.js +2 -0
- package/dist/es/models/api/identity/IIdentityProofCreateRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityProofCreateResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityProofCreateResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityProofVerifyRequest.js +2 -0
- package/dist/es/models/api/identity/IIdentityProofVerifyRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityProofVerifyResponse.js +4 -0
- package/dist/es/models/api/identity/IIdentityProofVerifyResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityRemoveRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityRemoveRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityServiceCreateRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityServiceCreateRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityServiceCreateResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityServiceCreateResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityServiceRemoveRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityServiceRemoveRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialCreateRequest.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialCreateRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialCreateResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialCreateResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialRevokeRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialRevokeRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialUnrevokeRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialUnrevokeRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialVerifyRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialVerifyRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialVerifyResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerifiableCredentialVerifyResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationCreateRequest.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationCreateRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationCreateResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationCreateResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationVerifyRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationVerifyRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationVerifyResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerifiablePresentationVerifyResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerificationMethodCreateRequest.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerificationMethodCreateRequest.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerificationMethodCreateResponse.js +2 -0
- package/dist/es/models/api/identity/IIdentityVerificationMethodCreateResponse.js.map +1 -0
- package/dist/es/models/api/identity/IIdentityVerificationMethodRemoveRequest.js +4 -0
- package/dist/es/models/api/identity/IIdentityVerificationMethodRemoveRequest.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileCreateRequest.js +2 -0
- package/dist/es/models/api/profile/IIdentityProfileCreateRequest.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileGetPublicRequest.js +4 -0
- package/dist/es/models/api/profile/IIdentityProfileGetPublicRequest.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileGetPublicResponse.js +2 -0
- package/dist/es/models/api/profile/IIdentityProfileGetPublicResponse.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileGetRequest.js +4 -0
- package/dist/es/models/api/profile/IIdentityProfileGetRequest.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileGetResponse.js +2 -0
- package/dist/es/models/api/profile/IIdentityProfileGetResponse.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileListRequest.js +4 -0
- package/dist/es/models/api/profile/IIdentityProfileListRequest.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileListResponse.js +2 -0
- package/dist/es/models/api/profile/IIdentityProfileListResponse.js.map +1 -0
- package/dist/es/models/api/profile/IIdentityProfileUpdateRequest.js +2 -0
- package/dist/es/models/api/profile/IIdentityProfileUpdateRequest.js.map +1 -0
- package/dist/es/models/api/resolver/IIdentityResolveRequest.js +4 -0
- package/dist/es/models/api/resolver/IIdentityResolveRequest.js.map +1 -0
- package/dist/es/models/api/resolver/IIdentityResolveResponse.js +2 -0
- package/dist/es/models/api/resolver/IIdentityResolveResponse.js.map +1 -0
- package/dist/es/types/did.js +53 -0
- package/dist/es/types/did.js.map +1 -0
- package/dist/es/utils/documentHelper.js +92 -0
- package/dist/es/utils/documentHelper.js.map +1 -0
- package/dist/es/utils/verificationHelper.js +79 -0
- package/dist/es/utils/verificationHelper.js.map +1 -0
- package/dist/types/contextIdHandlers/didContextIdHandler.d.ts +22 -0
- package/dist/types/factories/identityConnectorFactory.d.ts +1 -1
- package/dist/types/factories/identityProfileConnectorFactory.d.ts +1 -1
- package/dist/types/factories/identityResolverConnectorFactory.d.ts +1 -1
- package/dist/types/index.d.ts +46 -45
- package/dist/types/types/did.d.ts +28 -0
- package/dist/types/utils/verificationHelper.d.ts +1 -1
- package/docs/changelog.md +37 -0
- package/docs/reference/classes/{IdHelper.md → Did.md} +41 -5
- package/docs/reference/classes/DidContextIdHandler.md +79 -0
- package/docs/reference/classes/DocumentHelper.md +3 -3
- package/docs/reference/classes/VerificationHelper.md +2 -2
- package/docs/reference/index.md +2 -1
- package/docs/reference/interfaces/IIdentityComponent.md +4 -12
- package/docs/reference/interfaces/IIdentityConnector.md +4 -12
- package/docs/reference/interfaces/IIdentityProfileComponent.md +0 -8
- package/docs/reference/interfaces/IIdentityProfileConnector.md +0 -8
- package/docs/reference/interfaces/IIdentityResolverComponent.md +0 -8
- package/docs/reference/interfaces/IIdentityResolverConnector.md +0 -8
- package/locales/en.json +3 -2
- package/package.json +6 -7
- package/dist/cjs/index.cjs +0 -241
- package/dist/esm/index.mjs +0 -234
- package/dist/types/utils/idHelper.d.ts +0 -20
package/dist/esm/index.mjs
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { Factory, Is, GeneralError, Urn, Guards } from '@twin.org/core';
|
|
2
|
-
import { DidVerificationMethodType, ProofHelper } from '@twin.org/standards-w3c-did';
|
|
3
|
-
import { Jwt, Jwk } from '@twin.org/web';
|
|
4
|
-
|
|
5
|
-
// Copyright 2024 IOTA Stiftung.
|
|
6
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
7
|
-
/**
|
|
8
|
-
* Factory for creating identity connectors.
|
|
9
|
-
*/
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
11
|
-
const IdentityConnectorFactory = Factory.createFactory("identity-connector");
|
|
12
|
-
|
|
13
|
-
// Copyright 2024 IOTA Stiftung.
|
|
14
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
15
|
-
/**
|
|
16
|
-
* Factory for creating identity profile connectors.
|
|
17
|
-
*/
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
19
|
-
const IdentityProfileConnectorFactory = Factory.createFactory("identity-profile-connector");
|
|
20
|
-
|
|
21
|
-
// Copyright 2024 IOTA Stiftung.
|
|
22
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
23
|
-
/**
|
|
24
|
-
* Factory for creating identity resolver connectors.
|
|
25
|
-
*/
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
27
|
-
const IdentityResolverConnectorFactory = Factory.createFactory("identity-resolver-connector");
|
|
28
|
-
|
|
29
|
-
// Copyright 2024 IOTA Stiftung.
|
|
30
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
31
|
-
/**
|
|
32
|
-
* Helper methods for documents.
|
|
33
|
-
*/
|
|
34
|
-
class DocumentHelper {
|
|
35
|
-
/**
|
|
36
|
-
* Runtime name for the class.
|
|
37
|
-
*/
|
|
38
|
-
static CLASS_NAME = "DocumentHelper";
|
|
39
|
-
/**
|
|
40
|
-
* Parse the document id into its parts.
|
|
41
|
-
* @param documentId The full document id.
|
|
42
|
-
* @returns The parsed document id.
|
|
43
|
-
*/
|
|
44
|
-
static parseId(documentId) {
|
|
45
|
-
if (!Is.stringValue(documentId)) {
|
|
46
|
-
return {
|
|
47
|
-
id: "",
|
|
48
|
-
fragment: ""
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
const fragmentIndex = documentId.indexOf("#");
|
|
52
|
-
return {
|
|
53
|
-
id: fragmentIndex === -1 ? documentId : documentId.slice(0, fragmentIndex),
|
|
54
|
-
fragment: fragmentIndex === -1 ? undefined : documentId.slice(fragmentIndex + 1)
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Join the document id parts.
|
|
59
|
-
* @param documentId The document id.
|
|
60
|
-
* @param fragment The fragment part for the identifier.
|
|
61
|
-
* @returns The full id.
|
|
62
|
-
*/
|
|
63
|
-
static joinId(documentId, fragment) {
|
|
64
|
-
if (!Is.stringValue(documentId)) {
|
|
65
|
-
return "";
|
|
66
|
-
}
|
|
67
|
-
let fullId = documentId;
|
|
68
|
-
if (Is.stringValue(fragment)) {
|
|
69
|
-
if (fragment.startsWith(documentId)) {
|
|
70
|
-
fragment = fragment.slice(documentId.length);
|
|
71
|
-
}
|
|
72
|
-
if (fragment.startsWith("#")) {
|
|
73
|
-
fullId += fragment;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
fullId += `#${fragment}`;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return fullId;
|
|
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
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Copyright 2024 IOTA Stiftung.
|
|
120
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
121
|
-
/**
|
|
122
|
-
* Helper methods for Ids.
|
|
123
|
-
*/
|
|
124
|
-
class IdHelper {
|
|
125
|
-
/**
|
|
126
|
-
* Runtime name for the class.
|
|
127
|
-
*/
|
|
128
|
-
static CLASS_NAME = "IdHelper";
|
|
129
|
-
/**
|
|
130
|
-
* Parse and id in to it's constituent parts.
|
|
131
|
-
* @param id The id to parse.
|
|
132
|
-
* @returns The parsed id.
|
|
133
|
-
* @throws GeneralError if the id is not valid.
|
|
134
|
-
*/
|
|
135
|
-
static parseId(id) {
|
|
136
|
-
Urn.guard(IdHelper.CLASS_NAME, "id", id);
|
|
137
|
-
const didUrn = Urn.fromValidString(id);
|
|
138
|
-
const didParts = didUrn.parts();
|
|
139
|
-
if (didParts[0] !== "did") {
|
|
140
|
-
throw new GeneralError(IdHelper.CLASS_NAME, "invalidDocumentId", { id });
|
|
141
|
-
}
|
|
142
|
-
if (didParts.length === 3) {
|
|
143
|
-
return {
|
|
144
|
-
method: didParts[1],
|
|
145
|
-
id: didParts[2]
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
else if (didParts.length === 4) {
|
|
149
|
-
return {
|
|
150
|
-
method: didParts[1],
|
|
151
|
-
network: didParts[2],
|
|
152
|
-
id: didParts[3]
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
throw new GeneralError(IdHelper.CLASS_NAME, "invalidDocumentId", { id });
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Copyright 2024 IOTA Stiftung.
|
|
160
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
161
|
-
/**
|
|
162
|
-
* Helper methods for verification.
|
|
163
|
-
*/
|
|
164
|
-
class VerificationHelper {
|
|
165
|
-
/**
|
|
166
|
-
* Runtime name for the class.
|
|
167
|
-
*/
|
|
168
|
-
static CLASS_NAME = "VerificationHelper";
|
|
169
|
-
/**
|
|
170
|
-
* Verified the JWT.
|
|
171
|
-
* @param resolver The resolver to use for finding the document.
|
|
172
|
-
* @param jwt The token to verify.
|
|
173
|
-
* @returns The decoded payload.
|
|
174
|
-
*/
|
|
175
|
-
static async verifyJwt(resolver, jwt) {
|
|
176
|
-
Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
|
|
177
|
-
Guards.string(VerificationHelper.CLASS_NAME, "jwt", jwt);
|
|
178
|
-
const jwtDecoded = await Jwt.decode(jwt);
|
|
179
|
-
const jwtHeader = jwtDecoded.header;
|
|
180
|
-
const jwtPayload = jwtDecoded.payload;
|
|
181
|
-
const jwtSignature = jwtDecoded.signature;
|
|
182
|
-
if (!Is.object(jwtHeader) || !Is.object(jwtPayload) || !Is.uint8Array(jwtSignature)) {
|
|
183
|
-
throw new GeneralError(VerificationHelper.CLASS_NAME, "jwtDecodeFailed");
|
|
184
|
-
}
|
|
185
|
-
const iss = jwtHeader?.iss;
|
|
186
|
-
const kid = jwtHeader?.kid;
|
|
187
|
-
Guards.stringValue(VerificationHelper.CLASS_NAME, "iss", iss);
|
|
188
|
-
Guards.stringValue(VerificationHelper.CLASS_NAME, "kid", kid);
|
|
189
|
-
const didDocument = await resolver.identityResolve(iss);
|
|
190
|
-
const jwk = DocumentHelper.getJwk(didDocument, kid);
|
|
191
|
-
const publicKey = await Jwk.toCryptoKey(jwk);
|
|
192
|
-
return Jwt.verify(jwt, publicKey);
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Verified the proof for the document e.g. verifiable credential.
|
|
196
|
-
* @param resolver The resolver to use for finding the document.
|
|
197
|
-
* @param secureDocument The secure document to verify.
|
|
198
|
-
* @returns True if the verification is successful.
|
|
199
|
-
*/
|
|
200
|
-
static async verifyProof(resolver, secureDocument) {
|
|
201
|
-
Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
|
|
202
|
-
Guards.object(VerificationHelper.CLASS_NAME, "secureDocument", secureDocument);
|
|
203
|
-
Guards.object(VerificationHelper.CLASS_NAME, "secureDocument.proof", secureDocument.proof);
|
|
204
|
-
const proofList = Is.array(secureDocument.proof)
|
|
205
|
-
? secureDocument.proof
|
|
206
|
-
: [secureDocument.proof];
|
|
207
|
-
const documentCache = {};
|
|
208
|
-
for (const proof of proofList) {
|
|
209
|
-
if (!Is.stringValue(proof?.verificationMethod)) {
|
|
210
|
-
throw new GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
|
|
211
|
-
}
|
|
212
|
-
const proofVerificationMethod = DocumentHelper.parseId(proof.verificationMethod);
|
|
213
|
-
if (!Is.stringValue(proofVerificationMethod.fragment)) {
|
|
214
|
-
throw new GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
|
|
215
|
-
}
|
|
216
|
-
let document;
|
|
217
|
-
if (documentCache[proofVerificationMethod.id]) {
|
|
218
|
-
document = documentCache[proofVerificationMethod.id];
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
document = await resolver.identityResolve(proofVerificationMethod.id);
|
|
222
|
-
documentCache[proofVerificationMethod.id] = document;
|
|
223
|
-
}
|
|
224
|
-
const verificationJwk = await DocumentHelper.getJwk(document, proofVerificationMethod.id);
|
|
225
|
-
const verified = ProofHelper.verifyProof(secureDocument, proof, verificationJwk);
|
|
226
|
-
if (!verified) {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export { DocumentHelper, IdHelper, IdentityConnectorFactory, IdentityProfileConnectorFactory, IdentityResolverConnectorFactory, VerificationHelper };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Helper methods for Ids.
|
|
3
|
-
*/
|
|
4
|
-
export declare class IdHelper {
|
|
5
|
-
/**
|
|
6
|
-
* Runtime name for the class.
|
|
7
|
-
*/
|
|
8
|
-
static readonly CLASS_NAME: string;
|
|
9
|
-
/**
|
|
10
|
-
* Parse and id in to it's constituent parts.
|
|
11
|
-
* @param id The id to parse.
|
|
12
|
-
* @returns The parsed id.
|
|
13
|
-
* @throws GeneralError if the id is not valid.
|
|
14
|
-
*/
|
|
15
|
-
static parseId(id: string): {
|
|
16
|
-
method: string;
|
|
17
|
-
network?: string | undefined;
|
|
18
|
-
id: string;
|
|
19
|
-
};
|
|
20
|
-
}
|