import { Adapter , BaseMessageSignerWalletAdapter , EventEmitter , scopePollingDetectionStrategy , WalletConnectionError , WalletDisconnectedError , WalletDisconnectionError , WalletName , WalletNotConnectedError , WalletNotReadyError , WalletPublicKeyError , WalletReadyState , WalletSignTransactionError , } from '@solana/wallet-adapter-base' ; import { PublicKey , Transaction } from '@solana/web3.js' ; interface SolflareWalletEvents { connect ( ... args ) : unknown ; disconnect ( ... args ) : unknown ; } interface SolflareWallet extends EventEmitter < SolflareWalletEvents > { isSolflare ? : boolean ; publicKey ? : { toBytes ( ) : Uint8Array ; } ; isConnected : boolean ; signTransaction ( transaction ) : Promise < Transaction > ; signAllTransactions ( transactions ) : Promise < Transaction [ ] > ; signMessage ( message , encoding ) : Promise < { signature : Uint8Array ; } > ; connect ( ) : Promise < boolean > ; disconnect ( ) : Promise < boolean > ; } interface SolflareWindow extends Window { solflare ? : SolflareWallet ; } declare const window ; export interface SolflareWalletAdapterConfig { } export const SolflareWalletName = 'Solflare' as WalletName ; export class SolflareWalletAdapter extends BaseMessageSignerWalletAdapter { name = SolflareWalletName ; url = 'https://solflare.com' ; icon = 'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjUwIiB2aWV3Qm94PSIwIDAgNTAgNTAiIHdpZHRoPSI1MCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGxpbmVhckdyYWRpZW50IGlkPSJhIj48c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiNmZmMxMGIiLz48c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNmYjNmMmUiLz48L2xpbmVhckdyYWRpZW50PjxsaW5lYXJHcmFkaWVudCBpZD0iYiIgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiIHgxPSI2LjQ3ODM1IiB4Mj0iMzQuOTEwNyIgeGxpbms6aH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cy40MjU2MjktLjk0MDMuOTUwNjctLjk0MDN6bTQ3LjY3OTczLS45NTQ3Yy41MjUgMCAuOTUwNy40MjEuOTUwNy45NDAzcy0uNDI1Ny45NDAyLS45NTA3Ljk0MDItLjk1MDctLjQyMDktLjk1MDctLjk0MDIuNDI1Ny0uOTQwMy45NTA3LS45NDAzem0tMjQuNjI5Ni0yMi40Nzk3Yy41MjUgMCAuOTUwNi40MjA5NzMuOTUwNi45NDAyNyAwIC41MTkzLS40MjU2Ljk0MDI3LS45NTA2Ljk0MDI3LS41MjUxIDAtLjk1MDctLjQyMDk3LS45NTA3LS45NDAyNyAwLS41MTkyOTcuNDI1Ni0uOTQwMjcuOTUwNy0uOTQwMjd6IiBmaWxsPSJ1cmwoI2IpIi8+PHBhdGggZD0ibTI0LjU3MSAzMi43NzkyYzQuOTU5NiAwIDguOTgwMi0zLjk3NjUgOC45ODAyLTguODgxOSAwLTQuOTA1My00LjAyMDYtOC44ODE5LTguOTgwMi04Ljg4MTlzLTguOTgwMiAzLjk3NjYtOC45ODAyIDguODgxOWMwIDQuOTA1NCA0LjAyMDYgOC44ODE5IDguOTgwMiA4Ljg4MTl6IiBmaWxsPSJ1cmwoI2MpIi8+PC9zdmc+' ; private _connecting : boolean ; private _wallet : SolflareWallet | null ; private _publicKey : PublicKey | null ; private _readyState : WalletReadyState = typeof window === 'undefined' || typeof document === 'undefined' ? WalletReadyState . Unsupported : WalletReadyState . NotDetected ; constructor ( config = { } ) { super ( ) ; this . _connecting = false ; this . _wallet = null ; this . _publicKey = null ; if ( this . _readyState !== WalletReadyState . Unsupported ) { scopePollingDetectionStrategy ( ( ) => { if ( window . solflare ?. isSolflare ) { this . _readyState = WalletReadyState . Installed ; this . emit ( 'readyStateChange' , this . _readyState ) ; return true ; } return false ; } ) ; } } get publicKey ( ) : PublicKey | null { return this . _publicKey ; } get connecting ( ) : boolean { return this . _connecting ; } get connected ( ) : boolean { return ! ! this . _wallet ?. isConnected ; } get readyState ( ) : WalletReadyState { return this . _readyState ; } async connect ( ) { try { if ( this . connected || this . connecting ) return ; if ( this . _readyState !== WalletReadyState . Installed ) throw new WalletNotReadyError ( ) ; this . _connecting = true ; const wallet = window ! . solflare ! ; if ( ! wallet . isConnected ) { try { await wallet . connect ( ) ; } catch ( error ) { throw new WalletConnectionError ( error ?. message , error ) ; } } if ( ! wallet . publicKey ) throw new WalletConnectionError ( ) ; let publicKey ; try { publicKey = new PublicKey ( wallet . publicKey . toBytes ( ) ) ; } catch ( error ) { throw new WalletPublicKeyError ( error ?. message , error ) ; } wallet . on ( 'disconnect' , this . _disconnected ) ; this . _wallet = wallet ; this . _publicKey = publicKey ; this . emit ( 'connect' , publicKey ) ; } catch ( error ) { this . emit ( 'error' , error ) ; throw error ; } finally { this . _connecting = false ; } } async disconnect ( ) { const wallet = this . _wallet ; if ( wallet ) { wallet . off ( 'disconnect' , this . _disconnected ) ; this . _wallet = null ; this . _publicKey = null ; try { await wallet . disconnect ( ) ; } catch ( error ) { this . emit ( 'error' , new WalletDisconnectionError ( error ?. message , error ) ) ; } } this . emit ( 'disconnect' ) ; } async signTransaction ( transaction ) { try { const wallet = this . _wallet ; if ( ! wallet ) throw new WalletNotConnectedError ( ) ; try { return ( await wallet . signTransaction ( transaction ) ) || transaction ; } catch ( error ) { throw new WalletSignTransactionError ( error ?. message , error ) ; } } catch ( error ) { this . emit ( 'error' , error ) ; throw error ; } } async signAllTransactions ( transactions ) { try { const wallet = this . _wallet ; if ( ! wallet ) throw new WalletNotConnectedError ( ) ; try { return ( await wallet . signAllTransactions ( transactions ) ) || transactions ; } catch ( error ) { throw new WalletSignTransactionError ( error ?. message , error ) ; } } catch ( error ) { this . emit ( 'error' , error ) ; throw error ; } } async signMessage ( message ) { try { const wallet = this . _wallet ; if ( ! wallet ) throw new WalletNotConnectedError ( ) ; try { const { signature } = await wallet . signMessage ( message , 'utf8' ) ; return signature ; } catch ( error ) { throw new WalletSignTransactionError ( error ?. message , error ) ; } } catch ( error ) { this . emit ( 'error' , error ) ; throw error ; } } private _disconnected = ( ) => { const wallet = this . _wallet ; if ( wallet ) { wallet . off ( 'disconnect' , this . _disconnected ) ; this . _wallet = null ; this . _publicKey = null ; this . emit ( 'error' , new WalletDisconnectedError ( ) ) ; this . emit ( 'disconnect' ) ; } } ; }