|
| 1 | +import { WalletNotConnectedError } from '@solana/wallet-adapter-base'; |
| 2 | +import { SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, SOLANA_TESTNET_CHAIN } from '@solana/wallet-standard-chains'; |
| 3 | +import { |
| 4 | + SolanaSignAndSendTransaction, |
| 5 | + type SolanaSignAndSendTransactionFeature, |
| 6 | + type SolanaSignAndSendTransactionMethod, |
| 7 | + SolanaSignMessage, |
| 8 | + type SolanaSignMessageFeature, |
| 9 | + type SolanaSignMessageMethod, |
| 10 | + SolanaSignTransaction, |
| 11 | + type SolanaSignTransactionFeature, |
| 12 | + type SolanaSignTransactionMethod, |
| 13 | +} from '@solana/wallet-standard-features'; |
| 14 | +import type { default as SolflareMetaMask } from '@solflare-wallet/metamask-sdk'; |
| 15 | +import type { Wallet } from '@wallet-standard/base'; |
| 16 | +import { |
| 17 | + StandardConnect, |
| 18 | + type StandardConnectFeature, |
| 19 | + type StandardConnectMethod, |
| 20 | + StandardDisconnect, |
| 21 | + type StandardDisconnectFeature, |
| 22 | + type StandardDisconnectMethod, |
| 23 | + StandardEvents, |
| 24 | + type StandardEventsChangeProperties, |
| 25 | + type StandardEventsFeature, |
| 26 | + type StandardEventsListeners, |
| 27 | + type StandardEventsNames, |
| 28 | + type StandardEventsOnMethod, |
| 29 | +} from '@wallet-standard/features'; |
| 30 | +import { icon } from './icon.js'; |
| 31 | + |
| 32 | +export class SolflareMetaMaskWallet implements Wallet { |
| 33 | + readonly #listeners: { [E in StandardEventsNames]?: StandardEventsListeners[E][] } = {}; |
| 34 | + readonly #version = '1.0.0' as const; |
| 35 | + readonly #name = 'MetaMask' as const; |
| 36 | + readonly #icon = icon; |
| 37 | + #solflareMetaMask: SolflareMetaMask | null = null; |
| 38 | + |
| 39 | + get version() { |
| 40 | + return this.#version; |
| 41 | + } |
| 42 | + |
| 43 | + get name() { |
| 44 | + return this.#name; |
| 45 | + } |
| 46 | + |
| 47 | + get icon() { |
| 48 | + return this.#icon; |
| 49 | + } |
| 50 | + |
| 51 | + get chains() { |
| 52 | + return [SOLANA_MAINNET_CHAIN, SOLANA_DEVNET_CHAIN, SOLANA_TESTNET_CHAIN] as const; |
| 53 | + } |
| 54 | + |
| 55 | + get features(): StandardConnectFeature & |
| 56 | + StandardDisconnectFeature & |
| 57 | + StandardEventsFeature & |
| 58 | + SolanaSignAndSendTransactionFeature & |
| 59 | + SolanaSignTransactionFeature & |
| 60 | + SolanaSignMessageFeature { |
| 61 | + return { |
| 62 | + [StandardConnect]: { |
| 63 | + version: '1.0.0', |
| 64 | + connect: this.#connect, |
| 65 | + }, |
| 66 | + [StandardDisconnect]: { |
| 67 | + version: '1.0.0', |
| 68 | + disconnect: this.#disconnect, |
| 69 | + }, |
| 70 | + [StandardEvents]: { |
| 71 | + version: '1.0.0', |
| 72 | + on: this.#on, |
| 73 | + }, |
| 74 | + [SolanaSignAndSendTransaction]: { |
| 75 | + version: '1.0.0', |
| 76 | + supportedTransactionVersions: ['legacy', 0], |
| 77 | + signAndSendTransaction: this.#signAndSendTransaction, |
| 78 | + }, |
| 79 | + [SolanaSignTransaction]: { |
| 80 | + version: '1.0.0', |
| 81 | + supportedTransactionVersions: ['legacy', 0], |
| 82 | + signTransaction: this.#signTransaction, |
| 83 | + }, |
| 84 | + [SolanaSignMessage]: { |
| 85 | + version: '1.0.0', |
| 86 | + signMessage: this.#signMessage, |
| 87 | + }, |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + get accounts() { |
| 92 | + return this.#solflareMetaMask ? this.#solflareMetaMask.standardAccounts : []; |
| 93 | + } |
| 94 | + |
| 95 | + #on: StandardEventsOnMethod = (event, listener) => { |
| 96 | + this.#listeners[event]?.push(listener) || (this.#listeners[event] = [listener]); |
| 97 | + return (): void => this.#off(event, listener); |
| 98 | + }; |
| 99 | + |
| 100 | + #emit<E extends StandardEventsNames>(event: E, ...args: Parameters<StandardEventsListeners[E]>): void { |
| 101 | + // eslint-disable-next-line prefer-spread |
| 102 | + this.#listeners[event]?.forEach((listener) => listener.apply(null, args)); |
| 103 | + } |
| 104 | + |
| 105 | + #off<E extends StandardEventsNames>(event: E, listener: StandardEventsListeners[E]): void { |
| 106 | + this.#listeners[event] = this.#listeners[event]?.filter((existingListener) => listener !== existingListener); |
| 107 | + } |
| 108 | + |
| 109 | + #connect: StandardConnectMethod = async () => { |
| 110 | + if (!this.#solflareMetaMask) { |
| 111 | + let SolflareMetaMaskClass: typeof SolflareMetaMask; |
| 112 | + try { |
| 113 | + SolflareMetaMaskClass = (await import('@solflare-wallet/metamask-sdk')).default; |
| 114 | + } catch (error: any) { |
| 115 | + throw new Error('Unable to load Solflare MetaMask SDK'); |
| 116 | + } |
| 117 | + this.#solflareMetaMask = new SolflareMetaMaskClass(); |
| 118 | + this.#solflareMetaMask.on('standard_change', (properties: StandardEventsChangeProperties) => |
| 119 | + this.#emit('change', properties) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + if (!this.accounts.length) { |
| 124 | + await this.#solflareMetaMask.connect(); |
| 125 | + } |
| 126 | + |
| 127 | + return { accounts: this.accounts }; |
| 128 | + }; |
| 129 | + |
| 130 | + #disconnect: StandardDisconnectMethod = async () => { |
| 131 | + if (!this.#solflareMetaMask) return; |
| 132 | + await this.#solflareMetaMask.disconnect(); |
| 133 | + }; |
| 134 | + |
| 135 | + #signAndSendTransaction: SolanaSignAndSendTransactionMethod = async (...inputs) => { |
| 136 | + if (!this.#solflareMetaMask) throw new WalletNotConnectedError(); |
| 137 | + return await this.#solflareMetaMask.standardSignAndSendTransaction(...inputs); |
| 138 | + }; |
| 139 | + |
| 140 | + #signTransaction: SolanaSignTransactionMethod = async (...inputs) => { |
| 141 | + if (!this.#solflareMetaMask) throw new WalletNotConnectedError(); |
| 142 | + return await this.#solflareMetaMask.standardSignTransaction(...inputs); |
| 143 | + }; |
| 144 | + |
| 145 | + #signMessage: SolanaSignMessageMethod = async (...inputs) => { |
| 146 | + if (!this.#solflareMetaMask) throw new WalletNotConnectedError(); |
| 147 | + return await this.#solflareMetaMask.standardSignMessage(...inputs); |
| 148 | + }; |
| 149 | +} |
0 commit comments