UNPKG

@web3-vue-org2/math-wallet

Version:

202 lines (201 loc) 10.6 kB
"use strict"; 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.MathWallet = exports.NoMathAddressError = exports.NoMathWalletError = void 0; const types_1 = require("@web3-vue-org2/types"); class NoMathWalletError extends Error { constructor() { super('MathWallet not installed'); this.name = NoMathWalletError.name; Object.setPrototypeOf(this, NoMathWalletError.prototype); } } exports.NoMathWalletError = NoMathWalletError; class NoMathAddressError extends Error { constructor() { super('Invalid wallet address on chain in Math Wallet'); this.name = NoMathAddressError.name; Object.setPrototypeOf(this, NoMathAddressError.prototype); } } exports.NoMathAddressError = NoMathAddressError; function parseChainId(chainId) { return Number.parseInt(chainId, 16); } class MathWallet extends types_1.Connector { constructor({ actions, 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('MathWallet 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) => { if (chainId === '0') { this.resetState(); return; } this.actions.update({ chainId: Number.parseInt(chainId) }); }; this.onAccountsChanged = (accounts) => { if (accounts.length === 0) { // handle this edge case by disconnecting this.actions.resetState(); } else { this.actions.update({ accounts }); } }; } isomorphicInitialize() { return __awaiter(this, void 0, void 0, function* () { if (this.eagerConnection) return; return (this.eagerConnection = Promise.resolve(window.ethereum).then((provider) => { if (provider && provider.isMathWallet) { 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(); } }); } /** * 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. * @param times activate 方法被循环调用的次数 */ _activate(desiredChainIdOrChainParameters, times = 0) { return __awaiter(this, void 0, void 0, function* () { return this.isomorphicInitialize() .then(() => __awaiter(this, void 0, void 0, function* () { if (!this.provider) throw new NoMathWalletError(); const desiredChainId = typeof desiredChainIdOrChainParameters === 'number' ? desiredChainIdOrChainParameters : desiredChainIdOrChainParameters === null || desiredChainIdOrChainParameters === void 0 ? void 0 : desiredChainIdOrChainParameters.chainId; try { // 如果是第二次请求,等待500ms,以便于切换成功 if (times > 0) { yield new Promise((resolve) => { setTimeout(() => resolve(undefined), 500); }); } // 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); // 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 }); } } catch (e) { // 如果不是account缺失,或者重试次数过高,则报错 if (e.message !== 'Please create or import undefined account first' || times > 2) { throw e; } } if (!desiredChainId) return; const desiredChainIdHex = `0x${desiredChainId.toString(16)}`; // if we're here, we can try to switch networks // 如果用户是循环调用,证明已经进行过切换网络操作,此时将直接进行下一次循环 return (times > 0 ? Promise.resolve() : this.provider .request({ method: 'wallet_switchEthereumChain', params: [{ chainId: desiredChainIdHex }], }) .catch((error) => { if (error.message === 'Invalid Network' && typeof desiredChainIdOrChainParameters !== 'number') { if (!this.provider) throw new Error('No provider'); // if we're here, we can try to add a new network this.provider.wallet_addEthereumChain({ params: [Object.assign(Object.assign({}, desiredChainIdOrChainParameters), { chainId: desiredChainIdHex })], }); return 'addedWallet'; } throw error; })).then((value) => { this._activate(desiredChainId, times + 1); }); })) .catch((error) => { var _a; if (error.message === 'Please create or import undefined account first') { (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, new NoMathAddressError()); } throw error; }); }); } /** * 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, _b; return __awaiter(this, void 0, void 0, function* () { return this.startActive(!!((_b = (_a = this.provider) === null || _a === void 0 ? void 0 : _a.isConnected) === null || _b === void 0 ? void 0 : _b.call(_a)), desiredChainIdOrChainParameters); }); } } exports.MathWallet = MathWallet;