@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
138 lines (128 loc) • 4.8 kB
JavaScript
import { _ as _defineProperty } from '../../../../dist/defineProperty-7303a112.esm.js';
import { a as ConnectorNotFoundError, U as UserRejectedRequestError, R as ResourceUnavailableError } from '../../../../dist/errors-35fc3528.esm.js';
import { w as walletIds } from '../../../../dist/walletIds-c34dbb3f.esm.js';
import { InjectedConnector } from '../../injected/dist/thirdweb-dev-wallets-evm-connectors-injected.esm.js';
import { utils } from 'ethers';
import { g as getInjectedOKXProvider } from '../../../../dist/getInjectedOKXProvider-bef80221.esm.js';
import '../../../../dist/WagmiConnector-6011bbb1.esm.js';
import '@thirdweb-dev/chains';
import 'eventemitter3';
import '../../../../dist/assertWindowEthereum-2bcf9787.esm.js';
import '../../../../dist/url-0d129c6b.esm.js';
import '../../../../dist/normalizeChainId-abcb61d5.esm.js';
class OKXConnector extends InjectedConnector {
constructor(arg) {
const defaultOptions = {
name: "OKX",
shimDisconnect: true,
shimChainChangedDisconnect: true,
getProvider: getInjectedOKXProvider
};
const options = {
...defaultOptions,
...arg.options
};
super({
chains: arg.chains,
options,
connectorStorage: arg.connectorStorage
});
_defineProperty(this, "id", walletIds.okx);
}
/**
* Connect to injected OKX provider
*/
async connect() {
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
try {
const provider = await this.getProvider();
if (!provider) {
throw new ConnectorNotFoundError();
}
this.setupListeners();
// emit "connecting" event
this.emit("message", {
type: "connecting"
});
// Attempt to show wallet select prompt with `wallet_requestPermissions` when
// `shimDisconnect` is active and account is in disconnected state (flag in storage)
let account = null;
if (this.options?.shimDisconnect && !Boolean(this.connectorStorage.getItem(this.shimDisconnectKey))) {
account = await this.getAccount().catch(() => null);
const isConnected = !!account;
if (isConnected) {
// Attempt to show another prompt for selecting wallet if already connected
try {
await provider.request({
method: "wallet_requestPermissions",
params: [{
eth_accounts: {}
}]
});
} catch (error) {
// Not all injected providers support `wallet_requestPermissions` (e.g. iOS).
// Only bubble up error if user rejects request
if (this.isUserRejectedRequestError(error)) {
throw new UserRejectedRequestError(error);
}
}
}
}
// if account is not already set, request accounts and use the first account
if (!account) {
const accounts = await provider.request({
method: "eth_requestAccounts"
});
account = utils.getAddress(accounts[0]);
}
// get currently connected chainId
let connectedChainId = await this.getChainId();
// check if connected chain is unsupported
let isUnsupported = this.isChainUnsupported(connectedChainId);
// if chainId is given, but does not match the currently connected chainId, switch to the given chainId
if (options.chainId && connectedChainId !== options.chainId) {
try {
await this.switchChain(options.chainId);
// recalculate the chainId and isUnsupported
connectedChainId = options.chainId;
isUnsupported = this.isChainUnsupported(options.chainId);
} catch (e) {
console.error(`Could not switch to chain id : ${options.chainId}`, e);
}
}
// if shimDisconnect is enabled
if (this.options?.shimDisconnect) {
// add shimDisconnectKey in storage - this signals that connector is "connected"
await this.connectorStorage.setItem(this.shimDisconnectKey, "true");
}
const connectionInfo = {
chain: {
id: connectedChainId,
unsupported: isUnsupported
},
provider: provider,
account
};
this.emit("connect", connectionInfo);
return connectionInfo;
} catch (error) {
if (this.isUserRejectedRequestError(error)) {
throw new UserRejectedRequestError(error);
}
if (error.code === -32002) {
throw new ResourceUnavailableError(error);
}
throw error;
}
}
async switchAccount() {
const provider = await this.getProvider();
await provider.request({
method: "wallet_requestPermissions",
params: [{
eth_accounts: {}
}]
});
}
}
export { OKXConnector };