2023-07-06 02:33:44 +00:00
|
|
|
import {
|
|
|
|
ActiveQuery,
|
|
|
|
addHandler,
|
|
|
|
handleMessage,
|
|
|
|
} from "@lumeweb/libkernel/module";
|
2023-03-27 12:02:02 +00:00
|
|
|
import { createClient, RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
2023-03-28 11:03:15 +00:00
|
|
|
import Client from "./client/client.js";
|
|
|
|
import { Prover } from "./client/index.js";
|
2023-03-27 12:02:02 +00:00
|
|
|
|
|
|
|
onmessage = handleMessage;
|
|
|
|
|
|
|
|
let moduleReadyResolve: Function;
|
|
|
|
let moduleReady: Promise<void> = new Promise((resolve) => {
|
|
|
|
moduleReadyResolve = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
let client: Client;
|
|
|
|
let rpc: RpcNetwork;
|
|
|
|
|
2023-07-06 02:33:44 +00:00
|
|
|
addHandler("presentKey", handlePresentKey);
|
2023-03-27 12:17:36 +00:00
|
|
|
addHandler("ready", handleReady);
|
2023-03-27 12:02:02 +00:00
|
|
|
|
|
|
|
[
|
2023-03-27 19:22:32 +00:00
|
|
|
"eth_accounts",
|
|
|
|
"eth_requestAccounts",
|
2023-03-27 12:02:02 +00:00
|
|
|
"eth_getBalance",
|
|
|
|
"eth_chainId",
|
|
|
|
"eth_blockNumber",
|
|
|
|
"eth_getTransactionByHash",
|
|
|
|
"eth_getTransactionCount",
|
|
|
|
"eth_getBlockTransactionCountByHash",
|
|
|
|
"eth_getBlockTransactionCountByNumber",
|
|
|
|
"eth_getCode",
|
|
|
|
"eth_call",
|
|
|
|
"eth_estimateGas",
|
|
|
|
"eth_gasPrice",
|
|
|
|
"eth_maxPriorityFeePerGas",
|
|
|
|
"eth_sendRawTransaction",
|
|
|
|
"eth_getTransactionReceipt",
|
|
|
|
"eth_getLogs",
|
|
|
|
"net_version",
|
|
|
|
].forEach((rpcMethod) => {
|
2023-03-27 19:22:51 +00:00
|
|
|
addHandler(rpcMethod, async (aq: ActiveQuery) => {
|
|
|
|
aq.callerInput = {
|
|
|
|
params: aq.callerInput || {},
|
|
|
|
method: rpcMethod,
|
|
|
|
};
|
2023-03-28 11:03:15 +00:00
|
|
|
try {
|
|
|
|
const ret = await handleRpcMethod(aq);
|
|
|
|
aq.respond(ret);
|
|
|
|
} catch (e: any) {
|
|
|
|
aq.reject((e as Error).message);
|
|
|
|
}
|
2023-03-27 12:02:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-06 02:33:44 +00:00
|
|
|
async function handlePresentKey() {
|
2023-03-27 12:02:02 +00:00
|
|
|
await setup();
|
|
|
|
moduleReadyResolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleRpcMethod(aq: ActiveQuery) {
|
|
|
|
await moduleReady;
|
2023-03-28 11:03:15 +00:00
|
|
|
return client.provider.rpcMethod(
|
|
|
|
aq.callerInput?.method,
|
|
|
|
// @ts-ignore
|
2023-07-06 02:33:44 +00:00
|
|
|
aq.callerInput?.params as any[],
|
2023-03-28 11:03:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-29 04:03:48 +00:00
|
|
|
async function consensusHandler(method: string, data: any) {
|
2023-03-29 21:07:25 +00:00
|
|
|
// @ts-ignore
|
|
|
|
await (
|
|
|
|
await rpc.ready
|
|
|
|
)();
|
|
|
|
|
2023-03-28 11:03:15 +00:00
|
|
|
while (true) {
|
2023-03-29 04:03:48 +00:00
|
|
|
let query = await rpc.simpleQuery({
|
2023-03-28 11:03:15 +00:00
|
|
|
query: {
|
|
|
|
module: "eth",
|
2023-03-29 04:03:48 +00:00
|
|
|
method,
|
|
|
|
data,
|
2023-03-28 11:03:15 +00:00
|
|
|
},
|
|
|
|
options: {
|
|
|
|
relayTimeout: 10,
|
|
|
|
queryTimeout: 10,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ret = await query.result;
|
2023-03-29 04:03:48 +00:00
|
|
|
|
2023-03-28 11:03:15 +00:00
|
|
|
if (ret.data) {
|
|
|
|
return ret.data;
|
2023-03-27 12:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:03:15 +00:00
|
|
|
async function executionHandler(data: Map<string, string | any>) {
|
2023-03-29 21:07:25 +00:00
|
|
|
// @ts-ignore
|
|
|
|
await (
|
|
|
|
await rpc.ready
|
|
|
|
)();
|
2023-03-29 04:03:48 +00:00
|
|
|
while (true) {
|
|
|
|
let query = await rpc.simpleQuery({
|
|
|
|
query: {
|
|
|
|
module: "eth",
|
|
|
|
method: "execution_request",
|
|
|
|
data,
|
|
|
|
},
|
|
|
|
});
|
2023-03-28 11:03:15 +00:00
|
|
|
|
2023-03-29 04:03:48 +00:00
|
|
|
let ret = await query.result;
|
2023-03-28 11:03:15 +00:00
|
|
|
|
2023-03-29 04:03:48 +00:00
|
|
|
if (ret.data) {
|
|
|
|
return ret.data;
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 11:03:15 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:02:02 +00:00
|
|
|
async function setup() {
|
|
|
|
rpc = createClient();
|
|
|
|
// @ts-ignore
|
|
|
|
await (
|
|
|
|
await rpc.ready
|
|
|
|
)();
|
|
|
|
|
2023-03-28 11:03:15 +00:00
|
|
|
const prover = new Prover(consensusHandler);
|
|
|
|
client = new Client(prover, executionHandler);
|
2023-03-27 12:02:02 +00:00
|
|
|
await client.sync();
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:23:23 +00:00
|
|
|
async function handleReady(aq: ActiveQuery) {
|
|
|
|
await moduleReady;
|
2023-03-27 12:17:36 +00:00
|
|
|
|
2023-03-27 19:23:23 +00:00
|
|
|
aq.respond();
|
2023-03-27 12:17:36 +00:00
|
|
|
}
|