import { AdapterState, isInBrowser, WalletReadyState, WalletSignMessageError, WalletNotFoundError, WalletDisconnectedError, WalletConnectionError, WalletSignTransactionError, WalletGetNetworkError, AddonAdapter, WalletError, assertConnectAddress, } from '@tronweb3/tronwallet-abstract-adapter'; import type { Transaction, SignedTransaction, AdapterName, BaseAdapterConfig, Network, } from '@tronweb3/tronwallet-abstract-adapter'; import type { AccountsChangedEventData, TronLinkMessageEvent, TronLinkWallet, } from '@tronweb3/tronwallet-adapter-tronlink'; import { getNetworkInfoByTronWeb } from '@tronweb3/tronwallet-adapter-tronlink'; import { openBybitWallet, supportBybitWallet } from './utils.js'; declare global { interface Window { bybitWallet?: { tronLink: TronLinkWallet; }; } } export type BybitWalletAdapterConfig = BaseAdapterConfig; export const BybitWalletAdapterName = 'Bybit Wallet' as AdapterName<'Bybit Wallet'>; export class BybitWalletAdapter extends AddonAdapter { name = BybitWalletAdapterName; url = 'https://bybit.com/web3'; icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iODgiIGhlaWdodD0iODgiIHZpZXdCb3g9IjAgMCA4OCA4OCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTAgMTguN0MwIDguMzcyMjcgOC4zNzIyOCAwIDE4LjcgMEg2OS4zQzc5LjYyNzcgMCA4OCA4LjM3MjI4IDg4IDE4LjdWNjkuM0M4OCA3OS42Mjc3IDc5LjYyNzcgODggNjkuMyA4OEgxOC43QzguMzcyMjcgODggMCA3OS42Mjc3IDAgNjkuM1YxOC43WiIgZmlsbD0iIzQwNDM0NyIvPgo8cGF0aCBkPSJNNy41NzYxNyAyNi44MDY3QzYuNzg1MTYgMjQuMDc4NyA4LjQ3NzUgMjEuMjUzMSAxMS4yNTU5IDIwLjY2M0w1Ny42MDg3IDEwLjgxNzNDNTkuODA5IDEwLjM1IDYyLjA0NDMgMTEuNDQ0MyA2My4wMjQ3IDEzLjQ2ODlMODMuODQ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DU0LjkwNjQgNTAuMzM3NiA1My42NDU1QzUwLjMzNTcgNTIuMzgyNiA0OS40ODggNTEuNTM0MyA0Ny45NzE5IDUxLjUzNDNaTTQ4LjIwMzEgNTguOTcwOEg0NC40NzUzVjYzLjQ3MjhINDguMjAzMUM0OS44MjMyIDYzLjQ3MjggNTAuNTkzOCA2Mi40NDMyIDUwLjU5MzggNjEuMjA5MkM1MC41OTM4IDU5Ljk3MzQgNDkuODIxMyA1OC45NzA4IDQ4LjIwMzEgNTguOTcwOFoiIGZpbGw9IndoaXRlIi8+CjxwYXRoIGQ9Ik03My40MzkgNTEuNTM0M1Y2Ni42NjM3SDY5Ljg2NTRWNTEuNTM0M0g2NS4wODM5VjQ4LjM0MzRINzguMjIyNFY1MS41MzQzSDczLjQzOVoiIGZpbGw9IndoaXRlIi8+CjxkZWZzPgo8bGluZWFyR3JhZGllbnQgaWQ9InBhaW50MF9saW5lYXJfMzEyXzE3NTM0IiB4MT0iNy4zMzMwOCIgeTE9IjI1LjU5NCIgeDI9Ijg0LjYzODEiIHkyPSIyMS43MjE2IiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+CjxzdG9wIHN0b3AtY29sb3I9IiNGRkQ3NDgiLz4KPHN0b3Agb2Zmc2V0PSIxIiBzdG9wLWNvbG9yPSIjRjdBNjAwIi8+CjwvbGluZWFyR3JhZGllbnQ+CjwvZGVmcz4KPC9zdmc+Cg=='; config: Required; private _readyState: WalletReadyState = isInBrowser() ? WalletReadyState.Loading : WalletReadyState.NotFound; private _state: AdapterState = AdapterState.Loading; private _connecting: boolean; private _wallet: TronLinkWallet | null; private _address: string | null; constructor(config: BybitWalletAdapterConfig = {}) { super(config); this.config = { ...this.commonConfig, }; this._connecting = false; this._wallet = null; this._address = null; if (!isInBrowser()) { this._readyState = WalletReadyState.NotFound; this.setState(AdapterState.NotFound); return; } if (supportBybitWallet()) { this._readyState = WalletReadyState.Found; this._updateWallet().then(() => { if (this.connected) { this.emit('connect', this.address || ''); } }); } else { this._checkWallet().then(() => { if (this.connected) { this.emit('connect', this.address || ''); } }); } } get address() { return this._address; } get state() { return this._state; } get readyState() { return this._readyState; } get connecting() { return this._connecting; } /** * Get network information used by BybitWallet. * @returns {Network} Current network information. */ async network(): Promise { try { await this._checkWallet(); if (this.state !== AdapterState.Connected) throw new WalletDisconnectedError(); const wallet = this._wallet; if (!wallet || !wallet.tronWeb) throw new WalletDisconnectedError(); try { return await getNetworkInfoByTronWeb(wallet.tronWeb); } catch (e: any) { throw new WalletGetNetworkError(e?.message, e); } } catch (e: any) { this.emit('error', e); throw e; } } protected async _connect(): Promise { try { if (!(await this._beforeConnect())) return; if (!this._wallet) return; this._connecting = true; const wallet = this._wallet as TronLinkWallet; const res = await wallet.request({ method: 'tron_requestAccounts' }); if (!res) { throw new WalletConnectionError('Request connect error.'); } if (res.code === 4000) { throw new WalletConnectionError( 'The same DApp has already initiated a request to connect to BybitWallet, and the pop-up window has not been closed.' ); } if (res.code === 4001) { throw new WalletConnectionError('The user rejected connection.'); } const address = assertConnectAddress(wallet.tronWeb.defaultAddress?.base58); this.setAddress(address); this.setState(AdapterState.Connected); this._listenEvent(); this.connected && this.emit('connect', this.address || ''); } catch (error: any) { const err = error instanceof WalletError ? error : new WalletConnectionError(error?.message, error); this.emit('error', err); throw err; } finally { this._connecting = false; } } async disconnect(): Promise { this._stopListenEvent(); if (this.state !== AdapterState.Connected) { return; } this.setAddress(null); this.setState(AdapterState.Disconnect); this.emit('disconnect'); } async signTransaction(transaction: Transaction): Promise { try { const wallet = await this.checkAndGetWallet(); try { return await wallet.tronWeb.trx.sign(transaction); } catch (error: any) { if (error instanceof Error || (typeof error === 'object' && error.message)) { throw new WalletSignTransactionError(error.message, error); } else if (typeof error === 'string') { throw new WalletSignTransactionError(error, new Error(error)); } else { throw new WalletSignTransactionError('Unknown error', error); } } } catch (error: any) { this.emit('error', error); throw error; } } async multiSign(transaction: Transaction, options: { permissionId?: number } = {}): Promise { try { const wallet = await this.checkAndGetWallet(); try { return await wallet.tronWeb.trx.multiSign(transaction, undefined, options.permissionId); } catch (error: any) { if (error instanceof Error || (typeof error === 'object' && error.message)) { throw new WalletSignTransactionError(error.message, error); } else if (typeof error === 'string') { throw new WalletSignTransactionError(error, new Error(error)); } else { throw new WalletSignTransactionError('Unknown error', error); } } } catch (error: any) { this.emit('error', error); throw error; } } async signMessage(message: string): Promise { try { const wallet = await this.checkAndGetWallet(); try { return await wallet.tronWeb.trx.signMessageV2(message); } catch (error: any) { if (error instanceof Error || (typeof error === 'object' && error.message)) { throw new WalletSignMessageError(error.message, error); } else if (typeof error === 'string') { throw new WalletSignMessageError(error, new Error(error)); } else { throw new WalletSignMessageError('Unknown error', error); } } } catch (error: any) { this.emit('error', error); throw error; } } private async checkAndGetWallet() { this.checkIfOpenBybitWallet(); await this._checkWallet(); if (this.state !== AdapterState.Connected) throw new WalletDisconnectedError(); const wallet = this._wallet; if (!wallet || !wallet.tronWeb) throw new WalletDisconnectedError(); return wallet as TronLinkWallet; } private _listenEvent() { this._stopListenEvent(); window.addEventListener('message', this.messageHandler); } private _stopListenEvent() { this._eventGeneration++; if (this._accountsChangedTimer) { clearTimeout(this._accountsChangedTimer); this._accountsChangedTimer = null; } window.removeEventListener('message', this.messageHandler); } private _accountsChangedTimer: ReturnType | null = null; /** * Incremented whenever the listening session ends. Deferred `accountsChanged` * work and the awaited `connect` handler capture the value before they yield and * abandon themselves if it no longer matches, so an event that started before * `disconnect()` cannot write state after it. */ private _eventGeneration = 0; private messageHandler = async (e: TronLinkMessageEvent) => { if (e.origin !== window.location.origin) { return; } const message = e.data?.message; if (!message) { return; } if (message.action === 'accountsChanged') { if (this._accountsChangedTimer) { clearTimeout(this._accountsChangedTimer); } const generation = this._eventGeneration; this._accountsChangedTimer = setTimeout(async () => { this._accountsChangedTimer = null; if (generation !== this._eventGeneration) return; const preAddr = this.address || ''; const curAddr = (message.data as AccountsChangedEventData).address || ''; // An empty account is a disconnection, not a connection to the empty // address. Going Connected here leaves `connected === true` with no // address, and the block below then emits `disconnect` against a state // that still says Connected. if (curAddr && (this._wallet as TronLinkWallet)?.ready) { // Gate the connect transition with the security check. try { await this.checkSecurity(); } catch { if (generation !== this._eventGeneration) return; this.setAddress(null); this.setState(AdapterState.Disconnect); return; } // `checkSecurity()` was awaited, so the session may have ended while it // was pending — re-check before writing any state. if (generation !== this._eventGeneration) return; this.setAddress(curAddr); this.setState(AdapterState.Connected); } else { this.setAddress(null); this.setState(AdapterState.Disconnect); } const address = this.address || ''; if (address !== preAddr) { this.emit('accountsChanged', this.address || '', preAddr); } if (!preAddr && this.address) { this.emit('connect', this.address); } else if (preAddr && !this.address) { this.emit('disconnect'); } }, 200); } else if (message.action === 'connect') { const generation = this._eventGeneration; try { await this.checkSecurity(); } catch { // A stale rejection must not tear down a session that has since been // re-established, so the generation is honoured on this path too. if (generation !== this._eventGeneration) return; this.setAddress(null); this.setState(AdapterState.Disconnect); return; } // `checkSecurity()` was awaited, so the session may have ended while it // was pending — re-check before writing any state. if (generation !== this._eventGeneration) return; // Re-read after the await: the account may have changed or been cleared // while the check was pending. const address = (this._wallet as TronLinkWallet).tronWeb?.defaultAddress?.base58 || ''; if (!address) return; const isCurConnected = this.connected; const preAddress = this.address || ''; this.setAddress(address); this.setState(AdapterState.Connected); if (!isCurConnected) { this.emit('connect', address); } else if (address !== preAddress) { this.emit('accountsChanged', this.address || '', preAddress); } } else if (message.action === 'disconnect') { this.setAddress(null); this.setState(AdapterState.Disconnect); this.emit('disconnect'); } }; private checkIfOpenBybitWallet() { if (this.config.openAppWithDeeplink === false) { return; } if (openBybitWallet()) { throw new WalletNotFoundError(); } } protected _openAppByDeepLinkIfNeed(): boolean { if (this.config.openAppWithDeeplink === false) { return false; } return openBybitWallet(); } private _checkPromise: Promise | null = null; /** * Detection polls for the full `checkTimeout` only once. Later attempts re-check a * single time, so retrying is free when the wallet is genuinely absent. */ private _hasRunInitialDetection = false; /** * check if wallet exists by interval, the promise only resolve when wallet detected or timeout * @returns if BybitWallet exists */ protected _checkWallet(): Promise { if (this.readyState === WalletReadyState.Found) { return Promise.resolve(true); } if (this._checkPromise) { return this._checkPromise; } const interval = 100; const maxTimes = this._hasRunInitialDetection ? 0 : Math.floor(this.config.checkTimeout / interval); this._hasRunInitialDetection = true; let times = 0, timer: ReturnType; const detection = new Promise((resolve) => { const check = () => { times++; const isSupport = supportBybitWallet(); if (isSupport || times > maxTimes) { timer && clearInterval(timer); this._readyState = isSupport ? WalletReadyState.Found : WalletReadyState.NotFound; this._updateWallet(); this.emit('readyStateChanged', this.readyState); resolve(isSupport); } }; timer = setInterval(check, interval); check(); }); this._checkPromise = detection; // Never cache a failed detection. The extension may inject late, be switched on at // runtime, or a mobile WebView may still be initialising — in all of those cases the // next call has to look again instead of replaying the old negative answer. void detection.then((found) => { if (!found && this._checkPromise === detection) { this._checkPromise = null; } }); return detection; } private _updateWallet = async () => { let state; let address; if (supportBybitWallet()) { this._wallet = window.bybitWallet!.tronLink; this._listenEvent(); address = this._wallet.tronWeb?.defaultAddress?.base58 || null; if (address) { // Only run the security check once the wallet is actually connected. try { await this.checkSecurity(); } catch { this.setAddress(null); this.setState(AdapterState.Disconnect); return; } state = AdapterState.Connected; } else { state = AdapterState.Disconnect; } } else { this._wallet = null; address = null; state = AdapterState.NotFound; } this.setAddress(address); this.setState(state); }; private setAddress(address: string | null) { this._address = address; } private setState(state: AdapterState) { const preState = this.state; if (state !== preState) { this._state = state; this.emit('stateChanged', state); } } }