import WalletAdapter from './base'; import { Cluster, Transaction } from '@solana/web3.js'; import Wallet from '@project-serum/sol-wallet-adapter'; export default class WebAdapter extends WalletAdapter { private _instance: Wallet | null = null; private _provider: string; private _network: Cluster; private _pollTimer!: number; get publicKey () { return this._instance!.publicKey || null; } get connected () { return this._instance!.connected || false; } constructor (provider: string, network: Cluster) { super(); this._provider = provider; this._network = network; } async connect () { this._instance = new Wallet(this._provider, this._network); this._instance.on('connect', this._handleConnect); this._instance.on('disconnect', this._handleDisconnect); this._pollTimer = window.setInterval(() => { // @ts-ignore if (this._instance?._popup?.closed !== false) { this._handleDisconnect(); } }, 200); await this._instance.connect(); } async disconnect () { if (!this.connected) { throw new Error('Wallet not connected'); } this._instance!.removeAllListeners('connect'); this._instance!.removeAllListeners('disconnect'); await this._instance!.disconnect(); } async signTransaction (transaction: Transaction): Promise { if (!this.connected) { throw new Error('Wallet not connected'); } return await this._instance!.signTransaction(transaction); } async signAllTransactions (transactions: Transaction[]): Promise { if (!this.connected) { throw new Error('Wallet not connected'); } return await this._instance!.signAllTransactions(transactions); } async signMessage (data: Uint8Array, display: 'hex' | 'utf8' = 'hex'): Promise { if (!this.connected) { throw new Error('Wallet not connected'); } const { signature } = await this._instance!.sign(data, display); return Uint8Array.from(signature); } private _handleConnect = () => { this.emit('connect'); }; private _handleDisconnect = () => { window.clearInterval(this._pollTimer); this.emit('disconnect'); }; }