-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,344 additions
and
206 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
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 @@ | ||
SNAP_ORIGIN="" |
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 was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,15 @@ | ||
import ChainsafeSvg from './chainsafe.svg'; | ||
import FormTransferSvg from './form-transfer.svg'; | ||
import MetaMaskLogoSvg from './metaMaskLogo.svg'; | ||
import MetaMaskSnapsLogoSvg from './metamask-snaps-logo.svg'; | ||
import ZcashSvg from './zcash.svg'; | ||
import ZcashYellowSvg from './zcash-yellow.svg'; | ||
|
||
export { | ||
ChainsafeSvg, | ||
MetaMaskLogoSvg, | ||
MetaMaskSnapsLogoSvg, | ||
ZcashSvg, | ||
ZcashYellowSvg, | ||
FormTransferSvg, | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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 @@ | ||
export { defaultSnapOrigin } from './snap'; |
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,2 @@ | ||
export const defaultSnapOrigin = | ||
import.meta.env.SNAP_ORIGIN ?? `local:http://localhost:8080`; |
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,74 @@ | ||
import type { MetaMaskInpageProvider } from '@metamask/providers'; | ||
import type { ReactNode } from 'react'; | ||
import { createContext, useContext, useEffect, useState } from 'react'; | ||
|
||
import type { Snap } from '../types'; | ||
import { getSnapsProvider } from '../utils'; | ||
|
||
type MetaMaskContextType = { | ||
provider: MetaMaskInpageProvider | null; | ||
installedSnap: Snap | null; | ||
error: Error | null; | ||
setInstalledSnap: (snap: Snap | null) => void; | ||
setError: (error: Error) => void; | ||
}; | ||
|
||
export const MetaMaskContext = createContext<MetaMaskContextType>({ | ||
provider: null, | ||
installedSnap: null, | ||
error: null, | ||
setInstalledSnap: () => { | ||
/* no-op */ | ||
}, | ||
setError: () => { | ||
/* no-op */ | ||
}, | ||
}); | ||
|
||
/** | ||
* MetaMask context provider to handle MetaMask and snap status. | ||
* | ||
* @param props - React Props. | ||
* @param props.children - React component to be wrapped by the Provider. | ||
* @returns JSX. | ||
*/ | ||
export const MetaMaskProvider = ({ children }: { children: ReactNode }) => { | ||
const [provider, setProvider] = useState<MetaMaskInpageProvider | null>(null); | ||
const [installedSnap, setInstalledSnap] = useState<Snap | null>(null); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
useEffect(() => { | ||
getSnapsProvider().then(setProvider).catch(console.error); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (error) { | ||
const timeout = setTimeout(() => { | ||
setError(null); | ||
}, 10000); | ||
|
||
return () => { | ||
clearTimeout(timeout); | ||
}; | ||
} | ||
|
||
return undefined; | ||
}, [error]); | ||
|
||
return ( | ||
<MetaMaskContext.Provider | ||
value={{ provider, error, setError, installedSnap, setInstalledSnap }} | ||
> | ||
{children} | ||
</MetaMaskContext.Provider> | ||
); | ||
}; | ||
|
||
/** | ||
* Utility hook to consume the MetaMask context. | ||
* | ||
* @returns The MetaMask context. | ||
*/ | ||
export function useMetaMaskContext() { | ||
return useContext(MetaMaskContext); | ||
} |
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 @@ | ||
export * from './MetamaskContext'; | ||
export * from './useMetaMask'; | ||
export * from './useRequest'; | ||
export * from './useRequestSnap'; | ||
export * from './useInvokeSnap'; |
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,37 @@ | ||
import { defaultSnapOrigin } from '../config'; | ||
import { useRequest } from './useRequest'; | ||
|
||
export type InvokeSnapParams = { | ||
method: string; | ||
params?: Record<string, unknown>; | ||
}; | ||
|
||
/** | ||
* Utility hook to wrap the `wallet_invokeSnap` method. | ||
* | ||
* @param snapId - The Snap ID to invoke. Defaults to the snap ID specified in the | ||
* config. | ||
* @returns The invokeSnap wrapper method. | ||
*/ | ||
export const useInvokeSnap = (snapId = defaultSnapOrigin) => { | ||
const request = useRequest(); | ||
|
||
/** | ||
* Invoke the requested Snap method. | ||
* | ||
* @param params - The invoke params. | ||
* @param params.method - The method name. | ||
* @param params.params - The method params. | ||
* @returns The Snap response. | ||
*/ | ||
const invokeSnap = async ({ method, params }: InvokeSnapParams) => | ||
request({ | ||
method: 'wallet_invokeSnap', | ||
params: { | ||
snapId, | ||
request: params ? { method, params } : { method }, | ||
}, | ||
}); | ||
|
||
return invokeSnap; | ||
}; |
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,55 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { useMetaMaskContext } from './MetamaskContext'; | ||
import { useRequest } from './useRequest'; | ||
import { GetSnapsResponse } from '../types'; | ||
import { defaultSnapOrigin } from '../config'; | ||
|
||
/** | ||
* A Hook to retrieve useful data from MetaMask. | ||
* @returns The informations. | ||
*/ | ||
export const useMetaMask = () => { | ||
const { provider, setInstalledSnap, installedSnap } = useMetaMaskContext(); | ||
const request = useRequest(); | ||
const [isFlask, setIsFlask] = useState(false); | ||
|
||
const snapsDetected = provider !== null; | ||
|
||
/** | ||
* Detect if the version of MetaMask is Flask. | ||
*/ | ||
const detectFlask = async () => { | ||
const clientVersion = await request({ | ||
method: 'web3_clientVersion', | ||
}); | ||
|
||
const isFlaskDetected = (clientVersion as string[])?.includes('flask'); | ||
|
||
setIsFlask(isFlaskDetected); | ||
}; | ||
|
||
/** | ||
* Get the Snap informations from MetaMask. | ||
*/ | ||
const getSnap = async () => { | ||
const snaps = (await request({ | ||
method: 'wallet_getSnaps', | ||
})) as GetSnapsResponse; | ||
|
||
setInstalledSnap(snaps[defaultSnapOrigin] ?? null); | ||
}; | ||
|
||
useEffect(() => { | ||
const detect = async () => { | ||
if (provider) { | ||
await detectFlask(); | ||
await getSnap(); | ||
} | ||
}; | ||
|
||
detect().catch(console.error); | ||
}, [detectFlask, getSnap, provider]); | ||
|
||
return { isFlask, snapsDetected, installedSnap, getSnap }; | ||
}; |
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,40 @@ | ||
import type { RequestArguments } from '@metamask/providers'; | ||
|
||
import { useMetaMaskContext } from './MetamaskContext'; | ||
|
||
export type Request = (params: RequestArguments) => Promise<unknown | null>; | ||
|
||
/** | ||
* Utility hook to consume the provider `request` method with the available provider. | ||
* | ||
* @returns The `request` function. | ||
*/ | ||
export const useRequest = () => { | ||
const { provider, setError } = useMetaMaskContext(); | ||
|
||
/** | ||
* `provider.request` wrapper. | ||
* | ||
* @param params - The request params. | ||
* @param params.method - The method to call. | ||
* @param params.params - The method params. | ||
* @returns The result of the request. | ||
*/ | ||
const request: Request = async ({ method, params }) => { | ||
try { | ||
const data = | ||
(await provider?.request({ | ||
method, | ||
params, | ||
} as RequestArguments)) ?? null; | ||
|
||
return data; | ||
} catch (requestError: any) { | ||
setError(requestError); | ||
|
||
return null; | ||
} | ||
}; | ||
|
||
return request; | ||
}; |
Oops, something went wrong.