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

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,20 @@ 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
+ standardsW3cDid.DidContexts.ContextVCDataIntegrity
561
+ ];
563
562
  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();
563
+ if (core.Is.stringValue(immutableProof.proof.created)) {
564
+ proofEntity.dateCreated = immutableProof.proof.created;
565
+ }
566
566
  const compacted = await dataJsonLd.JsonLdProcessor.compact(immutableProof, immutableProof["@context"]);
567
- proofEntity.immutableStorageId = await this._immutableStorage.store(proofEntity.nodeIdentity, core.ObjectHelper.toBytes(compacted));
567
+ const immutableStoreResult = await this._immutableStorage.store(proofEntity.nodeIdentity, core.ObjectHelper.toBytes(compacted));
568
+ proofEntity.immutableStorageId = immutableStoreResult.id;
568
569
  await this._proofStorage.set(proofEntity);
569
570
  remainingProofs--;
570
571
  }
@@ -592,14 +593,19 @@ class ImmutableProofService {
592
593
  if (core.Is.empty(proofEntity)) {
593
594
  throw new core.NotFoundError(this.CLASS_NAME, "proofNotFound", id);
594
595
  }
595
- let proofModel = await this.proofEntityToModel(proofEntity);
596
+ let proofModel = await this.proofEntityToJsonLd(proofEntity);
596
597
  let verified = false;
597
598
  let failure = immutableProofModels.ImmutableProofFailure.NotIssued;
598
599
  if (core.Is.stringValue(proofEntity.immutableStorageId)) {
599
600
  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);
601
+ const immutableResult = await this._immutableStorage.get(proofEntity.immutableStorageId);
602
+ if (core.Is.uint8Array(immutableResult.data)) {
603
+ proofModel = core.ObjectHelper.fromBytes(immutableResult.data);
604
+ proofModel.immutableReceipt = immutableResult.receipt;
605
+ // As we are adding the receipt to the data we update its context
606
+ if (core.Is.array(proofModel["@context"])) {
607
+ proofModel["@context"].push(immutableStorageModels.ImmutableStorageTypes.ContextRoot);
608
+ }
603
609
  if (core.Is.object(proofModel.proof) && core.Is.object(proofObject)) {
604
610
  if (proofModel.proof.cryptosuite !== standardsW3cDid.DidCryptoSuites.EdDSAJcs2022) {
605
611
  failure = immutableProofModels.ImmutableProofFailure.CryptoSuiteMismatch;
@@ -636,7 +642,11 @@ class ImmutableProofService {
636
642
  * @internal
637
643
  */
638
644
  async generateHashData(nodeIdentity, immutableProof) {
639
- const canonicalDocument = core.JsonHelper.canonicalize(core.ObjectHelper.omit(immutableProof, ["proof"]));
645
+ // We hash the data for the proof with the proof or immutable receipt for the proof
646
+ // without these objects we can simplify the context
647
+ const object = core.ObjectHelper.omit(immutableProof, ["proof", "immutableReceipt"]);
648
+ object["@context"] = immutableProofModels.ImmutableProofTypes.ContextRoot;
649
+ const canonicalDocument = core.JsonHelper.canonicalize(object);
640
650
  const proofConfigKey = await this._vaultConnector.getKey(`${nodeIdentity}/${this._proofConfigKeyId}`);
641
651
  const proofConfigHash = crypto.Sha256.sum256(proofConfigKey.privateKey);
642
652
  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,20 @@ 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
+ DidContexts.ContextVCDataIntegrity
559
+ ];
561
560
  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();
561
+ if (Is.stringValue(immutableProof.proof.created)) {
562
+ proofEntity.dateCreated = immutableProof.proof.created;
563
+ }
564
564
  const compacted = await JsonLdProcessor.compact(immutableProof, immutableProof["@context"]);
565
- proofEntity.immutableStorageId = await this._immutableStorage.store(proofEntity.nodeIdentity, ObjectHelper.toBytes(compacted));
565
+ const immutableStoreResult = await this._immutableStorage.store(proofEntity.nodeIdentity, ObjectHelper.toBytes(compacted));
566
+ proofEntity.immutableStorageId = immutableStoreResult.id;
566
567
  await this._proofStorage.set(proofEntity);
567
568
  remainingProofs--;
568
569
  }
@@ -590,14 +591,19 @@ class ImmutableProofService {
590
591
  if (Is.empty(proofEntity)) {
591
592
  throw new NotFoundError(this.CLASS_NAME, "proofNotFound", id);
592
593
  }
593
- let proofModel = await this.proofEntityToModel(proofEntity);
594
+ let proofModel = await this.proofEntityToJsonLd(proofEntity);
594
595
  let verified = false;
595
596
  let failure = ImmutableProofFailure.NotIssued;
596
597
  if (Is.stringValue(proofEntity.immutableStorageId)) {
597
598
  failure = ImmutableProofFailure.ProofMissing;
598
- const immutableData = await this._immutableStorage.get(proofEntity.immutableStorageId);
599
- if (Is.uint8Array(immutableData)) {
600
- proofModel = ObjectHelper.fromBytes(immutableData);
599
+ const immutableResult = await this._immutableStorage.get(proofEntity.immutableStorageId);
600
+ if (Is.uint8Array(immutableResult.data)) {
601
+ proofModel = ObjectHelper.fromBytes(immutableResult.data);
602
+ proofModel.immutableReceipt = immutableResult.receipt;
603
+ // As we are adding the receipt to the data we update its context
604
+ if (Is.array(proofModel["@context"])) {
605
+ proofModel["@context"].push(ImmutableStorageTypes.ContextRoot);
606
+ }
601
607
  if (Is.object(proofModel.proof) && Is.object(proofObject)) {
602
608
  if (proofModel.proof.cryptosuite !== DidCryptoSuites.EdDSAJcs2022) {
603
609
  failure = ImmutableProofFailure.CryptoSuiteMismatch;
@@ -634,7 +640,11 @@ class ImmutableProofService {
634
640
  * @internal
635
641
  */
636
642
  async generateHashData(nodeIdentity, immutableProof) {
637
- const canonicalDocument = JsonHelper.canonicalize(ObjectHelper.omit(immutableProof, ["proof"]));
643
+ // We hash the data for the proof with the proof or immutable receipt for the proof
644
+ // without these objects we can simplify the context
645
+ const object = ObjectHelper.omit(immutableProof, ["proof", "immutableReceipt"]);
646
+ object["@context"] = ImmutableProofTypes.ContextRoot;
647
+ const canonicalDocument = JsonHelper.canonicalize(object);
638
648
  const proofConfigKey = await this._vaultConnector.getKey(`${nodeIdentity}/${this._proofConfigKeyId}`);
639
649
  const proofConfigHash = Sha256.sum256(proofConfigKey.privateKey);
640
650
  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.6
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.6",
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.6",
26
26
  "@twin.org/immutable-storage-models": "next",
27
27
  "@twin.org/nameof": "next",
28
28
  "@twin.org/standards-w3c-did": "next",