UNPKG

emmet.sdk

Version:
60 lines 2.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.detectEthereumProvider = void 0; const mobile_device_detect_1 = require("mobile-device-detect"); const EthreumConstants_1 = __importDefault(require("../../wallets/EthreumConstants")); function detectEthereumProvider(mustBeMetaMask = false, timeout = 4000) { let handled = false; let ethereum; if (window && mobile_device_detect_1.isMobile && !window.ethereum) { let temp = window.location.href; temp.replace("https://", "").replace("http://", ""); const metamaskDeepLink = `https://metamask.app.link/dapp/${temp}`; window.open(metamaskDeepLink); } ethereum = window.ethereum; return new Promise(resolve => { if (ethereum) { handleEthereum(); } else { window?.addEventListener(EthreumConstants_1.default.ETH_INITIALISED, handleEthereum, { once: true }); setTimeout(() => { handleEthereum(); }, timeout); } function handleEthereum() { if (handled) { return; } handled = true; let _tempEthereum; if (ethereum.providerMap) { for (const record of ethereum.providerMap) { if (record[0] === 'MetaMask') { _tempEthereum = record[1]; } } } if (_tempEthereum) { ethereum = _tempEthereum; } window?.removeEventListener(EthreumConstants_1.default.ETH_INITIALISED, handleEthereum); if (ethereum && (!mustBeMetaMask || ethereum.isMetaMask)) { resolve(ethereum); } else { const message = mustBeMetaMask && ethereum ? 'Non-MetaMask window.ethereum detected.' : 'Unable to detect window.ethereum.'; console.error(message); resolve(null); } } }); } exports.detectEthereumProvider = detectEthereumProvider; //# sourceMappingURL=detectEthereumProvider.js.map