fsl-authorization
Version:
## What id FSL ID
134 lines (133 loc) • 4.88 kB
JavaScript
import { Transaction, VersionedTransaction } from '@solana/web3.js';
import { close } from './close';
export var DEVICE_TYPE;
(function (DEVICE_TYPE) {
DEVICE_TYPE["IOS"] = "ios";
DEVICE_TYPE["ANDROID"] = "android";
DEVICE_TYPE["PC"] = "PC";
})(DEVICE_TYPE || (DEVICE_TYPE = {}));
export var getUserAgent = function () {
var ua = navigator.userAgent.toLowerCase();
var isIos = /(ios|ipad|iphone)/.test(ua) ||
(ua.indexOf('macintosh') > -1 && 'ontouchend' in document);
var isAndroid = /android/.test(ua);
if (isIos) {
return DEVICE_TYPE.IOS;
}
if (isAndroid) {
return DEVICE_TYPE.ANDROID;
}
return DEVICE_TYPE.PC;
};
var terminalType = getUserAgent();
var origin = 'https://id.fsl.com';
function initModal(or) {
if (typeof window === undefined || !document) {
return;
}
if (or)
origin = or;
// 创建遮罩层
var overlay = document.createElement('div');
overlay.id = 'custom-modal-overlay';
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.width = '100vw';
overlay.style.height = '100vh';
overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.6)';
overlay.style.display = 'none'; // 初始隐藏
overlay.style.flexDirection = 'column';
overlay.style.justifyContent = 'center';
overlay.style.alignItems = 'center';
overlay.style.zIndex = '2147483647';
// 创建容器
var modalContainer = document.createElement('div');
var closeBtn;
modalContainer.style.position = 'relative';
if (terminalType === DEVICE_TYPE.PC) {
modalContainer.style.width = '390px';
modalContainer.style.height = '844px';
modalContainer.style.borderRadius = '8px';
closeBtn = document.createElement('img');
closeBtn.style.width = '32px';
closeBtn.style.marginTop = '28px';
closeBtn.src = close;
closeBtn.addEventListener('click', function () {
window.handleMessage &&
window.handleMessage({
data: {
type: 'fsl_auth',
data: {
message: 'User action cancellation',
},
},
});
overlay.style.display = 'none';
iframe.src = ''; // 可选:释放 iframe 页面资源
});
}
else {
overlay.style.justifyContent = 'start';
modalContainer.style.width = '100%';
modalContainer.style.height = window.innerHeight + 'px';
window.addEventListener('resize', function () {
modalContainer.style.height = window.innerHeight + 'px';
});
}
modalContainer.style.backgroundColor = '#fff';
modalContainer.style.overflow = 'hidden';
modalContainer.style.boxShadow = '0 10px 40px rgba(0,0,0,0.3)';
// 阻止冒泡(避免点击关闭)
modalContainer.addEventListener('click', function (e) { return e.stopPropagation(); });
// iframe
var iframe = document.createElement('iframe');
iframe.id = 'custom-modal-iframe';
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 'none';
iframe.allow = "publickey-credentials-get ".concat(origin, "; publickey-credentials-create ").concat(origin);
modalContainer.appendChild(iframe);
overlay.appendChild(modalContainer);
closeBtn && overlay.appendChild(closeBtn);
document.body.appendChild(overlay);
}
var closeModal = function () {
var overlay = document.getElementById('custom-modal-overlay');
var iframe = document.getElementById('custom-modal-iframe');
if (overlay && iframe) {
overlay.style.display = 'none';
iframe.src = '';
}
};
// 公开接口
function showModal(url, silent) {
var overlay = document.getElementById('custom-modal-overlay');
var iframe = document.getElementById('custom-modal-iframe');
if (overlay && iframe) {
iframe.src = url;
!silent && (overlay.style.display = 'flex');
}
return iframe;
}
var bufferToTransaction = function (data, versions) {
var handledBufferStrs = data;
var transactions = [];
try {
for (var i = 0; i < versions.length; i++) {
if (versions[i] === 0) {
var newTransaction = VersionedTransaction.deserialize(Buffer.from(handledBufferStrs[i], 'base64'));
transactions.push(newTransaction);
}
else {
var newTransaction = Transaction.from(Buffer.from(handledBufferStrs[i], 'base64'));
transactions.push(newTransaction);
}
}
}
catch (err) {
console.log(err.message);
}
return transactions;
};
export { terminalType, initModal, closeModal, showModal, bufferToTransaction };