UNPKG

@0xsequence/connect

Version:
105 lines 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EcosystemWalletTransportProvider = void 0; const network_1 = require("@0xsequence/network"); const ethers_1 = require("ethers"); const viem_1 = require("viem"); const helpers_js_1 = require("../../utils/helpers.js"); const providerTransport_js_1 = require("./providerTransport.js"); class EcosystemWalletTransportProvider extends ethers_1.ethers.AbstractProvider { projectAccessKey; walletUrl; initialChainId; nodesUrl; jsonRpcProvider; currentNetwork; transport; constructor(projectAccessKey, walletUrl, initialChainId, nodesUrl) { super(initialChainId); this.projectAccessKey = projectAccessKey; this.walletUrl = walletUrl; this.initialChainId = initialChainId; this.nodesUrl = nodesUrl; const initialChainName = network_1.allNetworks.find(n => n.chainId === initialChainId)?.name; const initialJsonRpcProvider = new ethers_1.ethers.JsonRpcProvider(`${nodesUrl}/${initialChainName}/${projectAccessKey}`); this.transport = new providerTransport_js_1.ProviderTransport(walletUrl); this.jsonRpcProvider = initialJsonRpcProvider; this.currentNetwork = ethers_1.ethers.Network.from(initialChainId); } async request({ method, params }) { if (method === 'wallet_switchEthereumChain') { const chainId = (0, helpers_js_1.normalizeChainId)(params?.[0].chainId); const networkName = network_1.allNetworks.find(n => n.chainId === chainId)?.name; const jsonRpcProvider = new ethers_1.ethers.JsonRpcProvider(`${this.nodesUrl}/${networkName}/${this.projectAccessKey}`); this.jsonRpcProvider = jsonRpcProvider; this.currentNetwork = ethers_1.ethers.Network.from(chainId); return null; } if (method === 'eth_chainId') { return ethers_1.ethers.toQuantity(this.currentNetwork.chainId); } if (method === 'eth_accounts') { const address = this.transport.getWalletAddress(); if (!address) { return []; } const account = (0, viem_1.getAddress)(address); return [account]; } if (method === 'eth_sendTransaction') { if (!params) { throw new Error('No params'); } try { const response = await this.transport.sendRequest(method, params, this.getChainId()); if (response.walletAddress && response.walletAddress !== this.transport.getWalletAddress()) { this.transport.disconnect(); throw new viem_1.TransactionRejectedRpcError(new Error('Wallet address mismatch, please reconnect.')); } if (response.code === 'transactionFailed') { throw new viem_1.TransactionRejectedRpcError(new Error(`Unable to send transaction: ${response.data.error}`)); } if (response.code === 'transactionReceipt') { const { txHash } = response.data; return txHash; } } catch (e) { console.log('error in sendTransaction', e); throw new viem_1.TransactionRejectedRpcError(new Error(`Unable to send transaction: ${e}`)); } } if (method === 'eth_sign' || method === 'eth_signTypedData' || method === 'eth_signTypedData_v4' || method === 'personal_sign') { if (!params) { throw new Error('No params'); } try { const response = await this.transport.sendRequest(method, params, this.getChainId()); if (response.walletAddress && response.walletAddress !== this.transport.getWalletAddress()) { this.transport.disconnect(); throw new viem_1.TransactionRejectedRpcError(new Error('Wallet address mismatch, please reconnect.')); } return response.data.signature; } catch (e) { console.log('error in sign', e); throw new viem_1.TransactionRejectedRpcError(new Error(`Unable to sign: ${e}`)); } } return await this.jsonRpcProvider.send(method, params ?? []); } async getTransaction(txHash) { return await this.jsonRpcProvider.getTransaction(txHash); } detectNetwork() { return Promise.resolve(this.currentNetwork); } getChainId() { return Number(this.currentNetwork.chainId); } } exports.EcosystemWalletTransportProvider = EcosystemWalletTransportProvider; //# sourceMappingURL=provider.js.map