Skip to content

Commit

Permalink
feat: add utils to extract v2/v3 template data
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenRen93 authored Sep 22, 2021
1 parent 27d013d commit 2fa596e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
106 changes: 97 additions & 9 deletions src/shared/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as utils from "../utils";
import { wrapDocument } from "../../..";
import { WrappedDocument } from "../../../shared/@types/document";
import { OpenAttestationDocument, WrappedDocument } from "../../../shared/@types/document";
import * as v2 from "../../../__generated__/schema.2.0";
import * as v3 from "../../../__generated__/schema.3.0";
import * as v2RawDocument from "../../../../test/fixtures/v2/raw-document.json";
import * as v3RawDocument from "../../../../test/fixtures/v3/raw-document.json";
import * as v2WrappedVerifiableDocument from "../../../../test/fixtures/v2/not-obfuscated-wrapped.json";
import * as v3WrappedVerifiableDocument from "../../../../test/fixtures/v3/not-obfuscated-wrapped.json";
import * as v2WrappedDidDocument from "../../../../test/fixtures/v2/did-wrapped.json";
import * as v3WrappedDidDocument from "../../../../test/fixtures/v3/did-wrapped.json";
import * as v2WrappedTransferableDocument from "../../../../test/fixtures/v2/wrapped-transferable-document.json";
import * as v3WrappedTransferableDocument from "../../../../test/fixtures/v3/wrapped-transferable-document.json";
import v2RawDocument from "../../../../test/fixtures/v2/raw-document.json";
import v3RawDocument from "../../../../test/fixtures/v3/raw-document.json";
import v2WrappedVerifiableDocument from "../../../../test/fixtures/v2/not-obfuscated-wrapped.json";
import v3WrappedVerifiableDocument from "../../../../test/fixtures/v3/not-obfuscated-wrapped.json";
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";

describe("Util Functions", () => {
describe("hashArray", () => {
Expand Down Expand Up @@ -363,4 +363,92 @@ describe("Util Functions", () => {
expect(utils.isDocumentRevokable(v3WrappedDidDocument)).toStrictEqual(false);
});
});

describe("getDocumentData", () => {
it("should return document data for v2 wrapped document", () => {
const v2WrappedDocument = v2WrappedVerifiableDocument as WrappedDocument<OpenAttestationDocument>;
expect(utils.getDocumentData(v2WrappedDocument)).toMatchInlineSnapshot(`
Object {
"$template": Object {
"name": "main",
"type": "EMBEDDED_RENDERER",
"url": "https://tutorial-renderer.openattestation.com",
},
"issuers": Array [
Object {
"documentStore": "0x8bA63EAB43342AAc3AdBB4B827b68Cf4aAE5Caca",
"identityProof": Object {
"location": "demo-tradetrust.openattestation.com",
"type": "DNS-TXT",
},
"name": "Demo Issuer",
},
],
"recipient": Object {
"name": "John Doe",
},
}
`);
});

it("should return document data for v3 wrapped document", () => {
const v3WrappedDocument = v3WrappedVerifiableDocument as WrappedDocument<OpenAttestationDocument>;
const result = utils.getDocumentData(v3WrappedDocument);
expect(result).not.toHaveProperty("proof");
expect(result).toMatchInlineSnapshot(`
Object {
"@context": Array [
"https://www.w3.org/2018/credentials/v1",
"https://schemata.openattestation.com/com/openattestation/1.0/DrivingLicenceCredential.json",
"https://schemata.openattestation.com/com/openattestation/1.0/OpenAttestation.v3.json",
"https://schemata.openattestation.com/com/openattestation/1.0/CustomContext.json",
],
"credentialSubject": Object {
"id": "some:thing:here",
},
"issuanceDate": "2010-01-01T20:20:20Z",
"issuer": Object {
"id": "https://www.openattestation.com",
"name": "Demo Issuer",
},
"openAttestationMetadata": Object {
"identityProof": Object {
"identifier": "demo-tradetrust.openattestation.com",
"type": "DNS-TXT",
},
"proof": Object {
"method": "DOCUMENT_STORE",
"type": "OpenAttestationProofMethod",
"value": "0x8bA63EAB43342AAc3AdBB4B827b68Cf4aAE5Caca",
},
"template": Object {
"name": "main",
"type": "EMBEDDED_RENDERER",
"url": "https://tutorial-renderer.openattestation.com",
},
},
"recipient": Object {
"name": "Mary Jane",
},
"type": Array [
"VerifiableCredential",
],
"version": "https://schema.openattestation.com/3.0/schema.json",
}
`);
});

test("should return error when document is not OpenAttestation document", async () => {
const document: any = {
randomData: {
randomProperty: "RandomField",
},
};
expect(() => utils.getDocumentData(document)).toThrow(
new Error(
"Unsupported document type: Only can retrieve document data for wrapped OpenAttestation v2 & v3 documents."
)
);
});
});
});
13 changes: 13 additions & 0 deletions src/shared/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WrappedDocument as WrappedDocumentV3 } from "../../3.0/types";
import { unsaltData } from "../../2.0/salt";
import { ErrorObject } from "ajv";
import { isRawV2Document, isRawV3Document, isWrappedV2Document, isWrappedV3Document } from "./guard";
import { OpenAttestationDocument, WrappedDocument } from "../@types/document";

export type Hash = string | Buffer;
type Extract<P> = P extends WrappedDocumentV2<infer T> ? T : never;
Expand Down Expand Up @@ -121,6 +122,18 @@ export const getTemplateURL = (document: any): string | undefined => {
}
};

export const getDocumentData = (document: WrappedDocument<OpenAttestationDocument>): OpenAttestationDocument => {
if (isWrappedV3Document(document)) {
const omit = (keys: any, obj: any): any =>
Object.fromEntries(Object.entries(obj).filter(([k]) => !keys.includes(k)));
return omit(["proof"], document);
} else if (isWrappedV2Document(document)) {
return getData(document);
} else {
throw "Unsupported document type: Only can retrieve document data for wrapped OpenAttestation v2 & v3 documents.";
}
};

export const isTransferableAsset = (document: any): boolean => {
return (
!!getData(document)?.issuers[0]?.tokenRegistry ||
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/v2/raw-document.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@
"location": "few-green-cat.sandbox.openattestation.com"
}
}
],
"id": "did:example:JOHN_DOE_DID",
"licenseNumber": "S1234567a",
"birthDate": "1977-02-22",
"name": "John Doe",
"class": [
{ "type": "3", "effectiveDate": "2010-01-01T19:23:24Z" },
{ "type": "3A", "effectiveDate": "2010-01-01T19:23:24Z" }
]
}

0 comments on commit 2fa596e

Please sign in to comment.