import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base'; import { BaseMessageSignerWalletAdapter, isIosAndRedirectable, isVersionedTransaction, scopePollingDetectionStrategy, WalletAccountError, WalletConnectionError, WalletDisconnectedError, WalletDisconnectionError, WalletError, WalletNotConnectedError, WalletNotReadyError, WalletPublicKeyError, WalletReadyState, WalletSendTransactionError, WalletSignMessageError, WalletSignTransactionError, } from '@solana/wallet-adapter-base'; import type { Connection, SendOptions, Transaction, TransactionSignature, TransactionVersion, VersionedTransaction, } from '@solana/web3.js'; import { PublicKey } from '@solana/web3.js'; interface PhantomWalletEvents { connect(...args: unknown[]): unknown; disconnect(...args: unknown[]): unknown; accountChanged(newPublicKey: PublicKey): unknown; } interface PhantomWallet extends EventEmitter { isPhantom?: boolean; publicKey?: { toBytes(): Uint8Array }; isConnected: boolean; signTransaction(transaction: T): Promise; signAllTransactions(transactions: T[]): Promise; signAndSendTransaction( transaction: T, options?: SendOptions ): Promise<{ signature: TransactionSignature }>; signMessage(message: Uint8Array): Promise<{ signature: Uint8Array }>; connect(): Promise; disconnect(): Promise; } interface PhantomWindow extends Window { phantom?: { solana?: PhantomWallet; }; solana?: PhantomWallet; isPhantomInstalled?: boolean; } declare const window: PhantomWindow; export interface PhantomWalletAdapterConfig {} export const PhantomWalletName = 'Phantom' as WalletName<'Phantom'>; export class PhantomWalletAdapter extends BaseMessageSignerWalletAdapter { name = PhantomWalletName; url = 'https://phantom.app'; icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDgiIGhlaWdodD0iMTA4IiB2aWV3Qm94PSIwIDAgMTA4IDEwOCIgZmlsbD0ibm9uZSI+CjxyZWN0IHdpZHRoPSIxMDgiIGhlaWdodD0iMTA4IiByeD0iMjYiIGZpbGw9IiNBQjlGRjIiLz4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5v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MyA0Mi41Nzk1QzYxLjIzMyAzOC45ODg1IDYzLjMxNTcgMzcuMTkzMSA2NS43MjE2IDM3LjE5MzFDNjguMDkxNiAzNy4xOTMxIDcwLjIxMDMgMzguOTg4NSA3MC4yMTAzIDQyLjU3OTVaIiBmaWxsPSIjRkZGREY4Ii8+Cjwvc3ZnPg=='; supportedTransactionVersions: ReadonlySet = new Set(['legacy', 0]); private _connecting: boolean; private _wallet: PhantomWallet | null; private _publicKey: PublicKey | null; private _readyState: WalletReadyState = typeof window === 'undefined' || typeof document === 'undefined' ? WalletReadyState.Unsupported : WalletReadyState.NotDetected; constructor(config: PhantomWalletAdapterConfig = {}) { super(); this._connecting = false; this._wallet = null; this._publicKey = null; if (this._readyState !== WalletReadyState.Unsupported) { if (isIosAndRedirectable()) { // when in iOS (not webview), set Phantom as loadable instead of checking for install this._readyState = WalletReadyState.Loadable; this.emit('readyStateChange', this._readyState); } else { scopePollingDetectionStrategy(() => { if (window?.isPhantomInstalled && (window.phantom?.solana?.isPhantom || window.solana?.isPhantom)) { this._readyState = WalletReadyState.Installed; this.emit('readyStateChange', this._readyState); return true; } return false; }); } } } get publicKey() { return this._publicKey; } get connecting() { return this._connecting; } get readyState() { return this._readyState; } async autoConnect(): Promise { // Skip autoconnect in the Loadable state // We can't redirect to a universal link without user input if (this.readyState === WalletReadyState.Installed) { await this.connect(); } } async connect(): Promise { try { if (this.connected || this.connecting) return; if (this.readyState === WalletReadyState.Loadable) { // redirect to the Phantom /browse universal link // this will open the current URL in the Phantom in-wallet browser const url = encodeURIComponent(window.location.href); const ref = encodeURIComponent(window.location.origin); window.location.href = `https://phantom.app/ul/browse/${url}?ref=${ref}`; return; } if (this.readyState !== WalletReadyState.Installed) throw new WalletNotReadyError(); this._connecting = true; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const wallet = window.phantom?.solana || window.solana!; if (!wallet.isConnected) { try { await wallet.connect(); } catch (error: any) { throw new WalletConnectionError(error?.message, error); } } if (!wallet.publicKey) throw new WalletAccountError(); let publicKey: PublicKey; try { publicKey = new PublicKey(wallet.publicKey.toBytes()); } catch (error: any) { throw new WalletPublicKeyError(error?.message, error); } wallet.on('disconnect', this._disconnected); wallet.on('accountChanged', this._accountChanged); this._wallet = wallet; this._publicKey = publicKey; this.emit('connect', publicKey); } catch (error: any) { this.emit('error', error); throw error; } finally { this._connecting = false; } } async disconnect(): Promise { const wallet = this._wallet; if (wallet) { wallet.off('disconnect', this._disconnected); wallet.off('accountChanged', this._accountChanged); this._wallet = null; this._publicKey = null; try { await wallet.disconnect(); } catch (error: any) { this.emit('error', new WalletDisconnectionError(error?.message, error)); } } this.emit('disconnect'); } async sendTransaction( transaction: T, connection: Connection, options: SendTransactionOptions = {} ): Promise { try { const wallet = this._wallet; if (!wallet) throw new WalletNotConnectedError(); try { const { signers, ...sendOptions } = options; if (isVersionedTransaction(transaction)) { signers?.length && transaction.sign(signers); } else { transaction = (await this.prepareTransaction(transaction, connection, sendOptions)) as T; signers?.length && (transaction as Transaction).partialSign(...signers); } sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment; const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions); return signature; } catch (error: any) { if (error instanceof WalletError) throw error; throw new WalletSendTransactionError(error?.message, error); } } catch (error: any) { this.emit('error', error); throw error; } } async signTransaction(transaction: T): Promise { try { const wallet = this._wallet; if (!wallet) throw new WalletNotConnectedError(); try { return (await wallet.signTransaction(transaction)) || transaction; } catch (error: any) { throw new WalletSignTransactionError(error?.message, error); } } catch (error: any) { this.emit('error', error); throw error; } } async signAllTransactions(transactions: T[]): Promise { try { const wallet = this._wallet; if (!wallet) throw new WalletNotConnectedError(); try { return (await wallet.signAllTransactions(transactions)) || transactions; } catch (error: any) { throw new WalletSignTransactionError(error?.message, error); } } catch (error: any) { this.emit('error', error); throw error; } } async signMessage(message: Uint8Array): Promise { try { const wallet = this._wallet; if (!wallet) throw new WalletNotConnectedError(); try { const { signature } = await wallet.signMessage(message); return signature; } catch (error: any) { throw new WalletSignMessageError(error?.message, error); } } catch (error: any) { this.emit('error', error); throw error; } } private _disconnected = () => { const wallet = this._wallet; if (wallet) { wallet.off('disconnect', this._disconnected); wallet.off('accountChanged', this._accountChanged); this._wallet = null; this._publicKey = null; this.emit('error', new WalletDisconnectedError()); this.emit('disconnect'); } }; private _accountChanged = (newPublicKey: PublicKey) => { const publicKey = this._publicKey; if (!publicKey) return; try { newPublicKey = new PublicKey(newPublicKey.toBytes()); } catch (error: any) { this.emit('error', new WalletPublicKeyError(error?.message, error)); return; } if (publicKey.equals(newPublicKey)) return; this._publicKey = newPublicKey; this.emit('connect', newPublicKey); }; }