UNPKG

@0xsequence/connect

Version:
90 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ecosystemWallet = ecosystemWallet; const ethers_1 = require("ethers"); const viem_1 = require("viem"); const wagmi_1 = require("wagmi"); const helpers_js_1 = require("../../utils/helpers.js"); const provider_js_1 = require("./provider.js"); ecosystemWallet.type = 'ecosystem-wallet'; function ecosystemWallet(params) { const nodesUrl = params.nodesUrl ?? 'https://nodes.sequence.app'; const ecosystemProvider = new provider_js_1.EcosystemWalletTransportProvider(params.projectAccessKey, params.walletUrl, params.defaultNetwork, nodesUrl); return (0, wagmi_1.createConnector)(config => ({ id: `ecosystem-wallet`, name: 'Ecosystem Wallet', type: ecosystemWallet.type, ecosystemProvider, params, auxData: undefined, async setup() { if (typeof window !== 'object') { // (for SSR) only run in browser client return; } }, async connect(_connectInfo) { const provider = await this.getProvider(); let walletAddress = provider.transport.getWalletAddress(); if (!walletAddress) { try { const res = await provider.transport.connect(this.auxData); walletAddress = res.walletAddress; } catch (e) { console.log(e); await this.disconnect(); throw e; } } return { accounts: [(0, viem_1.getAddress)(walletAddress)], chainId: await this.getChainId() }; }, async disconnect() { const provider = await this.getProvider(); provider.transport.disconnect(); }, async getAccounts() { const provider = await this.getProvider(); const address = provider.transport.getWalletAddress(); if (address) { return [(0, viem_1.getAddress)(address)]; } return []; }, async getProvider() { return ecosystemProvider; }, async isAuthorized() { const provider = await this.getProvider(); return provider.transport.getWalletAddress() !== undefined; }, async switchChain({ chainId }) { const provider = await this.getProvider(); const chain = config.chains.find(c => c.id === chainId) || config.chains[0]; await provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: ethers_1.ethers.toQuantity(chainId) }] }); config.emitter.emit('change', { chainId }); return chain; }, async getChainId() { const provider = await this.getProvider(); return Number(provider.getChainId()); }, async onAccountsChanged(accounts) { return { account: accounts[0] }; }, async onChainChanged(chain) { config.emitter.emit('change', { chainId: (0, helpers_js_1.normalizeChainId)(chain) }); }, async onConnect(_connectInfo) { }, async onDisconnect() { await this.disconnect(); } })); } //# sourceMappingURL=ecosystemWallet.js.map