diff --git a/src/shared/utils/__tests__/utils.test.ts b/src/shared/utils/__tests__/utils.test.ts index e991eaa0..a657c01a 100644 --- a/src/shared/utils/__tests__/utils.test.ts +++ b/src/shared/utils/__tests__/utils.test.ts @@ -1,4 +1,5 @@ import * as utils from "../utils"; +import { Wallet } from "ethers"; import { wrapDocument } from "../../.."; import { OpenAttestationDocument, WrappedDocument } from "../../../shared/@types/document"; import * as v2 from "../../../__generated__/schema.2.0"; @@ -11,6 +12,14 @@ import v2WrappedDidDocument from "../../../../test/fixtures/v2/did-wrapped.json" import v3WrappedDidDocument from "../../../../test/fixtures/v3/did-wrapped.json"; import v2WrappedTransferableDocument from "../../../../test/fixtures/v2/wrapped-transferable-document.json"; import v3WrappedTransferableDocument from "../../../../test/fixtures/v3/wrapped-transferable-document.json"; +import v2VerifiableDocumentForm from "../../../../test/fixtures/v2/verifiable-document-form.json"; +import v2TransferableRecordForm from "../../../../test/fixtures/v2/transferable-record-form.json"; +import v2DNSDIDForm from "../../../../test/fixtures/v2/dns-did-form.json"; +import v2DIDForm from "../../../../test/fixtures/v2/did-form.json"; +import v3VerifiableDocumentForm from "../../../../test/fixtures/v3/verifiable-document-form.json"; +import v3TransferableRecordForm from "../../../../test/fixtures/v3/transferable-record-form.json"; +import v3DNSDIDForm from "../../../../test/fixtures/v3/dns-did-form.json"; +import v3DIDForm from "../../../../test/fixtures/v3/did-form.json"; describe("Util Functions", () => { describe("hashArray", () => { @@ -451,4 +460,145 @@ describe("Util Functions", () => { ); }); }); + + describe("updateFormV2", () => { + const wallet = Wallet.fromMnemonic( + "tourist quality multiply denial diary height funny calm disease buddy speed gold" + ); + const walletString = JSON.stringify(wallet); + + it("should update the form from a verifiable document correctly", () => { + const updatedForm = utils.updateFormV2({ + wallet: { type: "", encryptedJson: walletString }, + form: v2VerifiableDocumentForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.issuers[0].identityProof.location).toStrictEqual("VerifiableDNS.com"); + expect(updatedForm.defaults.issuers[0].documentStore).toStrictEqual("0xabcDocumentStore"); + }); + + it("should update the form from a DID verifiable document correctly", () => { + const updatedForm = utils.updateFormV2({ + wallet: { type: "", encryptedJson: walletString }, + form: v2DIDForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + + expect(updatedForm.defaults.issuers[0].id).toStrictEqual("did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C"); + expect(updatedForm.defaults.issuers[0].identityProof.key).toStrictEqual( + "did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C#controller" + ); + expect(updatedForm.defaults.issuers[0].identityProof.location).toStrictEqual(undefined); + }); + + it("should update the form from a DNS-DID verifiable document correctly", () => { + const updatedForm = utils.updateFormV2({ + wallet: { type: "", encryptedJson: walletString }, + form: v2DNSDIDForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.issuers[0].id).toStrictEqual("did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C"); + expect(updatedForm.defaults.issuers[0].identityProof.key).toStrictEqual( + "did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C#controller" + ); + expect(updatedForm.defaults.issuers[0].identityProof.location).toStrictEqual("DNSDID.com"); + }); + + it("should update the form from a transferable document correctly", () => { + const updatedForm = utils.updateFormV2({ + wallet: { type: "", encryptedJson: walletString }, + form: v2TransferableRecordForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.issuers[0].identityProof.location).toStrictEqual("TransferableDNS.com"); + expect(updatedForm.defaults.issuers[0].tokenRegistry).toStrictEqual("0xabcTokenRegistry"); + }); + }); + + describe("updateFormV3", () => { + const wallet = Wallet.fromMnemonic( + "tourist quality multiply denial diary height funny calm disease buddy speed gold" + ); + const walletString = JSON.stringify(wallet); + + it("should update the form from a verifiable document correctly", () => { + const updatedForm = utils.updateFormV3({ + wallet: { type: "", encryptedJson: walletString }, + form: v3VerifiableDocumentForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.openAttestationMetadata.identityProof.identifier).toStrictEqual("VerifiableDNS.com"); + expect(updatedForm.defaults.openAttestationMetadata.proof.value).toStrictEqual("0xabcDocumentStore"); + }); + + it("should update the form from a DID verifiable document correctly", () => { + const updatedForm = utils.updateFormV3({ + wallet: { type: "", encryptedJson: walletString }, + form: v3DIDForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.openAttestationMetadata.proof.value).toStrictEqual( + "did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C" + ); + expect(updatedForm.defaults.openAttestationMetadata.identityProof.identifier).toStrictEqual( + "did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C" + ); + }); + + it("should update the form from a DNS-DID verifiable document correctly", () => { + const updatedForm = utils.updateFormV3({ + wallet: { type: "", encryptedJson: walletString }, + form: v3DNSDIDForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.openAttestationMetadata.proof.value).toStrictEqual( + "did:ethr:0x0x906FB815De8976b1e38D9a4C1014a3acE16Ce53C" + ); + expect(updatedForm.defaults.openAttestationMetadata.identityProof.identifier).toStrictEqual("DNSDID.com"); + }); + + it("should update the form from a transferable document correctly", () => { + const updatedForm = utils.updateFormV3({ + wallet: { type: "", encryptedJson: walletString }, + form: v3TransferableRecordForm, + documentStoreAddress: "0xabcDocumentStore", + tokenRegistryAddress: "0xabcTokenRegistry", + dnsVerifiable: "VerifiableDNS.com", + dnsDid: "DNSDID.com", + dnsTransferableRecord: "TransferableDNS.com", + }); + expect(updatedForm.defaults.openAttestationMetadata.identityProof.identifier).toStrictEqual( + "TransferableDNS.com" + ); + expect(updatedForm.defaults.openAttestationMetadata.proof.value).toStrictEqual("0xabcTokenRegistry"); + }); + }); }); diff --git a/src/shared/utils/utils.ts b/src/shared/utils/utils.ts index 32f0191f..bab43e29 100644 --- a/src/shared/utils/utils.ts +++ b/src/shared/utils/utils.ts @@ -217,3 +217,97 @@ export const getObfuscatedData = ( "Unsupported document type: Can only retrieve obfuscated data from wrapped OpenAttestation v2 & v3 documents." ); }; + +interface UpdateForm { + wallet: { type: string; encryptedJson: string }; + form: any; + documentStoreAddress: string; + tokenRegistryAddress: string; + dnsVerifiable: string; + dnsDid: string; + dnsTransferableRecord: string; +} + +export const updateFormV2 = ({ + wallet, + form, + documentStoreAddress, + tokenRegistryAddress, + dnsVerifiable, + dnsDid, + dnsTransferableRecord, +}: UpdateForm) => { + const { encryptedJson } = wallet; + const { address } = JSON.parse(encryptedJson); + + if (form.type === "VERIFIABLE_DOCUMENT") { + const updatedIssuers = form.defaults.issuers.map((issuer: any) => { + if (issuer.identityProof) { + if (issuer.identityProof.type === "DNS-TXT") { + issuer.documentStore = documentStoreAddress; + issuer.identityProof.location = dnsVerifiable; + } else if (issuer.identityProof.type === "DNS-DID" || issuer.identityProof.type === "DID") { + issuer.id = `did:ethr:0x${address}`; + issuer.identityProof.key = `did:ethr:0x${address}#controller`; + + if (issuer.identityProof.type === "DNS-DID") { + issuer.identityProof.location = dnsDid; + } else if (issuer.identityProof.type === "DID") { + delete issuer.identityProof.location; + } + } + } + return issuer; + }); + form.defaults.issuers = updatedIssuers; + } + + if (form.type === "TRANSFERABLE_RECORD") { + const updatedIssuers = form.defaults.issuers.map((issuer: any) => { + issuer.tokenRegistry = tokenRegistryAddress; + if (issuer.identityProof) { + issuer.identityProof.location = dnsTransferableRecord; + } + return issuer; + }); + form.defaults.issuers = updatedIssuers; + } + + return form; +}; + +export const updateFormV3 = ({ + wallet, + form, + documentStoreAddress, + tokenRegistryAddress, + dnsVerifiable, + dnsDid, + dnsTransferableRecord, +}: UpdateForm) => { + const { encryptedJson } = wallet; + const { address } = JSON.parse(encryptedJson); + + if (form.type === "VERIFIABLE_DOCUMENT") { + if (form.defaults.openAttestationMetadata.proof.method === "DOCUMENT_STORE") { + form.defaults.openAttestationMetadata.proof.value = documentStoreAddress; + } else if (form.defaults.openAttestationMetadata.proof.method === "DID") { + form.defaults.openAttestationMetadata.proof.value = `did:ethr:0x${address}`; + } + + if (form.defaults.openAttestationMetadata.identityProof.type === "DNS-TXT") { + form.defaults.openAttestationMetadata.identityProof.identifier = dnsVerifiable; + } else if (form.defaults.openAttestationMetadata.identityProof.type === "DNS-DID") { + form.defaults.openAttestationMetadata.identityProof.identifier = dnsDid; + } else if (form.defaults.openAttestationMetadata.identityProof.type === "DID") { + form.defaults.openAttestationMetadata.identityProof.identifier = `did:ethr:0x${address}`; + } + } + + if (form.type === "TRANSFERABLE_RECORD") { + form.defaults.openAttestationMetadata.proof.value = tokenRegistryAddress; + form.defaults.openAttestationMetadata.identityProof.identifier = dnsTransferableRecord; + } + + return form; +}; diff --git a/test/fixtures/v2/did-form.json b/test/fixtures/v2/did-form.json new file mode 100644 index 00000000..dad76841 --- /dev/null +++ b/test/fixtures/v2/did-form.json @@ -0,0 +1,25 @@ +{ + "name": "A DID Verifiable Document Form", + "type": "VERIFIABLE_DOCUMENT", + "defaults": { + "$template": { + "type": "EMBEDDED_RENDERER", + "name": "CHAFTA_COO", + "url": "https://generic-templates.openattestation.io" + }, + "issuers": [ + { + "id": "did:ethr:0x123123123123123123123123123123123123", + "name": "DEMO DID", + "revocation": { + "type": "NONE" + }, + "identityProof": { + "type": "DID", + "key": "did:ethr:0x123123123123123123123123123123123123#controller", + "location": "demo-oa.openattestation.com" + } + } + ] + } +} diff --git a/test/fixtures/v2/dns-did-form.json b/test/fixtures/v2/dns-did-form.json new file mode 100644 index 00000000..da9e18e9 --- /dev/null +++ b/test/fixtures/v2/dns-did-form.json @@ -0,0 +1,25 @@ +{ + "name": "A DNS-DID Verifiable Document Form", + "type": "VERIFIABLE_DOCUMENT", + "defaults": { + "$template": { + "type": "EMBEDDED_RENDERER", + "name": "CHAFTA_COO", + "url": "https://generic-templates.openattestation.io" + }, + "issuers": [ + { + "id": "did:ethr:0x123123123123123123123123123123123123", + "name": "DEMO DID", + "revocation": { + "type": "NONE" + }, + "identityProof": { + "type": "DNS-DID", + "key": "did:ethr:0x123123123123123123123123123123123123#controller", + "location": "demo-oa.openattestation.com" + } + } + ] + } +} diff --git a/test/fixtures/v2/transferable-record-form.json b/test/fixtures/v2/transferable-record-form.json new file mode 100644 index 00000000..d8a543e0 --- /dev/null +++ b/test/fixtures/v2/transferable-record-form.json @@ -0,0 +1,21 @@ +{ + "name": "A Transferable Record Form", + "type": "TRANSFERABLE_RECORD", + "defaults": { + "$template": { + "type": "EMBEDDED_RENDERER", + "name": "SAMPLE_BOL", + "url": "https://generic-templates.openattestation.io" + }, + "issuers": [ + { + "identityProof": { + "type": "DNS-TXT", + "location": "demo-oa.openattestation.com" + }, + "name": "DEMO TOKEN REGISTRY", + "tokenRegistry": "0x123123123123123123123123123" + } + ] + } +} diff --git a/test/fixtures/v2/verifiable-document-form.json b/test/fixtures/v2/verifiable-document-form.json new file mode 100644 index 00000000..add72ae3 --- /dev/null +++ b/test/fixtures/v2/verifiable-document-form.json @@ -0,0 +1,21 @@ +{ + "name": "A Verifiable Document Form", + "type": "VERIFIABLE_DOCUMENT", + "defaults": { + "$template": { + "type": "EMBEDDED_RENDERER", + "name": "SIMPLE_COO", + "url": "https://generic-templates.openattestation.io" + }, + "issuers": [ + { + "identityProof": { + "type": "DNS-TXT", + "location": "demo-oa.openattestation.com" + }, + "name": "DEMO DOCUMENT STORE", + "documentStore": "0x123123123123123123123123" + } + ] + } +} diff --git a/test/fixtures/v3/did-form.json b/test/fixtures/v3/did-form.json new file mode 100644 index 00000000..247d705f --- /dev/null +++ b/test/fixtures/v3/did-form.json @@ -0,0 +1,38 @@ +{ + "name": "A V3 DID Verifiable Document Form", + "type": "VERIFIABLE_DOCUMENT", + "defaults": { + "version": "https://schema.openattestation.com/3.0/schema.json", + "@context": [ + "https://www.w3.org/2018/credentials/v1", + "https://schemata.openattestation.com/com/openattestation/1.0/OpenAttestation.v3.json" + ], + "type": ["VerifiableCredential", "OpenAttestationCredential"], + "issuanceDate": "2010-01-01T19:23:24Z", + "openAttestationMetadata": { + "template": { + "type": "EMBEDDED_RENDERER", + "name": "CHAFTA_COO", + "url": "https://generic-templates.oa.io" + }, + "proof": { + "type": "OpenAttestationProofMethod", + "method": "DID", + "value": "did:ethr:0x123", + "revocation": { + "type": "NONE" + } + }, + "identityProof": { + "type": "DID", + "identifier": "demo-oa.openattestation.com" + } + }, + "credentialSubject": {}, + "issuer": { + "id": "https://example.com", + "name": "DEMO DID", + "type": "OpenAttestationIssuer" + } + } +} diff --git a/test/fixtures/v3/dns-did-form.json b/test/fixtures/v3/dns-did-form.json new file mode 100644 index 00000000..a0a6465b --- /dev/null +++ b/test/fixtures/v3/dns-did-form.json @@ -0,0 +1,38 @@ +{ + "name": "A V3 DNS-DID Verifiable Document Form", + "type": "VERIFIABLE_DOCUMENT", + "defaults": { + "version": "https://schema.openattestation.com/3.0/schema.json", + "@context": [ + "https://www.w3.org/2018/credentials/v1", + "https://schemata.openattestation.com/com/openattestation/1.0/OpenAttestation.v3.json" + ], + "type": ["VerifiableCredential", "OpenAttestationCredential"], + "issuanceDate": "2010-01-01T19:23:24Z", + "openAttestationMetadata": { + "template": { + "type": "EMBEDDED_RENDERER", + "name": "CHAFTA_COO", + "url": "https://generic-templates.oa.io" + }, + "proof": { + "type": "OpenAttestationProofMethod", + "method": "DID", + "value": "did:ethr:0x123", + "revocation": { + "type": "NONE" + } + }, + "identityProof": { + "type": "DNS-DID", + "identifier": "demo-oa.openattestation.com" + } + }, + "credentialSubject": {}, + "issuer": { + "id": "https://example.com", + "name": "DEMO DID", + "type": "OpenAttestationIssuer" + } + } +} diff --git a/test/fixtures/v3/transferable-record-form.json b/test/fixtures/v3/transferable-record-form.json new file mode 100644 index 00000000..d75e7895 --- /dev/null +++ b/test/fixtures/v3/transferable-record-form.json @@ -0,0 +1,35 @@ +{ + "name": "A V3 Transferable Record Form", + "type": "TRANSFERABLE_RECORD", + "defaults": { + "version": "https://schema.openattestation.com/3.0/schema.json", + "@context": [ + "https://www.w3.org/2018/credentials/v1", + "https://schemata.openattestation.com/com/openattestation/1.0/OpenAttestation.v3.json" + ], + "type": ["VerifiableCredential", "OpenAttestationCredential"], + "issuanceDate": "2010-01-01T19:23:24Z", + "openAttestationMetadata": { + "template": { + "type": "EMBEDDED_RENDERER", + "name": "BILL_OF_LADING", + "url": "https://generic-templates.oa.io" + }, + "proof": { + "type": "OpenAttestationProofMethod", + "method": "TOKEN_REGISTRY", + "value": "0x72d9a82203Ef9177239A5E3cB7A8FB9a78D04f17" + }, + "identityProof": { + "type": "DNS-TXT", + "identifier": "demo-oa.openattestation.com" + } + }, + "credentialSubject": {}, + "issuer": { + "id": "https://example.com", + "name": "DEMO TOKEN REGISTRY", + "type": "OpenAttestationIssuer" + } + } +} diff --git a/test/fixtures/v3/verifiable-document-form.json b/test/fixtures/v3/verifiable-document-form.json new file mode 100644 index 00000000..fb8d49a3 --- /dev/null +++ b/test/fixtures/v3/verifiable-document-form.json @@ -0,0 +1,35 @@ +{ + "name": "A V3 Verifiable Document Form", + "type": "VERIFIABLE_DOCUMENT", + "defaults": { + "version": "https://schema.openattestation.com/3.0/schema.json", + "@context": [ + "https://www.w3.org/2018/credentials/v1", + "https://schemata.openattestation.com/com/openattestation/1.0/OpenAttestation.v3.json" + ], + "type": ["VerifiableCredential", "OpenAttestationCredential"], + "issuanceDate": "2010-01-01T19:23:24Z", + "openAttestationMetadata": { + "template": { + "type": "EMBEDDED_RENDERER", + "name": "CHAFTA_COO", + "url": "https://generic-templates.oa.io" + }, + "proof": { + "type": "OpenAttestationProofMethod", + "method": "DOCUMENT_STORE", + "value": "0x8bA63EAB43342AAc3AdBB4B827b68Cf4aAE5Caca" + }, + "identityProof": { + "type": "DNS-TXT", + "identifier": "demo-oa.openattestation.com" + } + }, + "credentialSubject": {}, + "issuer": { + "id": "https://example.com", + "name": "DEMO DOCUMENT STORE", + "type": "OpenAttestationIssuer" + } + } +}