@twin.org/immutable-proof-service 0.0.1-next.4 → 0.0.1-next.5

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.
@@ -6,7 +6,6 @@ var standardsW3cDid = require('@twin.org/standards-w3c-did');
6
6
  var web = require('@twin.org/web');
7
7
  var crypto = require('@twin.org/crypto');
8
8
  var dataJsonLd = require('@twin.org/data-json-ld');
9
- var dataSchemaOrg = require('@twin.org/data-schema-org');
10
9
  var entity = require('@twin.org/entity');
11
10
  var entityStorageModels = require('@twin.org/entity-storage-models');
12
11
  var identityModels = require('@twin.org/identity-models');
@@ -362,7 +361,6 @@ class ImmutableProofService {
362
361
  this._config = options?.config ?? {};
363
362
  this._assertionMethodId = this._config.assertionMethodId ?? "immutable-proof";
364
363
  this._proofConfigKeyId = this._config.proofConfigKeyId ?? "immutable-proof";
365
- dataSchemaOrg.SchemaOrgDataTypes.registerRedirects();
366
364
  this._processing = false;
367
365
  }
368
366
  /**
@@ -507,13 +505,9 @@ class ImmutableProofService {
507
505
  * @returns The model.
508
506
  * @internal
509
507
  */
510
- proofEntityToModel(proofEntity) {
508
+ proofEntityToJsonLd(proofEntity) {
511
509
  const model = {
512
- "@context": [
513
- immutableProofModels.ImmutableProofTypes.ContextRoot,
514
- dataSchemaOrg.SchemaOrgTypes.ContextRoot,
515
- standardsW3cDid.DidContexts.ContextVCDataIntegrity
516
- ],
510
+ "@context": immutableProofModels.ImmutableProofTypes.ContextRoot,
517
511
  type: immutableProofModels.ImmutableProofTypes.ImmutableProof,
518
512
  id: proofEntity.id,
519
513
  userIdentity: proofEntity.userIdentity,
@@ -558,13 +552,21 @@ class ImmutableProofService {
558
552
  remainingProofs = pendingProofs.entities.length;
559
553
  if (remainingProofs > 0) {
560
554
  const proofEntity = pendingProofs.entities[0];
561
- const immutableProof = this.proofEntityToModel(proofEntity);
555
+ const immutableProof = this.proofEntityToJsonLd(proofEntity);
562
556
  const hashData = await this.generateHashData(proofEntity.nodeIdentity, immutableProof);
557
+ // As we are adding the proof to the data we update its context
558
+ immutableProof["@context"] = [
559
+ immutableProofModels.ImmutableProofTypes.ContextRoot,
560
+ immutableStorageModels.ImmutableStorageTypes.ContextRoot,
561
+ standardsW3cDid.DidContexts.ContextVCDataIntegrity
562
+ ];
563
563
  immutableProof.proof = await this._identityConnector.createProof(proofEntity.nodeIdentity, `${proofEntity.nodeIdentity}#${this._assertionMethodId}`, hashData);
564
- proofEntity.dateCreated =
565
- immutableProof.proof.created ?? new Date(Date.now()).toISOString();
564
+ if (core.Is.stringValue(immutableProof.proof.created)) {
565
+ proofEntity.dateCreated = immutableProof.proof.created;
566
+ }
566
567
  const compacted = await dataJsonLd.JsonLdProcessor.compact(immutableProof, immutableProof["@context"]);
567
- proofEntity.immutableStorageId = await this._immutableStorage.store(proofEntity.nodeIdentity, core.ObjectHelper.toBytes(compacted));
568
+ const immutableStoreResult = await this._immutableStorage.store(proofEntity.nodeIdentity, core.ObjectHelper.toBytes(compacted));
569
+ proofEntity.immutableStorageId = immutableStoreResult.id;
568
570
  await this._proofStorage.set(proofEntity);
569
571
  remainingProofs--;
570
572
  }
@@ -592,14 +594,15 @@ class ImmutableProofService {
592
594
  if (core.Is.empty(proofEntity)) {
593
595
  throw new core.NotFoundError(this.CLASS_NAME, "proofNotFound", id);
594
596
  }
595
- let proofModel = await this.proofEntityToModel(proofEntity);
597
+ let proofModel = await this.proofEntityToJsonLd(proofEntity);
596
598
  let verified = false;
597
599
  let failure = immutableProofModels.ImmutableProofFailure.NotIssued;
598
600
  if (core.Is.stringValue(proofEntity.immutableStorageId)) {
599
601
  failure = immutableProofModels.ImmutableProofFailure.ProofMissing;
600
- const immutableData = await this._immutableStorage.get(proofEntity.immutableStorageId);
601
- if (core.Is.uint8Array(immutableData)) {
602
- proofModel = core.ObjectHelper.fromBytes(immutableData);
602
+ const immutableResult = await this._immutableStorage.get(proofEntity.immutableStorageId);
603
+ if (core.Is.uint8Array(immutableResult.data)) {
604
+ proofModel = core.ObjectHelper.fromBytes(immutableResult.data);
605
+ proofModel.immutableReceipt = immutableResult.receipt;
603
606
  if (core.Is.object(proofModel.proof) && core.Is.object(proofObject)) {
604
607
  if (proofModel.proof.cryptosuite !== standardsW3cDid.DidCryptoSuites.EdDSAJcs2022) {
605
608
  failure = immutableProofModels.ImmutableProofFailure.CryptoSuiteMismatch;
@@ -636,7 +639,11 @@ class ImmutableProofService {
636
639
  * @internal
637
640
  */
638
641
  async generateHashData(nodeIdentity, immutableProof) {
639
- const canonicalDocument = core.JsonHelper.canonicalize(core.ObjectHelper.omit(immutableProof, ["proof"]));
642
+ // We hash the data for the proof with the proof or immutable receipt for the proof
643
+ // without these objects we can simplify the context
644
+ const object = core.ObjectHelper.omit(immutableProof, ["proof", "immutableReceipt"]);
645
+ object["@context"] = immutableProofModels.ImmutableProofTypes.ContextRoot;
646
+ const canonicalDocument = core.JsonHelper.canonicalize(object);
640
647
  const proofConfigKey = await this._vaultConnector.getKey(`${nodeIdentity}/${this._proofConfigKeyId}`);
641
648
  const proofConfigHash = crypto.Sha256.sum256(proofConfigKey.privateKey);
642
649
  const transformedDocumentHash = crypto.Sha256.sum256(core.Converter.utf8ToBytes(canonicalDocument));
@@ -4,11 +4,10 @@ import { DidContexts, DidTypes, DidCryptoSuites } from '@twin.org/standards-w3c-
4
4
  import { HttpStatusCode, HeaderTypes, MimeTypes } from '@twin.org/web';
5
5
  import { Blake2b, Sha256 } from '@twin.org/crypto';
6
6
  import { JsonLdHelper, JsonLdProcessor } from '@twin.org/data-json-ld';
7
- import { SchemaOrgDataTypes, SchemaOrgTypes } from '@twin.org/data-schema-org';
8
7
  import { ComparisonOperator, SortDirection, property, entity, EntitySchemaFactory, EntitySchemaHelper } from '@twin.org/entity';
9
8
  import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
10
9
  import { IdentityConnectorFactory } from '@twin.org/identity-models';
11
- import { ImmutableStorageConnectorFactory } from '@twin.org/immutable-storage-models';
10
+ import { ImmutableStorageConnectorFactory, ImmutableStorageTypes } from '@twin.org/immutable-storage-models';
12
11
  import { VaultConnectorFactory } from '@twin.org/vault-models';
13
12
 
14
13
  /**
@@ -360,7 +359,6 @@ class ImmutableProofService {
360
359
  this._config = options?.config ?? {};
361
360
  this._assertionMethodId = this._config.assertionMethodId ?? "immutable-proof";
362
361
  this._proofConfigKeyId = this._config.proofConfigKeyId ?? "immutable-proof";
363
- SchemaOrgDataTypes.registerRedirects();
364
362
  this._processing = false;
365
363
  }
366
364
  /**
@@ -505,13 +503,9 @@ class ImmutableProofService {
505
503
  * @returns The model.
506
504
  * @internal
507
505
  */
508
- proofEntityToModel(proofEntity) {
506
+ proofEntityToJsonLd(proofEntity) {
509
507
  const model = {
510
- "@context": [
511
- ImmutableProofTypes.ContextRoot,
512
- SchemaOrgTypes.ContextRoot,
513
- DidContexts.ContextVCDataIntegrity
514
- ],
508
+ "@context": ImmutableProofTypes.ContextRoot,
515
509
  type: ImmutableProofTypes.ImmutableProof,
516
510
  id: proofEntity.id,
517
511
  userIdentity: proofEntity.userIdentity,
@@ -556,13 +550,21 @@ class ImmutableProofService {
556
550
  remainingProofs = pendingProofs.entities.length;
557
551
  if (remainingProofs > 0) {
558
552
  const proofEntity = pendingProofs.entities[0];
559
- const immutableProof = this.proofEntityToModel(proofEntity);
553
+ const immutableProof = this.proofEntityToJsonLd(proofEntity);
560
554
  const hashData = await this.generateHashData(proofEntity.nodeIdentity, immutableProof);
555
+ // As we are adding the proof to the data we update its context
556
+ immutableProof["@context"] = [
557
+ ImmutableProofTypes.ContextRoot,
558
+ ImmutableStorageTypes.ContextRoot,
559
+ DidContexts.ContextVCDataIntegrity
560
+ ];
561
561
  immutableProof.proof = await this._identityConnector.createProof(proofEntity.nodeIdentity, `${proofEntity.nodeIdentity}#${this._assertionMethodId}`, hashData);
562
- proofEntity.dateCreated =
563
- immutableProof.proof.created ?? new Date(Date.now()).toISOString();
562
+ if (Is.stringValue(immutableProof.proof.created)) {
563
+ proofEntity.dateCreated = immutableProof.proof.created;
564
+ }
564
565
  const compacted = await JsonLdProcessor.compact(immutableProof, immutableProof["@context"]);
565
- proofEntity.immutableStorageId = await this._immutableStorage.store(proofEntity.nodeIdentity, ObjectHelper.toBytes(compacted));
566
+ const immutableStoreResult = await this._immutableStorage.store(proofEntity.nodeIdentity, ObjectHelper.toBytes(compacted));
567
+ proofEntity.immutableStorageId = immutableStoreResult.id;
566
568
  await this._proofStorage.set(proofEntity);
567
569
  remainingProofs--;
568
570
  }
@@ -590,14 +592,15 @@ class ImmutableProofService {
590
592
  if (Is.empty(proofEntity)) {
591
593
  throw new NotFoundError(this.CLASS_NAME, "proofNotFound", id);
592
594
  }
593
- let proofModel = await this.proofEntityToModel(proofEntity);
595
+ let proofModel = await this.proofEntityToJsonLd(proofEntity);
594
596
  let verified = false;
595
597
  let failure = ImmutableProofFailure.NotIssued;
596
598
  if (Is.stringValue(proofEntity.immutableStorageId)) {
597
599
  failure = ImmutableProofFailure.ProofMissing;
598
- const immutableData = await this._immutableStorage.get(proofEntity.immutableStorageId);
599
- if (Is.uint8Array(immutableData)) {
600
- proofModel = ObjectHelper.fromBytes(immutableData);
600
+ const immutableResult = await this._immutableStorage.get(proofEntity.immutableStorageId);
601
+ if (Is.uint8Array(immutableResult.data)) {
602
+ proofModel = ObjectHelper.fromBytes(immutableResult.data);
603
+ proofModel.immutableReceipt = immutableResult.receipt;
601
604
  if (Is.object(proofModel.proof) && Is.object(proofObject)) {
602
605
  if (proofModel.proof.cryptosuite !== DidCryptoSuites.EdDSAJcs2022) {
603
606
  failure = ImmutableProofFailure.CryptoSuiteMismatch;
@@ -634,7 +637,11 @@ class ImmutableProofService {
634
637
  * @internal
635
638
  */
636
639
  async generateHashData(nodeIdentity, immutableProof) {
637
- const canonicalDocument = JsonHelper.canonicalize(ObjectHelper.omit(immutableProof, ["proof"]));
640
+ // We hash the data for the proof with the proof or immutable receipt for the proof
641
+ // without these objects we can simplify the context
642
+ const object = ObjectHelper.omit(immutableProof, ["proof", "immutableReceipt"]);
643
+ object["@context"] = ImmutableProofTypes.ContextRoot;
644
+ const canonicalDocument = JsonHelper.canonicalize(object);
638
645
  const proofConfigKey = await this._vaultConnector.getKey(`${nodeIdentity}/${this._proofConfigKeyId}`);
639
646
  const proofConfigHash = Sha256.sum256(proofConfigKey.privateKey);
640
647
  const transformedDocumentHash = Sha256.sum256(Converter.utf8ToBytes(canonicalDocument));
@@ -1,5 +1,5 @@
1
1
  import { type IJsonLdNodeObject } from "@twin.org/data-json-ld";
2
- import { type IImmutableProofVerification, type IImmutableProof, type IImmutableProofComponent } from "@twin.org/immutable-proof-models";
2
+ import { type IImmutableProof, type IImmutableProofComponent, type IImmutableProofVerification } from "@twin.org/immutable-proof-models";
3
3
  import type { IImmutableProofServiceConfig } from "./models/IImmutableProofServiceConfig";
4
4
  /**
5
5
  * Class for performing immutable proof operations.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/immutable-proof-service - Changelog
2
2
 
3
- ## v0.0.1-next.4
3
+ ## v0.0.1-next.5
4
4
 
5
5
  - Initial Release
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/immutable-proof-service",
3
- "version": "0.0.1-next.4",
3
+ "version": "0.0.1-next.5",
4
4
  "description": "Immutable proof contract implementation and REST endpoint definitions",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,7 @@
22
22
  "@twin.org/entity": "next",
23
23
  "@twin.org/entity-storage-models": "next",
24
24
  "@twin.org/identity-models": "next",
25
- "@twin.org/immutable-proof-models": "0.0.1-next.4",
25
+ "@twin.org/immutable-proof-models": "0.0.1-next.5",
26
26
  "@twin.org/immutable-storage-models": "next",
27
27
  "@twin.org/nameof": "next",
28
28
  "@twin.org/standards-w3c-did": "next",