UNPKG

@web3-react/walletconnect-v2

Version:
181 lines (180 loc) 9.44 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WalletConnect = exports.URI_AVAILABLE = void 0; const types_1 = require("@web3-react/types"); const eventemitter3_1 = __importDefault(require("eventemitter3")); const utils_1 = require("./utils"); exports.URI_AVAILABLE = 'URI_AVAILABLE'; const DEFAULT_TIMEOUT = 5000; class WalletConnect extends types_1.Connector { constructor({ actions, defaultChainId, options, timeout = DEFAULT_TIMEOUT, onError }) { super(actions, onError); this.events = new eventemitter3_1.default(); this.disconnectListener = (error) => { var _a; this.actions.resetState(); if (error) (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error); }; this.chainChangedListener = (chainId) => { this.actions.update({ chainId: Number.parseInt(chainId, 16) }); }; this.accountsChangedListener = (accounts) => { this.actions.update({ accounts }); }; this.URIListener = (uri) => { this.events.emit(exports.URI_AVAILABLE, uri); }; const { rpcMap, rpc } = options, rest = __rest(options, ["rpcMap", "rpc"]); this.options = rest; this.defaultChainId = defaultChainId; this.rpcMap = rpcMap || rpc; this.timeout = timeout; const { chains, optionalChains } = this.getChainProps(rest.chains, rest.optionalChains, defaultChainId); this.chains = chains; this.optionalChains = optionalChains; } initializeProvider(desiredChainId = this.defaultChainId) { return __awaiter(this, void 0, void 0, function* () { const rpcMap = this.rpcMap ? (0, utils_1.getBestUrlMap)(this.rpcMap, this.timeout) : undefined; const chainProps = this.getChainProps(this.chains, this.optionalChains, desiredChainId); const ethProviderModule = yield Promise.resolve().then(() => __importStar(require('@walletconnect/ethereum-provider'))); this.provider = yield ethProviderModule.default.init(Object.assign(Object.assign(Object.assign({}, this.options), chainProps), { rpcMap: yield rpcMap })); return this.provider .on('disconnect', this.disconnectListener) .on('chainChanged', this.chainChangedListener) .on('accountsChanged', this.accountsChangedListener) .on('display_uri', this.URIListener); }); } getChainProps(chains, optionalChains, desiredChainId = this.defaultChainId) { // Reorder chains and optionalChains if necessary const orderedChains = (0, utils_1.getChainsWithDefault)(chains, desiredChainId); const orderedOptionalChains = (0, utils_1.getChainsWithDefault)(optionalChains, desiredChainId); // Validate and return the result. // Type discrimination requires that we use these typeguard checks to guarantee a valid return type. if ((0, utils_1.isArrayOneOrMore)(orderedChains)) { return { chains: orderedChains, optionalChains: orderedOptionalChains }; } else if ((0, utils_1.isArrayOneOrMore)(orderedOptionalChains)) { return { chains: orderedChains, optionalChains: orderedOptionalChains }; } throw new Error('Either chains or optionalChains must have at least one item.'); } isomorphicInitialize(desiredChainId = this.defaultChainId) { if (this.eagerConnection) return this.eagerConnection; return (this.eagerConnection = this.initializeProvider(desiredChainId)); } /** {@inheritdoc Connector.connectEagerly} */ connectEagerly() { return __awaiter(this, void 0, void 0, function* () { const cancelActivation = this.actions.startActivation(); try { const provider = yield this.isomorphicInitialize(); // WalletConnect automatically persists and restores active sessions if (!provider.session) { throw new Error('No active session found. Connect your wallet first.'); } this.actions.update({ accounts: provider.accounts, chainId: provider.chainId }); } catch (error) { yield this.deactivate(); cancelActivation(); throw error; } }); } /** * @param desiredChainId - The desired chainId to connect to. */ activate(desiredChainId) { var _a; return __awaiter(this, void 0, void 0, function* () { const provider = yield this.isomorphicInitialize(desiredChainId); if (provider.session) { if (!desiredChainId || desiredChainId === provider.chainId) return; // WalletConnect exposes connected accounts, not chains: `eip155:${chainId}:${address}` const isConnectedToDesiredChain = provider.session.namespaces.eip155.accounts.some((account) => account.startsWith(`eip155:${desiredChainId}:`)); if (!isConnectedToDesiredChain) { if ((_a = this.options.optionalChains) === null || _a === void 0 ? void 0 : _a.includes(desiredChainId)) { throw new Error(`Cannot activate an optional chain (${desiredChainId}), as the wallet is not connected to it.\n\tYou should handle this error in application code, as there is no guarantee that a wallet is connected to a chain configured in "optionalChains".`); } throw new Error(`Unknown chain (${desiredChainId}). Make sure to include any chains you might connect to in the "chains" or "optionalChains" parameters when initializing WalletConnect.`); } return provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: `0x${desiredChainId.toString(16)}` }], }); } const cancelActivation = this.actions.startActivation(); try { yield provider.enable(); this.actions.update({ chainId: provider.chainId, accounts: provider.accounts }); } catch (error) { yield this.deactivate(); cancelActivation(); throw error; } }); } /** {@inheritdoc Connector.deactivate} */ deactivate() { var _a; return __awaiter(this, void 0, void 0, function* () { (_a = this.provider) === null || _a === void 0 ? void 0 : _a.removeListener('disconnect', this.disconnectListener).removeListener('chainChanged', this.chainChangedListener).removeListener('accountsChanged', this.accountsChangedListener).removeListener('display_uri', this.URIListener).disconnect(); this.provider = undefined; this.eagerConnection = undefined; this.actions.resetState(); }); } } exports.WalletConnect = WalletConnect;