kernel/src/coreModules.ts

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-09-01 23:04:07 +00:00
import { internalModuleCall, modules } from "./queries.js";
import { SignedRegistryEntry } from "@lumeweb/libs5";
import {
decodeRegistryValue,
encodeCid,
decodeRegistryCid,
} from "@lumeweb/libweb";
const CORE_MODULES = {
2023-09-03 05:01:11 +00:00
swarm: "zdiLmwHCC15afFNLYzzT2DVV7m27SrBde7oXHdSzAe95GpFZXzdpatUN6b",
2023-09-02 13:42:09 +00:00
peerDiscoveryRegistry:
2023-09-03 05:01:11 +00:00
"zdiLW9MtAAMssP5vLBgd1FitouiVXzNUYZszFYG44uVKqCPDqUQox9aq1y",
2023-09-02 13:42:09 +00:00
ircPeerDiscovery:
2023-09-03 05:01:11 +00:00
"zdiLZaKjWwXkMU88GNEWf6d5NREHe1Yk4M7eQm1owSC4ezeqFGGuGpfYXR",
s5: "zdiT6quMF8gh8BhQdXE7CZYhp8S1BxSgsucSS48WuTGdars1noejvak6Qo",
};
export async function networkReady() {
for (const module of [
CORE_MODULES.peerDiscoveryRegistry,
CORE_MODULES.ircPeerDiscovery,
CORE_MODULES.swarm,
]) {
if (!moduleLoaded(module)) {
return false;
}
}
const resolvers = await internalModuleCall(CORE_MODULES.swarm, "getRelays");
return resolvers.length > 0;
}
function moduleLoaded(module: string) {
return module in modules;
}
export async function resolveModuleRegistryEntry(module: string) {
const [cid] = decodeRegistryCid(module);
const pubkey = cid.hash;
const signedEntry = (await internalModuleCall(
CORE_MODULES.s5,
"getRegistryEntry",
{ pubkey },
)) as SignedRegistryEntry;
let [decodedRegistry, err] = decodeRegistryValue(signedEntry.data);
if (err) {
throw new Error(err);
}
let rawEncCid;
[rawEncCid, err] = encodeCid(decodedRegistry);
if (err) {
throw new Error(err);
}
return rawEncCid;
}