@bozhkovatanas/wallet-mock
Version:
Mock Web3 Browser wallets, like Metamask, in Playwright tests.
63 lines • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.installMockWallet = void 0;
const createWallet_1 = require("./createWallet");
const crypto_1 = require("crypto");
let wallets = new Map();
async function installMockWallet(params) {
const { account, transports } = params;
const browserOrPage = "browserContext" in params ? params.browserContext : params.page;
// Connecting the browser context to the Node.js playwright context
await browserOrPage.exposeFunction("eip1193Request", eip1193Request);
// Everytime we call installMockWallet, we create a new uuid to identify the wallet.
const uuid = (0, crypto_1.randomUUID)();
wallets.set(uuid, (0, createWallet_1.createWallet)(account, transports));
await browserOrPage.addInitScript(({ uuid }) => {
// This function needs to be declared in the browser context
function announceMockWallet() {
const provider = {
request: async (request) => {
return await eip1193Request({
...request,
uuid,
});
},
on: () => { },
removeListener: () => { },
};
const info = {
uuid,
name: "Mock Wallet",
icon: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>",
rdns: "com.example.mock-wallet",
};
const detail = { info, provider };
const announceEvent = new CustomEvent("eip6963:announceProvider", {
detail: Object.freeze(detail),
});
window.dispatchEvent(announceEvent);
}
// Wait for the DOM to be ready before announcing the wallet - useful for auto-connect flows
window.addEventListener("DOMContentLoaded", () => {
announceMockWallet();
});
announceMockWallet();
window.addEventListener("eip6963:requestProvider", () => {
announceMockWallet();
});
}, { uuid });
}
exports.installMockWallet = installMockWallet;
async function eip1193Request({ method, params, uuid, }) {
const wallet = wallets.get(uuid);
if (wallet == null)
throw new Error("Account or transport not found");
// console.log("eip1193Request", method, params);
const result = await wallet.request({
method,
params,
});
// console.log("eip1193Result", result);
return result;
}
//# sourceMappingURL=installMockWallet.js.map