-
Notifications
You must be signed in to change notification settings - Fork 2
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
10 changed files
with
71 additions
and
221 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
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,10 +1,2 @@ | ||
/** | ||
* The snap origin to use. | ||
* Will default to the local hosted snap if no value is provided in environment. | ||
* | ||
* You may be tempted to change this to the URL where your production snap is hosted, but please | ||
* don't. Instead, rename `.env.production.dist` to `.env.production` and set the production URL | ||
* there. Running `yarn build` will automatically use the production environment variables. | ||
*/ | ||
// TODO - Change this to the massa snap origin when published | ||
export const MASSA_SNAP_ID = `local:http://localhost:8080`; | ||
export const MASSA_SNAP_ID = 'npm:@massalabs/metamask-snap'; | ||
// export const MASSA_SNAP_ID = `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
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,94 +1,66 @@ | ||
import type { MetaMaskInpageProvider } from '@metamask/providers'; | ||
|
||
import { MASSA_SNAP_ID } from './config'; | ||
import type { GetSnapsResponse, Snap } from './types'; | ||
import { type GetSnapsResponse, Snap } from './types'; | ||
|
||
const getInstalledSnaps = async ( | ||
export const getInstalledSnaps = async ( | ||
provider: MetaMaskInpageProvider, | ||
): Promise<GetSnapsResponse> => | ||
provider.request({ | ||
method: 'wallet_getSnaps', | ||
}); | ||
|
||
export const getMassaSnapInfo = async ( | ||
provider: MetaMaskInpageProvider, | ||
version?: string, | ||
): Promise<Snap | undefined> => { | ||
try { | ||
const snaps = await getInstalledSnaps(provider); | ||
|
||
return Object.values(snaps).find( | ||
(snap) => | ||
snap.id === MASSA_SNAP_ID && (!version || snap.version === version), | ||
); | ||
} catch (error) { | ||
console.error('Failed to obtain installed snap', error); | ||
Check warning on line 25 in src/metamaskSnap/snap.ts
|
||
return undefined; | ||
} | ||
}; | ||
|
||
/** | ||
* Connect a snap to MetaMask. | ||
* | ||
* @param provider - The MetaMask inpage provider. | ||
* @param snapId - The ID of the snap. | ||
*/ | ||
export const isConnected = async ( | ||
provider: MetaMaskInpageProvider, | ||
): Promise<boolean> => { | ||
const snap = await getMassaSnapInfo(provider); | ||
return !!snap; | ||
}; | ||
|
||
/** | ||
* Connect a snap to MetaMask. | ||
* | ||
* @param provider - The MetaMask inpage provider. | ||
* @param params - The params to pass with the snap to connect. | ||
*/ | ||
export const connectSnap = async ( | ||
provider: MetaMaskInpageProvider, | ||
snapId: string = MASSA_SNAP_ID, | ||
params: Record<'version' | string, unknown> = {}, | ||
) => { | ||
provider.request({ | ||
method: 'wallet_requestSnaps', | ||
params: { | ||
[snapId]: params, | ||
[MASSA_SNAP_ID]: params, | ||
}, | ||
}); | ||
}; | ||
|
||
export const getMassaSnapInfo = async ( | ||
provider: MetaMaskInpageProvider, | ||
version?: string, | ||
): Promise<Snap | undefined> => { | ||
try { | ||
const snaps = await getInstalledSnaps(provider); | ||
|
||
return Object.values(snaps).find( | ||
(snap) => | ||
snap.id === MASSA_SNAP_ID && (!version || snap.version === version), | ||
); | ||
} catch (error) { | ||
console.error('Failed to obtain installed snap', error); | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const showPrivateKey = async (provider: MetaMaskInpageProvider) => { | ||
return provider.request({ | ||
method: 'wallet_invokeSnap', | ||
params: { snapId: MASSA_SNAP_ID, request: { method: 'showSecretKey' } }, | ||
}); | ||
}; | ||
|
||
export const isLocalSnap = (snapId: string) => snapId.startsWith('local:'); | ||
|
||
export async function isDappConnectedToSnap(snapId: string): Promise<boolean> { | ||
if (typeof window.ethereum === 'undefined') { | ||
console.error('MetaMask is not installed.'); | ||
return false; | ||
} | ||
|
||
try { | ||
// Request all installed snaps | ||
const installedSnaps = await window.ethereum.request({ | ||
method: 'wallet_getSnaps', | ||
}); | ||
|
||
// Check if the specific Snap is installed | ||
const snap = installedSnaps[snapId]; | ||
if (!snap) { | ||
console.log(`Snap with ID ${snapId} is not installed.`); | ||
return false; | ||
} | ||
|
||
// Check if the current dApp is allowed | ||
const currentOrigin = window.location.origin; | ||
const allowedOrigins = | ||
snap.permissions?.snap_allowedOrigins?.caveats?.[0]?.value || []; | ||
|
||
if (allowedOrigins.includes(currentOrigin)) { | ||
console.log(`DApp ${currentOrigin} is already connected to the Snap.`); | ||
return true; | ||
} else { | ||
console.log(`DApp ${currentOrigin} is NOT connected to the Snap.`); | ||
return false; | ||
} | ||
} catch (error) { | ||
console.error('Error checking Snap connection:', error); | ||
return false; | ||
} | ||
} |
Oops, something went wrong.