Skip to content

Commit

Permalink
fix: allow last field of an object to be obfuscated
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulis committed Aug 19, 2020
1 parent 1ba0450 commit a0c783f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/digest/digest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { get, omitBy, sortBy } from "lodash";
import { keccak256 } from "js-sha3";
import { flatten } from "../serialize/flatten";
import { SchematisedDocument, OpenAttestationDocument } from "../@types/document";
import { OpenAttestationDocument, SchematisedDocument } from "../@types/document";

const isKeyOrValueUndefined = (value: any, key: any) => value === undefined || key === undefined;
const isEmptyObject = (value: any) => typeof value === "object" && Object.keys(value).length === 0;

export const flattenHashArray = (data: any) => {
const flattenedData = omitBy(flatten(data), isKeyOrValueUndefined);
// remove values or keys if undefined and empty object (with no keys)
const flattenedData = omitBy(
flatten(data),
(value: any, key: any) => value === undefined || key === undefined || isEmptyObject(value)
);
return Object.keys(flattenedData).map(k => {
const obj: any = {};
obj[k] = flattenedData[k];
Expand Down
57 changes: 56 additions & 1 deletion test/e2e.v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
IdentityProofType as v2IdentityProofType,
OpenAttestationDocument as v2OpenAttestationDocument
} from "../src/__generated__/schemaV2";
import { wrapDocument, sign, ProofType } from "../src";
import { obfuscateDocument, ProofType, SchemaId, sign, validateSchema, verifySignature, wrapDocument } from "../src";
import { ethers } from "ethers";

const openAttestationDatav2: v2OpenAttestationDocument & { foo: string } = {
Expand Down Expand Up @@ -62,4 +63,58 @@ describe("v2 E2E Test Scenarios", () => {
expect(wrappedDocument.signature.merkleRoot).toBe(wrappedDocument.signature.targetHash);
});
});
describe("obfuscation", () => {
test("obfuscate data when there is one field to obfuscate", async () => {
const newDocument = wrapDocument({ key1: "value1", ...openAttestationDatav2 }, { version: SchemaId.v2 });
const obfuscatedDocument = obfuscateDocument(newDocument, ["key1"]);

expect(verifySignature(obfuscatedDocument)).toBe(true);
expect(validateSchema(obfuscatedDocument)).toBe(true);
expect(obfuscatedDocument.data.key1).toBeUndefined();
});
test("obfuscate data when there are multiple fields to obfuscate", async () => {
const newDocument = wrapDocument(
{ key1: "value1", key2: "value2", key3: "value3", ...openAttestationDatav2 },
{ version: SchemaId.v2 }
);
const obfuscatedDocument = obfuscateDocument(newDocument, ["key2", "key3"]);

expect(verifySignature(obfuscatedDocument)).toBe(true);
expect(validateSchema(obfuscatedDocument)).toBe(true);
expect(obfuscatedDocument.data.key2).toBeUndefined();
expect(obfuscatedDocument.data.key3).toBeUndefined();
});
test("obfuscate data transistively", () => {
const newDocument = wrapDocument(
{ key1: "value1", key2: "value2", key3: "value3", ...openAttestationDatav2 },
{ version: SchemaId.v2 }
);
const intermediateDocument = obfuscateDocument(newDocument, ["key2"]);
const obfuscatedDocument = obfuscateDocument(intermediateDocument, ["key3"]);

const comparison = obfuscateDocument(intermediateDocument, ["key2", "key3"]);

expect(comparison).toEqual(obfuscatedDocument);
});

test("obfuscate data when the obfuscated field is the last element of an object", async () => {
const newDocument = wrapDocument(
{ key1: "value1", key2: { key22: "value22" }, ...openAttestationDatav2 },
{ version: SchemaId.v2 }
);
const firstObfuscatedDocument = obfuscateDocument(newDocument, ["key2.key22"]);
expect(verifySignature(firstObfuscatedDocument)).toBe(true);
expect(validateSchema(firstObfuscatedDocument)).toBe(true);
expect(firstObfuscatedDocument.data.key2).toStrictEqual({});

// let's make sure we can obfuscate the full object again after
const secondObfuscatedDocument = obfuscateDocument(newDocument, ["key2"]);
expect(verifySignature(secondObfuscatedDocument)).toBe(true);
expect(validateSchema(secondObfuscatedDocument)).toBe(true);
expect(secondObfuscatedDocument.data.key2).toBeUndefined();
expect(firstObfuscatedDocument.privacy!.obfuscatedData).toStrictEqual(
secondObfuscatedDocument.privacy!.obfuscatedData
);
});
});
});

0 comments on commit a0c783f

Please sign in to comment.