UNPKG

unipass-js-sdk

Version:
195 lines (194 loc) 6.61 kB
// unipass.ts var createHash = require('crypto').createHash; ; (function (factory) { // nodejs if (typeof exports === 'object' && typeof module !== 'undefined') { module.exports = factory(); } // browser if (typeof window !== 'undefined') { window.unipass = factory(); } })(function () { var Sea = { // json parse json: function (s) { try { return JSON.parse(s); } catch (err) { return s; } }, // Local storage localStorage: function (key, val) { if (val === undefined) { return this.json(window.localStorage.getItem(key)); } else if (val === '') { window.localStorage.removeItem(key); } else { window.localStorage.setItem(key, JSON.stringify(val)); } }, params: function (key, value) { var obj = new window.URL(window.location.href); if (value) { obj.searchParams.set(key, value); } else if (value === '') { obj.searchParams["delete"](key); } else if (key) { return obj.searchParams.get(key); } else { return obj.searchParams; } history.replaceState({}, '', obj.href); } }; var _callbackRet = function (ret) { if (ret && ret.data && ret.code) { if (Sea.params('unipass_ret')) { Sea.params('unipass_ret', ''); } unipass.callback(ret); if (ret.info === 'login success') { Sea.localStorage('unipass_provider', ret.data); unipass.provider = ret.data; } } }; var _jump = function (type, data) { if (data === void 0) { data = {}; } if (type === 'sign') { var pubkey = unipass.provider.pubkey; if (pubkey) { data.pubkey = pubkey; } else { console.error('Please login first'); return; } // handle message if (data.message) { data.message = encodeURIComponent(data.message); if (data.type !== 'personal_sign') { data.message = createHash('SHA256') .update(data.message) .digest('hex') .toString(); } } else { console.error('Please enter message'); return; } } data.success_url = encodeURIComponent(window.location.href); var query = Object.keys(data) .map(function (key) { return key + "=" + data[key]; }) .join('&'); var url = unipass.url + "/" + type + "?" + query; window.location.href = url; }; var _popup = function (type, data) { if (data === void 0) { data = {}; } if (unipass.isBuiltInBrowser()) { console.error('Popup Window is not supported by low compatibility browser'); return; } if (type === 'sign') { var pubkey = unipass.provider.pubkey; if (pubkey) { data.pubkey = pubkey; } else { console.error('Please login first'); return; } // handle message if (data.message) { data.message = encodeURIComponent(data.message); if (data.type !== 'personal_sign') { data.message = createHash('SHA256') .update(data.message) .digest('hex') .toString(); } } else { console.error('Please enter message'); return; } } var page = window.open(unipass.url + "/" + type, type, 'width=380,height=675,top=40,left=100,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'); if (page) { page.onload = function () { data.success_url = 'open'; page.postMessage(data, '*'); }; } }; var unipass = { callback: function (_ret) { }, url: 'https://t.unipass.xyz', provider: {}, init: function (baseUrl, callback) { if (typeof window === 'undefined' || window.unipassIsBindEvent) { return; } this.callback = callback; if (baseUrl) { this.url = baseUrl; } window.unipassIsBindEvent = true; var url = new URL(window.location.href); var ret = Sea.json(url.searchParams.get('unipass_ret')); if (ret) { _callbackRet(ret); } window.addEventListener('message', function (event) { var ret = event.data; _callbackRet(ret); }, false); unipass.provider = Sea.localStorage('unipass_provider') || {}; }, login: function () { _jump('login'); }, sign: function (data) { if (data === void 0) { data = {}; } _jump('sign', data); }, popupLogin: function () { _popup('login'); }, popupSign: function (data) { if (data === void 0) { data = {}; } _popup('sign', data); }, exit: function () { Sea.localStorage('unipass_provider', ''); window.location.reload(); }, isBuiltInBrowser: function () { if (typeof navigator === 'undefined') { return true; } var userAgent = navigator.userAgent.toLowerCase(); var list = ['micromessenger', 'qqbrowser', 'weibo', 'alipay']; for (var _i = 0, list_1 = list; _i < list_1.length; _i++) { var browser = list_1[_i]; if (userAgent.includes(browser)) { return true; } } return false; } }; return unipass; });