-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add procedure for creating weaveVM blob storage references (…
…#683) * feat(blob-storage-manager): add weavevm blob storage * style: update weavevm name + rename api endpoint env var * refactor(api): move auth logic from context to a separate file * feat(api): add weavevm references update endpoint * chrore: add changesets * feat(docs): add weaveVM api key to env vars section * fix(api): modify weaveVM reference endpoint to create references instead of upserting
- Loading branch information
Showing
22 changed files
with
304 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@blobscan/api": minor | ||
--- | ||
|
||
Added a protected procedure to upsert weaveVM blob storage references |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
|
||
import { t } from "../trpc-client"; | ||
import type { APIClientType } from "../utils"; | ||
|
||
export const withAuthed = (expectedApiClientType: APIClientType) => | ||
t.middleware(({ ctx, next }) => { | ||
if (ctx.apiClient?.type !== expectedApiClientType) { | ||
throw new TRPCError({ code: "UNAUTHORIZED" }); | ||
} | ||
|
||
return next({ | ||
ctx, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { withAuthed } from "../middlewares/withAuthed"; | ||
import type { APIClientType } from "../utils"; | ||
import { publicProcedure } from "./public"; | ||
|
||
export const createAuthedProcedure = (apiClientType: APIClientType) => | ||
publicProcedure.use(withAuthed(apiClientType)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./jwt-authed"; | ||
export * from "./authed"; | ||
export * from "./public"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
|
||
import { BlobStorage } from "@blobscan/db/prisma/enums"; | ||
import { z } from "@blobscan/zod"; | ||
|
||
import { createAuthedProcedure } from "../../procedures"; | ||
|
||
const inputSchema = z.object({ | ||
blobHashes: z.array(z.string()), | ||
}); | ||
|
||
export const createWeaveVMReferences = createAuthedProcedure("weavevm") | ||
.meta({ | ||
openapi: { | ||
method: "POST", | ||
path: "/blobs/weavevm-references", | ||
tags: ["blobs"], | ||
summary: "Creates WeaveVM references for a given set of blobs.", | ||
}, | ||
}) | ||
.input(inputSchema) | ||
.output(z.void()) | ||
.mutation(async ({ ctx: { prisma }, input: { blobHashes } }) => { | ||
if (!blobHashes.length) { | ||
return; | ||
} | ||
|
||
const dbVersionedHashes = await prisma.blob | ||
.findMany({ | ||
select: { | ||
versionedHash: true, | ||
}, | ||
where: { | ||
versionedHash: { | ||
in: blobHashes, | ||
}, | ||
}, | ||
}) | ||
.then((blobs) => blobs.map((b) => b.versionedHash)); | ||
|
||
const missingHashes = blobHashes | ||
.filter((hash) => !dbVersionedHashes.includes(hash)) | ||
.map((hash) => `"${hash}"`); | ||
|
||
if (missingHashes.length > 0) { | ||
throw new TRPCError({ | ||
code: "BAD_REQUEST", | ||
message: `Couldn't found the following blobs: ${missingHashes.join( | ||
", " | ||
)}`, | ||
}); | ||
} | ||
|
||
await prisma.blobDataStorageReference.createMany({ | ||
data: blobHashes.map((hash) => ({ | ||
blobHash: hash, | ||
dataReference: hash, | ||
blobStorage: BlobStorage.WEAVEVM, | ||
})), | ||
skipDuplicates: true, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import type { CreateNextContextOptions } from "@trpc/server/adapters/next"; | ||
import type { NodeHTTPRequest } from "@trpc/server/adapters/node-http"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
import { env } from "@blobscan/env"; | ||
|
||
type NextHTTPRequest = CreateNextContextOptions["req"]; | ||
|
||
type HTTPRequest = NodeHTTPRequest | NextHTTPRequest; | ||
|
||
export type APIClientType = "indexer" | "weavevm"; | ||
|
||
export type APIClient = { | ||
type: APIClientType; | ||
}; | ||
|
||
function verifyIndexerClient(token: string) { | ||
const decoded = jwt.verify(token, env.SECRET_KEY) as string; | ||
|
||
return decoded; | ||
} | ||
|
||
function verifyWeaveVMClient(token: string) { | ||
return token === env.WEAVEVM_API_KEY; | ||
} | ||
|
||
export function retrieveAPIClient(req: HTTPRequest): APIClient | undefined { | ||
const authHeader = req.headers.authorization; | ||
|
||
if (!authHeader) { | ||
return; | ||
} | ||
|
||
const [type, token] = authHeader.split(" "); | ||
|
||
if (type !== "Bearer" || !token) { | ||
return; | ||
} | ||
|
||
try { | ||
if (verifyWeaveVMClient(token)) { | ||
return { type: "weavevm" }; | ||
} | ||
|
||
if (verifyIndexerClient(token)) { | ||
return { type: "indexer" }; | ||
} | ||
} catch (err) { | ||
if (err instanceof jwt.JsonWebTokenError) { | ||
return; | ||
} | ||
|
||
throw new TRPCError({ code: "BAD_REQUEST" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./auth"; | ||
export * from "./blob"; | ||
export * from "./identifiers"; | ||
export * from "./schemas"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.