-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #824 from vsakos/solflare-metamask-standard-adapter
Solflare: Initialize MetaMask standard adapter
- Loading branch information
Showing
7 changed files
with
251 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@solana/wallet-adapter-solflare': patch | ||
--- | ||
|
||
Add Solflare MetaMask Snap support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { EthereumProvider, WindowWithEthereum } from '@solflare-wallet/metamask-sdk'; | ||
import { registerWallet } from '@wallet-standard/wallet'; | ||
import { SolflareMetaMaskWallet } from './wallet.js'; | ||
|
||
let stopPolling = false; | ||
|
||
/** @internal */ | ||
export function detectAndRegisterSolflareMetaMaskWallet(): boolean { | ||
// If detected, stop polling. | ||
if (stopPolling) return true; | ||
(async function () { | ||
try { | ||
// Try to detect, stop polling if detected, and register the wallet. | ||
if (await isSnapProviderDetected()) { | ||
if (!stopPolling) { | ||
stopPolling = true; | ||
registerWallet(new SolflareMetaMaskWallet()); | ||
} | ||
} | ||
} catch (error) { | ||
// Stop polling on unhandled errors (this should never happen). | ||
stopPolling = true; | ||
} | ||
})(); | ||
// Keep polling. | ||
return false; | ||
} | ||
|
||
async function isSnapProviderDetected(): Promise<boolean> { | ||
try { | ||
const provider = (window as WindowWithEthereum).ethereum; | ||
if (!provider) return false; | ||
|
||
const providerProviders = provider.providers; | ||
if (providerProviders && Array.isArray(providerProviders)) { | ||
for (const provider of providerProviders) { | ||
if (await isSnapSupported(provider)) return true; | ||
} | ||
} | ||
|
||
const providerDetected = provider.detected; | ||
if (providerDetected && Array.isArray(providerDetected)) { | ||
for (const provider of providerDetected) { | ||
if (await isSnapSupported(provider)) return true; | ||
} | ||
} | ||
|
||
return await isSnapSupported(provider); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
async function isSnapSupported(provider: EthereumProvider): Promise<boolean> { | ||
try { | ||
await provider.request({ method: 'wallet_getSnaps' }); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { WalletIcon } from '@wallet-standard/base'; | ||
|
||
/** @internal */ | ||
export const icon: WalletIcon = | ||
'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjMxIiB2aWV3Qm94PSIwIDAgMzEgMzEiIHdpZHRoPSIzMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGxpbmVhckdyYWRpZW50IGlkPSJhIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjIwLjI1IiB4Mj0iMjYuNTcxIiB5MT0iMjcuMTczIiB5Mj0iMTkuODU4Ij48c3RvcCBvZmZzZXQ9Ii4wOCIgc3RvcC1jb2xvcj0iIzk5NDVmZiIvPjxzdG9wIG9mZnNldD0iLjMiIHN0b3AtY29sb3I9IiM4NzUyZjMiLz48c3RvcCBvZmZzZ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y4wMTItLjAyNi4wMTUtLjA1Ni4wMS0uMDg0cy0uMDE4LS4wNTUtLjAzOS0uMDc2em0tNi40MzEtLjk4M2g2LjIwOWEuMzEuMzEgMCAwIDAgLjEyMS0uMDI0Yy4wMzgtLjAxNi4wNzMtLjA0LjEwMS0uMDdsMS4zMS0xLjM2OGMuMDItLjAyMS4wMzQtLjA0Ny4wMzktLjA3NnMuMDAxLS4wNTgtLjAxLS4wODRhLjE1LjE1IDAgMCAwIC0uMDU2LS4wNjVjLS4wMjUtLjAxNi0uMDU0LS4wMjQtLjA4My0uMDI0aC02LjIwOGEuMzEuMzEgMCAwIDAgLS4xMjEuMDI1Yy0uMDM4LjAxNi0uMDcyLjA0LS4xMDEuMDY5bC0xLjMxIDEuMzY4Yy0uMDIuMDIxLS4wMzQuMDQ3LS4wMzkuMDc2cy0uMDAxLjA1OC4wMS4wODQuMDMxLjA0OS4wNTYuMDY1LjA1NC4wMjQuMDgzLjAyNHoiIGZpbGw9InVybCgjYSkiLz48L3N2Zz4=' as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { WalletNotConnectedError } from '@solana/wallet-adapter-base'; | ||
import { SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, SOLANA_TESTNET_CHAIN } from '@solana/wallet-standard-chains'; | ||
import { | ||
SolanaSignAndSendTransaction, | ||
type SolanaSignAndSendTransactionFeature, | ||
type SolanaSignAndSendTransactionMethod, | ||
SolanaSignMessage, | ||
type SolanaSignMessageFeature, | ||
type SolanaSignMessageMethod, | ||
SolanaSignTransaction, | ||
type SolanaSignTransactionFeature, | ||
type SolanaSignTransactionMethod, | ||
} from '@solana/wallet-standard-features'; | ||
import type { default as SolflareMetaMask } from '@solflare-wallet/metamask-sdk'; | ||
import type { Wallet } from '@wallet-standard/base'; | ||
import { | ||
StandardConnect, | ||
type StandardConnectFeature, | ||
type StandardConnectMethod, | ||
StandardDisconnect, | ||
type StandardDisconnectFeature, | ||
type StandardDisconnectMethod, | ||
StandardEvents, | ||
type StandardEventsChangeProperties, | ||
type StandardEventsFeature, | ||
type StandardEventsListeners, | ||
type StandardEventsNames, | ||
type StandardEventsOnMethod, | ||
} from '@wallet-standard/features'; | ||
import { icon } from './icon.js'; | ||
|
||
export class SolflareMetaMaskWallet implements Wallet { | ||
readonly #listeners: { [E in StandardEventsNames]?: StandardEventsListeners[E][] } = {}; | ||
readonly #version = '1.0.0' as const; | ||
readonly #name = 'MetaMask' as const; | ||
readonly #icon = icon; | ||
#solflareMetaMask: SolflareMetaMask | null = null; | ||
|
||
get version() { | ||
return this.#version; | ||
} | ||
|
||
get name() { | ||
return this.#name; | ||
} | ||
|
||
get icon() { | ||
return this.#icon; | ||
} | ||
|
||
get chains() { | ||
return [SOLANA_MAINNET_CHAIN, SOLANA_DEVNET_CHAIN, SOLANA_TESTNET_CHAIN] as const; | ||
} | ||
|
||
get features(): StandardConnectFeature & | ||
StandardDisconnectFeature & | ||
StandardEventsFeature & | ||
SolanaSignAndSendTransactionFeature & | ||
SolanaSignTransactionFeature & | ||
SolanaSignMessageFeature { | ||
return { | ||
[StandardConnect]: { | ||
version: '1.0.0', | ||
connect: this.#connect, | ||
}, | ||
[StandardDisconnect]: { | ||
version: '1.0.0', | ||
disconnect: this.#disconnect, | ||
}, | ||
[StandardEvents]: { | ||
version: '1.0.0', | ||
on: this.#on, | ||
}, | ||
[SolanaSignAndSendTransaction]: { | ||
version: '1.0.0', | ||
supportedTransactionVersions: ['legacy', 0], | ||
signAndSendTransaction: this.#signAndSendTransaction, | ||
}, | ||
[SolanaSignTransaction]: { | ||
version: '1.0.0', | ||
supportedTransactionVersions: ['legacy', 0], | ||
signTransaction: this.#signTransaction, | ||
}, | ||
[SolanaSignMessage]: { | ||
version: '1.0.0', | ||
signMessage: this.#signMessage, | ||
}, | ||
}; | ||
} | ||
|
||
get accounts() { | ||
return this.#solflareMetaMask ? this.#solflareMetaMask.standardAccounts : []; | ||
} | ||
|
||
#on: StandardEventsOnMethod = (event, listener) => { | ||
this.#listeners[event]?.push(listener) || (this.#listeners[event] = [listener]); | ||
return (): void => this.#off(event, listener); | ||
}; | ||
|
||
#emit<E extends StandardEventsNames>(event: E, ...args: Parameters<StandardEventsListeners[E]>): void { | ||
// eslint-disable-next-line prefer-spread | ||
this.#listeners[event]?.forEach((listener) => listener.apply(null, args)); | ||
} | ||
|
||
#off<E extends StandardEventsNames>(event: E, listener: StandardEventsListeners[E]): void { | ||
this.#listeners[event] = this.#listeners[event]?.filter((existingListener) => listener !== existingListener); | ||
} | ||
|
||
#connect: StandardConnectMethod = async () => { | ||
if (!this.#solflareMetaMask) { | ||
let SolflareMetaMaskClass: typeof SolflareMetaMask; | ||
try { | ||
SolflareMetaMaskClass = (await import('@solflare-wallet/metamask-sdk')).default; | ||
} catch (error: any) { | ||
throw new Error('Unable to load Solflare MetaMask SDK'); | ||
} | ||
this.#solflareMetaMask = new SolflareMetaMaskClass(); | ||
this.#solflareMetaMask.on('standard_change', (properties: StandardEventsChangeProperties) => | ||
this.#emit('change', properties) | ||
); | ||
} | ||
|
||
if (!this.accounts.length) { | ||
await this.#solflareMetaMask.connect(); | ||
} | ||
|
||
return { accounts: this.accounts }; | ||
}; | ||
|
||
#disconnect: StandardDisconnectMethod = async () => { | ||
if (!this.#solflareMetaMask) return; | ||
await this.#solflareMetaMask.disconnect(); | ||
}; | ||
|
||
#signAndSendTransaction: SolanaSignAndSendTransactionMethod = async (...inputs) => { | ||
if (!this.#solflareMetaMask) throw new WalletNotConnectedError(); | ||
return await this.#solflareMetaMask.standardSignAndSendTransaction(...inputs); | ||
}; | ||
|
||
#signTransaction: SolanaSignTransactionMethod = async (...inputs) => { | ||
if (!this.#solflareMetaMask) throw new WalletNotConnectedError(); | ||
return await this.#solflareMetaMask.standardSignTransaction(...inputs); | ||
}; | ||
|
||
#signMessage: SolanaSignMessageMethod = async (...inputs) => { | ||
if (!this.#solflareMetaMask) throw new WalletNotConnectedError(); | ||
return await this.#solflareMetaMask.standardSignMessage(...inputs); | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.