UNPKG

@web3-vue-org2/crypto-com

Version:

214 lines (213 loc) 10.8 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CryptoCom = exports.NoCryptoComError = void 0; const types_1 = require("@web3-vue-org2/types"); class NoCryptoComError extends Error { constructor() { super('Crypto.com not installed'); this.name = NoCryptoComError.name; Object.setPrototypeOf(this, NoCryptoComError.prototype); } } exports.NoCryptoComError = NoCryptoComError; function parseChainId(chainId) { return Number.parseInt(chainId, 16); } class CryptoCom extends types_1.Connector { constructor({ actions, options, onError }) { super(actions, true, onError); this.onConnect = ({ chainId }) => { this.actions.update({ chainId: parseChainId(chainId) }); }; this.onDisconnect = (error) => { var _a; // 1013 indicates that Coin98 is attempting to reestablish the connection // https://github.com/MetaMask/providers/releases/tag/v8.0.0 if (error.code === 1013) { console.debug('Crypto.com logged connection error 1013: "Try again later"'); return; } this.actions.resetState(); (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error); }; this.onChainChanged = (chainId) => { this.actions.update({ chainId: parseChainId(chainId) }); }; this.onAccountsChanged = (accounts) => { if (accounts.length === 0) { // handle this edge case by disconnecting this.actions.resetState(); } else { this.actions.update({ accounts }); } }; this.options = options; } isomorphicInitialize() { return __awaiter(this, void 0, void 0, function* () { if (this.eagerConnection) return; return (this.eagerConnection = Promise.resolve().then(() => __importStar(require('@deficonnect/provider'))).then((m) => { const provider = new m.DeFiConnectProvider(this.options); if (provider) { this.provider = provider; this.provider.on('connect', this.onConnect); this.provider.on('disconnect', this.onDisconnect); this.provider.on('chainChanged', this.onChainChanged); this.provider.on('accountsChanged', this.onAccountsChanged); } })); }); } /** {@inheritdoc Connector.connectEagerly} */ connectEagerly() { return __awaiter(this, void 0, void 0, function* () { const cancelActivation = this.actions.startActivation(); try { yield this.isomorphicInitialize(); if (!this.provider) return cancelActivation(); // Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing // chains; they should be requested serially, with accounts first, so that the chainId can settle. const accounts = (yield this.provider.request({ method: 'eth_accounts' })); if (!accounts.length) throw new Error('No accounts returned'); const chainId = (yield this.provider.request({ method: 'eth_chainId' })); this.actions.update({ chainId: parseChainId(chainId), accounts, changing: false }); } catch (error) { console.debug('Could not connect eagerly', error); // we should be able to use `cancelActivation` here, but on mobile, metamask emits a 'connect' // event, meaning that chainId is updated, and cancelActivation doesn't work because an intermediary // update has occurred, so we reset state instead this.actions.resetState(); } }); } _activate(desiredChainIdOrChainParameters) { return __awaiter(this, void 0, void 0, function* () { return this.isomorphicInitialize().then(() => __awaiter(this, void 0, void 0, function* () { if (!this.provider) throw new NoCryptoComError(); // Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing // chains; they should be requested serially, with accounts first, so that the chainId can settle. const accounts = (yield this.provider.request({ method: 'eth_requestAccounts' })); const chainId = (yield this.provider.request({ method: 'eth_chainId' })); const receivedChainId = parseChainId(chainId); const desiredChainId = typeof desiredChainIdOrChainParameters === 'number' ? desiredChainIdOrChainParameters : desiredChainIdOrChainParameters === null || desiredChainIdOrChainParameters === void 0 ? void 0 : desiredChainIdOrChainParameters.chainId; // if there's no desired chain, or it's equal to the received, update if (!desiredChainId || receivedChainId === desiredChainId) return this.actions.update({ chainId: receivedChainId, accounts, changing: false }); const desiredChainIdHex = `0x${desiredChainId.toString(16)}`; // if we're here, we can try to switch networks return this.provider .request({ method: 'wallet_switchEthereumChain', params: [{ chainId: desiredChainIdHex }], }) .catch((error) => { if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') { if (!this.provider) throw new Error('No provider'); // if we're here, we can try to add a new network return this.provider.request({ method: 'wallet_addEthereumChain', params: [Object.assign(Object.assign({}, desiredChainIdOrChainParameters), { chainId: desiredChainIdHex })], }); } throw error; }) .then(() => this.activate(desiredChainId)); })); }); } /** * Initiates a connection. * * @param desiredChainIdOrChainParameters - If defined, indicates the desired chain to connect to. If the user is * already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch * to the chain, if one of two conditions is met: either they already have it added in their extension, or the * argument is of type AddEthereumChainParameter, in which case the user will be prompted to add the chain with the * specified parameters first, before being prompted to switch. */ activate(desiredChainIdOrChainParameters) { var _a; return __awaiter(this, void 0, void 0, function* () { return this.startActive(!!((_a = this.provider) === null || _a === void 0 ? void 0 : _a.connected), desiredChainIdOrChainParameters); }); } watchAsset({ address, symbol, decimals, image }) { return __awaiter(this, void 0, void 0, function* () { if (!this.provider) throw new Error('No provider'); return this.provider .request({ method: 'wallet_watchAsset', params: { type: 'ERC20', options: { address, symbol, decimals, image, // A string url of the token logo }, }, }) .then((success) => { if (!success) throw new Error('Rejected'); return true; }); }); } deactivate() { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { (_a = this.provider) === null || _a === void 0 ? void 0 : _a.off('disconnect', this.onDisconnect); (_b = this.provider) === null || _b === void 0 ? void 0 : _b.off('chainChanged', this.onChainChanged); (_c = this.provider) === null || _c === void 0 ? void 0 : _c.off('accountsChanged', this.onAccountsChanged); // we don't unregister the display_uri handler because the walletconnect types/inheritances are really broken. // it doesn't matter, anyway, as the connector object is destroyed yield ((_d = this.provider) === null || _d === void 0 ? void 0 : _d.close()); this.provider = undefined; this.eagerConnection = undefined; this.actions.resetState(); }); } } exports.CryptoCom = CryptoCom;