This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to install snap (#154)
* feat: add support for installing metamask flaask * feat: ability to install snaps * fi lint * fix starting a snap * fix snap install Co-authored-by: Bernard <bero4net@gmail.com>
- Loading branch information
Showing
29 changed files
with
12,773 additions
and
45 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,2 @@ | ||
export { flaskOnly } from "./utils"; | ||
export { installSnap } from "./install"; |
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,91 @@ | ||
import { MetaMaskInpageProvider } from "@metamask/providers"; | ||
import { Page } from "puppeteer"; | ||
import { | ||
clickOnButton, | ||
clickOnElement, | ||
clickOnLogo, | ||
openProfileDropdown, | ||
} from "../helpers"; | ||
import { flaskOnly } from "./utils"; | ||
|
||
declare let window: { ethereum: MetaMaskInpageProvider }; | ||
|
||
export type InstallStep = (page: Page) => Promise<void>; | ||
|
||
export async function installSnap( | ||
page: Page, | ||
snapId: string, | ||
opts: { | ||
hasPermissions: boolean; | ||
hasKeyPermissions: boolean; | ||
customSteps?: InstallStep[]; | ||
version?: string; | ||
} | ||
): Promise<void> { | ||
flaskOnly(page); | ||
//need to open page to access window.ethereum | ||
const installPage = await page.browser().newPage(); | ||
await installPage.goto("https://google.com"); | ||
const installAction = installPage.evaluate( | ||
(opts: { snapId: string; version?: string }) => | ||
window.ethereum.request({ | ||
method: "wallet_enable", | ||
params: [ | ||
{ | ||
[`wallet_snap_${opts.snapId}`]: { | ||
version: opts.version ?? "latest", | ||
}, | ||
}, | ||
], | ||
}), | ||
{ snapId, version: opts.version } | ||
); | ||
|
||
await page.bringToFront(); | ||
await page.reload(); | ||
await clickOnButton(page, "Connect"); | ||
if (opts.hasPermissions) { | ||
await clickOnButton(page, "Approve & install"); | ||
if (opts.hasKeyPermissions) { | ||
const checkbox = await page.waitForSelector(".checkbox-label", { | ||
visible: true, | ||
}); | ||
await checkbox.click(); | ||
await clickOnButton(page, "Confirm"); | ||
} | ||
} else { | ||
await clickOnButton(page, "Install"); | ||
} | ||
|
||
await installAction; | ||
await installPage.close({ runBeforeUnload: true }); | ||
|
||
for (const step of opts.customSteps ?? []) { | ||
await step(page); | ||
} | ||
if (!(await isSnapInstalled(page, snapId))) { | ||
throw new Error("Failed to install snap " + snapId); | ||
} | ||
} | ||
|
||
export async function isSnapInstalled( | ||
page: Page, | ||
snapId: string | ||
): Promise<boolean> { | ||
await page.bringToFront(); | ||
await openProfileDropdown(page); | ||
|
||
await clickOnElement(page, "Settings"); | ||
await clickOnElement(page, "Snaps"); | ||
let found = false; | ||
try { | ||
await page.waitForXPath(`//*[contains(text(), '${snapId}')]`, { | ||
timeout: 5000, | ||
}); | ||
found = true; | ||
} catch (e) { | ||
found = false; | ||
} | ||
await clickOnLogo(page); | ||
return found; | ||
} |
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,10 @@ | ||
import { Page } from "puppeteer"; | ||
import { DappeteerBrowser } from "../setup"; | ||
|
||
export function flaskOnly(page: Page): void { | ||
if ((page.browser() as DappeteerBrowser).flask == null) { | ||
throw new Error( | ||
"This method is only available when running Metamask Flask" | ||
); | ||
} | ||
} |
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,65 @@ | ||
<!doctype html> | ||
<html> | ||
</head> | ||
<title>Hello, Snaps!</title> | ||
<link rel="icon" type="image/svg" href="./images/icon.svg"/> | ||
</head> | ||
|
||
<body> | ||
<h1>Hello, Snaps!</h1> | ||
<details> | ||
<summary>Instructions</summary> | ||
<ul> | ||
<li>First, click "Connect". Then, try out the other buttons!</li> | ||
<li>Please note that:</li> | ||
<ul> | ||
<li> | ||
The <code>snap.manifest.json</code> and <code>package.json</code> must be located in the server root directory... | ||
</li> | ||
<li> | ||
The Snap bundle must be hosted at the location specified by the <code>location</code> field of <code>snap.manifest.json</code>. | ||
</li> | ||
</ul> | ||
</ul> | ||
</details> | ||
<br/> | ||
|
||
<button class="connect">Connect</button> | ||
<button class="sendHello">Send Hello</button> | ||
</body> | ||
|
||
<script> | ||
const snapId = `local:${window.location.href}`; | ||
|
||
const connectButton = document.querySelector('button.connect') | ||
const sendButton = document.querySelector('button.sendHello') | ||
|
||
connectButton.addEventListener('click', connect) | ||
sendButton.addEventListener('click', send) | ||
|
||
// here we get permissions to interact with and install the snap | ||
async function connect () { | ||
await ethereum.request({ | ||
method: 'wallet_enable', | ||
params: [{ | ||
wallet_snap: { [snapId]: {} }, | ||
}] | ||
}) | ||
} | ||
|
||
// here we call the snap's "hello" method | ||
async function send () { | ||
try { | ||
await ethereum.request({ | ||
method: 'wallet_invokeSnap', | ||
params: [snapId, { | ||
method: 'hello' | ||
}] | ||
}) | ||
} catch (err) { | ||
console.error(err) | ||
alert('Problem happened: ' + err.message || err) | ||
} | ||
} | ||
</script> | ||
</html> |
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,18 @@ | ||
{ | ||
"name": "base-snap", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"prefix": "mm-snap build", | ||
"fix": "mm-snap manifest --fix" | ||
}, | ||
"devDependencies": { | ||
"@metamask/snaps-cli": "^0.22.0" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@metamask/snap-types": "^0.22.0" | ||
} | ||
} |
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,6 @@ | ||
module.exports = { | ||
cliOptions: { | ||
src: './src/index.ts', | ||
port: 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,17 @@ | ||
{ | ||
"version": "1.0.0", | ||
"description": "An example Snap written in TypeScript.", | ||
"proposedName": "Base Snap\n", | ||
"source": { | ||
"shasum": "sApNS/DJjjs8psniSQzWlOdkzJ75IsbtlpdipfKarRI=", | ||
"location": { | ||
"npm": { | ||
"filePath": "dist/bundle.js", | ||
"packageName": "base-snap", | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} | ||
}, | ||
"initialPermissions": {}, | ||
"manifestVersion": "0.1" | ||
} |
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,21 @@ | ||
import { OnRpcRequestHandler } from "@metamask/snap-types"; | ||
|
||
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => { | ||
switch (request.method) { | ||
case "hello": | ||
return wallet.request({ | ||
method: "snap_confirm", | ||
params: [ | ||
{ | ||
prompt: `Hello, ${origin}!`, | ||
description: | ||
"This custom confirmation is just for display purposes.", | ||
textAreaContent: | ||
"But you can edit the snap source code to make it do something, if you want to!", | ||
}, | ||
], | ||
}); | ||
default: | ||
throw new Error("Method not found."); | ||
} | ||
}; |
Oops, something went wrong.