Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat: Add invokeSnap method; update installSnap method parameter; (#159)
Browse files Browse the repository at this point in the history
* Add invokeSnap method; update installSnap method parameter;

* adjust invokeSnap method after review

* Fix params type in invokeSnap method

* update invokeSnap Return type

* update invokeSnap with generic params

* update test expectation

* fix lint
  • Loading branch information
Lykhoyda authored Oct 26, 2022
1 parent d1a3ec4 commit e7d8435
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/snap/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { flaskOnly } from "./utils";
export { installSnap } from "./install";
export { invokeSnap } from "./invokeSnap";
5 changes: 3 additions & 2 deletions src/snap/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export async function installSnap(
hasKeyPermissions: boolean;
customSteps?: InstallStep[];
version?: string;
}
},
installationSnapUrl: string = "https://google.com"
): Promise<void> {
flaskOnly(page);
//need to open page to access window.ethereum
const installPage = await page.browser().newPage();
await installPage.goto("https://google.com");
await installPage.goto(installationSnapUrl);
const installAction = installPage.evaluate(
(opts: { snapId: string; version?: string }) =>
window.ethereum.request<{ snaps: { [snapId: string]: {} } }>({
Expand Down
29 changes: 29 additions & 0 deletions src/snap/invokeSnap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Page, Serializable } from "puppeteer";
import { flaskOnly } from "./utils";

export async function invokeSnap<
R = unknown,
P extends Serializable = Serializable
>(
page: Page,
snapId: string,
method: string,
params: P
): ReturnType<typeof window.ethereum.request<R>> {
flaskOnly(page);
return page.evaluate(
async (opts: { snapId: string; method: string; params: P }) => {
return window.ethereum.request<R>({
method: "wallet_invokeSnap",
params: [
`${opts.snapId}`,
{
method: opts.method,
params: opts.params,
},
],
});
},
{ snapId, method, params }
);
}
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import * as puppeteer from "puppeteer";

import { MetaMaskInpageProvider } from "@metamask/providers";
import { Path } from "./setup/metaMaskDownloader";

import { RECOMMENDED_METAMASK_VERSION } from "./index";

declare global {
interface Window {
ethereum: MetaMaskInpageProvider;
}
}

export type LaunchOptions = (OfficialOptions | CustomOptions) & {
//install flask (canary) version of metamask.
metaMaskFlask?: boolean;
Expand Down
54 changes: 41 additions & 13 deletions test/flask/snaps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { expect } from "chai";
import * as dappeteer from "../../src";
import { installSnap } from "../../src/snap";
import { installSnap, invokeSnap } from "../../src/snap";
import { TestContext } from "../constant";
import { Snaps } from "../deploy";
import { toUrl } from "../utils/utils";
import { clickOnButton } from "../../src/helpers";

function getSnapIdByName(testContext: TestContext, snapName: Snaps): string {
return `local:${toUrl(testContext.snapServers[snapName].address())}`;
}

describe("snaps", function () {
let metamask: dappeteer.Dappeteer;
Expand All @@ -19,20 +25,16 @@ describe("snaps", function () {
});

it("should install base snap from npm", async function (this: TestContext) {
await installSnap(
metamask.page,
"local:" + toUrl(this.snapServers[Snaps.BASE_SNAP].address()),
{
hasPermissions: false,
hasKeyPermissions: false,
}
);
await installSnap(metamask.page, getSnapIdByName(this, Snaps.BASE_SNAP), {
hasPermissions: false,
hasKeyPermissions: false,
});
});

it("should install permissions snap from npm", async function (this: TestContext) {
await installSnap(
metamask.page,
"local:" + toUrl(this.snapServers[Snaps.PERMISSIONS_SNAP].address()),
getSnapIdByName(this, Snaps.PERMISSIONS_SNAP),
{
hasPermissions: true,
hasKeyPermissions: false,
Expand All @@ -41,13 +43,39 @@ describe("snaps", function () {
});

it("should install keys snap from npm", async function (this: TestContext) {
await installSnap(metamask.page, getSnapIdByName(this, Snaps.KEYS_SNAP), {
hasPermissions: true,
hasKeyPermissions: true,
});
});

it("should invoke provided snap method", async function (this: TestContext) {
const snapAddress = "http://localhost:8545";
await installSnap(
metamask.page,
"local:" + toUrl(this.snapServers[Snaps.KEYS_SNAP].address()),
getSnapIdByName(this, Snaps.PERMISSIONS_SNAP),
{
hasPermissions: true,
hasKeyPermissions: true,
}
hasKeyPermissions: false,
},
snapAddress
);

const testPage = await metamask.page.browser().newPage();
await testPage.goto(snapAddress);

const invokeAction = invokeSnap(
testPage,
getSnapIdByName(this, Snaps.PERMISSIONS_SNAP),
"hello",
{ version: "latest" }
);

await metamask.page.bringToFront();
await metamask.page.reload();

await clickOnButton(metamask.page, "Approve");

expect(await invokeAction).to.equal(true);
});
});

0 comments on commit e7d8435

Please sign in to comment.