@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
230 lines (226 loc) • 7.54 kB
JavaScript
import { _ as _defineProperty } from '../../../../dist/defineProperty-350fc508.browser.esm.js';
import { W as WagmiConnector } from '../../../../dist/WagmiConnector-2f14002d.browser.esm.js';
import { U as UserRejectedRequestError, C as ChainNotConfiguredError, A as AddChainError, S as SwitchChainError } from '../../../../dist/errors-9edc08c8.browser.esm.js';
import { utils, providers } from 'ethers';
import { w as walletIds } from '../../../../dist/walletIds-dff6dced.browser.esm.js';
import { g as getValidPublicRPCUrl } from '../../../../dist/url-a45219bd.browser.esm.js';
import { n as normalizeChainId } from '../../../../dist/normalizeChainId-1fb9aedf.browser.esm.js';
import '@thirdweb-dev/chains';
import 'eventemitter3';
class CoinbaseWalletConnector extends WagmiConnector {
constructor(_ref) {
let {
chains,
options
} = _ref;
super({
chains,
options: {
reloadOnDisconnect: false,
...options
}
});
_defineProperty(this, "id", walletIds.coinbase);
_defineProperty(this, "name", "Coinbase Wallet");
_defineProperty(this, "ready", true);
_defineProperty(this, "onAccountsChanged", accounts => {
if (accounts.length === 0) {
this.emit("disconnect");
} else {
this.emit("change", {
account: utils.getAddress(accounts[0])
});
}
});
_defineProperty(this, "onChainChanged", chainId => {
const id = normalizeChainId(chainId);
const unsupported = this.isChainUnsupported(id);
this.emit("change", {
chain: {
id,
unsupported
}
});
});
_defineProperty(this, "onDisconnect", () => {
this.emit("disconnect");
});
}
async connect() {
let {
chainId
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
try {
const provider = await this.getProvider();
this.setupListeners();
this.emit("message", {
type: "connecting"
});
const accounts = await provider.enable();
const account = utils.getAddress(accounts[0]);
// Switch to chain if provided
let id = await this.getChainId();
let unsupported = this.isChainUnsupported(id);
if (chainId && id !== chainId) {
try {
const chain = await this.switchChain(chainId);
id = chain.chainId;
unsupported = this.isChainUnsupported(id);
} catch (e) {
console.error(`Connected but failed to switch to desired chain ${chainId}`, e);
}
}
return {
account,
chain: {
id,
unsupported
},
provider: new providers.Web3Provider(provider)
};
} catch (error) {
if (/(user closed modal|accounts received is empty)/i.test(error.message)) {
throw new UserRejectedRequestError(error);
}
throw error;
}
}
async disconnect() {
if (!this._provider) {
return;
}
const provider = await this.getProvider();
provider.removeListener("accountsChanged", this.onAccountsChanged);
provider.removeListener("chainChanged", this.onChainChanged);
provider.removeListener("disconnect", this.onDisconnect);
provider.disconnect();
provider.close();
}
async getAccount() {
const provider = await this.getProvider();
const accounts = await provider.request({
method: "eth_accounts"
});
if (accounts.length === 0) {
throw new Error("No accounts found");
}
// return checksum address
return utils.getAddress(accounts[0]);
}
async getChainId() {
const provider = await this.getProvider();
const chainId = normalizeChainId(provider.chainId);
return chainId;
}
async getProvider() {
if (!this._provider) {
let CoinbaseWalletSDK = (await import('@coinbase/wallet-sdk')).default;
// Workaround for Vite dev import errors
// https://github.com/vitejs/vite/issues/7112
if (typeof CoinbaseWalletSDK !== "function" &&
// @ts-expect-error This import error is not visible to TypeScript
typeof CoinbaseWalletSDK.default === "function") {
CoinbaseWalletSDK = CoinbaseWalletSDK.default;
}
this._client = new CoinbaseWalletSDK(this.options);
const walletExtensionChainId = this._client.walletExtension?.getChainId();
const chain = this.chains.find(chain_ => this.options.chainId ? chain_.chainId === this.options.chainId : chain_.chainId === walletExtensionChainId) || this.chains[0];
const chainId = this.options.chainId || chain?.chainId;
const jsonRpcUrl = this.options.jsonRpcUrl || chain?.rpc[0];
this._provider = this._client.makeWeb3Provider(jsonRpcUrl, chainId);
}
return this._provider;
}
async getSigner() {
let {
chainId
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const [provider, account] = await Promise.all([this.getProvider(), this.getAccount()]);
return new providers.Web3Provider(provider, chainId).getSigner(account);
}
async isAuthorized() {
try {
const account = await this.getAccount();
return !!account;
} catch {
return false;
}
}
async switchChain(chainId) {
const provider = await this.getProvider();
const id = utils.hexValue(chainId);
try {
await provider.request({
method: "wallet_switchEthereumChain",
params: [{
chainId: id
}]
});
return this.chains.find(x => x.chainId === chainId) ?? {
chainId: chainId,
name: `Chain ${id}`,
slug: `${id}`,
nativeCurrency: {
name: "Ether",
decimals: 18,
symbol: "ETH"
},
rpc: [""],
testnet: false,
chain: "ethereum",
shortName: "eth"
};
} catch (error) {
const chain = this.chains.find(x => x.chainId === chainId);
if (!chain) {
throw new ChainNotConfiguredError({
chainId,
connectorId: this.id
});
}
// Indicates chain is not added to provider
if (error.code === 4902) {
try {
await provider.request({
method: "wallet_addEthereumChain",
params: [{
chainId: id,
chainName: chain.name,
nativeCurrency: chain.nativeCurrency,
rpcUrls: getValidPublicRPCUrl(chain),
// no client id on purpose here
blockExplorerUrls: this.getBlockExplorerUrls(chain)
}]
});
return chain;
} catch (addError) {
if (this._isUserRejectedRequestError(addError)) {
throw new UserRejectedRequestError(addError);
}
throw new AddChainError();
}
}
if (this._isUserRejectedRequestError(error)) {
throw new UserRejectedRequestError(error);
}
throw new SwitchChainError(error);
}
}
_isUserRejectedRequestError(error) {
return /(user rejected)/i.test(error.message);
}
async setupListeners() {
const provider = await this.getProvider();
provider.on("accountsChanged", this.onAccountsChanged);
provider.on("chainChanged", this.onChainChanged);
provider.on("disconnect", this.onDisconnect);
}
async getQrUrl() {
await this.getProvider();
if (!this._client) {
throw new Error("Coinbase Wallet SDK not initialized");
}
return this._client.getQrUrl();
}
}
export { CoinbaseWalletConnector };