UNPKG

@thirdweb-dev/wallets

Version:

<p align="center"> <br /> <a href="https://thirdweb.com"><img src="https://github.com/thirdweb-dev/js/blob/main/legacy_packages/sdk/logo.svg?raw=true" width="200" alt=""/></a> <br /> </p> <h1 align="center">thirdweb Wallet SDK</h1> <p align="center"> <a h

90 lines (87 loc) 2.56 kB
import { _ as _defineProperty } from '../../../../dist/defineProperty-350fc508.browser.esm.js'; import { n as normalizeChainId } from '../../../../dist/normalizeChainId-1fb9aedf.browser.esm.js'; import { C as Connector } from '../../../../dist/connector-05689d68.browser.esm.js'; import { getChainProvider } from '@thirdweb-dev/sdk'; import 'eventemitter3'; class SignerConnector extends Connector { constructor(options) { super(); _defineProperty(this, "onChainChanged", chainId => { const id = normalizeChainId(chainId); const unsupported = !this.options.chains.find(c => c.chainId === id); this.emit("change", { chain: { id, unsupported } }); }); this.options = options; } async connect(args) { if (args.chainId) { this.switchChain(args.chainId); } const signer = await this.getSigner(); const address = await signer.getAddress(); return address; } async disconnect() { this._provider = undefined; this._signer = undefined; } async getAddress() { const signer = await this.getSigner(); if (!signer) { throw new Error("No signer found"); } return await signer.getAddress(); } async isConnected() { try { const addr = await this.getAddress(); return !!addr; } catch { return false; } } async getProvider() { if (!this._provider) { this._provider = getChainProvider(this.options.chain, { clientId: this.options.clientId, secretKey: this.options.secretKey }); } return this._provider; } async getSigner() { if (!this._signer) { const provider = await this.getProvider(); this._signer = getUpdatedSigner(this.options.signer, provider); } return this._signer; } async switchChain(chainId) { const chain = this.options.chains.find(c => c.chainId === chainId); if (!chain) { throw new Error(`Chain not found for chainId ${chainId}, please add it to the chains property when creating this wallet`); } this._provider = getChainProvider(chain, { clientId: this.options.clientId, secretKey: this.options.secretKey }); this._signer = getUpdatedSigner(this.options.signer, this._provider); this.onChainChanged(chainId); } async setupListeners() {} updateChains(chains) { this.options.chains = chains; } } function getUpdatedSigner(signer, provider) { if (provider) { return signer.connect(provider); } return signer; } export { SignerConnector };