refactor: change module cid detection so that we support resolver cids instead of the raw pubkey

This commit is contained in:
Derrick Hammer 2023-09-02 08:06:21 -04:00
parent 075dab28c7
commit 985d1c2e8a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 23 additions and 10 deletions

View File

@ -11,11 +11,13 @@ import {
sha512, sha512,
} from "@lumeweb/libkernel"; } from "@lumeweb/libkernel";
import { import {
CID,
decodeCid,
deriveChildKey, deriveChildKey,
downloadSmallObject, downloadSmallObject,
hexToBytes,
verifyCid, verifyCid,
} from "@lumeweb/libweb"; } from "@lumeweb/libweb";
import { CID_TYPES, CID_HASH_TYPES } from "@lumeweb/libs5";
import type { moduleQuery, presentKeyData } from "@lumeweb/libkernel/module"; import type { moduleQuery, presentKeyData } from "@lumeweb/libkernel/module";
import { readableStreamToUint8Array } from "binconv"; import { readableStreamToUint8Array } from "binconv";
import { getSavedRegistryEntry } from "./registry.js"; import { getSavedRegistryEntry } from "./registry.js";
@ -322,21 +324,32 @@ async function handleModuleCall(
return; return;
} }
let isRegistryEntry = false; let validCid = false;
try { let isResolver = false;
isRegistryEntry = hexToBytes(event.data.data.module)?.length === 32;
} catch {}
if ( if (
typeof event.data.data.module !== "string" || typeof event.data.data.module === "string" &&
(!verifyCid(event.data.data.module) && !isRegistryEntry) verifyCid(event.data.data.module)
) { ) {
const decodedCid = decodeCid(event.data.data.module);
if (!decodedCid[1]) {
const { type, hashType } = decodedCid[0];
if (type === CID_TYPES.RAW && hashType === CID_HASH_TYPES.BLAKE3) {
validCid = true;
}
if (type === CID_TYPES.RESOLVER && hashType === CID_HASH_TYPES.ED25519) {
validCid = true;
isResolver = true;
}
}
}
if (!validCid) {
logErr("moduleCall", "received moduleCall with malformed module"); logErr("moduleCall", "received moduleCall with malformed module");
respondErr( respondErr(
event, event,
messagePortal, messagePortal,
isWorker, isWorker,
"'module' field in moduleCall is expected to be a base58 encoded blake3 hash + filesize or a registry entry pubkey", "'module' field in moduleCall is expected to be a raw CID or a resolver CID",
); );
return; return;
} }
@ -394,7 +407,7 @@ async function handleModuleCall(
let moduleDomain = event.data.data.module; // Can change with overrides. let moduleDomain = event.data.data.module; // Can change with overrides.
let finalModule = moduleDomain; // Can change with overrides. let finalModule = moduleDomain; // Can change with overrides.
if (isRegistryEntry) { if (isResolver) {
const registryFail = () => { const registryFail = () => {
logErr("moduleCall", "received moduleCall with no known registry entry"); logErr("moduleCall", "received moduleCall with no known registry entry");
respondErr( respondErr(