UNPKG

@sky-mavis/tanto-widget

Version:
103 lines (99 loc) 3.97 kB
'use strict'; var tantoConnect = require('@sky-mavis/tanto-connect'); var uaParserJs = require('ua-parser-js'); var viem = require('viem'); var index = require('../constants/index.cjs'); var wallet = require('../types/wallet.cjs'); const notEmpty = value => typeof value !== 'undefined' && value !== null; const isClient = () => typeof window !== 'undefined'; const isRoninInAppBrowser = () => isClient() && !!window.isWalletApp && window.ronin !== undefined && !!window.ethereum?.isRonin; const isSafeWallet = async () => { try { return !!(await tantoConnect.requestSafeProvider()); } catch { return false; } }; const getUserAgent = () => { if (!isClient()) return undefined; return uaParserJs.UAParser(navigator.userAgent); }; const detectOS = () => { const parser = getUserAgent(); return parser?.os.name ?? ''; }; const isIOS = () => { const os = detectOS(); return os.toLowerCase().includes('ios'); }; const isAndroid = () => { const os = detectOS(); return os.toLowerCase().includes('android'); }; const isMobile = () => isAndroid() || isIOS(); const isDesktop = () => !isMobile(); const isInjectedConnector = connectorType => connectorType === 'injected'; const isRoninWallet = connectorId => connectorId === wallet.WALLET_IDS.RONIN_WALLET; const isRoninWalletInjected = connectorId => connectorId === wallet.WALLET_IDS.RONIN_WALLET_INJECTED; const isWCConnector = connectorId => connectorId === wallet.WALLET_IDS.WALLET_CONNECT; const isWaypointConnector = connectorId => connectorId === wallet.WALLET_IDS.WAYPOINT; const isSafeConnector = connectorId => connectorId === wallet.WALLET_IDS.SAFE; const isCoinbaseConnector = connectorId => connectorId === wallet.WALLET_IDS.COINBASE_WALLET; const generateRoninMobileWCLink = (uri, prefix = `${index.RONIN_WALLET_WEB_LINK}/`) => `${prefix}auth-connect?uri=${encodeURIComponent(uri)}`; const isRoninExtensionInstalled = connectors => connectors.some(connector => connector.id === wallet.WALLET_IDS.RONIN_WALLET_INJECTED); const truncate = (value, options) => { const { prefixChar = 8, suffixChar = 6, bridge = '•••' } = {}; if (!value) return ''; if (value.length <= prefixChar + suffixChar + bridge.length) return value; return `${value.slice(0, prefixChar)}${bridge}${value.slice(-suffixChar)}`; }; const formatBalance = amount => { const remainder = amount % BigInt(1e14); return viem.formatUnits(amount - remainder, 18); }; const isValidURL = url => { try { new URL(url); return true; } catch { return false; } }; const getReverseNode = address => { const node = address.startsWith('0x') ? address.substring(2) : address; return `${node.toLowerCase()}.addr.reverse`; }; const svgToBase64 = svgText => { const encoded = encodeURIComponent(svgText).replace(/'/g, '%27').replace(/"/g, '%22'); return `data:image/svg+xml;charset=utf-8,${encoded}`; }; const getVersionInfo = () => `${'@sky-mavis/tanto-widget'}@${'0.0.1'}`; exports.detectOS = detectOS; exports.formatBalance = formatBalance; exports.generateRoninMobileWCLink = generateRoninMobileWCLink; exports.getReverseNode = getReverseNode; exports.getUserAgent = getUserAgent; exports.getVersionInfo = getVersionInfo; exports.isAndroid = isAndroid; exports.isClient = isClient; exports.isCoinbaseConnector = isCoinbaseConnector; exports.isDesktop = isDesktop; exports.isIOS = isIOS; exports.isInjectedConnector = isInjectedConnector; exports.isMobile = isMobile; exports.isRoninExtensionInstalled = isRoninExtensionInstalled; exports.isRoninInAppBrowser = isRoninInAppBrowser; exports.isRoninWallet = isRoninWallet; exports.isRoninWalletInjected = isRoninWalletInjected; exports.isSafeConnector = isSafeConnector; exports.isSafeWallet = isSafeWallet; exports.isValidURL = isValidURL; exports.isWCConnector = isWCConnector; exports.isWaypointConnector = isWaypointConnector; exports.notEmpty = notEmpty; exports.svgToBase64 = svgToBase64; exports.truncate = truncate;