@zkpass/transgate-js-sdk
Version:
<p align="center"> <img src="assets/logo.png" width="300" alt="transgate-js-sdk.js" /> </p>
190 lines (189 loc) • 6.64 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genPublicFieldHash = exports.launchAppForAndroid = exports.isTransgateAvailable = exports.removeMetaTag = exports.injectMetaTag = exports.launchApp = exports.insertQrcodeMask = exports.insertMobileDialog = exports.getDeviceType = exports.getObjectValues = exports.hexToBytes = exports.parseSignature = void 0;
const web3_1 = __importDefault(require("web3"));
const transgateWrapper_1 = require("./transgateWrapper");
/**
* parse signature to v, r, s
* @param signature
* @returns
*/
const parseSignature = (signature) => {
signature = signature.slice(2);
return {
v: parseInt('0x' + signature.slice(128, 130), 16),
r: '0x' + signature.slice(0, 64),
s: '0x' + signature.slice(64, 128),
};
};
exports.parseSignature = parseSignature;
const hexToBytes = (hex) => {
const bytes = [];
for (let c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substring(c, c + 2), 16));
return new Uint8Array(bytes);
};
exports.hexToBytes = hexToBytes;
function getObjectValues(json) {
let values = [];
function recurse(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
recurse(obj[key]); // it's a nested object, so we do it again
}
else {
values.push(obj[key]); // it's not an object, so we just push the value
}
}
}
}
recurse(json);
return values.join('');
}
exports.getObjectValues = getObjectValues;
function getDeviceType() {
const userAgent = navigator.userAgent || navigator.vendor;
if (/iPhone|iPad|iPod/i.test(userAgent)) {
return 'iOS';
}
else if (/Android/i.test(userAgent)) {
return 'Android';
}
else {
return 'Browser'; //default to browser
}
}
exports.getDeviceType = getDeviceType;
function insertMobileDialog() {
const modal = document.createElement('div');
modal.style.position = 'fixed';
modal.style.top = '0';
modal.style.left = '0';
modal.style.width = '100%';
modal.style.height = '100%';
modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
modal.style.display = 'flex';
modal.style.justifyContent = 'center';
modal.style.alignItems = 'center';
modal.style.zIndex = '9999';
modal.style.pointerEvents = 'auto';
document.getElementsByTagName('body')[0].appendChild(modal);
modal.innerHTML = transgateWrapper_1.mobileDialog;
return { remove: () => modal.remove() };
}
exports.insertMobileDialog = insertMobileDialog;
function insertQrcodeMask() {
//create a modal to show the qrcode
const modal = document.createElement('div');
modal.style.position = 'fixed';
modal.style.top = '0';
modal.style.left = '0';
modal.style.width = '100%';
modal.style.height = '100%';
modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
modal.style.display = 'flex';
modal.style.justifyContent = 'center';
modal.style.alignItems = 'center';
modal.style.zIndex = '9999';
modal.style.pointerEvents = 'auto';
document.getElementsByTagName('body')[0].appendChild(modal);
modal.innerHTML = transgateWrapper_1.transgateWrapper;
const canvasElement = document.getElementById('zkpass-canvas');
if (!canvasElement) {
modal.remove();
throw new Error('generate qrcode failed');
}
return { canvasElement, remove: () => modal.remove() };
}
exports.insertQrcodeMask = insertQrcodeMask;
function launchApp(url) {
const link = document.createElement('a');
link.href = url;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
link.remove();
}
exports.launchApp = launchApp;
function injectMetaTag(name, content) {
const meta = document.createElement('meta');
meta.name = name;
meta.content = content;
document.getElementsByTagName('head')[0].appendChild(meta);
}
exports.injectMetaTag = injectMetaTag;
function removeMetaTag(name) {
const metaCollection = Array.from(document.getElementsByTagName('meta')) || [];
for (const meta of metaCollection) {
if (meta.name == name) {
meta.remove();
}
}
}
exports.removeMetaTag = removeMetaTag;
function isSafari() {
return (navigator.vendor === 'Apple Computer, Inc.' &&
navigator.userAgent.includes('Safari') &&
!navigator.userAgent.includes('CriOS') &&
!navigator.userAgent.includes('FxiOS'));
}
async function isTransgateAvailable(extensionId) {
try {
const url = `chrome-extension://${extensionId}/images/icon-16.png`;
const { statusText } = await fetch(url);
if (statusText === 'OK') {
return true;
}
return false;
}
catch (error) {
return false;
}
}
exports.isTransgateAvailable = isTransgateAvailable;
function launchAppForAndroid(url, backupUrl) {
try {
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = url;
document.body.appendChild(iframe);
const fallbackTimeout = setTimeout(() => {
window.location.href = backupUrl;
}, 1500);
window.addEventListener('blur', () => {
clearTimeout(fallbackTimeout);
iframe.remove();
});
}
catch (error) {
console.error('launchAppForAndroid error:', error);
}
}
exports.launchAppForAndroid = launchAppForAndroid;
function genPublicFieldHash(publicFields = []) {
const publicData = publicFields.map((item) => {
delete item.str;
return item;
});
let values = [];
function recurse(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
recurse(obj[key]); // it's a nested object, so we do it again
}
else {
values.push(obj[key]); // it's not an object, so we just push the value
}
}
}
}
recurse(publicData);
const publicFieldStr = values.join('');
return web3_1.default.utils.soliditySha3(!!publicFieldStr ? web3_1.default.utils.stringToHex(publicFieldStr) : web3_1.default.utils.utf8ToHex('1'));
}
exports.genPublicFieldHash = genPublicFieldHash;
;