@web3auth/metamask-adapter
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -57,5 +57,32 @@ /******/ (() => { // webpackBootstrap | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/asyncToGenerator" | ||
const asyncToGenerator_namespaceObject = require("@babel/runtime/helpers/asyncToGenerator"); | ||
var asyncToGenerator_default = /*#__PURE__*/__webpack_require__.n(asyncToGenerator_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/classCallCheck" | ||
const classCallCheck_namespaceObject = require("@babel/runtime/helpers/classCallCheck"); | ||
var classCallCheck_default = /*#__PURE__*/__webpack_require__.n(classCallCheck_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/createClass" | ||
const createClass_namespaceObject = require("@babel/runtime/helpers/createClass"); | ||
var createClass_default = /*#__PURE__*/__webpack_require__.n(createClass_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/assertThisInitialized" | ||
const assertThisInitialized_namespaceObject = require("@babel/runtime/helpers/assertThisInitialized"); | ||
var assertThisInitialized_default = /*#__PURE__*/__webpack_require__.n(assertThisInitialized_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/get" | ||
const get_namespaceObject = require("@babel/runtime/helpers/get"); | ||
var get_default = /*#__PURE__*/__webpack_require__.n(get_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/inherits" | ||
const inherits_namespaceObject = require("@babel/runtime/helpers/inherits"); | ||
var inherits_default = /*#__PURE__*/__webpack_require__.n(inherits_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/possibleConstructorReturn" | ||
const possibleConstructorReturn_namespaceObject = require("@babel/runtime/helpers/possibleConstructorReturn"); | ||
var possibleConstructorReturn_default = /*#__PURE__*/__webpack_require__.n(possibleConstructorReturn_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/getPrototypeOf" | ||
const getPrototypeOf_namespaceObject = require("@babel/runtime/helpers/getPrototypeOf"); | ||
var getPrototypeOf_default = /*#__PURE__*/__webpack_require__.n(getPrototypeOf_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/helpers/defineProperty" | ||
const defineProperty_namespaceObject = require("@babel/runtime/helpers/defineProperty"); | ||
var defineProperty_default = /*#__PURE__*/__webpack_require__.n(defineProperty_namespaceObject); | ||
;// CONCATENATED MODULE: external "@babel/runtime/regenerator" | ||
const regenerator_namespaceObject = require("@babel/runtime/regenerator"); | ||
var regenerator_default = /*#__PURE__*/__webpack_require__.n(regenerator_namespaceObject); | ||
;// CONCATENATED MODULE: external "@metamask/detect-provider" | ||
@@ -74,167 +101,377 @@ const detect_provider_namespaceObject = require("@metamask/detect-provider"); | ||
class MetamaskAdapter extends base_evm_adapter_namespaceObject.BaseEvmAdapter { | ||
constructor(adapterOptions) { | ||
super(adapterOptions); | ||
defineProperty_default()(this, "adapterNamespace", base_namespaceObject.ADAPTER_NAMESPACES.EIP155); | ||
defineProperty_default()(this, "currentChainNamespace", base_namespaceObject.CHAIN_NAMESPACES.EIP155); | ||
defineProperty_default()(this, "type", base_namespaceObject.ADAPTER_CATEGORY.EXTERNAL); | ||
defineProperty_default()(this, "name", base_namespaceObject.WALLET_ADAPTERS.METAMASK); | ||
defineProperty_default()(this, "status", base_namespaceObject.ADAPTER_STATUS.NOT_READY); | ||
defineProperty_default()(this, "rehydrated", false); | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = getPrototypeOf_default()(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = getPrototypeOf_default()(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return possibleConstructorReturn_default()(this, result); }; } | ||
defineProperty_default()(this, "metamaskProvider", null); | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
this.chainConfig = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.chainConfig) || null; | ||
this.sessionTime = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.sessionTime) || 86400; | ||
var MetamaskAdapter = /*#__PURE__*/function (_BaseEvmAdapter) { | ||
inherits_default()(MetamaskAdapter, _BaseEvmAdapter); | ||
var _super = _createSuper(MetamaskAdapter); | ||
function MetamaskAdapter(adapterOptions) { | ||
var _this; | ||
classCallCheck_default()(this, MetamaskAdapter); | ||
_this = _super.call(this, adapterOptions); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "adapterNamespace", base_namespaceObject.ADAPTER_NAMESPACES.EIP155); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "currentChainNamespace", base_namespaceObject.CHAIN_NAMESPACES.EIP155); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "type", base_namespaceObject.ADAPTER_CATEGORY.EXTERNAL); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "name", base_namespaceObject.WALLET_ADAPTERS.METAMASK); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "status", base_namespaceObject.ADAPTER_STATUS.NOT_READY); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "rehydrated", false); | ||
defineProperty_default()(assertThisInitialized_default()(_this), "metamaskProvider", null); | ||
_this.chainConfig = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.chainConfig) || null; | ||
_this.sessionTime = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.sessionTime) || 86400; | ||
return _this; | ||
} | ||
get provider() { | ||
if (this.status === base_namespaceObject.ADAPTER_STATUS.CONNECTED && this.metamaskProvider) { | ||
return this.metamaskProvider; | ||
createClass_default()(MetamaskAdapter, [{ | ||
key: "provider", | ||
get: function get() { | ||
if (this.status === base_namespaceObject.ADAPTER_STATUS.CONNECTED && this.metamaskProvider) { | ||
return this.metamaskProvider; | ||
} | ||
return null; | ||
}, | ||
set: function set(_) { | ||
throw new Error("Not implemented"); | ||
} | ||
}, { | ||
key: "init", | ||
value: function () { | ||
var _init = asyncToGenerator_default()( /*#__PURE__*/regenerator_default().mark(function _callee(options) { | ||
return regenerator_default().wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
get_default()(getPrototypeOf_default()(MetamaskAdapter.prototype), "checkInitializationRequirements", this).call(this); | ||
return null; | ||
} | ||
_context.next = 3; | ||
return detect_provider_default()({ | ||
mustBeMetaMask: true | ||
}); | ||
set provider(_) { | ||
throw new Error("Not implemented"); | ||
} | ||
case 3: | ||
this.metamaskProvider = _context.sent; | ||
async init(options) { | ||
super.checkInitializationRequirements(); | ||
this.metamaskProvider = await detect_provider_default()({ | ||
mustBeMetaMask: true | ||
}); | ||
if (!this.metamaskProvider) throw base_namespaceObject.WalletInitializationError.notInstalled("Metamask extension is not installed"); | ||
this.status = base_namespaceObject.ADAPTER_STATUS.READY; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.READY, base_namespaceObject.WALLET_ADAPTERS.METAMASK); | ||
if (this.metamaskProvider) { | ||
_context.next = 6; | ||
break; | ||
} | ||
try { | ||
base_namespaceObject.log.debug("initializing metamask adapter"); | ||
throw base_namespaceObject.WalletInitializationError.notInstalled("Metamask extension is not installed"); | ||
if (options.autoConnect) { | ||
this.rehydrated = true; | ||
await this.connect(); | ||
case 6: | ||
this.status = base_namespaceObject.ADAPTER_STATUS.READY; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.READY, base_namespaceObject.WALLET_ADAPTERS.METAMASK); | ||
_context.prev = 8; | ||
base_namespaceObject.log.debug("initializing metamask adapter"); | ||
if (!options.autoConnect) { | ||
_context.next = 14; | ||
break; | ||
} | ||
this.rehydrated = true; | ||
_context.next = 14; | ||
return this.connect(); | ||
case 14: | ||
_context.next = 19; | ||
break; | ||
case 16: | ||
_context.prev = 16; | ||
_context.t0 = _context["catch"](8); | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.ERRORED, _context.t0); | ||
case 19: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this, [[8, 16]]); | ||
})); | ||
function init(_x) { | ||
return _init.apply(this, arguments); | ||
} | ||
} catch (error) { | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.ERRORED, error); | ||
} | ||
} | ||
setAdapterSettings(options) { | ||
if (this.status === base_namespaceObject.ADAPTER_STATUS.READY) return; | ||
return init; | ||
}() | ||
}, { | ||
key: "setAdapterSettings", | ||
value: function setAdapterSettings(options) { | ||
if (this.status === base_namespaceObject.ADAPTER_STATUS.READY) return; | ||
if (options !== null && options !== void 0 && options.sessionTime) { | ||
this.sessionTime = options.sessionTime; | ||
} | ||
if (options !== null && options !== void 0 && options.sessionTime) { | ||
this.sessionTime = options.sessionTime; | ||
} | ||
if (options !== null && options !== void 0 && options.clientId) { | ||
this.clientId = options.clientId; | ||
if (options !== null && options !== void 0 && options.clientId) { | ||
this.clientId = options.clientId; | ||
} | ||
} | ||
} | ||
}, { | ||
key: "connect", | ||
value: function () { | ||
var _connect = asyncToGenerator_default()( /*#__PURE__*/regenerator_default().mark(function _callee2() { | ||
var _this2 = this; | ||
async connect() { | ||
super.checkConnectionRequirements(); // set default to mainnet | ||
var chainId; | ||
return regenerator_default().wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
get_default()(getPrototypeOf_default()(MetamaskAdapter.prototype), "checkConnectionRequirements", this).call(this); // set default to mainnet | ||
if (!this.chainConfig) this.chainConfig = (0,base_namespaceObject.getChainConfig)(base_namespaceObject.CHAIN_NAMESPACES.EIP155, 1); | ||
this.status = base_namespaceObject.ADAPTER_STATUS.CONNECTING; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.CONNECTING, { | ||
adapter: base_namespaceObject.WALLET_ADAPTERS.METAMASK | ||
}); | ||
if (!this.metamaskProvider) throw base_namespaceObject.WalletLoginError.notConnectedError("Not able to connect with metamask"); | ||
try { | ||
await this.metamaskProvider.request({ | ||
method: "eth_requestAccounts" | ||
}); | ||
const { | ||
chainId | ||
} = this.metamaskProvider; | ||
if (!this.chainConfig) this.chainConfig = (0,base_namespaceObject.getChainConfig)(base_namespaceObject.CHAIN_NAMESPACES.EIP155, 1); | ||
this.status = base_namespaceObject.ADAPTER_STATUS.CONNECTING; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.CONNECTING, { | ||
adapter: base_namespaceObject.WALLET_ADAPTERS.METAMASK | ||
}); | ||
if (chainId !== this.chainConfig.chainId) { | ||
await this.switchChain(this.chainConfig); | ||
if (this.metamaskProvider) { | ||
_context2.next = 6; | ||
break; | ||
} | ||
throw base_namespaceObject.WalletLoginError.notConnectedError("Not able to connect with metamask"); | ||
case 6: | ||
_context2.prev = 6; | ||
_context2.next = 9; | ||
return this.metamaskProvider.request({ | ||
method: "eth_requestAccounts" | ||
}); | ||
case 9: | ||
chainId = this.metamaskProvider.chainId; | ||
if (!(chainId !== this.chainConfig.chainId)) { | ||
_context2.next = 13; | ||
break; | ||
} | ||
_context2.next = 13; | ||
return this.switchChain(this.chainConfig); | ||
case 13: | ||
this.status = base_namespaceObject.ADAPTER_STATUS.CONNECTED; | ||
if (this.provider) { | ||
_context2.next = 16; | ||
break; | ||
} | ||
throw base_namespaceObject.WalletLoginError.notConnectedError("Failed to connect with provider"); | ||
case 16: | ||
this.provider.once("disconnect", function () { | ||
// ready to be connected again | ||
_this2.disconnect(); | ||
}); | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.CONNECTED, { | ||
adapter: base_namespaceObject.WALLET_ADAPTERS.METAMASK, | ||
reconnected: this.rehydrated | ||
}); | ||
return _context2.abrupt("return", this.provider); | ||
case 21: | ||
_context2.prev = 21; | ||
_context2.t0 = _context2["catch"](6); | ||
// ready again to be connected | ||
this.status = base_namespaceObject.ADAPTER_STATUS.READY; | ||
this.rehydrated = false; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.ERRORED, _context2.t0); | ||
throw base_namespaceObject.WalletLoginError.connectionError("Failed to login with metamask wallet"); | ||
case 27: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this, [[6, 21]]); | ||
})); | ||
function connect() { | ||
return _connect.apply(this, arguments); | ||
} | ||
this.status = base_namespaceObject.ADAPTER_STATUS.CONNECTED; | ||
if (!this.provider) throw base_namespaceObject.WalletLoginError.notConnectedError("Failed to connect with provider"); | ||
this.provider.once("disconnect", () => { | ||
// ready to be connected again | ||
this.disconnect(); | ||
}); | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.CONNECTED, { | ||
adapter: base_namespaceObject.WALLET_ADAPTERS.METAMASK, | ||
reconnected: this.rehydrated | ||
}); | ||
return this.provider; | ||
} catch (error) { | ||
// ready again to be connected | ||
this.status = base_namespaceObject.ADAPTER_STATUS.READY; | ||
this.rehydrated = false; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.ERRORED, error); | ||
throw base_namespaceObject.WalletLoginError.connectionError("Failed to login with metamask wallet"); | ||
} | ||
} | ||
return connect; | ||
}() | ||
}, { | ||
key: "disconnect", | ||
value: function () { | ||
var _disconnect = asyncToGenerator_default()( /*#__PURE__*/regenerator_default().mark(function _callee3() { | ||
var _this$provider; | ||
async disconnect() { | ||
var _this$provider; | ||
var options, | ||
_args3 = arguments; | ||
return regenerator_default().wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
options = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : { | ||
cleanup: false | ||
}; | ||
_context3.next = 3; | ||
return get_default()(getPrototypeOf_default()(MetamaskAdapter.prototype), "disconnect", this).call(this); | ||
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
cleanup: false | ||
}; | ||
await super.disconnect(); | ||
(_this$provider = this.provider) === null || _this$provider === void 0 ? void 0 : _this$provider.removeAllListeners(); | ||
case 3: | ||
(_this$provider = this.provider) === null || _this$provider === void 0 ? void 0 : _this$provider.removeAllListeners(); | ||
if (options.cleanup) { | ||
this.status = base_namespaceObject.ADAPTER_STATUS.NOT_READY; | ||
this.metamaskProvider = null; | ||
} else { | ||
// ready to be connected again | ||
this.status = base_namespaceObject.ADAPTER_STATUS.READY; | ||
} | ||
if (options.cleanup) { | ||
this.status = base_namespaceObject.ADAPTER_STATUS.NOT_READY; | ||
this.metamaskProvider = null; | ||
} else { | ||
// ready to be connected again | ||
this.status = base_namespaceObject.ADAPTER_STATUS.READY; | ||
} | ||
this.rehydrated = false; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.DISCONNECTED); | ||
} | ||
this.rehydrated = false; | ||
this.emit(base_namespaceObject.ADAPTER_EVENTS.DISCONNECTED); | ||
async getUserInfo() { | ||
if (this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); | ||
return {}; | ||
} | ||
case 7: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
})); | ||
async switchChain(chainConfig) { | ||
if (!this.metamaskProvider) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet"); | ||
function disconnect() { | ||
return _disconnect.apply(this, arguments); | ||
} | ||
try { | ||
await this.metamaskProvider.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId | ||
}] | ||
}); | ||
} catch (switchError) { | ||
// This error code indicates that the chain has not been added to MetaMask. | ||
if (switchError.code === 4902) { | ||
await this.metamaskProvider.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId, | ||
chainName: chainConfig.displayName, | ||
rpcUrls: [chainConfig.rpcTarget] | ||
}] | ||
}); | ||
} else { | ||
throw switchError; | ||
return disconnect; | ||
}() | ||
}, { | ||
key: "getUserInfo", | ||
value: function () { | ||
var _getUserInfo = asyncToGenerator_default()( /*#__PURE__*/regenerator_default().mark(function _callee4() { | ||
return regenerator_default().wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
if (!(this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED)) { | ||
_context4.next = 2; | ||
break; | ||
} | ||
throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); | ||
case 2: | ||
return _context4.abrupt("return", {}); | ||
case 3: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
function getUserInfo() { | ||
return _getUserInfo.apply(this, arguments); | ||
} | ||
} | ||
} | ||
} | ||
return getUserInfo; | ||
}() | ||
}, { | ||
key: "switchChain", | ||
value: function () { | ||
var _switchChain = asyncToGenerator_default()( /*#__PURE__*/regenerator_default().mark(function _callee5(chainConfig) { | ||
return regenerator_default().wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
if (this.metamaskProvider) { | ||
_context5.next = 2; | ||
break; | ||
} | ||
throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet"); | ||
case 2: | ||
_context5.prev = 2; | ||
_context5.next = 5; | ||
return this.metamaskProvider.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId | ||
}] | ||
}); | ||
case 5: | ||
_context5.next = 15; | ||
break; | ||
case 7: | ||
_context5.prev = 7; | ||
_context5.t0 = _context5["catch"](2); | ||
if (!(_context5.t0.code === 4902)) { | ||
_context5.next = 14; | ||
break; | ||
} | ||
_context5.next = 12; | ||
return this.metamaskProvider.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId, | ||
chainName: chainConfig.displayName, | ||
rpcUrls: [chainConfig.rpcTarget] | ||
}] | ||
}); | ||
case 12: | ||
_context5.next = 15; | ||
break; | ||
case 14: | ||
throw _context5.t0; | ||
case 15: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this, [[2, 7]]); | ||
})); | ||
function switchChain(_x2) { | ||
return _switchChain.apply(this, arguments); | ||
} | ||
return switchChain; | ||
}() | ||
}]); | ||
return MetamaskAdapter; | ||
}(base_evm_adapter_namespaceObject.BaseEvmAdapter); | ||
;// CONCATENATED MODULE: ./src/index.ts | ||
@@ -241,0 +478,0 @@ |
@@ -0,171 +1,380 @@ | ||
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; | ||
import _classCallCheck from '@babel/runtime/helpers/classCallCheck'; | ||
import _createClass from '@babel/runtime/helpers/createClass'; | ||
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized'; | ||
import _get from '@babel/runtime/helpers/get'; | ||
import _inherits from '@babel/runtime/helpers/inherits'; | ||
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn'; | ||
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf'; | ||
import _defineProperty from '@babel/runtime/helpers/defineProperty'; | ||
import _regeneratorRuntime from '@babel/runtime/regenerator'; | ||
import detectEthereumProvider from '@metamask/detect-provider'; | ||
import { ADAPTER_NAMESPACES, CHAIN_NAMESPACES, ADAPTER_CATEGORY, WALLET_ADAPTERS, ADAPTER_STATUS, WalletInitializationError, ADAPTER_EVENTS, log, getChainConfig, WalletLoginError } from '@web3auth/base'; | ||
import { ADAPTER_NAMESPACES, CHAIN_NAMESPACES, ADAPTER_CATEGORY, WALLET_ADAPTERS, ADAPTER_STATUS, ADAPTER_EVENTS, log, WalletInitializationError, WalletLoginError, getChainConfig } from '@web3auth/base'; | ||
import { BaseEvmAdapter } from '@web3auth/base-evm-adapter'; | ||
class MetamaskAdapter extends BaseEvmAdapter { | ||
constructor(adapterOptions) { | ||
super(adapterOptions); | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
_defineProperty(this, "adapterNamespace", ADAPTER_NAMESPACES.EIP155); | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
_defineProperty(this, "currentChainNamespace", CHAIN_NAMESPACES.EIP155); | ||
var MetamaskAdapter = /*#__PURE__*/function (_BaseEvmAdapter) { | ||
_inherits(MetamaskAdapter, _BaseEvmAdapter); | ||
_defineProperty(this, "type", ADAPTER_CATEGORY.EXTERNAL); | ||
var _super = _createSuper(MetamaskAdapter); | ||
_defineProperty(this, "name", WALLET_ADAPTERS.METAMASK); | ||
function MetamaskAdapter(adapterOptions) { | ||
var _this; | ||
_defineProperty(this, "status", ADAPTER_STATUS.NOT_READY); | ||
_classCallCheck(this, MetamaskAdapter); | ||
_defineProperty(this, "rehydrated", false); | ||
_this = _super.call(this, adapterOptions); | ||
_defineProperty(this, "metamaskProvider", null); | ||
_defineProperty(_assertThisInitialized(_this), "adapterNamespace", ADAPTER_NAMESPACES.EIP155); | ||
this.chainConfig = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.chainConfig) || null; | ||
this.sessionTime = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.sessionTime) || 86400; | ||
_defineProperty(_assertThisInitialized(_this), "currentChainNamespace", CHAIN_NAMESPACES.EIP155); | ||
_defineProperty(_assertThisInitialized(_this), "type", ADAPTER_CATEGORY.EXTERNAL); | ||
_defineProperty(_assertThisInitialized(_this), "name", WALLET_ADAPTERS.METAMASK); | ||
_defineProperty(_assertThisInitialized(_this), "status", ADAPTER_STATUS.NOT_READY); | ||
_defineProperty(_assertThisInitialized(_this), "rehydrated", false); | ||
_defineProperty(_assertThisInitialized(_this), "metamaskProvider", null); | ||
_this.chainConfig = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.chainConfig) || null; | ||
_this.sessionTime = (adapterOptions === null || adapterOptions === void 0 ? void 0 : adapterOptions.sessionTime) || 86400; | ||
return _this; | ||
} | ||
get provider() { | ||
if (this.status === ADAPTER_STATUS.CONNECTED && this.metamaskProvider) { | ||
return this.metamaskProvider; | ||
_createClass(MetamaskAdapter, [{ | ||
key: "provider", | ||
get: function get() { | ||
if (this.status === ADAPTER_STATUS.CONNECTED && this.metamaskProvider) { | ||
return this.metamaskProvider; | ||
} | ||
return null; | ||
}, | ||
set: function set(_) { | ||
throw new Error("Not implemented"); | ||
} | ||
}, { | ||
key: "init", | ||
value: function () { | ||
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(options) { | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_get(_getPrototypeOf(MetamaskAdapter.prototype), "checkInitializationRequirements", this).call(this); | ||
return null; | ||
} | ||
_context.next = 3; | ||
return detectEthereumProvider({ | ||
mustBeMetaMask: true | ||
}); | ||
set provider(_) { | ||
throw new Error("Not implemented"); | ||
} | ||
case 3: | ||
this.metamaskProvider = _context.sent; | ||
async init(options) { | ||
super.checkInitializationRequirements(); | ||
this.metamaskProvider = await detectEthereumProvider({ | ||
mustBeMetaMask: true | ||
}); | ||
if (!this.metamaskProvider) throw WalletInitializationError.notInstalled("Metamask extension is not installed"); | ||
this.status = ADAPTER_STATUS.READY; | ||
this.emit(ADAPTER_EVENTS.READY, WALLET_ADAPTERS.METAMASK); | ||
if (this.metamaskProvider) { | ||
_context.next = 6; | ||
break; | ||
} | ||
try { | ||
log.debug("initializing metamask adapter"); | ||
throw WalletInitializationError.notInstalled("Metamask extension is not installed"); | ||
if (options.autoConnect) { | ||
this.rehydrated = true; | ||
await this.connect(); | ||
case 6: | ||
this.status = ADAPTER_STATUS.READY; | ||
this.emit(ADAPTER_EVENTS.READY, WALLET_ADAPTERS.METAMASK); | ||
_context.prev = 8; | ||
log.debug("initializing metamask adapter"); | ||
if (!options.autoConnect) { | ||
_context.next = 14; | ||
break; | ||
} | ||
this.rehydrated = true; | ||
_context.next = 14; | ||
return this.connect(); | ||
case 14: | ||
_context.next = 19; | ||
break; | ||
case 16: | ||
_context.prev = 16; | ||
_context.t0 = _context["catch"](8); | ||
this.emit(ADAPTER_EVENTS.ERRORED, _context.t0); | ||
case 19: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this, [[8, 16]]); | ||
})); | ||
function init(_x) { | ||
return _init.apply(this, arguments); | ||
} | ||
} catch (error) { | ||
this.emit(ADAPTER_EVENTS.ERRORED, error); | ||
} | ||
} | ||
setAdapterSettings(options) { | ||
if (this.status === ADAPTER_STATUS.READY) return; | ||
return init; | ||
}() | ||
}, { | ||
key: "setAdapterSettings", | ||
value: function setAdapterSettings(options) { | ||
if (this.status === ADAPTER_STATUS.READY) return; | ||
if (options !== null && options !== void 0 && options.sessionTime) { | ||
this.sessionTime = options.sessionTime; | ||
} | ||
if (options !== null && options !== void 0 && options.sessionTime) { | ||
this.sessionTime = options.sessionTime; | ||
} | ||
if (options !== null && options !== void 0 && options.clientId) { | ||
this.clientId = options.clientId; | ||
if (options !== null && options !== void 0 && options.clientId) { | ||
this.clientId = options.clientId; | ||
} | ||
} | ||
} | ||
}, { | ||
key: "connect", | ||
value: function () { | ||
var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { | ||
var _this2 = this; | ||
async connect() { | ||
super.checkConnectionRequirements(); // set default to mainnet | ||
var chainId; | ||
return _regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_get(_getPrototypeOf(MetamaskAdapter.prototype), "checkConnectionRequirements", this).call(this); // set default to mainnet | ||
if (!this.chainConfig) this.chainConfig = getChainConfig(CHAIN_NAMESPACES.EIP155, 1); | ||
this.status = ADAPTER_STATUS.CONNECTING; | ||
this.emit(ADAPTER_EVENTS.CONNECTING, { | ||
adapter: WALLET_ADAPTERS.METAMASK | ||
}); | ||
if (!this.metamaskProvider) throw WalletLoginError.notConnectedError("Not able to connect with metamask"); | ||
try { | ||
await this.metamaskProvider.request({ | ||
method: "eth_requestAccounts" | ||
}); | ||
const { | ||
chainId | ||
} = this.metamaskProvider; | ||
if (!this.chainConfig) this.chainConfig = getChainConfig(CHAIN_NAMESPACES.EIP155, 1); | ||
this.status = ADAPTER_STATUS.CONNECTING; | ||
this.emit(ADAPTER_EVENTS.CONNECTING, { | ||
adapter: WALLET_ADAPTERS.METAMASK | ||
}); | ||
if (chainId !== this.chainConfig.chainId) { | ||
await this.switchChain(this.chainConfig); | ||
if (this.metamaskProvider) { | ||
_context2.next = 6; | ||
break; | ||
} | ||
throw WalletLoginError.notConnectedError("Not able to connect with metamask"); | ||
case 6: | ||
_context2.prev = 6; | ||
_context2.next = 9; | ||
return this.metamaskProvider.request({ | ||
method: "eth_requestAccounts" | ||
}); | ||
case 9: | ||
chainId = this.metamaskProvider.chainId; | ||
if (!(chainId !== this.chainConfig.chainId)) { | ||
_context2.next = 13; | ||
break; | ||
} | ||
_context2.next = 13; | ||
return this.switchChain(this.chainConfig); | ||
case 13: | ||
this.status = ADAPTER_STATUS.CONNECTED; | ||
if (this.provider) { | ||
_context2.next = 16; | ||
break; | ||
} | ||
throw WalletLoginError.notConnectedError("Failed to connect with provider"); | ||
case 16: | ||
this.provider.once("disconnect", function () { | ||
// ready to be connected again | ||
_this2.disconnect(); | ||
}); | ||
this.emit(ADAPTER_EVENTS.CONNECTED, { | ||
adapter: WALLET_ADAPTERS.METAMASK, | ||
reconnected: this.rehydrated | ||
}); | ||
return _context2.abrupt("return", this.provider); | ||
case 21: | ||
_context2.prev = 21; | ||
_context2.t0 = _context2["catch"](6); | ||
// ready again to be connected | ||
this.status = ADAPTER_STATUS.READY; | ||
this.rehydrated = false; | ||
this.emit(ADAPTER_EVENTS.ERRORED, _context2.t0); | ||
throw WalletLoginError.connectionError("Failed to login with metamask wallet"); | ||
case 27: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this, [[6, 21]]); | ||
})); | ||
function connect() { | ||
return _connect.apply(this, arguments); | ||
} | ||
this.status = ADAPTER_STATUS.CONNECTED; | ||
if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider"); | ||
this.provider.once("disconnect", () => { | ||
// ready to be connected again | ||
this.disconnect(); | ||
}); | ||
this.emit(ADAPTER_EVENTS.CONNECTED, { | ||
adapter: WALLET_ADAPTERS.METAMASK, | ||
reconnected: this.rehydrated | ||
}); | ||
return this.provider; | ||
} catch (error) { | ||
// ready again to be connected | ||
this.status = ADAPTER_STATUS.READY; | ||
this.rehydrated = false; | ||
this.emit(ADAPTER_EVENTS.ERRORED, error); | ||
throw WalletLoginError.connectionError("Failed to login with metamask wallet"); | ||
} | ||
} | ||
return connect; | ||
}() | ||
}, { | ||
key: "disconnect", | ||
value: function () { | ||
var _disconnect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() { | ||
var _this$provider; | ||
async disconnect() { | ||
var _this$provider; | ||
var options, | ||
_args3 = arguments; | ||
return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
options = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : { | ||
cleanup: false | ||
}; | ||
_context3.next = 3; | ||
return _get(_getPrototypeOf(MetamaskAdapter.prototype), "disconnect", this).call(this); | ||
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
cleanup: false | ||
}; | ||
await super.disconnect(); | ||
(_this$provider = this.provider) === null || _this$provider === void 0 ? void 0 : _this$provider.removeAllListeners(); | ||
case 3: | ||
(_this$provider = this.provider) === null || _this$provider === void 0 ? void 0 : _this$provider.removeAllListeners(); | ||
if (options.cleanup) { | ||
this.status = ADAPTER_STATUS.NOT_READY; | ||
this.metamaskProvider = null; | ||
} else { | ||
// ready to be connected again | ||
this.status = ADAPTER_STATUS.READY; | ||
} | ||
if (options.cleanup) { | ||
this.status = ADAPTER_STATUS.NOT_READY; | ||
this.metamaskProvider = null; | ||
} else { | ||
// ready to be connected again | ||
this.status = ADAPTER_STATUS.READY; | ||
} | ||
this.rehydrated = false; | ||
this.emit(ADAPTER_EVENTS.DISCONNECTED); | ||
} | ||
this.rehydrated = false; | ||
this.emit(ADAPTER_EVENTS.DISCONNECTED); | ||
async getUserInfo() { | ||
if (this.status !== ADAPTER_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); | ||
return {}; | ||
} | ||
case 7: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
})); | ||
async switchChain(chainConfig) { | ||
if (!this.metamaskProvider) throw WalletLoginError.notConnectedError("Not connected with wallet"); | ||
function disconnect() { | ||
return _disconnect.apply(this, arguments); | ||
} | ||
try { | ||
await this.metamaskProvider.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId | ||
}] | ||
}); | ||
} catch (switchError) { | ||
// This error code indicates that the chain has not been added to MetaMask. | ||
if (switchError.code === 4902) { | ||
await this.metamaskProvider.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId, | ||
chainName: chainConfig.displayName, | ||
rpcUrls: [chainConfig.rpcTarget] | ||
}] | ||
}); | ||
} else { | ||
throw switchError; | ||
return disconnect; | ||
}() | ||
}, { | ||
key: "getUserInfo", | ||
value: function () { | ||
var _getUserInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() { | ||
return _regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
if (!(this.status !== ADAPTER_STATUS.CONNECTED)) { | ||
_context4.next = 2; | ||
break; | ||
} | ||
throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); | ||
case 2: | ||
return _context4.abrupt("return", {}); | ||
case 3: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
function getUserInfo() { | ||
return _getUserInfo.apply(this, arguments); | ||
} | ||
} | ||
} | ||
} | ||
return getUserInfo; | ||
}() | ||
}, { | ||
key: "switchChain", | ||
value: function () { | ||
var _switchChain = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(chainConfig) { | ||
return _regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
if (this.metamaskProvider) { | ||
_context5.next = 2; | ||
break; | ||
} | ||
throw WalletLoginError.notConnectedError("Not connected with wallet"); | ||
case 2: | ||
_context5.prev = 2; | ||
_context5.next = 5; | ||
return this.metamaskProvider.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId | ||
}] | ||
}); | ||
case 5: | ||
_context5.next = 15; | ||
break; | ||
case 7: | ||
_context5.prev = 7; | ||
_context5.t0 = _context5["catch"](2); | ||
if (!(_context5.t0.code === 4902)) { | ||
_context5.next = 14; | ||
break; | ||
} | ||
_context5.next = 12; | ||
return this.metamaskProvider.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [{ | ||
chainId: chainConfig.chainId, | ||
chainName: chainConfig.displayName, | ||
rpcUrls: [chainConfig.rpcTarget] | ||
}] | ||
}); | ||
case 12: | ||
_context5.next = 15; | ||
break; | ||
case 14: | ||
throw _context5.t0; | ||
case 15: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this, [[2, 7]]); | ||
})); | ||
function switchChain(_x2) { | ||
return _switchChain.apply(this, arguments); | ||
} | ||
return switchChain; | ||
}() | ||
}]); | ||
return MetamaskAdapter; | ||
}(BaseEvmAdapter); | ||
export { MetamaskAdapter }; | ||
//# sourceMappingURL=metamaskAdapter.esm.js.map |
@@ -10,2 +10,4 @@ /*! | ||
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ | ||
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */ |
{ | ||
"name": "@web3auth/metamask-adapter", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "metamask wallet adapter for web3auth", | ||
@@ -39,4 +39,4 @@ "keywords": [ | ||
"@metamask/detect-provider": "^1.2.0", | ||
"@web3auth/base": "^2.1.2", | ||
"@web3auth/base-evm-adapter": "^2.1.2" | ||
"@web3auth/base": "^2.1.3", | ||
"@web3auth/base-evm-adapter": "^2.1.3" | ||
}, | ||
@@ -59,3 +59,3 @@ "lint-staged": { | ||
}, | ||
"gitHead": "92d8964eae883b2e5ae01489a02484cc0a8f259a" | ||
"gitHead": "1d119ce5c57eebd7e6c10f6d484505b779542a0b" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
842636
1427
Updated@web3auth/base@^2.1.3