w3wallets 0.10.2 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -19
- package/dist/index.d.mts +125 -42
- package/dist/index.d.ts +125 -42
- package/dist/index.js +277 -241
- package/dist/index.mjs +266 -237
- package/package.json +2 -2
- package/src/scripts/download.js +366 -67
package/dist/index.mjs
CHANGED
|
@@ -1,19 +1,119 @@
|
|
|
1
1
|
// src/withWallets.ts
|
|
2
2
|
import path from "path";
|
|
3
3
|
import fs from "fs";
|
|
4
|
-
|
|
5
|
-
// tests/utils/sleep.ts
|
|
6
|
-
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
-
|
|
8
|
-
// src/withWallets.ts
|
|
4
|
+
import crypto from "crypto";
|
|
9
5
|
import {
|
|
10
6
|
chromium
|
|
11
7
|
} from "@playwright/test";
|
|
8
|
+
var W3WALLETS_DIR = ".w3wallets";
|
|
9
|
+
function sleep(ms) {
|
|
10
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
}
|
|
12
|
+
function withWallets(test, ...wallets) {
|
|
13
|
+
const extensionInfo = wallets.map((w) => {
|
|
14
|
+
const extPath = path.join(process.cwd(), W3WALLETS_DIR, w.extensionDir);
|
|
15
|
+
ensureWalletExtensionExists(extPath, w.name);
|
|
16
|
+
const extensionId = w.extensionId ?? getExtensionId(extPath);
|
|
17
|
+
return { path: extPath, id: extensionId, name: w.name };
|
|
18
|
+
});
|
|
19
|
+
const extensionPaths = extensionInfo.map((e) => e.path);
|
|
20
|
+
const fixtures = {
|
|
21
|
+
context: async ({}, use, testInfo) => {
|
|
22
|
+
const userDataDir = path.join(
|
|
23
|
+
process.cwd(),
|
|
24
|
+
W3WALLETS_DIR,
|
|
25
|
+
".context",
|
|
26
|
+
testInfo.testId
|
|
27
|
+
);
|
|
28
|
+
cleanUserDataDir(userDataDir);
|
|
29
|
+
const context = await chromium.launchPersistentContext(userDataDir, {
|
|
30
|
+
headless: testInfo.project.use.headless ?? true,
|
|
31
|
+
channel: "chromium",
|
|
32
|
+
args: [
|
|
33
|
+
`--disable-extensions-except=${extensionPaths.join(",")}`,
|
|
34
|
+
`--load-extension=${extensionPaths.join(",")}`
|
|
35
|
+
]
|
|
36
|
+
});
|
|
37
|
+
while (context.serviceWorkers().length < extensionPaths.length) {
|
|
38
|
+
await sleep(1e3);
|
|
39
|
+
}
|
|
40
|
+
await Promise.all(context.pages().map((page) => page.close()));
|
|
41
|
+
await use(context);
|
|
42
|
+
await context.close();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
for (let i = 0; i < wallets.length; i++) {
|
|
46
|
+
const wallet = wallets[i];
|
|
47
|
+
const info = extensionInfo[i];
|
|
48
|
+
fixtures[wallet.name] = async ({ context }, use) => {
|
|
49
|
+
const instance = await initializeExtension(
|
|
50
|
+
context,
|
|
51
|
+
wallet.WalletClass,
|
|
52
|
+
info.id,
|
|
53
|
+
wallet.name
|
|
54
|
+
);
|
|
55
|
+
await use(instance);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return test.extend(fixtures);
|
|
59
|
+
}
|
|
60
|
+
function cleanUserDataDir(userDataDir) {
|
|
61
|
+
if (fs.existsSync(userDataDir)) {
|
|
62
|
+
fs.rmSync(userDataDir, { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function ensureWalletExtensionExists(walletPath, walletName) {
|
|
66
|
+
if (!fs.existsSync(path.join(walletPath, "manifest.json"))) {
|
|
67
|
+
const cliAlias = walletName.toLowerCase();
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Cannot find ${walletName}. Please download it via 'npx w3wallets ${cliAlias}'.`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function getExtensionId(extensionPath) {
|
|
74
|
+
const absolutePath = path.resolve(extensionPath);
|
|
75
|
+
const manifestPath = path.join(absolutePath, "manifest.json");
|
|
76
|
+
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
|
|
77
|
+
let dataToHash;
|
|
78
|
+
if (manifest.key) {
|
|
79
|
+
dataToHash = Buffer.from(manifest.key, "base64");
|
|
80
|
+
} else {
|
|
81
|
+
dataToHash = Buffer.from(absolutePath);
|
|
82
|
+
}
|
|
83
|
+
const hash = crypto.createHash("sha256").update(dataToHash).digest();
|
|
84
|
+
const ALPHABET = "abcdefghijklmnop";
|
|
85
|
+
let extensionId = "";
|
|
86
|
+
for (let i = 0; i < 16; i++) {
|
|
87
|
+
const byte = hash[i];
|
|
88
|
+
extensionId += ALPHABET[byte >> 4 & 15];
|
|
89
|
+
extensionId += ALPHABET[byte & 15];
|
|
90
|
+
}
|
|
91
|
+
return extensionId;
|
|
92
|
+
}
|
|
93
|
+
async function initializeExtension(context, ExtensionClass, expectedExtensionId, walletName) {
|
|
94
|
+
const expectedUrl = `chrome-extension://${expectedExtensionId}/`;
|
|
95
|
+
const worker = context.serviceWorkers().find((w) => w.url().startsWith(expectedUrl));
|
|
96
|
+
if (!worker) {
|
|
97
|
+
const availableIds = context.serviceWorkers().map((w) => w.url().split("/")[2]).filter(Boolean);
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Service worker for ${walletName} (ID: ${expectedExtensionId}) not found. Available extension IDs: [${availableIds.join(", ")}]`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const page = await context.newPage();
|
|
103
|
+
const extension = new ExtensionClass(page, expectedExtensionId);
|
|
104
|
+
await extension.gotoOnboardPage();
|
|
105
|
+
return extension;
|
|
106
|
+
}
|
|
12
107
|
|
|
13
|
-
// src/
|
|
108
|
+
// src/core/types.ts
|
|
109
|
+
function createWallet(config) {
|
|
110
|
+
return config;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/wallets/backpack/backpack.ts
|
|
14
114
|
import { expect } from "@playwright/test";
|
|
15
115
|
|
|
16
|
-
// src/wallet.ts
|
|
116
|
+
// src/core/wallet.ts
|
|
17
117
|
var Wallet = class {
|
|
18
118
|
constructor(page, extensionId) {
|
|
19
119
|
this.page = page;
|
|
@@ -21,7 +121,7 @@ var Wallet = class {
|
|
|
21
121
|
}
|
|
22
122
|
};
|
|
23
123
|
|
|
24
|
-
// src/backpack/backpack.ts
|
|
124
|
+
// src/wallets/backpack/backpack.ts
|
|
25
125
|
var Backpack = class extends Wallet {
|
|
26
126
|
defaultPassword = "11111111";
|
|
27
127
|
currentAccountId = 0;
|
|
@@ -105,13 +205,146 @@ var Backpack = class extends Wallet {
|
|
|
105
205
|
}
|
|
106
206
|
};
|
|
107
207
|
|
|
108
|
-
// src/
|
|
208
|
+
// src/wallets/metamask/metamask.ts
|
|
109
209
|
import { expect as expect2 } from "@playwright/test";
|
|
210
|
+
var Metamask = class extends Wallet {
|
|
211
|
+
defaultPassword = "TestPassword123!";
|
|
212
|
+
async gotoOnboardPage() {
|
|
213
|
+
await this.page.goto(
|
|
214
|
+
`chrome-extension://${this.extensionId}/home.html#onboarding/welcome`
|
|
215
|
+
);
|
|
216
|
+
await expect2(
|
|
217
|
+
this.page.getByRole("button", { name: "I have an existing wallet" })
|
|
218
|
+
).toBeVisible();
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Onboard MetaMask with a mnemonic phrase
|
|
222
|
+
* @param mnemonic - 12 or 24 word recovery phrase
|
|
223
|
+
* @param password - Optional password (defaults to TestPassword123!)
|
|
224
|
+
*/
|
|
225
|
+
async onboard(mnemonic, password) {
|
|
226
|
+
const pwd = password ?? this.defaultPassword;
|
|
227
|
+
await this.page.getByRole("button", { name: "I have an existing wallet" }).click();
|
|
228
|
+
await this.page.getByRole("button", { name: "Import using Secret Recovery Phrase" }).click();
|
|
229
|
+
const textbox = this.page.getByRole("textbox");
|
|
230
|
+
await textbox.click();
|
|
231
|
+
await this.page.keyboard.type(mnemonic, { delay: 5 });
|
|
232
|
+
const continueBtn = this.page.getByTestId("import-srp-confirm");
|
|
233
|
+
await continueBtn.click();
|
|
234
|
+
const passwordInputs = this.page.locator('input[type="password"]');
|
|
235
|
+
await passwordInputs.nth(0).fill(pwd);
|
|
236
|
+
await passwordInputs.nth(1).fill(pwd);
|
|
237
|
+
await this.page.getByRole("checkbox").click();
|
|
238
|
+
await this.page.getByRole("button", { name: "Create password" }).click();
|
|
239
|
+
const metametricsBtn = this.page.getByTestId("metametrics-i-agree");
|
|
240
|
+
await metametricsBtn.waitFor({ state: "visible", timeout: 3e4 });
|
|
241
|
+
await metametricsBtn.click();
|
|
242
|
+
const openWalletBtn = this.page.getByRole("button", {
|
|
243
|
+
name: /open wallet/i
|
|
244
|
+
});
|
|
245
|
+
await openWalletBtn.waitFor({ state: "visible", timeout: 3e4 });
|
|
246
|
+
await openWalletBtn.click();
|
|
247
|
+
await this.page.goto(
|
|
248
|
+
`chrome-extension://${this.extensionId}/sidepanel.html`
|
|
249
|
+
);
|
|
250
|
+
await this.page.getByTestId("account-options-menu-button").waitFor({ state: "visible", timeout: 3e4 });
|
|
251
|
+
}
|
|
252
|
+
async approve() {
|
|
253
|
+
await this.page.getByTestId("confirm-btn").or(this.page.getByTestId("confirm-footer-button")).or(this.page.getByTestId("page-container-footer-next")).or(this.page.getByRole("button", { name: /confirm/i })).click();
|
|
254
|
+
await this.page.getByTestId("account-options-menu-button").waitFor({ state: "visible", timeout: 3e4 });
|
|
255
|
+
}
|
|
256
|
+
async deny() {
|
|
257
|
+
const cancelBtn = this.page.getByTestId("cancel-btn").or(this.page.getByTestId("confirm-footer-cancel-button")).or(this.page.getByTestId("page-container-footer-cancel")).or(this.page.getByRole("button", { name: /cancel|reject/i }));
|
|
258
|
+
await cancelBtn.first().click();
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Lock the MetaMask wallet
|
|
262
|
+
*/
|
|
263
|
+
async lock() {
|
|
264
|
+
await this.page.getByTestId("account-options-menu-button").click();
|
|
265
|
+
await this.page.locator("text=Lock MetaMask").click();
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Unlock MetaMask with password
|
|
269
|
+
*/
|
|
270
|
+
async unlock(password) {
|
|
271
|
+
const pwd = password ?? this.defaultPassword;
|
|
272
|
+
const passwordInput = this.page.getByTestId("unlock-password");
|
|
273
|
+
await passwordInput.fill(pwd);
|
|
274
|
+
await this.page.getByTestId("unlock-submit").click();
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Switch to an existing network in MetaMask
|
|
278
|
+
* @param networkName - Name of the network to switch to (e.g., "Ethereum Mainnet", "Sepolia")
|
|
279
|
+
*/
|
|
280
|
+
async switchNetwork(networkName, networkType = "Popular") {
|
|
281
|
+
await this.page.getByTestId("sort-by-networks").click();
|
|
282
|
+
if (networkType === "Custom") {
|
|
283
|
+
await this.page.getByRole("tab", { name: "Custom" }).click();
|
|
284
|
+
}
|
|
285
|
+
await this.page.getByText(networkName).click();
|
|
286
|
+
await expect2(this.page.getByTestId("sort-by-networks")).toHaveText(
|
|
287
|
+
networkName
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
async switchAccount(accountName) {
|
|
291
|
+
await this.page.getByTestId("account-menu-icon").click();
|
|
292
|
+
await this.page.getByText(accountName, { exact: true }).click();
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Add a custom network to MetaMask
|
|
296
|
+
*/
|
|
297
|
+
async addNetwork(network) {
|
|
298
|
+
await this.page.goto(
|
|
299
|
+
`chrome-extension://${this.extensionId}/home.html#settings/networks/add-network`
|
|
300
|
+
);
|
|
301
|
+
await this.page.getByTestId("network-form-network-name").fill(network.name);
|
|
302
|
+
await this.page.getByTestId("network-form-rpc-url").fill(network.rpc);
|
|
303
|
+
await this.page.getByTestId("network-form-chain-id").fill(network.chainId.toString());
|
|
304
|
+
await this.page.getByTestId("network-form-ticker-input").fill(network.currencySymbol);
|
|
305
|
+
await this.page.getByRole("button", { name: /save/i }).click();
|
|
306
|
+
}
|
|
307
|
+
async addCustomNetwork(settings) {
|
|
308
|
+
await this.page.getByTestId("account-options-menu-button").click();
|
|
309
|
+
await this.page.getByTestId("global-menu-networks").click();
|
|
310
|
+
await this.page.getByRole("button", { name: "Add a custom network" }).click();
|
|
311
|
+
await this.page.getByTestId("network-form-network-name").fill(settings.name);
|
|
312
|
+
await this.page.getByTestId("network-form-chain-id").fill(settings.chainId.toString());
|
|
313
|
+
await this.page.getByTestId("network-form-ticker-input").fill(settings.currencySymbol);
|
|
314
|
+
await this.page.getByTestId("test-add-rpc-drop-down").click();
|
|
315
|
+
await this.page.getByRole("button", { name: "Add RPC URL" }).click();
|
|
316
|
+
await this.page.getByTestId("rpc-url-input-test").fill(settings.rpc);
|
|
317
|
+
await this.page.getByRole("button", { name: "Add URL" }).click();
|
|
318
|
+
await this.page.getByRole("button", { name: "Save" }).click();
|
|
319
|
+
}
|
|
320
|
+
async enableTestNetworks() {
|
|
321
|
+
await this.page.getByTestId("account-options-menu-button").click();
|
|
322
|
+
await this.page.getByTestId("global-menu-networks").click();
|
|
323
|
+
await this.page.locator("text=Show test networks >> xpath=following-sibling::label").click();
|
|
324
|
+
await this.page.keyboard.press("Escape");
|
|
325
|
+
}
|
|
326
|
+
async importAccount(privateKey) {
|
|
327
|
+
await this.page.getByTestId("account-menu-icon").click();
|
|
328
|
+
await this.page.getByTestId("account-list-add-wallet-button").click();
|
|
329
|
+
await this.page.getByTestId("add-wallet-modal-import-account").click();
|
|
330
|
+
await this.page.locator("#private-key-box").fill(privateKey);
|
|
331
|
+
await this.page.getByTestId("import-account-confirm-button").click();
|
|
332
|
+
await this.page.getByRole("button", { name: "Back" }).click();
|
|
333
|
+
}
|
|
334
|
+
async accountNameIs(accountName) {
|
|
335
|
+
await expect2(this.page.getByTestId("account-menu-icon")).toContainText(
|
|
336
|
+
accountName
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// src/wallets/polkadot-js/polkadot-js.ts
|
|
342
|
+
import { expect as expect3 } from "@playwright/test";
|
|
110
343
|
var PolkadotJS = class extends Wallet {
|
|
111
344
|
defaultPassword = "11111111";
|
|
112
345
|
async gotoOnboardPage() {
|
|
113
346
|
await this.page.goto(`chrome-extension://${this.extensionId}/index.html`);
|
|
114
|
-
await
|
|
347
|
+
await expect3(
|
|
115
348
|
this.page.getByText("Before we start, just a couple of notes")
|
|
116
349
|
).toBeVisible();
|
|
117
350
|
}
|
|
@@ -163,233 +396,29 @@ var PolkadotJS = class extends Wallet {
|
|
|
163
396
|
}
|
|
164
397
|
};
|
|
165
398
|
|
|
166
|
-
// src/
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
await this.page.getByTestId("create-password-new-input").fill(password);
|
|
183
|
-
await this.page.getByTestId("create-password-confirm-input").fill(password);
|
|
184
|
-
await this.page.getByTestId("create-password-terms").click();
|
|
185
|
-
await this.page.getByTestId("create-password-submit").click();
|
|
186
|
-
await this.page.getByTestId("metametrics-i-agree").click();
|
|
187
|
-
await this.page.getByTestId("onboarding-complete-done").click();
|
|
188
|
-
await this.clickTopRightCornerToCloseAllTheMarketingBullshit();
|
|
189
|
-
}
|
|
190
|
-
// async switchAccount(accountAddress: { address: string }): Promise<void>;
|
|
191
|
-
async switchAccount(accountNameOrAddress) {
|
|
192
|
-
await this.page.getByTestId("account-menu-icon").click();
|
|
193
|
-
await this.page.getByText(accountNameOrAddress.name, { exact: true }).click();
|
|
194
|
-
}
|
|
195
|
-
async importAccount(privateKey) {
|
|
196
|
-
await this.page.getByTestId("account-menu-icon").click();
|
|
197
|
-
await this.page.getByTestId("account-list-add-wallet-button").click();
|
|
198
|
-
await this.page.getByTestId("add-wallet-modal-import-account").click();
|
|
199
|
-
await this.page.locator("#private-key-box").fill(privateKey);
|
|
200
|
-
await this.page.getByTestId("import-account-confirm-button").click();
|
|
201
|
-
await this.page.getByRole("button", { name: "Back" }).click();
|
|
202
|
-
}
|
|
203
|
-
async addAccount(accountName) {
|
|
204
|
-
await this.page.getByTestId("account-menu-icon").click();
|
|
205
|
-
await this.page.getByTestId("multichain-account-menu-popover-action-button").click();
|
|
206
|
-
await this.page.getByTestId("multichain-account-menu-popover-add-account").click();
|
|
207
|
-
if (accountName) {
|
|
208
|
-
await this.page.locator("#account-name").fill(accountName);
|
|
209
|
-
}
|
|
210
|
-
await this.page.getByTestId("submit-add-account-with-name").click();
|
|
211
|
-
}
|
|
212
|
-
async getAccountName() {
|
|
213
|
-
const accountSelect = this.page.getByTestId("account-menu-icon");
|
|
214
|
-
await expect3(accountSelect).toBeVisible();
|
|
215
|
-
const text = await accountSelect.textContent();
|
|
216
|
-
if (!text) throw Error("Cannot get account name");
|
|
217
|
-
return text;
|
|
218
|
-
}
|
|
219
|
-
async connectToNetwork(networkName, networkType = "Popular") {
|
|
220
|
-
await this.page.getByTestId("sort-by-networks").click();
|
|
221
|
-
await this.page.getByRole("tab", { name: networkType, exact: true }).click();
|
|
222
|
-
const additionalNetwork = this.page.getByTestId("additional-network-item").getByText(networkName);
|
|
223
|
-
await this.page.getByText(networkName).click();
|
|
224
|
-
}
|
|
225
|
-
async addCustomNetwork(settings) {
|
|
226
|
-
await this.page.getByTestId("account-options-menu-button").click();
|
|
227
|
-
await this.page.getByTestId("global-menu-networks").click();
|
|
228
|
-
await this.page.getByRole("button", { name: "Add a custom network" }).click();
|
|
229
|
-
await this.page.getByTestId("network-form-network-name").fill(settings.name);
|
|
230
|
-
await this.page.getByTestId("network-form-chain-id").fill(settings.chainId.toString());
|
|
231
|
-
await this.page.getByTestId("network-form-ticker-input").fill(settings.currencySymbol);
|
|
232
|
-
await this.page.getByTestId("test-add-rpc-drop-down").click();
|
|
233
|
-
await this.page.getByRole("button", { name: "Add RPC URL" }).click();
|
|
234
|
-
await this.page.getByTestId("rpc-url-input-test").fill(settings.rpc);
|
|
235
|
-
await this.page.getByRole("button", { name: "Add URL" }).click();
|
|
236
|
-
await this.page.getByRole("button", { name: "Save" }).click();
|
|
237
|
-
}
|
|
238
|
-
async enableTestNetworks() {
|
|
239
|
-
await this.page.getByTestId("account-options-menu-button").click();
|
|
240
|
-
await this.page.getByTestId("global-menu-networks").click();
|
|
241
|
-
await this.page.locator("text=Show test networks >> xpath=following-sibling::label").click();
|
|
242
|
-
await this.page.keyboard.press("Escape");
|
|
243
|
-
}
|
|
244
|
-
async approve() {
|
|
245
|
-
const p = await this.page.context().newPage();
|
|
246
|
-
await p.goto(`chrome-extension://${this.extensionId}/notification.html`);
|
|
247
|
-
await p.locator(
|
|
248
|
-
'[data-testid="confirm-footer-button"], [data-testid="confirm-btn"], [data-testid="page-container-footer-next"], [data-testid="confirmation-submit-button"]'
|
|
249
|
-
).click();
|
|
250
|
-
await p.waitForSelector(".multichain-app-header", {
|
|
251
|
-
timeout: 1e4
|
|
252
|
-
});
|
|
253
|
-
await p.close();
|
|
254
|
-
}
|
|
255
|
-
async deny() {
|
|
256
|
-
return this.usingNotificationPage(
|
|
257
|
-
(p) => p.getByTestId("cancel-btn").click()
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
async usingNotificationPage(action) {
|
|
261
|
-
const p = await this.page.context().newPage();
|
|
262
|
-
await p.goto(`chrome-extension://${this.extensionId}/notification.html`);
|
|
263
|
-
await action(p);
|
|
264
|
-
await p.close();
|
|
265
|
-
}
|
|
266
|
-
async clickTopRightCornerToCloseAllTheMarketingBullshit() {
|
|
267
|
-
await this.page.waitForTimeout(500);
|
|
268
|
-
await this.page.keyboard.press("Escape");
|
|
269
|
-
await this.page.mouse.click(1e3, 10);
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
// src/withWallets.ts
|
|
274
|
-
var w3walletsDir = ".w3wallets";
|
|
275
|
-
function withWallets(test, ...config) {
|
|
276
|
-
const withBackpack = config.includes("backpack");
|
|
277
|
-
const withPolkadotJS = config.includes("polkadotJS");
|
|
278
|
-
const withMetamask = config.includes("metamask");
|
|
279
|
-
const backpackPath = path.join(process.cwd(), w3walletsDir, "backpack");
|
|
280
|
-
const polkadotJSPath = path.join(process.cwd(), w3walletsDir, "polkadotJS");
|
|
281
|
-
const metamaskPath = path.join(process.cwd(), w3walletsDir, "metamask");
|
|
282
|
-
return test.extend({
|
|
283
|
-
/**
|
|
284
|
-
* Sets up a persistent browser context with the requested extensions loaded.
|
|
285
|
-
*/
|
|
286
|
-
context: async ({}, use, testInfo) => {
|
|
287
|
-
const userDataDir = path.join(
|
|
288
|
-
process.cwd(),
|
|
289
|
-
".w3wallets",
|
|
290
|
-
".context",
|
|
291
|
-
testInfo.testId
|
|
292
|
-
);
|
|
293
|
-
cleanUserDataDir(userDataDir);
|
|
294
|
-
const extensionPaths = [];
|
|
295
|
-
if (withBackpack) {
|
|
296
|
-
ensureWalletExtensionExists(backpackPath, "backpack");
|
|
297
|
-
extensionPaths.push(backpackPath);
|
|
298
|
-
}
|
|
299
|
-
if (withPolkadotJS) {
|
|
300
|
-
ensureWalletExtensionExists(polkadotJSPath, "polkadotJS");
|
|
301
|
-
extensionPaths.push(polkadotJSPath);
|
|
302
|
-
}
|
|
303
|
-
if (withMetamask) {
|
|
304
|
-
ensureWalletExtensionExists(metamaskPath, "metamask");
|
|
305
|
-
extensionPaths.push(metamaskPath);
|
|
306
|
-
}
|
|
307
|
-
const context = await chromium.launchPersistentContext(userDataDir, {
|
|
308
|
-
headless: testInfo.project.use.headless ?? true,
|
|
309
|
-
channel: "chromium",
|
|
310
|
-
args: [
|
|
311
|
-
`--disable-extensions-except=${extensionPaths.join(",")}`,
|
|
312
|
-
`--load-extension=${extensionPaths.join(",")}`
|
|
313
|
-
]
|
|
314
|
-
});
|
|
315
|
-
while (context.serviceWorkers().length < extensionPaths.length) {
|
|
316
|
-
await sleep(1e3);
|
|
317
|
-
}
|
|
318
|
-
await use(context);
|
|
319
|
-
await context.close();
|
|
320
|
-
},
|
|
321
|
-
backpack: async ({ context }, use) => {
|
|
322
|
-
if (!withBackpack) {
|
|
323
|
-
throw Error(
|
|
324
|
-
"The Backpack wallet hasn't been loaded. Add it to the withWallets function."
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
const backpack = await initializeExtension(
|
|
328
|
-
context,
|
|
329
|
-
Backpack,
|
|
330
|
-
"Backpack is not initialized"
|
|
331
|
-
);
|
|
332
|
-
await use(backpack);
|
|
333
|
-
},
|
|
334
|
-
polkadotJS: async ({ context }, use) => {
|
|
335
|
-
if (!withPolkadotJS) {
|
|
336
|
-
throw Error(
|
|
337
|
-
"The Polkadot{.js} wallet hasn't been loaded. Add it to the withWallets function."
|
|
338
|
-
);
|
|
339
|
-
}
|
|
340
|
-
const polkadotJS = await initializeExtension(
|
|
341
|
-
context,
|
|
342
|
-
PolkadotJS,
|
|
343
|
-
"Polkadot{.js} is not initialized"
|
|
344
|
-
);
|
|
345
|
-
await use(polkadotJS);
|
|
346
|
-
},
|
|
347
|
-
metamask: async ({ context }, use) => {
|
|
348
|
-
if (!withMetamask) {
|
|
349
|
-
throw Error(
|
|
350
|
-
"The Metamask wallet hasn't been loaded. Add it to the withWallets function."
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
const metamask = await initializeExtension(
|
|
354
|
-
context,
|
|
355
|
-
Metamask,
|
|
356
|
-
"Metamask is not initialized"
|
|
357
|
-
);
|
|
358
|
-
await use(metamask);
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
function cleanUserDataDir(userDataDir) {
|
|
363
|
-
if (fs.existsSync(userDataDir)) {
|
|
364
|
-
fs.rmSync(userDataDir, { recursive: true });
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
function ensureWalletExtensionExists(walletPath, walletName) {
|
|
368
|
-
if (!fs.existsSync(path.join(walletPath, "manifest.json"))) {
|
|
369
|
-
throw new Error(
|
|
370
|
-
`Cannot find ${walletName}. Please download it via 'npx w3wallets ${walletName}'.`
|
|
371
|
-
);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
async function initializeExtension(context, ExtensionClass, notInitializedErrorMessage) {
|
|
375
|
-
const serviceWorkers = context.serviceWorkers();
|
|
376
|
-
let page = await context.newPage();
|
|
377
|
-
for (const worker of serviceWorkers) {
|
|
378
|
-
const extensionId = worker.url().split("/")[2];
|
|
379
|
-
if (!extensionId) {
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
const extension = new ExtensionClass(page, extensionId);
|
|
383
|
-
try {
|
|
384
|
-
await extension.gotoOnboardPage();
|
|
385
|
-
return extension;
|
|
386
|
-
} catch {
|
|
387
|
-
await page.close();
|
|
388
|
-
page = await context.newPage();
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
throw new Error(notInitializedErrorMessage);
|
|
392
|
-
}
|
|
399
|
+
// src/wallets/index.ts
|
|
400
|
+
var backpack = createWallet({
|
|
401
|
+
name: "backpack",
|
|
402
|
+
extensionDir: "backpack",
|
|
403
|
+
WalletClass: Backpack
|
|
404
|
+
});
|
|
405
|
+
var metamask = createWallet({
|
|
406
|
+
name: "metamask",
|
|
407
|
+
extensionDir: "metamask",
|
|
408
|
+
WalletClass: Metamask
|
|
409
|
+
});
|
|
410
|
+
var polkadotJS = createWallet({
|
|
411
|
+
name: "polkadotJS",
|
|
412
|
+
extensionDir: "polkadotjs",
|
|
413
|
+
WalletClass: PolkadotJS
|
|
414
|
+
});
|
|
393
415
|
export {
|
|
416
|
+
Backpack,
|
|
417
|
+
Metamask,
|
|
418
|
+
PolkadotJS,
|
|
419
|
+
backpack,
|
|
420
|
+
createWallet,
|
|
421
|
+
metamask,
|
|
422
|
+
polkadotJS,
|
|
394
423
|
withWallets
|
|
395
424
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w3wallets",
|
|
3
3
|
"description": "browser wallets for playwright",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0-beta.1",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"homepage": "https://github.com/Maksandre/w3wallets",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"bin": "./src/scripts/download.js",
|
|
34
34
|
"scripts": {
|
|
35
|
-
"start:
|
|
35
|
+
"start:dapp": "yarn workspace @w3wallets/test-app start:all",
|
|
36
36
|
"download-wallets": "npx w3wallets backpack polkadotJS metamask",
|
|
37
37
|
"test": "npx playwright test --project=local --workers=2",
|
|
38
38
|
"test:ci": "npx playwright test --project=ci",
|