@twin.org/immutable-proof-service 0.0.2-next.3 → 0.0.3-next.10

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