-
Notifications
You must be signed in to change notification settings - Fork 973
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 #834 from vsakos/solflare-metamask-inpage-provider
Initialize the MetaMask extension provider internally
- Loading branch information
Showing
3 changed files
with
50 additions
and
54 deletions.
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 | ||
--- | ||
|
||
Optimize Solflare MetaMask snap detection |
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 |
---|---|---|
@@ -1,61 +1,52 @@ | ||
import type { EthereumProvider, WindowWithEthereum } from '@solflare-wallet/metamask-sdk'; | ||
import { registerWallet } from '@wallet-standard/wallet'; | ||
import { SolflareMetaMaskWallet } from './wallet.js'; | ||
|
||
let stopPolling = false; | ||
let registered = 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; | ||
function register() { | ||
if (registered) return; | ||
registerWallet(new SolflareMetaMaskWallet()); | ||
registered = true; | ||
} | ||
|
||
async function isSnapProviderDetected(): Promise<boolean> { | ||
try { | ||
const provider = (window as WindowWithEthereum).ethereum; | ||
if (!provider) return false; | ||
/** @internal */ | ||
export async function detectAndRegisterSolflareMetaMaskWallet(): Promise<void> { | ||
const id = 'solflare-detect-metamask'; | ||
|
||
const providerProviders = provider.providers; | ||
if (providerProviders && Array.isArray(providerProviders)) { | ||
for (const provider of providerProviders) { | ||
if (await isSnapSupported(provider)) return true; | ||
} | ||
} | ||
function postMessage() { | ||
window.postMessage( | ||
{ | ||
target: 'metamask-contentscript', | ||
data: { | ||
name: 'metamask-provider', | ||
data: { | ||
id, | ||
jsonrpc: '2.0', | ||
method: 'wallet_getSnaps', | ||
}, | ||
}, | ||
}, | ||
window.location.origin | ||
); | ||
} | ||
|
||
function onMessage(event: MessageEvent) { | ||
const message = event.data; | ||
if (message?.target === 'metamask-inpage' && message.data?.name === 'metamask-provider') { | ||
if (message.data.data?.id === id) { | ||
window.removeEventListener('message', onMessage); | ||
|
||
const providerDetected = provider.detected; | ||
if (providerDetected && Array.isArray(providerDetected)) { | ||
for (const provider of providerDetected) { | ||
if (await isSnapSupported(provider)) return true; | ||
if (!message.data.data.error) { | ||
register(); | ||
} | ||
} else { | ||
postMessage(); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
window.addEventListener('message', onMessage); | ||
window.setTimeout(() => window.removeEventListener('message', onMessage), 5000); | ||
|
||
postMessage(); | ||
} |