detect-provider.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.detectEthereumProvider = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. "use strict";
  3. /**
  4. * Returns a Promise that resolves to the value of window.ethereum if it is
  5. * set within the given timeout, or null.
  6. * The Promise will not reject, but an error will be thrown if invalid options
  7. * are provided.
  8. *
  9. * @param options - Options bag.
  10. * @param options.mustBeMetaMask - Whether to only look for MetaMask providers.
  11. * Default: false
  12. * @param options.silent - Whether to silence console errors. Does not affect
  13. * thrown errors. Default: false
  14. * @param options.timeout - Milliseconds to wait for 'ethereum#initialized' to
  15. * be dispatched. Default: 3000
  16. * @returns A Promise that resolves with the Provider if it is detected within
  17. * given timeout, otherwise null.
  18. */
  19. function detectEthereumProvider({ mustBeMetaMask = false, silent = false, timeout = 3000, } = {}) {
  20. _validateInputs();
  21. let handled = false;
  22. return new Promise((resolve) => {
  23. if (window.ethereum) {
  24. handleEthereum();
  25. }
  26. else {
  27. window.addEventListener('ethereum#initialized', handleEthereum, { once: true });
  28. setTimeout(() => {
  29. handleEthereum();
  30. }, timeout);
  31. }
  32. function handleEthereum() {
  33. if (handled) {
  34. return;
  35. }
  36. handled = true;
  37. window.removeEventListener('ethereum#initialized', handleEthereum);
  38. const { ethereum } = window;
  39. if (ethereum && (!mustBeMetaMask || ethereum.isMetaMask)) {
  40. resolve(ethereum);
  41. }
  42. else {
  43. const message = mustBeMetaMask && ethereum
  44. ? 'Non-MetaMask window.ethereum detected.'
  45. : 'Unable to detect window.ethereum.';
  46. !silent && console.error('@metamask/detect-provider:', message);
  47. resolve(null);
  48. }
  49. }
  50. });
  51. function _validateInputs() {
  52. if (typeof mustBeMetaMask !== 'boolean') {
  53. throw new Error(`@metamask/detect-provider: Expected option 'mustBeMetaMask' to be a boolean.`);
  54. }
  55. if (typeof silent !== 'boolean') {
  56. throw new Error(`@metamask/detect-provider: Expected option 'silent' to be a boolean.`);
  57. }
  58. if (typeof timeout !== 'number') {
  59. throw new Error(`@metamask/detect-provider: Expected option 'timeout' to be a number.`);
  60. }
  61. }
  62. }
  63. module.exports = detectEthereumProvider;
  64. },{}]},{},[1])(1)
  65. });