@privateid/cryptonets-web-sdk-alpha
Version:
CryptoNets WebAssembly SDK
430 lines • 17.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { simd } from 'wasm-feature-detect';
import { wrap } from 'comlink';
import { detect } from 'detect-browser';
import { debugTypes, LANDSCAPE, PORTRAIT } from './constants';
import { LOGTYPE } from './types';
const functions = wrap(new Worker('./workers/wasm.worker.js'));
export const { isLoad, isValidInternal, prividAgePredict, FHE_enrollOnefa, FHE_predictOnefa, scanDocument, scanDocumentNoFace, deleteUUID, isValidBarCode, prividFaceISO, prividFaceCompareLocal, prividDocumentMugshotFaceCompare, antispoofCheck, } = functions;
export function iOS() {
return ['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod'].includes(navigator.platform);
}
export function isMobileFunc() {
let check = false;
(function (a) {
if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a) ||
/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4)))
check = true;
})(navigator.userAgent || navigator.vendor || window.opera);
return check;
}
export const getIsSIMD = () => __awaiter(void 0, void 0, void 0, function* () {
const isSimd = yield simd();
return isSimd;
});
export const getUrlParameter = function getUrlParameter(sParam, defaultValue) {
const sPageURL = window.location.search.substring(1);
const sURLVariables = sPageURL.split('&');
let sParameterName;
let i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return typeof sParameterName[1] === undefined ? defaultValue : decodeURIComponent(sParameterName[1]);
}
}
return defaultValue;
};
export const getDebugType = () => {
let debugType = getUrlParameter('debug_type', '0');
if (!debugTypes.includes(debugType)) {
debugType = '0';
}
return debugType;
};
/**
* This function does the printing in the console based on the type and will only print based on @param debugType
* Please make sure to use this so that logs on the module would only print depending on the debug level
* @param title title of the printing. sample console.log(tiltle, message)
* @param message message of the printing. sample console.log(tiltle, message) if this is not needed just throw an empy string. sample: printLogs(title, '', debugType)
* @param debugType this variable will determine the type of printing in the console
* @param type this variable will determing the type of printing in the console, "LOG", "WARNING" or "ERROR" by default it is "LOG"
*/
export const printLogs = (title, message, debugType, type = LOGTYPE.LOG) => {
const errorLogs = ['1', '2', '3', '901', '902', '903'];
const warningLogs = ['2', '3', '902', '903'];
const allLogs = ['3', '903'];
if (type === LOGTYPE.ERROR && errorLogs.includes(debugType.toString())) {
console.error(title, message);
}
else if (type === LOGTYPE.WARN && warningLogs.includes(debugType.toString())) {
console.warn(title, message);
}
else if (type === LOGTYPE.LOG && allLogs.includes(debugType.toString())) {
console.log(title, message);
}
};
export const getPortraitBase64 = (content) => __awaiter(void 0, void 0, void 0, function* () {
const canvas = document.createElement('canvas');
canvas.width = content.width;
canvas.height = content.height;
const context = canvas.getContext('2d');
context.putImageData(content, 0, 0);
const blob = yield new Promise((resolve) => canvas.toBlob(resolve));
const base64 = yield blobToBase64(blob);
return base64;
});
export const createImages = (images, title, action) => __awaiter(void 0, void 0, void 0, function* () {
const promises = images.map((content) => new Promise((resolve) => __awaiter(void 0, void 0, void 0, function* () {
const canvas = document.createElement('canvas');
canvas.width = content.width;
canvas.height = content.height;
const context = canvas.getContext('2d');
context.putImageData(content, 0, 0);
const blob = yield new Promise((resolve) => canvas.toBlob(resolve, 'image/jpeg'));
const base64 = yield blobToBase64(blob);
const url = URL.createObjectURL(blob);
resolve({ url, base64 });
})));
const data = yield Promise.all(promises);
const a = document.createElement('a');
for (let i = 0; i < data.length; i++) {
// if(!iOS()) {
yield new Promise((resolve) => {
a.href = data[i].url;
a.type = 'image/png';
a.download = `${title}_${action ? 'enroll' : 'predict'}` + '.png';
document.body.appendChild(a);
a.click();
setTimeout(() => {
resolve();
}, 1000);
});
// }
}
});
function blobToBase64(blob) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
export const wait = (timeout) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve) => {
setTimeout(() => {
resolve('true');
}, timeout);
});
});
export function setDefaultCameraDeviceId(deviceId) {
localStorage.setItem('defaultDeviceId', deviceId);
}
export function getDefaultCameraDeviceId() {
return localStorage.getItem('defaultDeviceId');
}
export const convertCroppedImage = (croppedBarcode, cropImageWidth, cropImageHeight) => __awaiter(void 0, void 0, void 0, function* () {
try {
const image = new ImageData(croppedBarcode, cropImageWidth, cropImageHeight);
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const context = canvas.getContext('2d');
context.putImageData(image, 0, 0);
return canvas.toDataURL('image/png');
}
catch (e) {
console.error(e);
}
});
export const isMobileDevice = () => {
const { os } = detect();
return ['iOS', 'Android OS', 'Windows Mobile'].includes(os);
};
export var MessageType;
(function (MessageType) {
MessageType["faceValidation"] = "Face Validation";
MessageType["antispoofStatus"] = "Antispoof Status";
})(MessageType || (MessageType = {}));
export const getStatusMessage = (result, type = MessageType.faceValidation) => {
if (type === MessageType.faceValidation) {
switch (result) {
case -100:
case -1:
return '';
case 0:
return 'Processing hold still';
case 1:
case 2:
case 3:
return 'Please move back';
case 4:
return 'Please move closer';
case 5:
case 6:
case 7:
case 8:
return 'Move face into circle';
case 9:
return 'Please hold still';
case 10:
return 'Remove glasses';
case 11:
return 'Remove mask ';
case 12:
case 13:
return 'Please look at camera';
case 14:
if (isMobileDevice()) {
return 'Raise phone level to face';
}
else {
return 'Look down a little';
}
case 15:
if (isMobileDevice()) {
return '“Lower phone level to face';
}
else {
return 'Look up a little';
}
case 16:
return 'Too dim - increase lighting';
case 17:
return 'Too bright - lower lighting';
case 18:
return 'Please hold still';
case 19:
return '';
case 20:
return 'Please hold still';
case 21:
return 'Please close mouth';
case 22:
case 23:
return 'Please straighten head';
case 24:
return 'Remove mask and glasses';
default:
return '';
}
}
else if (type === MessageType.antispoofStatus) {
switch (result) {
case -100:
case -5:
case -4:
case -2:
case 1:
return 'Too dim - increase lighting';
case -3:
return 'Move face into circle';
case -1:
return 'Looking for face';
case 0:
return '';
default:
return '';
}
}
};
export const getRawFaceValidationStatus = (status) => {
switch (status) {
case -1:
return 'No Face';
case 0:
return 'Valid Face';
case 1:
return 'Image Spoof';
case 2:
return 'Video Spoof';
case 3:
return 'Too Close';
case 4:
return 'Too far away';
case 5:
return 'Too far to right';
case 6:
return 'Too far to left';
case 7:
return 'Too far up';
case 8:
return 'Too far down';
case 9:
return 'Too blurry';
case 10:
return 'Glasses on';
case 11:
return 'Mask on';
case 12:
return 'Chin too far left';
case 13:
return 'Chin too far right';
case 14:
return 'Chin too far up';
case 15:
return 'Chin too far down';
case 16:
return 'Image too dim';
case 17:
return 'Image too bright';
case 18:
return 'Face low confidence value';
case 19:
return 'Invalid face background';
case 20:
return 'Eyes blink';
case 21:
return 'Mouth Open';
case 22:
return 'Face rotated right';
case 23:
return 'Face rotated left';
case 24:
return 'Face mask and Eye glasses detected';
default:
return '';
}
};
export const getRawSpoofStatusMessage = (status) => {
switch (status) {
case -100:
return 'Generic Error';
case -4:
return 'Invalid Face';
case -3:
return 'Face Too Close To Edge';
case -2:
return 'Mobile Phone Detected';
case -1:
return 'No Face Detected';
case 0:
return 'Real';
case 1:
return 'Spoof Detected';
default:
return '';
}
};
export const checkPackageSupport = () => __awaiter(void 0, void 0, void 0, function* () {
const { name: browserName, os, version } = detect();
const parsedVersion = parseInt(version.split('.')[0], 10);
if (os === 'Mac OS') {
if (browserName === 'safari' && parseInt(version.split('.')[0], 10) <= 10) {
return { support: false, message: 'Safari version 10 and below is not supported.' };
}
}
if (os === 'iOS') {
if (browserName === 'safari' && parseInt(version.split('.')[0], 10) <= 10) {
return { support: false, message: 'Safari version 10 and below is not supported.' };
}
if (browserName === 'opera' && parseInt(version.split('.')[0], 10) <= 72) {
return { support: false, message: 'Opera mobile version 72 and below is not support WASM' };
}
}
if (os === 'Android OS' || os === 'android') {
if (browserName === 'chrome' && parseInt(version.split('.')[0], 10) <= 96) {
return { support: false, message: 'Android Chrome version 96 and below is not supported.' };
}
if (browserName === 'samsung' && parseInt(version.split('.')[0], 10) <= 6.2) {
return { support: false, message: 'Samsung browser version 6 and below is not supported.' };
}
if (browserName === 'firefox' && parseInt(version.split('.')[0], 10) <= 94) {
return { support: false, message: 'Firefox mobile version 94 and below is not supported.' };
}
if (browserName === 'opera' && parseInt(version.split('.')[0], 10) <= 72) {
return { support: false, message: 'Opera mobile version 72 and below is not support WASM' };
}
if (browserName === 'android' && parseInt(version.split('.')[0], 10) <= 96) {
return { support: false, message: 'Android Browser version 96 and below is not supported.' };
}
}
if (browserName === 'ie') {
return { support: false, message: 'Internet Explorer is not supported.' };
}
if (browserName === 'edge' && parseInt(version.split('.')[0], 10) <= 78) {
return { support: false, message: 'Edge version 78 and below is not supported.' };
}
if (browserName === 'chrome' && parseInt(version.split('.')[0], 10) <= 57) {
return { support: false, message: 'Chrome version 57 and below is not support WASM' };
}
if (browserName === 'firefox' && parseInt(version.split('.')[0], 10) <= 52) {
return { support: false, message: 'Firefox version 52 and below is not supported.' };
}
if (browserName === 'opera' && parseInt(version.split('.')[0], 10) <= 43) {
return { support: false, message: 'Opera version 43 and below is not support WASM' };
}
return { support: true };
});
export const getFrontDocumentStatusMessage = (result) => {
switch (result) {
case 0:
return 'Success';
case 12:
return '';
case -1:
return 'Move Closer';
case 18:
return 'Please show front of the ID';
case 3:
return 'Please hold still';
case 4:
return 'Move just a little closer';
case 9:
return 'Too blurry, please hold still';
case -2:
return 'SYSTEM ERROR. Please try again later.';
case 5:
case 6:
case 7:
case 8:
return 'Please show ID at the center of the screen';
case -100:
return 'Please show front of the ID';
default:
return '';
}
};
export const getBackDocumentStatusMessage = (result) => {
switch (result) {
case 0:
return 'Success';
case 12:
return '';
case -1:
case 10:
return 'Move closer to barcode';
case 3:
return 'Please move closer to barcode';
case 4:
return 'Move just a little closer';
case 18:
return 'Please show barcode in the box';
case 9:
return 'Too blurry, please hold still';
case -2:
return 'SYSTEM ERROR. Please try again later.';
case 5:
case 6:
case 7:
case 8:
return 'Please show ID at the center of the screen';
default:
return '';
}
};
export function getScreenOrientation() {
if (window.innerHeight > window.innerWidth) {
return PORTRAIT;
}
return LANDSCAPE;
}
//# sourceMappingURL=utils.js.map