Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added typeRoots #190

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
.nyc_output
yarn-error.log
yarn.lock
/dist
/.idea
*.iml
/public
Expand Down
29 changes: 29 additions & 0 deletions dist/cjs/2.0/digest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.digestDocument = exports.flattenHashArray = void 0;
var lodash_1 = require("lodash");
var js_sha3_1 = require("js-sha3");
var flatten_1 = require("../shared/serialize/flatten");
var isKeyOrValueUndefined = function (value, key) { return value === undefined || key === undefined; };
var flattenHashArray = function (data) {
var flattenedData = lodash_1.omitBy(flatten_1.flatten(data), isKeyOrValueUndefined);
return Object.keys(flattenedData).map(function (k) {
var obj = {};
obj[k] = flattenedData[k];
return js_sha3_1.keccak256(JSON.stringify(obj));
});
};
exports.flattenHashArray = flattenHashArray;
var digestDocument = function (document) {
// Prepare array of hashes from filtered data
var hashedDataArray = lodash_1.get(document, "privacy.obfuscatedData", []);
// Prepare array of hashes from visible data
var unhashedData = lodash_1.get(document, "data");
var hashedUnhashedDataArray = exports.flattenHashArray(unhashedData);
// Combine both array and sort them to ensure determinism
var combinedHashes = hashedDataArray.concat(hashedUnhashedDataArray);
var sortedHashes = lodash_1.sortBy(combinedHashes);
// Finally, return the digest of the entire set of data
return js_sha3_1.keccak256(JSON.stringify(sortedHashes));
};
exports.digestDocument = digestDocument;
47 changes: 47 additions & 0 deletions dist/cjs/2.0/obfuscate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.obfuscateDocument = exports.obfuscateData = void 0;
var lodash_1 = require("lodash");
var flatten_1 = require("../shared/serialize/flatten");
var utils_1 = require("../shared/utils");
var obfuscateData = function (_data, fields) {
var data = lodash_1.cloneDeep(_data); // Prevents alteration of original data
var fieldsToRemove = Array.isArray(fields) ? fields : [fields];
// Obfuscate data by hashing them with the key
var dataToObfuscate = flatten_1.flatten(lodash_1.pick(data, fieldsToRemove));
var obfuscatedData = Object.keys(dataToObfuscate).map(function (k) {
var obj = {};
obj[k] = dataToObfuscate[k];
return utils_1.toBuffer(obj).toString("hex");
});
// Return remaining data
fieldsToRemove.forEach(function (path) {
lodash_1.unset(data, path);
});
return {
data: data,
obfuscatedData: obfuscatedData,
};
};
exports.obfuscateData = obfuscateData;
// TODO the return type could be improve by using Exclude eventually to remove the obfuscated properties
var obfuscateDocument = function (document, fields) {
var _a, _b;
var existingData = document.data;
var _c = exports.obfuscateData(existingData, fields), data = _c.data, obfuscatedData = _c.obfuscatedData;
var currentObfuscatedData = (_b = (_a = document === null || document === void 0 ? void 0 : document.privacy) === null || _a === void 0 ? void 0 : _a.obfuscatedData) !== null && _b !== void 0 ? _b : [];
var newObfuscatedData = currentObfuscatedData.concat(obfuscatedData);
return __assign(__assign({}, document), { data: data, privacy: __assign(__assign({}, document.privacy), { obfuscatedData: newObfuscatedData }) });
};
exports.obfuscateDocument = obfuscateDocument;
113 changes: 113 additions & 0 deletions dist/cjs/2.0/salt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.unsaltData = exports.saltData = exports.unsalt = exports.uuidSalt = exports.typedStringToPrimitive = exports.primitiveToTypedString = exports.deepMap = void 0;
var lodash_1 = require("lodash");
var isUUID_1 = __importDefault(require("validator/lib/isUUID"));
var uuid_1 = require("uuid");
var UUIDV4_LENGTH = 37;
var PRIMITIVE_TYPES = ["string", "number", "boolean", "undefined"];
/* eslint-disable no-use-before-define */
/**
* Curried function that takes (iteratee)(value),
* if value is a collection then recurse into it
* otherwise apply `iteratee` on the primitive value
*/
var recursivelyApply = function (iteratee) { return function (value) {
if (lodash_1.includes(PRIMITIVE_TYPES, typeof value) || value === null) {
return iteratee(value);
}
return exports.deepMap(value, iteratee); // eslint-disable-line @typescript-eslint/no-use-before-define
}; };
/**
* Applies `iteratee` to all fields in objects, goes into arrays as well.
* Refer to test for example
*/
var deepMap = function (collection, iteratee) {
if (iteratee === void 0) { iteratee = lodash_1.identity; }
if (collection instanceof Array) {
return lodash_1.map(collection, recursivelyApply(iteratee));
}
if (typeof collection === "object") {
return lodash_1.mapValues(collection, recursivelyApply(iteratee));
}
return collection;
};
exports.deepMap = deepMap;
/* eslint-enable no-use-before-define */
// disabling this because of mutual recursion
var startsWithUuidV4 = function (input) {
if (input && typeof input === "string") {
var elements = input.split(":");
return isUUID_1.default(elements[0], 4);
}
return false;
};
/**
* Detects the type of a value and returns a string with type annotation
*/
function primitiveToTypedString(value) {
switch (typeof value) {
case "number":
case "string":
case "boolean":
case "undefined":
return typeof value + ":" + String(value);
default:
if (value === null) {
// typeof null is 'object' so we have to check for it
return "null:null";
}
throw new Error("Parsing error, value is not of primitive type: " + value);
}
}
exports.primitiveToTypedString = primitiveToTypedString;
/**
* Returns an appropriately typed value given a string with type annotations, e.g: "number:5"
*/
function typedStringToPrimitive(input) {
var _a = input.split(":"), type = _a[0], valueArray = _a.slice(1);
var value = valueArray.join(":"); // just in case there are colons in the value
switch (type) {
case "number":
return Number(value);
case "string":
return String(value);
case "boolean":
return value === "true";
case "null":
return null;
case "undefined":
return undefined;
default:
throw new Error("Parsing error, type annotation not found in string: " + input);
}
}
exports.typedStringToPrimitive = typedStringToPrimitive;
/**
* Returns a salted value using a randomly generated uuidv4 string for salt
*/
function uuidSalt(value) {
var salt = uuid_1.v4();
return salt + ":" + primitiveToTypedString(value);
}
exports.uuidSalt = uuidSalt;
/**
* Value salted string in the format "salt:type:value", example: "ee7f3323-1634-4dea-8c12-f0bb83aff874:number:5"
* Returns an appropriately typed value when given a salted string with type annotation
*/
function unsalt(value) {
if (startsWithUuidV4(value)) {
var untypedValue = value.substring(UUIDV4_LENGTH).trim();
return typedStringToPrimitive(untypedValue);
}
return value;
}
exports.unsalt = unsalt;
// Use uuid salting method to recursively salt data
var saltData = function (data) { return exports.deepMap(data, uuidSalt); };
exports.saltData = saltData;
var unsaltData = function (data) { return exports.deepMap(data, unsalt); };
exports.unsaltData = unsaltData;
Loading