@twin.org/immutable-proof-service 0.0.2-next.3 → 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/entities/immutableProof.js +53 -0
- package/dist/es/entities/immutableProof.js.map +1 -0
- package/dist/es/immutableProofRoutes.js +278 -0
- package/dist/es/immutableProofRoutes.js.map +1 -0
- package/dist/es/immutableProofService.js +335 -0
- package/dist/es/immutableProofService.js.map +1 -0
- package/dist/es/index.js +10 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IImmutableProofServiceConfig.js +4 -0
- package/dist/es/models/IImmutableProofServiceConfig.js.map +1 -0
- package/dist/es/models/IImmutableProofServiceConstructorOptions.js +2 -0
- package/dist/es/models/IImmutableProofServiceConstructorOptions.js.map +1 -0
- package/dist/es/restEntryPoints.js +10 -0
- package/dist/es/restEntryPoints.js.map +1 -0
- package/dist/es/schema.js +11 -0
- package/dist/es/schema.js.map +1 -0
- package/dist/types/entities/immutableProof.d.ts +0 -8
- package/dist/types/immutableProofService.d.ts +14 -6
- package/dist/types/index.d.ts +7 -7
- package/dist/types/models/IImmutableProofServiceConstructorOptions.d.ts +1 -1
- package/docs/changelog.md +27 -0
- package/docs/open-api/spec.json +10 -20
- package/docs/reference/classes/ImmutableProof.md +0 -16
- package/docs/reference/classes/ImmutableProofService.md +45 -19
- package/package.json +8 -9
- package/dist/cjs/index.cjs +0 -701
- package/dist/esm/index.mjs +0 -692
package/dist/cjs/index.cjs
DELETED
|
@@ -1,701 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var entity = require('@twin.org/entity');
|
|
4
|
-
var core = require('@twin.org/core');
|
|
5
|
-
var immutableProofModels = require('@twin.org/immutable-proof-models');
|
|
6
|
-
var standardsW3cDid = require('@twin.org/standards-w3c-did');
|
|
7
|
-
var web = require('@twin.org/web');
|
|
8
|
-
var backgroundTaskModels = require('@twin.org/background-task-models');
|
|
9
|
-
var crypto = require('@twin.org/crypto');
|
|
10
|
-
var dataJsonLd = require('@twin.org/data-json-ld');
|
|
11
|
-
var entityStorageModels = require('@twin.org/entity-storage-models');
|
|
12
|
-
var identityModels = require('@twin.org/identity-models');
|
|
13
|
-
var verifiableStorageModels = require('@twin.org/verifiable-storage-models');
|
|
14
|
-
|
|
15
|
-
// Copyright 2024 IOTA Stiftung.
|
|
16
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
17
|
-
/**
|
|
18
|
-
* Class describing the immutable proof.
|
|
19
|
-
*/
|
|
20
|
-
exports.ImmutableProof = class ImmutableProof {
|
|
21
|
-
/**
|
|
22
|
-
* The id of the proof.
|
|
23
|
-
*/
|
|
24
|
-
id;
|
|
25
|
-
/**
|
|
26
|
-
* The identity of the node which controls the proof.
|
|
27
|
-
*/
|
|
28
|
-
nodeIdentity;
|
|
29
|
-
/**
|
|
30
|
-
* The identity of the user which created the proof.
|
|
31
|
-
*/
|
|
32
|
-
userIdentity;
|
|
33
|
-
/**
|
|
34
|
-
* The date/time of when the proof was created.
|
|
35
|
-
*/
|
|
36
|
-
dateCreated;
|
|
37
|
-
/**
|
|
38
|
-
* The associated id for the item.
|
|
39
|
-
*/
|
|
40
|
-
proofObjectId;
|
|
41
|
-
/**
|
|
42
|
-
* The associated hash for the item.
|
|
43
|
-
*/
|
|
44
|
-
proofObjectHash;
|
|
45
|
-
/**
|
|
46
|
-
* The verifiable storage id.
|
|
47
|
-
*/
|
|
48
|
-
verifiableStorageId;
|
|
49
|
-
};
|
|
50
|
-
__decorate([
|
|
51
|
-
entity.property({ type: "string", isPrimary: true }),
|
|
52
|
-
__metadata("design:type", String)
|
|
53
|
-
], exports.ImmutableProof.prototype, "id", void 0);
|
|
54
|
-
__decorate([
|
|
55
|
-
entity.property({ type: "string" }),
|
|
56
|
-
__metadata("design:type", String)
|
|
57
|
-
], exports.ImmutableProof.prototype, "nodeIdentity", void 0);
|
|
58
|
-
__decorate([
|
|
59
|
-
entity.property({ type: "string" }),
|
|
60
|
-
__metadata("design:type", String)
|
|
61
|
-
], exports.ImmutableProof.prototype, "userIdentity", void 0);
|
|
62
|
-
__decorate([
|
|
63
|
-
entity.property({ type: "string", format: "date-time", sortDirection: entity.SortDirection.Descending }),
|
|
64
|
-
__metadata("design:type", String)
|
|
65
|
-
], exports.ImmutableProof.prototype, "dateCreated", void 0);
|
|
66
|
-
__decorate([
|
|
67
|
-
entity.property({ type: "string", optional: true }),
|
|
68
|
-
__metadata("design:type", String)
|
|
69
|
-
], exports.ImmutableProof.prototype, "proofObjectId", void 0);
|
|
70
|
-
__decorate([
|
|
71
|
-
entity.property({ type: "string" }),
|
|
72
|
-
__metadata("design:type", String)
|
|
73
|
-
], exports.ImmutableProof.prototype, "proofObjectHash", void 0);
|
|
74
|
-
__decorate([
|
|
75
|
-
entity.property({ type: "string", optional: true }),
|
|
76
|
-
__metadata("design:type", String)
|
|
77
|
-
], exports.ImmutableProof.prototype, "verifiableStorageId", void 0);
|
|
78
|
-
exports.ImmutableProof = __decorate([
|
|
79
|
-
entity.entity()
|
|
80
|
-
], exports.ImmutableProof);
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* The source used when communicating about these routes.
|
|
84
|
-
*/
|
|
85
|
-
const ROUTES_SOURCE = "immutableProofRoutes";
|
|
86
|
-
/**
|
|
87
|
-
* The tag to associate with the routes.
|
|
88
|
-
*/
|
|
89
|
-
const tagsImmutableProof = [
|
|
90
|
-
{
|
|
91
|
-
name: "Immutable Proof",
|
|
92
|
-
description: "Endpoints which are modelled to access an immutable proof contract."
|
|
93
|
-
}
|
|
94
|
-
];
|
|
95
|
-
/**
|
|
96
|
-
* The REST routes for immutable proof.
|
|
97
|
-
* @param baseRouteName Prefix to prepend to the paths.
|
|
98
|
-
* @param componentName The name of the component to use in the routes stored in the ComponentFactory.
|
|
99
|
-
* @returns The generated routes.
|
|
100
|
-
*/
|
|
101
|
-
function generateRestRoutesImmutableProof(baseRouteName, componentName) {
|
|
102
|
-
const createRoute = {
|
|
103
|
-
operationId: "immutableProofCreate",
|
|
104
|
-
summary: "Create a proof",
|
|
105
|
-
tag: tagsImmutableProof[0].name,
|
|
106
|
-
method: "POST",
|
|
107
|
-
path: `${baseRouteName}/`,
|
|
108
|
-
handler: async (httpRequestContext, request) => immutableProofCreate(httpRequestContext, componentName, request),
|
|
109
|
-
requestType: {
|
|
110
|
-
type: "IImmutableProofCreateRequest",
|
|
111
|
-
examples: [
|
|
112
|
-
{
|
|
113
|
-
id: "immutableProofCreateRequestExample",
|
|
114
|
-
request: {
|
|
115
|
-
body: {
|
|
116
|
-
document: {
|
|
117
|
-
"@context": "https://schema.org",
|
|
118
|
-
type: "Person",
|
|
119
|
-
name: "John Smith"
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
]
|
|
125
|
-
},
|
|
126
|
-
responseType: [
|
|
127
|
-
{
|
|
128
|
-
type: "ICreatedResponse",
|
|
129
|
-
examples: [
|
|
130
|
-
{
|
|
131
|
-
id: "immutableProofCreateResponseExample",
|
|
132
|
-
response: {
|
|
133
|
-
statusCode: web.HttpStatusCode.created,
|
|
134
|
-
headers: {
|
|
135
|
-
[web.HeaderTypes.Location]: "test:1234567890"
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
]
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
type: "INotFoundResponse"
|
|
143
|
-
}
|
|
144
|
-
]
|
|
145
|
-
};
|
|
146
|
-
const getRoute = {
|
|
147
|
-
operationId: "immutableProofGet",
|
|
148
|
-
summary: "Get a proof",
|
|
149
|
-
tag: tagsImmutableProof[0].name,
|
|
150
|
-
method: "GET",
|
|
151
|
-
path: `${baseRouteName}/:id`,
|
|
152
|
-
handler: async (httpRequestContext, request) => immutableProofGet(httpRequestContext, componentName, request),
|
|
153
|
-
requestType: {
|
|
154
|
-
type: "IImmutableProofGetRequest",
|
|
155
|
-
examples: [
|
|
156
|
-
{
|
|
157
|
-
id: "immutableProofGetRequestExample",
|
|
158
|
-
request: {
|
|
159
|
-
headers: {
|
|
160
|
-
[web.HeaderTypes.Accept]: web.MimeTypes.Json
|
|
161
|
-
},
|
|
162
|
-
pathParams: {
|
|
163
|
-
id: "ais:1234567890"
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
]
|
|
168
|
-
},
|
|
169
|
-
responseType: [
|
|
170
|
-
{
|
|
171
|
-
type: "IImmutableProofGetResponse",
|
|
172
|
-
examples: [
|
|
173
|
-
{
|
|
174
|
-
id: "immutableProofGetResponseExample",
|
|
175
|
-
response: {
|
|
176
|
-
body: {
|
|
177
|
-
"@context": [
|
|
178
|
-
immutableProofModels.ImmutableProofContexts.ContextRoot,
|
|
179
|
-
immutableProofModels.ImmutableProofContexts.ContextRootCommon
|
|
180
|
-
],
|
|
181
|
-
type: immutableProofModels.ImmutableProofTypes.ImmutableProof,
|
|
182
|
-
id: "ais:1234567890",
|
|
183
|
-
nodeIdentity: "node-1",
|
|
184
|
-
userIdentity: "user-1",
|
|
185
|
-
proofObjectId: "test:1234567890",
|
|
186
|
-
proofObjectHash: "EAOKyDN0mYQbBh91eMdVeroxQx1H4GfnRbmt6n/2L/Y=",
|
|
187
|
-
proof: {
|
|
188
|
-
"@context": standardsW3cDid.DidContexts.ContextDataIntegrity,
|
|
189
|
-
type: standardsW3cDid.ProofTypes.DataIntegrityProof,
|
|
190
|
-
cryptosuite: standardsW3cDid.DidCryptoSuites.EdDSAJcs2022,
|
|
191
|
-
created: "2024-08-22T11:56:56.272Z",
|
|
192
|
-
proofPurpose: "assertionMethod",
|
|
193
|
-
proofValue: "7DdiPPYtxLjCD3wA1po2rv..."
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
]
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
type: "IImmutableProofGetResponse",
|
|
202
|
-
mimeType: web.MimeTypes.JsonLd,
|
|
203
|
-
examples: [
|
|
204
|
-
{
|
|
205
|
-
id: "immutableProofJsonLdGetResponseExample",
|
|
206
|
-
response: {
|
|
207
|
-
headers: {
|
|
208
|
-
[web.HeaderTypes.ContentType]: web.MimeTypes.JsonLd
|
|
209
|
-
},
|
|
210
|
-
body: {
|
|
211
|
-
"@context": [
|
|
212
|
-
immutableProofModels.ImmutableProofContexts.ContextRoot,
|
|
213
|
-
immutableProofModels.ImmutableProofContexts.ContextRootCommon
|
|
214
|
-
],
|
|
215
|
-
type: immutableProofModels.ImmutableProofTypes.ImmutableProof,
|
|
216
|
-
id: "ais:1234567890",
|
|
217
|
-
nodeIdentity: "node-1",
|
|
218
|
-
userIdentity: "user-1",
|
|
219
|
-
proofObjectId: "test:1234567890",
|
|
220
|
-
proofObjectHash: "EAOKyDN0mYQbBh91eMdVeroxQx1H4GfnRbmt6n/2L/Y=",
|
|
221
|
-
proof: {
|
|
222
|
-
"@context": standardsW3cDid.DidContexts.ContextDataIntegrity,
|
|
223
|
-
type: standardsW3cDid.ProofTypes.DataIntegrityProof,
|
|
224
|
-
cryptosuite: standardsW3cDid.DidCryptoSuites.EdDSAJcs2022,
|
|
225
|
-
created: "2024-08-22T11:56:56.272Z",
|
|
226
|
-
proofPurpose: "assertionMethod",
|
|
227
|
-
proofValue: "7DdiPPYtxLjCD3wA1po2rv..."
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
]
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
type: "INotFoundResponse"
|
|
236
|
-
}
|
|
237
|
-
]
|
|
238
|
-
};
|
|
239
|
-
const verifyRoute = {
|
|
240
|
-
operationId: "immutableProofVerify",
|
|
241
|
-
summary: "Verify a proof",
|
|
242
|
-
tag: tagsImmutableProof[0].name,
|
|
243
|
-
method: "GET",
|
|
244
|
-
path: `${baseRouteName}/:id/verify`,
|
|
245
|
-
handler: async (httpRequestContext, request) => immutableProofVerify(httpRequestContext, componentName, request),
|
|
246
|
-
requestType: {
|
|
247
|
-
type: "IImmutableProofVerifyRequest",
|
|
248
|
-
examples: [
|
|
249
|
-
{
|
|
250
|
-
id: "immutableProofVerifyRequestExample",
|
|
251
|
-
request: {
|
|
252
|
-
pathParams: {
|
|
253
|
-
id: "ais:1234567890"
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
]
|
|
258
|
-
},
|
|
259
|
-
responseType: [
|
|
260
|
-
{
|
|
261
|
-
type: "IImmutableProofVerifyResponse",
|
|
262
|
-
examples: [
|
|
263
|
-
{
|
|
264
|
-
id: "immutableProofVerifyResponseExample",
|
|
265
|
-
response: {
|
|
266
|
-
body: {
|
|
267
|
-
"@context": immutableProofModels.ImmutableProofContexts.ContextRoot,
|
|
268
|
-
type: immutableProofModels.ImmutableProofTypes.ImmutableProofVerification,
|
|
269
|
-
verified: true
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
]
|
|
274
|
-
},
|
|
275
|
-
{
|
|
276
|
-
type: "IImmutableProofVerifyResponse",
|
|
277
|
-
examples: [
|
|
278
|
-
{
|
|
279
|
-
id: "immutableProofVerifyResponseFailExample",
|
|
280
|
-
response: {
|
|
281
|
-
body: {
|
|
282
|
-
"@context": immutableProofModels.ImmutableProofContexts.ContextRoot,
|
|
283
|
-
type: immutableProofModels.ImmutableProofTypes.ImmutableProofVerification,
|
|
284
|
-
verified: false,
|
|
285
|
-
failure: immutableProofModels.ImmutableProofFailure.ProofTypeMismatch
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
]
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
type: "INotFoundResponse"
|
|
293
|
-
}
|
|
294
|
-
]
|
|
295
|
-
};
|
|
296
|
-
return [createRoute, getRoute, verifyRoute];
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Create a proof.
|
|
300
|
-
* @param httpRequestContext The request context for the API.
|
|
301
|
-
* @param componentName The name of the component to use in the routes.
|
|
302
|
-
* @param request The request.
|
|
303
|
-
* @returns The response object with additional http response properties.
|
|
304
|
-
*/
|
|
305
|
-
async function immutableProofCreate(httpRequestContext, componentName, request) {
|
|
306
|
-
core.Guards.object(ROUTES_SOURCE, "request", request);
|
|
307
|
-
core.Guards.object(ROUTES_SOURCE, "request.body.document", request.body.document);
|
|
308
|
-
const component = core.ComponentFactory.get(componentName);
|
|
309
|
-
const result = await component.create(request.body.document, httpRequestContext.userIdentity, httpRequestContext.nodeIdentity);
|
|
310
|
-
return {
|
|
311
|
-
statusCode: web.HttpStatusCode.created,
|
|
312
|
-
headers: {
|
|
313
|
-
[web.HeaderTypes.Location]: result
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Get the proof.
|
|
319
|
-
* @param httpRequestContext The request context for the API.
|
|
320
|
-
* @param componentName The name of the component to use in the routes.
|
|
321
|
-
* @param request The request.
|
|
322
|
-
* @returns The response object with additional http response properties.
|
|
323
|
-
*/
|
|
324
|
-
async function immutableProofGet(httpRequestContext, componentName, request) {
|
|
325
|
-
core.Guards.object(ROUTES_SOURCE, "request", request);
|
|
326
|
-
core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
|
|
327
|
-
core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
328
|
-
const mimeType = request.headers?.[web.HeaderTypes.Accept] === web.MimeTypes.JsonLd ? "jsonld" : "json";
|
|
329
|
-
const component = core.ComponentFactory.get(componentName);
|
|
330
|
-
const result = await component.get(request.pathParams.id);
|
|
331
|
-
return {
|
|
332
|
-
headers: {
|
|
333
|
-
[web.HeaderTypes.ContentType]: mimeType === "json" ? web.MimeTypes.Json : web.MimeTypes.JsonLd
|
|
334
|
-
},
|
|
335
|
-
body: result
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Verify the proof.
|
|
340
|
-
* @param httpRequestContext The request context for the API.
|
|
341
|
-
* @param componentName The name of the component to use in the routes.
|
|
342
|
-
* @param request The request.
|
|
343
|
-
* @returns The response object with additional http response properties.
|
|
344
|
-
*/
|
|
345
|
-
async function immutableProofVerify(httpRequestContext, componentName, request) {
|
|
346
|
-
core.Guards.object(ROUTES_SOURCE, "request", request);
|
|
347
|
-
core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
|
|
348
|
-
core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
349
|
-
const mimeType = request.headers?.[web.HeaderTypes.Accept] === web.MimeTypes.JsonLd ? "jsonld" : "json";
|
|
350
|
-
const component = core.ComponentFactory.get(componentName);
|
|
351
|
-
const result = await component.verify(request.pathParams.id);
|
|
352
|
-
return {
|
|
353
|
-
headers: {
|
|
354
|
-
[web.HeaderTypes.ContentType]: mimeType === "json" ? web.MimeTypes.Json : web.MimeTypes.JsonLd
|
|
355
|
-
},
|
|
356
|
-
body: result
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// Copyright 2024 IOTA Stiftung.
|
|
361
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
362
|
-
/**
|
|
363
|
-
* Class for performing immutable proof operations.
|
|
364
|
-
*/
|
|
365
|
-
class ImmutableProofService {
|
|
366
|
-
/**
|
|
367
|
-
* Runtime name for the class.
|
|
368
|
-
*/
|
|
369
|
-
static CLASS_NAME = "ImmutableProofService";
|
|
370
|
-
/**
|
|
371
|
-
* The namespace for the service.
|
|
372
|
-
* @internal
|
|
373
|
-
*/
|
|
374
|
-
static _NAMESPACE = "immutable-proof";
|
|
375
|
-
/**
|
|
376
|
-
* The configuration for the connector.
|
|
377
|
-
* @internal
|
|
378
|
-
*/
|
|
379
|
-
_config;
|
|
380
|
-
/**
|
|
381
|
-
* The identity connector.
|
|
382
|
-
* @internal
|
|
383
|
-
*/
|
|
384
|
-
_identityConnector;
|
|
385
|
-
/**
|
|
386
|
-
* The entity storage for proofs.
|
|
387
|
-
* @internal
|
|
388
|
-
*/
|
|
389
|
-
_proofStorage;
|
|
390
|
-
/**
|
|
391
|
-
* The verifiable storage for the credentials.
|
|
392
|
-
* @internal
|
|
393
|
-
*/
|
|
394
|
-
_verifiableStorage;
|
|
395
|
-
/**
|
|
396
|
-
* The background task connector.
|
|
397
|
-
* @internal
|
|
398
|
-
*/
|
|
399
|
-
_backgroundTaskConnector;
|
|
400
|
-
/**
|
|
401
|
-
* The event bus component.
|
|
402
|
-
* @internal
|
|
403
|
-
*/
|
|
404
|
-
_eventBusComponent;
|
|
405
|
-
/**
|
|
406
|
-
* The verification method id to use for the proofs.
|
|
407
|
-
* @internal
|
|
408
|
-
*/
|
|
409
|
-
_verificationMethodId;
|
|
410
|
-
/**
|
|
411
|
-
* The identity connector type.
|
|
412
|
-
* @internal
|
|
413
|
-
*/
|
|
414
|
-
_identityConnectorType;
|
|
415
|
-
/**
|
|
416
|
-
* Create a new instance of ImmutableProofService.
|
|
417
|
-
* @param options The dependencies for the immutable proof connector.
|
|
418
|
-
*/
|
|
419
|
-
constructor(options) {
|
|
420
|
-
this._proofStorage = entityStorageModels.EntityStorageConnectorFactory.get(options?.immutableProofEntityStorageType ?? "immutable-proof");
|
|
421
|
-
this._verifiableStorage = verifiableStorageModels.VerifiableStorageConnectorFactory.get(options?.verifiableStorageType ?? "verifiable-storage");
|
|
422
|
-
this._identityConnectorType = options?.identityConnectorType ?? "identity";
|
|
423
|
-
this._identityConnector = identityModels.IdentityConnectorFactory.get(this._identityConnectorType);
|
|
424
|
-
this._backgroundTaskConnector = backgroundTaskModels.BackgroundTaskConnectorFactory.get(options?.backgroundTaskConnectorType ?? "background-task");
|
|
425
|
-
if (core.Is.stringValue(options?.eventBusComponentType)) {
|
|
426
|
-
this._eventBusComponent = core.ComponentFactory.get(options.eventBusComponentType);
|
|
427
|
-
}
|
|
428
|
-
this._config = options?.config ?? {};
|
|
429
|
-
this._verificationMethodId = this._config.verificationMethodId ?? "immutable-proof-assertion";
|
|
430
|
-
this._backgroundTaskConnector.registerHandler("immutable-proof", "@twin.org/immutable-proof-task", "processProofTask", async (task) => {
|
|
431
|
-
await this.finaliseTask(task);
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Create a new proof.
|
|
436
|
-
* @param document The document to create the proof for.
|
|
437
|
-
* @param userIdentity The identity to create the immutable proof operation with.
|
|
438
|
-
* @param nodeIdentity The node identity to use for vault operations.
|
|
439
|
-
* @returns The id of the new proof.
|
|
440
|
-
*/
|
|
441
|
-
async create(document, userIdentity, nodeIdentity) {
|
|
442
|
-
core.Guards.object(ImmutableProofService.CLASS_NAME, "document", document);
|
|
443
|
-
core.Guards.stringValue(ImmutableProofService.CLASS_NAME, "userIdentity", userIdentity);
|
|
444
|
-
core.Guards.stringValue(ImmutableProofService.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
|
445
|
-
try {
|
|
446
|
-
const validationFailures = [];
|
|
447
|
-
await dataJsonLd.JsonLdHelper.validate(document, validationFailures);
|
|
448
|
-
core.Validation.asValidationError(ImmutableProofService.CLASS_NAME, "document", validationFailures);
|
|
449
|
-
const id = core.Converter.bytesToHex(core.RandomHelper.generate(32), false);
|
|
450
|
-
const dateCreated = new Date(Date.now()).toISOString();
|
|
451
|
-
const proofObjectId = core.ObjectHelper.extractProperty(document, ["@id", "id"], false);
|
|
452
|
-
// We don't want to store the whole document in the immutable proof, as this could be large
|
|
453
|
-
// and also reveal information that should not be stored in the proof so we hash the document
|
|
454
|
-
// and store the hash
|
|
455
|
-
const proofObjectHash = this.calculateDocumentHash(document);
|
|
456
|
-
const proofEntity = {
|
|
457
|
-
id,
|
|
458
|
-
nodeIdentity,
|
|
459
|
-
userIdentity,
|
|
460
|
-
dateCreated,
|
|
461
|
-
proofObjectId,
|
|
462
|
-
proofObjectHash
|
|
463
|
-
};
|
|
464
|
-
await this._proofStorage.set(proofEntity);
|
|
465
|
-
const immutableProof = this.proofEntityToJsonLd(proofEntity);
|
|
466
|
-
const proofTaskPayload = {
|
|
467
|
-
proofId: id,
|
|
468
|
-
nodeIdentity,
|
|
469
|
-
identityConnectorType: this._identityConnectorType,
|
|
470
|
-
verificationMethodId: this._verificationMethodId,
|
|
471
|
-
document: immutableProof
|
|
472
|
-
};
|
|
473
|
-
await this._backgroundTaskConnector.create("immutable-proof", proofTaskPayload);
|
|
474
|
-
return new core.Urn(ImmutableProofService._NAMESPACE, id).toString();
|
|
475
|
-
}
|
|
476
|
-
catch (error) {
|
|
477
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "createFailed", undefined, error);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Get a proof.
|
|
482
|
-
* @param id The id of the proof to get.
|
|
483
|
-
* @returns The proof.
|
|
484
|
-
* @throws NotFoundError if the proof is not found.
|
|
485
|
-
*/
|
|
486
|
-
async get(id) {
|
|
487
|
-
core.Guards.stringValue(ImmutableProofService.CLASS_NAME, "id", id);
|
|
488
|
-
const urnParsed = core.Urn.fromValidString(id);
|
|
489
|
-
if (urnParsed.namespaceIdentifier() !== ImmutableProofService._NAMESPACE) {
|
|
490
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "namespaceMismatch", {
|
|
491
|
-
namespace: ImmutableProofService._NAMESPACE,
|
|
492
|
-
id
|
|
493
|
-
});
|
|
494
|
-
}
|
|
495
|
-
try {
|
|
496
|
-
const { immutableProof } = await this.internalGet(id, false);
|
|
497
|
-
return dataJsonLd.JsonLdProcessor.compact(immutableProof, immutableProof["@context"]);
|
|
498
|
-
}
|
|
499
|
-
catch (error) {
|
|
500
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "getFailed", undefined, error);
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Verify a proof.
|
|
505
|
-
* @param id The id of the proof to verify.
|
|
506
|
-
* @returns The result of the verification and any failures.
|
|
507
|
-
* @throws NotFoundError if the proof is not found.
|
|
508
|
-
*/
|
|
509
|
-
async verify(id) {
|
|
510
|
-
core.Guards.stringValue(ImmutableProofService.CLASS_NAME, "id", id);
|
|
511
|
-
const urnParsed = core.Urn.fromValidString(id);
|
|
512
|
-
if (urnParsed.namespaceIdentifier() !== ImmutableProofService._NAMESPACE) {
|
|
513
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "namespaceMismatch", {
|
|
514
|
-
namespace: ImmutableProofService._NAMESPACE,
|
|
515
|
-
id
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
try {
|
|
519
|
-
const { verified, failure } = await this.internalGet(id, true);
|
|
520
|
-
return {
|
|
521
|
-
"@context": immutableProofModels.ImmutableProofContexts.ContextRoot,
|
|
522
|
-
type: immutableProofModels.ImmutableProofTypes.ImmutableProofVerification,
|
|
523
|
-
verified,
|
|
524
|
-
failure
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
catch (error) {
|
|
528
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "verifyFailed", undefined, error);
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* Remove the verifiable storage for the proof.
|
|
533
|
-
* @param id The id of the proof to remove the storage from.
|
|
534
|
-
* @param nodeIdentity The node identity to use for vault operations.
|
|
535
|
-
* @returns Nothing.
|
|
536
|
-
* @throws NotFoundError if the proof is not found.
|
|
537
|
-
*/
|
|
538
|
-
async removeVerifiable(id, nodeIdentity) {
|
|
539
|
-
core.Guards.stringValue(ImmutableProofService.CLASS_NAME, "id", id);
|
|
540
|
-
core.Guards.stringValue(ImmutableProofService.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
|
541
|
-
const urnParsed = core.Urn.fromValidString(id);
|
|
542
|
-
if (urnParsed.namespaceIdentifier() !== ImmutableProofService._NAMESPACE) {
|
|
543
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "namespaceMismatch", {
|
|
544
|
-
namespace: ImmutableProofService._NAMESPACE,
|
|
545
|
-
id
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
try {
|
|
549
|
-
const streamId = urnParsed.namespaceSpecific(0);
|
|
550
|
-
const streamEntity = await this._proofStorage.get(streamId);
|
|
551
|
-
if (core.Is.empty(streamEntity)) {
|
|
552
|
-
throw new core.NotFoundError(ImmutableProofService.CLASS_NAME, "proofNotFound", id);
|
|
553
|
-
}
|
|
554
|
-
if (core.Is.stringValue(streamEntity.verifiableStorageId)) {
|
|
555
|
-
await this._verifiableStorage.remove(nodeIdentity, streamEntity.verifiableStorageId);
|
|
556
|
-
delete streamEntity.verifiableStorageId;
|
|
557
|
-
await this._proofStorage.set(streamEntity);
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
catch (error) {
|
|
561
|
-
throw new core.GeneralError(ImmutableProofService.CLASS_NAME, "removeVerifiableFailed", undefined, error);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Calculate the object hash.
|
|
566
|
-
* @param object The entry to calculate the hash for.
|
|
567
|
-
* @returns The hash.
|
|
568
|
-
* @internal
|
|
569
|
-
*/
|
|
570
|
-
calculateDocumentHash(nodeObject) {
|
|
571
|
-
return `sha256:${core.Converter.bytesToBase64(crypto.Sha256.sum256(core.ObjectHelper.toBytes(core.JsonHelper.canonicalize(nodeObject))))}`;
|
|
572
|
-
}
|
|
573
|
-
/**
|
|
574
|
-
* Map the stream entity to a model.
|
|
575
|
-
* @param proofEntity The stream entity.
|
|
576
|
-
* @returns The model.
|
|
577
|
-
* @internal
|
|
578
|
-
*/
|
|
579
|
-
proofEntityToJsonLd(proofEntity) {
|
|
580
|
-
const jsonLd = {
|
|
581
|
-
"@context": [immutableProofModels.ImmutableProofContexts.ContextRoot, immutableProofModels.ImmutableProofContexts.ContextRootCommon],
|
|
582
|
-
type: immutableProofModels.ImmutableProofTypes.ImmutableProof,
|
|
583
|
-
id: proofEntity.id,
|
|
584
|
-
nodeIdentity: proofEntity.nodeIdentity,
|
|
585
|
-
userIdentity: proofEntity.userIdentity,
|
|
586
|
-
proofObjectId: proofEntity.proofObjectId,
|
|
587
|
-
proofObjectHash: proofEntity.proofObjectHash,
|
|
588
|
-
verifiableStorageId: proofEntity.verifiableStorageId
|
|
589
|
-
};
|
|
590
|
-
return jsonLd;
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Process a proof.
|
|
594
|
-
* @param proofEntity The proof entity to process.
|
|
595
|
-
* @internal
|
|
596
|
-
*/
|
|
597
|
-
async finaliseTask(task) {
|
|
598
|
-
if (task.status === backgroundTaskModels.TaskStatus.Success && core.Is.object(task.payload) && core.Is.object(task.result)) {
|
|
599
|
-
const proofEntity = await this._proofStorage.get(task.payload.proofId);
|
|
600
|
-
if (core.Is.object(proofEntity)) {
|
|
601
|
-
const immutableProof = this.proofEntityToJsonLd(proofEntity);
|
|
602
|
-
// As we are adding the proof to the data we update its context
|
|
603
|
-
immutableProof["@context"] = dataJsonLd.JsonLdProcessor.combineContexts([immutableProofModels.ImmutableProofContexts.ContextRoot, immutableProofModels.ImmutableProofContexts.ContextRootCommon], task.result.proof["@context"]);
|
|
604
|
-
immutableProof.proof = task.result.proof;
|
|
605
|
-
core.ObjectHelper.propertyDelete(immutableProof.proof, "@context");
|
|
606
|
-
if (core.Is.stringValue(immutableProof.proof.created)) {
|
|
607
|
-
proofEntity.dateCreated = immutableProof.proof.created;
|
|
608
|
-
}
|
|
609
|
-
const compacted = await dataJsonLd.JsonLdProcessor.compact(immutableProof, immutableProof["@context"]);
|
|
610
|
-
const verifiableCreateResult = await this._verifiableStorage.create(proofEntity.nodeIdentity, core.ObjectHelper.toBytes(compacted));
|
|
611
|
-
proofEntity.verifiableStorageId = verifiableCreateResult.id;
|
|
612
|
-
await this._proofStorage.set(proofEntity);
|
|
613
|
-
await this._eventBusComponent?.publish(immutableProofModels.ImmutableProofTopics.ProofCreated, { id: new core.Urn(ImmutableProofService._NAMESPACE, task.payload.proofId).toString() });
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* Verify a proof.
|
|
619
|
-
* @param id The id of the proof to verify.
|
|
620
|
-
* @param verify Validate the proof.
|
|
621
|
-
* @returns The result of the verification and any failures.
|
|
622
|
-
* @throws NotFoundError if the proof is not found.
|
|
623
|
-
* @internal
|
|
624
|
-
*/
|
|
625
|
-
async internalGet(id, verify) {
|
|
626
|
-
const urnParsed = core.Urn.fromValidString(id);
|
|
627
|
-
const proofId = urnParsed.namespaceSpecific(0);
|
|
628
|
-
const proofEntity = await this._proofStorage.get(proofId);
|
|
629
|
-
if (core.Is.empty(proofEntity)) {
|
|
630
|
-
throw new core.NotFoundError(ImmutableProofService.CLASS_NAME, "proofNotFound", id);
|
|
631
|
-
}
|
|
632
|
-
let proofJsonLd = this.proofEntityToJsonLd(proofEntity);
|
|
633
|
-
let verified = false;
|
|
634
|
-
let failure = immutableProofModels.ImmutableProofFailure.NotIssued;
|
|
635
|
-
if (core.Is.stringValue(proofEntity.verifiableStorageId)) {
|
|
636
|
-
failure = immutableProofModels.ImmutableProofFailure.ProofMissing;
|
|
637
|
-
const immutableResult = await this._verifiableStorage.get(proofEntity.verifiableStorageId);
|
|
638
|
-
if (core.Is.uint8Array(immutableResult.data)) {
|
|
639
|
-
proofJsonLd = core.ObjectHelper.fromBytes(immutableResult.data);
|
|
640
|
-
const unsecureDocument = core.ObjectHelper.clone(proofJsonLd);
|
|
641
|
-
proofJsonLd.immutableReceipt = immutableResult.receipt;
|
|
642
|
-
proofJsonLd.verifiableStorageId = proofEntity.verifiableStorageId;
|
|
643
|
-
// As we are adding the receipt to the data we update the JSON-LD context
|
|
644
|
-
const receiptContext = immutableResult.receipt["@context"];
|
|
645
|
-
if (!core.Is.empty(receiptContext)) {
|
|
646
|
-
proofJsonLd["@context"] = dataJsonLd.JsonLdProcessor.combineContexts(proofJsonLd["@context"], receiptContext);
|
|
647
|
-
}
|
|
648
|
-
if (verify && core.Is.object(proofJsonLd.proof)) {
|
|
649
|
-
if (proofJsonLd.proof.cryptosuite !== standardsW3cDid.DidCryptoSuites.EdDSAJcs2022) {
|
|
650
|
-
failure = immutableProofModels.ImmutableProofFailure.CryptoSuiteMismatch;
|
|
651
|
-
}
|
|
652
|
-
else if (proofJsonLd.proof.type !== standardsW3cDid.ProofTypes.DataIntegrityProof) {
|
|
653
|
-
failure = immutableProofModels.ImmutableProofFailure.ProofTypeMismatch;
|
|
654
|
-
}
|
|
655
|
-
else {
|
|
656
|
-
const isVerified = await this._identityConnector.verifyProof(unsecureDocument, proofJsonLd.proof);
|
|
657
|
-
if (isVerified) {
|
|
658
|
-
verified = true;
|
|
659
|
-
failure = undefined;
|
|
660
|
-
}
|
|
661
|
-
else {
|
|
662
|
-
failure = immutableProofModels.ImmutableProofFailure.SignatureMismatch;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
return {
|
|
669
|
-
immutableProof: proofJsonLd,
|
|
670
|
-
verified,
|
|
671
|
-
failure
|
|
672
|
-
};
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
const restEntryPoints = [
|
|
677
|
-
{
|
|
678
|
-
name: "immutable-proof",
|
|
679
|
-
defaultBaseRoute: "immutable-proof",
|
|
680
|
-
tags: tagsImmutableProof,
|
|
681
|
-
generateRoutes: generateRestRoutesImmutableProof
|
|
682
|
-
}
|
|
683
|
-
];
|
|
684
|
-
|
|
685
|
-
// Copyright 2024 IOTA Stiftung.
|
|
686
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
687
|
-
/**
|
|
688
|
-
* Initialize the schema for the immutable proof entity storage connector.
|
|
689
|
-
*/
|
|
690
|
-
function initSchema() {
|
|
691
|
-
entity.EntitySchemaFactory.register("ImmutableProof", () => entity.EntitySchemaHelper.getSchema(exports.ImmutableProof));
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
exports.ImmutableProofService = ImmutableProofService;
|
|
695
|
-
exports.generateRestRoutesImmutableProof = generateRestRoutesImmutableProof;
|
|
696
|
-
exports.immutableProofCreate = immutableProofCreate;
|
|
697
|
-
exports.immutableProofGet = immutableProofGet;
|
|
698
|
-
exports.immutableProofVerify = immutableProofVerify;
|
|
699
|
-
exports.initSchema = initSchema;
|
|
700
|
-
exports.restEntryPoints = restEntryPoints;
|
|
701
|
-
exports.tagsImmutableProof = tagsImmutableProof;
|