UNPKG

fsl-authorization

Version:
887 lines (886 loc) 48.2 kB
import { __assign, __awaiter, __generator } from "tslib"; import { pick } from 'lodash'; import { verifyMessage, verifyTypedData } from 'ethers/lib/utils'; import { v4 as uuidv4 } from 'uuid'; import { getAppInfo, initRequestToken, logout, optAutoApproveState, } from './api'; import { bufferToTransaction, closeModal, DEVICE_TYPE, getUserAgent, initModal, showModal, } from './utils'; import SDKStorage from './store'; var clearLoopObserver = function (clientWindow, handleMessage) { var interval = setInterval(function () { if (!clientWindow || clientWindow.closed) { clearInterval(interval); window.removeEventListener('message', handleMessage); } }, 500); return interval; }; var checkWindowOpenStatus = function (clientWindow, reject, msg) { if (msg === void 0) { msg = 'The pop-up cannot be ejected'; } var id = setTimeout(function () { clearTimeout(id); if (!clientWindow) { reject(msg); } }, 2000); }; var envPromiseCheckWrapper = function (callback, clientWindow, msg) { return new Promise(function (resolve, reject) { checkWindowOpenStatus(clientWindow, reject, msg); callback(resolve, reject); }); }; var FSLAuthorization = /** @class */ (function () { function FSLAuthorization(opt) { var _this = this; this.domain = 'https://id.fsl.com'; this.windowFeatures = "left=".concat(window.screen.width / 2 - 200, ",top=").concat(window.screen.height / 2 - 500, ",width=500,height=800,popup=1"); this.receiveLoginMessage = function (e) { _this.sdkStorage.setItem('token', e.data.data.requestToken); _this.sdkStorage.setItem('passkeyHash', e.data.data.passkeyHash); _this.sdkStorage.setItem('hash', e.data.data.hash); _this.sdkStorage.setItem('local', e.data.data.local); _this.sdkStorage.setItem('hasDownload', e.data.data.hasDownload); _this.sdkStorage.setItem('email', e.data.data.email); _this.sdkStorage.setItem('ico', e.data.data.ico); _this.sdkStorage.setItem('refreshToken', e.data.data.refreshToken); _this.sdkStorage.setItem('pwdKey', e.data.data.pwdKey); _this.sdkStorage.setItem('accessToken', e.data.data.accessToken); _this.sdkStorage.setItem('credentialId', e.data.data.credentialId); e.data.data.requestToken && initRequestToken(e.data.data.requestToken); }; var responseType = opt.responseType, appKey = opt.appKey, redirectUri = opt.redirectUri, scope = opt.scope, state = opt.state, useModal = opt.useModal, usePopup = opt.usePopup, domain = opt.domain, isApp = opt.isApp, uid = opt.uid; this.appKey = appKey; this.responseType = responseType; this.redirectUri = redirectUri; this.scope = scope; this.usePopup = usePopup; this.useModal = useModal; this.state = state; this.isApp = isApp; this.inWhiteList = false; this.uid = uid; this.handling = false; if (domain) this.domain = domain; window.apiOrigin = this.domain; if (this.uid) { this.sdkStorage = new SDKStorage(this.uid + ''); var token = this.sdkStorage.getItem('token'); if (token) { initRequestToken(token); } } } FSLAuthorization.init = function (opt) { return __awaiter(this, void 0, void 0, function () { var auth, doneCount, MAX_DONE, authResolve, authReject, authPromise, state, handler_1, childFrame_1, handleMessage_1, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: auth = new FSLAuthorization(opt); doneCount = 0, MAX_DONE = 1; authPromise = new Promise(function (res, rej) { authResolve = res; authReject = rej; }); if (!opt.useModal) return [3 /*break*/, 5]; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, getAppInfo(opt.appKey)]; case 2: state = (_a.sent()).state; if (state & 1) { auth.inWhiteList = true; // opener initiate wallet if (window.opener && opt.uid) { // set maximum number of callback MAX_DONE = 2; handler_1 = function (e) { if (e.data.type === 'fsl_login' && opt.uid === +e.data.data.id) { auth.receiveLoginMessage(e); window.removeEventListener('message', handler_1); doneCount++; if (doneCount === MAX_DONE) { authResolve(auth); } } }; window.addEventListener('message', handler_1, false); window.opener.postMessage({ type: 'fsl_login', data: 'done', }, auth.domain); } initModal(auth.domain); childFrame_1 = showModal(auth.domain, true); handleMessage_1 = function (e) { var _a; if (e.data.type === 'fsl_auth' && e.data.data === 'done') { childFrame_1 && ((_a = childFrame_1.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage({ type: 'fsl_users', data: JSON.stringify(SDKStorage.getAllUsers()), }, auth.domain)); window.removeEventListener('message', handleMessage_1); doneCount++; if (doneCount === MAX_DONE) { authResolve(auth); } } }; window.addEventListener('message', handleMessage_1, false); } else { authResolve(auth); } return [3 /*break*/, 4]; case 3: err_1 = _a.sent(); console.log(err_1.msg || err_1.message); authReject(err_1); return [3 /*break*/, 4]; case 4: return [3 /*break*/, 6]; case 5: authResolve(auth); _a.label = 6; case 6: return [2 /*return*/, authPromise]; } }); }); }; FSLAuthorization.prototype.getApproveDDL = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, optAutoApproveState(this.appKey)]; }); }); }; FSLAuthorization.prototype.openAutoApprove = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, optAutoApproveState(this.appKey, 1)]; }); }); }; FSLAuthorization.prototype.closeAutoApprove = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, optAutoApproveState(this.appKey, -1)]; }); }); }; FSLAuthorization.prototype.signIn = function (args) { return __awaiter(this, void 0, void 0, function () { var callUrl, commonArgs, key, clientWindow_1; var _this = this; return __generator(this, function (_a) { callUrl = new URL("".concat(this.domain, "/login/fslUsers")); commonArgs = { response_type: this.responseType, appkey: this.appKey, scope: this.scope, state: this.state, is_app: this.isApp ? '1' : undefined, withState: (args === null || args === void 0 ? void 0 : args.withState) ? '1' : undefined, }; for (key in commonArgs) { if (commonArgs[key]) { callUrl.searchParams.append(key, commonArgs[key]); } } if (this.inWhiteList && this.useModal) { callUrl.searchParams.append('use_modal', '1'); getUserAgent() !== DEVICE_TYPE.PC && callUrl.searchParams.append('isMobile', '1'); showModal(callUrl.toString()); return [2 /*return*/, new Promise(function (resolve) { window.handleMessage = function (e) { if (e.data.type === 'fsl_auth') { resolve(pick(e.data.data, _this.responseType, 'token')); _this.uid = +e.data.data.id; _this.sdkStorage = new SDKStorage(e.data.data.id + ''); _this.receiveLoginMessage(e); window.removeEventListener('message', window.handleMessage); closeModal(); window.handleMessage = null; } }; window.addEventListener('message', window.handleMessage, false); })]; } if (!this.usePopup) { callUrl.searchParams.append('redirect_uri', this.redirectUri); location.href = callUrl.toString(); return [2 /*return*/, Promise.resolve(null)]; } else { callUrl.searchParams.append('use_popup', '1'); if (this.isApp) { callUrl.searchParams.append('redirect_uri', this.redirectUri); } clientWindow_1 = window.open(callUrl.toString(), this.isApp ? '_blank' : "signWindow", this.windowFeatures); if (this.isApp) { return [2 /*return*/, Promise.resolve(null)]; } return [2 /*return*/, envPromiseCheckWrapper(function (resolve) { var handleMessage = function (e) { if (e.data.type === 'fsl_auth') { resolve(e.data.data); window.removeEventListener('message', handleMessage); } }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow_1, handleMessage); }, clientWindow_1)]; } return [2 /*return*/]; }); }); }; FSLAuthorization.prototype.signOut = function () { var _a; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_b) { (_a = this.sdkStorage) === null || _a === void 0 ? void 0 : _a.removeItem('token'); return [2 /*return*/, logout()]; }); }); }; FSLAuthorization.prototype.signInV2 = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.signIn({ withState: true })]; }); }); }; FSLAuthorization.prototype.checkAndAutoHandle = function (uid, callUrl, params, format) { return __awaiter(this, void 0, void 0, function () { var time, childFrame, token, err_2, terminalType, url; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (this.handling) { return [2 /*return*/, Promise.reject({ message: 'There is currently one transaction that has not been processed', })]; } this.handling = true; time = 0; if (uid && (!this.sdkStorage || +this.sdkStorage.getItem('id') !== uid)) { this.sdkStorage = new SDKStorage(uid + ''); token = this.sdkStorage.getItem('token'); if (token) initRequestToken(token); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.getApproveDDL()]; case 2: time = (_a.sent()); return [3 /*break*/, 4]; case 3: err_2 = _a.sent(); this.handling = false; return [2 /*return*/, err_2]; case 4: terminalType = getUserAgent(); if (time) { url = new URL("".concat(this.domain, "/authorization/silent")); url.search = callUrl.search; // token && url.searchParams.append('token', token); childFrame = showModal(url.toString(), true); } else { terminalType !== DEVICE_TYPE.PC && callUrl.searchParams.append('isMobile', '1'); // token && callUrl.searchParams.append('token', token); childFrame = showModal(callUrl.toString()); } return [2 /*return*/, new Promise(function (resolve, reject) { window.handleMessage = function (e) { var _a; if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { childFrame && ((_a = childFrame.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage({ type: 'fsl_params', data: JSON.stringify(params), }, _this.domain)); return; } else { resolve(e.data.data); } } else if (typeof e.data.data === 'object' && ('msg' in e.data.data || 'message' in e.data.data)) { reject(e.data.data); } else { var data = format ? format(e.data.data) : e.data.data; resolve(data); } window.removeEventListener('message', window.handleMessage); closeModal(); window.handleMessage = null; _this.handling = false; } }; window.addEventListener('message', window.handleMessage, false); })]; } }); }); }; FSLAuthorization.evmVerifyMessage = function (msg, signature) { return verifyMessage(msg, signature); }; FSLAuthorization.evmVerifyTypedData = function (domain, types, message, signature) { return verifyTypedData(domain, types, message, signature); }; FSLAuthorization.prototype.callEvmSign = function (args) { return __awaiter(this, void 0, void 0, function () { var msg, chainId, chain, rpc, signDigest, _a, uid, callUrl, type, uuid, url, clientWindow; return __generator(this, function (_b) { msg = args.msg, chainId = args.chainId, chain = args.chain, rpc = args.rpc, signDigest = args.signDigest, _a = args.uid, uid = _a === void 0 ? this.uid : _a; callUrl = new URL("".concat(args.domain || this.domain, "/authorization/sign")); switch (true) { case msg instanceof Uint8Array: type = 'unit8Array'; break; case msg instanceof Uint16Array: type = 'unit16Array'; break; case msg instanceof Uint32Array: type = 'unit32Array'; break; default: type = ''; } uuid = uuidv4(); callUrl.searchParams.append('arguments', JSON.stringify({ id: uuid, appKey: this.appKey, chainId: chainId, chain: chain, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { id: uuid, msg: msg, type: type, rpc: rpc, signDigest: signDigest ? 1 : undefined, name: 'callEvmSign', })]; } url = callUrl.toString(); clientWindow = window.open(url, "evmSignWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ id: uuid, msg: msg, type: type, rpc: rpc, signDigest: signDigest ? 1 : undefined, }), }, '*'); return; } else { resolve(e.data.data); } } else { reject(e.data.data); } window.removeEventListener('message', handleMessage); } }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.callEvmSignDigest = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.callEvmSign(__assign(__assign({}, args), { signDigest: true }))]; }); }); }; FSLAuthorization.prototype.signTypedData = function (args) { return __awaiter(this, void 0, void 0, function () { var domain, types, message, mockDomain, chain, chainId, _a, uid, uuid, callUrl, url, clientWindow; return __generator(this, function (_b) { domain = args.domain, types = args.types, message = args.message, mockDomain = args.mockDomain, chain = args.chain, chainId = args.chainId, _a = args.uid, uid = _a === void 0 ? this.uid : _a; uuid = uuidv4(); callUrl = new URL("".concat(mockDomain || this.domain, "/authorization/sign-v4")); callUrl.searchParams.append('arguments', JSON.stringify({ id: uuid, appKey: this.appKey, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { message: message, chain: chain, chainId: chainId, types: types, domain: domain, id: uuid, name: 'signTypedData', })]; } url = callUrl.toString(); clientWindow = window.open(url, "typedSignWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ message: message, chain: chain, chainId: chainId, types: types, domain: domain, id: uuid, }), }, '*'); } else { resolve(e.data.data); window.removeEventListener('message', handleMessage); } } else { reject(e.data.data); window.removeEventListener('message', handleMessage); } } }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.callEvmContractByCallData = function (args) { return __awaiter(this, void 0, void 0, function () { var uuid, contractAddress, callData, chainId, gasLimit, value, chain, rpc, domain, nonce, onlySign, maxPriorityFeePerGasValue, maxFeePerGasValue, _a, uid, _b, confirmed, callUrl, url, clientWindow; return __generator(this, function (_c) { uuid = uuidv4(); contractAddress = args.contractAddress, callData = args.callData, chainId = args.chainId, gasLimit = args.gasLimit, value = args.value, chain = args.chain, rpc = args.rpc, domain = args.domain, nonce = args.nonce, onlySign = args.onlySign, maxPriorityFeePerGasValue = args.maxPriorityFeePerGasValue, maxFeePerGasValue = args.maxFeePerGasValue, _a = args.uid, uid = _a === void 0 ? this.uid : _a, _b = args.confirmed, confirmed = _b === void 0 ? false : _b; callUrl = new URL("".concat(domain || this.domain, "/authorization/call-data")); callUrl.searchParams.append('arguments', JSON.stringify({ id: uuid, appKey: this.appKey, chainId: chainId, chain: chain, onlySign: onlySign ? 'onlySign' : undefined, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { contractAddress: contractAddress, callData: callData, gasLimit: gasLimit, value: value, rpc: rpc, chainId: chainId, chain: chain, nonce: nonce, onlySign: onlySign ? 'onlySign' : undefined, maxFeePerGasValue: maxFeePerGasValue, maxPriorityFeePerGasValue: maxPriorityFeePerGasValue, id: uuid, confirmed: confirmed, name: 'callEvmContractByCallData', })]; } url = callUrl.toString(); clientWindow = window.open(url, "callDataWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ contractAddress: contractAddress, callData: callData, chainId: chainId, gasLimit: gasLimit, value: value, chain: chain, rpc: rpc, nonce: nonce, maxFeePerGasValue: maxFeePerGasValue, maxPriorityFeePerGasValue: maxPriorityFeePerGasValue, id: uuid, confirmed: confirmed, }), }, '*'); } else { resolve(e.data.data); window.removeEventListener('message', handleMessage); } } else if (typeof e.data.data === 'object' && 'transactionHash' in e.data.data) { resolve(e.data.data); window.removeEventListener('message', handleMessage); } else { reject(e.data.data); window.removeEventListener('message', handleMessage); } } }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.signCallDataTransaction = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.callEvmContractByCallData(__assign(__assign({}, args), { onlySign: true }))]; }); }); }; FSLAuthorization.prototype.signSolMessage = function (args) { return __awaiter(this, void 0, void 0, function () { var msg, domain, _a, uid, uuid, callUrl, url, clientWindow; return __generator(this, function (_b) { msg = args.msg, domain = args.domain, _a = args.uid, uid = _a === void 0 ? this.uid : _a; uuid = uuidv4(); callUrl = new URL("".concat(domain || this.domain, "/authorization/sol-sign")); callUrl.searchParams.append('arguments', JSON.stringify({ id: uuid, appKey: this.appKey, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { msg: msg, id: uuid, name: 'signSolMessage', })]; } url = callUrl.toString(); clientWindow = window.open(url, "signSolMsgWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ msg: msg, id: uuid, }), }, '*'); } } else if (e.data.data && typeof e.data.data === 'object' && 'length' in e.data.data) { resolve(e.data.data); } else { reject(e.data.data); } window.removeEventListener('message', handleMessage); } }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.callSolInstructions = function (args) { return __awaiter(this, void 0, void 0, function () { var domain, instructions, rpc, unitPrice, unitLimit, onlySign, _a, uid, uuid, callUrl, url, clientWindow; var _this = this; return __generator(this, function (_b) { domain = args.domain, instructions = args.instructions, rpc = args.rpc, unitPrice = args.unitPrice, unitLimit = args.unitLimit, onlySign = args.onlySign, _a = args.uid, uid = _a === void 0 ? this.uid : _a; uuid = uuidv4(); callUrl = new URL("".concat(domain || this.domain, "/authorization/sol-trade")); callUrl.searchParams.append('arguments', JSON.stringify({ appKey: this.appKey, onlySign: onlySign ? 'onlySign' : void 0, id: uuid, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { instructions: instructions, unitLimit: unitLimit, unitPrice: unitPrice, id: uuid, rpc: rpc, onlySign: onlySign ? 'onlySign' : void 0, name: 'callSolInstructions', })]; } url = callUrl.toString(); clientWindow = window.open(url, "signSolCallWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ instructions: instructions, unitLimit: unitLimit, unitPrice: unitPrice, rpc: rpc, id: uuid, }), }, '*'); } else { resolve(e.data.data); window.removeEventListener('message', handleMessage); } } else { reject(e.data.data); window.removeEventListener('message', handleMessage); } } return [2 /*return*/]; }); }); }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.signSolInstructions = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.callSolInstructions(__assign(__assign({}, args), { onlySign: true }))]; }); }); }; FSLAuthorization.prototype.signSolTransaction = function (args) { return __awaiter(this, void 0, void 0, function () { var transactions, _a, uid, bufferStrs, versions, uuid, callUrl, url, clientWindow; var _this = this; return __generator(this, function (_b) { transactions = args.transactions, _a = args.uid, uid = _a === void 0 ? this.uid : _a; bufferStrs = []; versions = []; try { bufferStrs = transactions.map(function (item) { if (item.version === 0) { versions.push(0); return Buffer.from(item.serialize({ verifySignatures: false })).toString('base64'); } else { versions.push('legacy'); return item.serialize({ verifySignatures: false }).toString('base64'); } }); } catch (err) { return [2 /*return*/, Promise.reject(err.message)]; } uuid = uuidv4(); callUrl = new URL("".concat(this.domain, "/authorization/sol-transaction")); callUrl.searchParams.append('arguments', JSON.stringify({ id: uuid, appKey: this.appKey, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { transactions: bufferStrs, versions: versions, id: uuid, name: 'signSolTransaction', }, function (data) { var transactions = data; if (Array.isArray(data)) { transactions = bufferToTransaction(data, versions); } return transactions; })]; } url = callUrl.toString(); clientWindow = window.open(url, "signSolTrsWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { return __awaiter(_this, void 0, void 0, function () { var transactions_1; return __generator(this, function (_a) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ transactions: bufferStrs, versions: versions, id: uuid, }), }, '*'); } return [2 /*return*/]; } else if (Array.isArray(e.data.data)) { transactions_1 = bufferToTransaction(e.data.data, versions); resolve(transactions_1); } else { reject(e.data.data); } window.removeEventListener('message', handleMessage); } return [2 /*return*/]; }); }); }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.callEvmContract = function (args) { return __awaiter(this, void 0, void 0, function () { var contractAddress, methodName, abi, chainId, chain, value, gasLimit, params, to, rpc, domain, nonce, maxPriorityFeePerGasValue, maxFeePerGasValue, onlySign, _a, uid, id, callUrl, url, clientWindow; return __generator(this, function (_b) { contractAddress = args.contractAddress, methodName = args.methodName, abi = args.abi, chainId = args.chainId, chain = args.chain, value = args.value, gasLimit = args.gasLimit, params = args.params, to = args.to, rpc = args.rpc, domain = args.domain, nonce = args.nonce, maxPriorityFeePerGasValue = args.maxPriorityFeePerGasValue, maxFeePerGasValue = args.maxFeePerGasValue, onlySign = args.onlySign, _a = args.uid, uid = _a === void 0 ? this.uid : _a; id = uuidv4(); callUrl = new URL("".concat(domain || this.domain, "/authorization/trade-v2")); callUrl.searchParams.append('arguments', JSON.stringify({ appKey: this.appKey, onlySign: onlySign ? 'onlySign' : undefined, chainId: chainId, chain: chain, id: id, })); if (uid) { callUrl.searchParams.append('uid', uid + ''); } url = callUrl.toString(); if (this.useModal && this.inWhiteList) { return [2 /*return*/, this.checkAndAutoHandle(uid, callUrl, { contractAddress: contractAddress, methodName: methodName, abi: abi, value: value, gasLimit: gasLimit, params: params, to: to, nonce: nonce, maxPriorityFeePerGasValue: maxPriorityFeePerGasValue, maxFeePerGasValue: maxFeePerGasValue, id: id, rpc: rpc, onlySign: onlySign ? 'onlySign' : undefined, name: 'callEvmContract', })]; } clientWindow = window.open(url, "evmContractWindow", this.windowFeatures); return [2 /*return*/, envPromiseCheckWrapper(function (resolve, reject) { var handleMessage = function (e) { if (e.data.type === 'fsl_auth') { if (typeof e.data.data === 'string') { if (e.data.data === 'done') { clientWindow && clientWindow.postMessage({ type: 'fsl_params', data: JSON.stringify({ contractAddress: contractAddress, methodName: methodName, abi: abi, value: value, gasLimit: gasLimit, params: params, to: to, nonce: nonce, maxPriorityFeePerGasValue: maxPriorityFeePerGasValue, maxFeePerGasValue: maxFeePerGasValue, id: id, rpc: rpc, }), }, '*'); } else { resolve(e.data.data); window.removeEventListener('message', handleMessage); } } else if (e.data.data && typeof e.data.data === 'object' && 'transactionHash' in e.data.data) { resolve(e.data.data); window.removeEventListener('message', handleMessage); } else { reject(e.data.data); window.removeEventListener('message', handleMessage); } } }; window.addEventListener('message', handleMessage, false); clearLoopObserver(clientWindow, handleMessage); }, clientWindow)]; }); }); }; FSLAuthorization.prototype.signEvmContract = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.callEvmContract(__assign(__assign({}, args), { onlySign: true }))]; }); }); }; return FSLAuthorization; }()); export default FSLAuthorization;