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 154
/
index.ts
93 lines (88 loc) · 3.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Dappeteer } from "..";
import { DappeteerBrowser } from "../browser";
import { DappeteerPage } from "../page";
import { getAllNotifications, installSnap, invokeSnap } from "../snap";
import { getNotificationEmitter } from "../snap/getNotificationEmitter";
import { createDialogMethods } from "../snap/dialog";
import { acceptAddNetwork, rejectAddNetwork } from "./addNetwork";
import { approve } from "./approve";
import { confirmTransaction } from "./confirmTransaction";
import { deleteAccount, deleteNetwork, getTokenBalance } from "./helpers";
import { importPk } from "./importPk";
import { lock } from "./lock";
import { sign } from "./sign";
import { signTypedData } from "./signTypedData";
import { switchAccount } from "./switchAccount";
import { switchNetwork } from "./switchNetwork";
import { unlock } from "./unlock";
import { acceptAddToken, rejectAddToken } from "./addToken";
import { createAccount } from "./createAccount";
export type SetSignedIn = (state: boolean) => Promise<void>;
export type GetSingedIn = () => Promise<boolean>;
export const getMetaMask = (page: DappeteerPage): Promise<Dappeteer> => {
// modified window object to kep state between tests
const setSignedIn = async (state: boolean): Promise<void> => {
const evaluateFn = (s: boolean): void => {
(window as unknown as { signedIn: boolean }).signedIn = s;
};
await page.evaluate(evaluateFn, state);
};
const getSingedIn = (): Promise<boolean> => {
const evaluateFn = (): boolean =>
(window as unknown as { signedIn: boolean | undefined }).signedIn !==
undefined
? (window as unknown as { signedIn: boolean }).signedIn
: true;
return page.evaluate(evaluateFn);
};
return new Promise<Dappeteer>((resolve) => {
resolve({
acceptAddNetwork: acceptAddNetwork(page),
rejectAddNetwork: rejectAddNetwork(page),
approve: approve(page),
confirmTransaction: confirmTransaction(page, getSingedIn),
importPK: importPk(page),
lock: lock(page, setSignedIn, getSingedIn),
sign: sign(page, getSingedIn),
signTypedData: signTypedData(page, getSingedIn),
switchAccount: switchAccount(page),
switchNetwork: switchNetwork(page),
unlock: unlock(page, setSignedIn, getSingedIn),
acceptAddToken: acceptAddToken(page),
rejectAddToken: rejectAddToken(page),
createAccount: createAccount(page),
helpers: {
getTokenBalance: getTokenBalance(page),
deleteAccount: deleteAccount(page),
deleteNetwork: deleteNetwork(page),
},
snaps: {
invokeSnap,
getNotificationEmitter: getNotificationEmitter(page),
getAllNotifications: getAllNotifications(page),
installSnap: installSnap(page),
dialog: createDialogMethods(page),
},
page,
});
});
};
/**
* Return MetaMask instance
* */
export async function getMetaMaskWindow(
browser: DappeteerBrowser
): Promise<Dappeteer> {
const metaMaskPage = await new Promise<DappeteerPage>((resolve, reject) => {
browser
.pages()
.then((pages) => {
for (const page of pages) {
if (page.url().includes("chrome-extension")) resolve(page);
}
reject("MetaMask extension not found");
})
.catch((e) => reject(e));
});
return getMetaMask(metaMaskPage);
}