kernel-sandbox/src/index.ts

77 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-06-30 20:12:43 +00:00
import {
b64ToBuf,
bufToHex,
deriveChildSeed,
dictionary,
seedPhraseToSeed,
taggedRegistryEntryKeys,
} from "libskynet";
import * as path from "path";
import { overwriteRegistryEntry } from "libskynetnode";
import * as kernel from "libkernel";
import { webcrypto } from "crypto";
2023-03-31 19:42:33 +00:00
import * as ed from "@noble/ed25519";
2022-06-30 20:12:43 +00:00
// @ts-ignore
2022-07-20 18:01:54 +00:00
import StaticServer from "static-server";
2022-06-30 20:12:43 +00:00
import { Page } from "puppeteer";
2022-07-20 18:01:54 +00:00
import { errTuple } from "libskynet";
import * as url from "url";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
2022-06-30 20:12:43 +00:00
export function generateSeedPhrase() {
2023-03-31 19:42:33 +00:00
return ed.utils.randomPrivateKey();
2022-06-30 20:12:43 +00:00
}
export async function login(page: Page, seed = generateSeedPhrase()) {
2023-03-31 19:42:33 +00:00
await page.goto("http://kernel.lumeweb.com");
2022-06-30 20:12:43 +00:00
2023-03-31 19:42:33 +00:00
let userSeed = seed;
2022-06-30 20:12:43 +00:00
let seedHex = bufToHex(userSeed);
await page.evaluate((seed: string) => {
2023-03-31 19:42:33 +00:00
window.localStorage.setItem("v1-key", seed);
2022-06-30 20:12:43 +00:00
}, seedHex);
}
export async function loadTester(page: Page, port = 8080) {
const server = new StaticServer({
rootPath: path.resolve(__dirname, "..", "public"),
port,
host: "localhost",
});
await new Promise((resolve) => {
server.start(resolve);
});
const stop = () => server.stop();
process.on("SIGTERM", stop);
page.browser().on("disconnected", stop);
2022-06-30 20:12:43 +00:00
await page.goto(`http://localhost:${port}/`);
await page.evaluate(() => {
return kernel.init();
});
}
class Tester {
private page: Page;
constructor(page: Page) {
this.page = page;
}
async callModule(id: string, method: string, data = {}): Promise<errTuple> {
return this.page.evaluate(
async (id, method, data) => {
return kernel.callModule(id, method, data);
},
id,
method,
data
);
}
}
export const tester = (page: Page) => new Tester(page);