Skip to content

Commit

Permalink
Merge pull request #20 from desci-labs/develop
Browse files Browse the repository at this point in the history
Push last two patches to `main`
  • Loading branch information
hubsmoke authored May 23, 2024
2 parents 267af21 + 841b096 commit 059f180
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 112 deletions.
3 changes: 2 additions & 1 deletion src/Base64Binary.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Expand Down Expand Up @@ -69,7 +70,7 @@ const Base64Binary = {
if (arrayBuffer) uarray = new Uint8Array(arrayBuffer);
else uarray = new Uint8Array(bytes);

input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
input = input.replace(/[^A-Za-z0-9+/=]/g, "");

for (i = 0; i < bytes; i += 3) {
//get the 3 octects in 4 ascii chars
Expand Down
3 changes: 2 additions & 1 deletion src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export enum LogEventType {
export interface LogRequest {
dpid: number;
version: number;
extra: any;
extra: unknown;
eventType: LogEventType;
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
let supabase: SupabaseClient<any, "public", any> | undefined;
if (process.env.SUPABASE_URL && process.env.SUPABASE_KEY) {
supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY);
Expand Down
8 changes: 5 additions & 3 deletions src/api/v1/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const transformGraphResult =
id: r.id,
recentCid: safeHexToCid(r.researchObject.versions[r.researchObject.versions.length - 1].cid),
researchObject: {
owner: r.researchObject.owner,
id: r.researchObject.id,
versions: r.researchObject.versions.map((v, index) => ({
...v,
Expand Down Expand Up @@ -66,8 +67,9 @@ export const list = async (req: Request, res: Response) => {
).researchObjectVersions;

res.json(graphResult.map(transformGraphResult(transactionHashToDpid)));
} catch (err: any) {
res.json({ ok: false, error: err.message, path: "/api/v1/dpid" }).status(500);
logger.error("ERROR", err.message);
} catch (err) {
const error = err as Error;
res.json({ ok: false, error: error.message, path: "/api/v1/dpid" }).status(500);
logger.error("ERROR", error.message);
}
};
38 changes: 14 additions & 24 deletions src/dpid-reader/DpidReader.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-explicit-any */
const RPC_URL = "https://eth-sepolia.g.alchemy.com/v2/Dg4eT90opKOFZ7w-YCxVwX9O-sriKn0N";
//'https://goerli.infura.io/v3/';
import { AlchemyEth, createAlchemyWeb3 } from "@alch/alchemy-web3";
import goerliContract from "../deployments/goerli/config.json";
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
import sepoliaDevContract from "../deployments/sepoliaDev/config.json";
import sepoliaProdContract from "../deployments/sepoliaProd/config.json";
import { encode, decode } from "url-safe-base64";
import { encode } from "url-safe-base64";
import { getIndexedResearchObjects } from "./TheGraphResolver";
import { base16 } from "multiformats/bases/base16";
import { CID } from "multiformats/cid";
import fs from "fs";
import https from "https";
import axios from "axios";
import {
DataBucketComponent,
ResearchObject,
ResearchObjectComponentType,
ResearchObjectV1,
ResearchObjectV1Component,
RoCrateTransformer,
} from "@desci-labs/desci-models";
import parentLogger from "../logger";

const logger = parentLogger.child({ module: "DpidReader" });
export interface DpidRequest {
dpid: string;
Expand Down Expand Up @@ -50,7 +47,7 @@ export const convertHexTo64PID = (hex: string, hexToBytes: any) => {
const bytes: number[] = hexToBytes(hex);

const base64encoded = Buffer.from(bytes).toString("base64");
const base64SafePID = encode(base64encoded).replace(/[\.=]$/, "");
const base64SafePID = encode(base64encoded).replace(/[.=]$/, "");
return base64SafePID;
};

Expand Down Expand Up @@ -81,11 +78,6 @@ logger.info({
DEFAULT_IPFS_GATEWAY,
});

interface ContractConfig {
address: string;
abi: any;
}

interface DpidResult {
id16: string;
id64: string;
Expand Down Expand Up @@ -120,11 +112,8 @@ export const hexToCid = (hexCid: string) => {
export interface DataResponse {
data: object;
}
// TODO: remove sealstorage specific cert, no longer needed
const caBundle = fs.readFileSync("ssl/sealstorage-bundle.crt");
const agent = new https.Agent({ ca: caBundle, rejectUnauthorized: false });
export class DpidReader {
static read = async ({ dpid, version, suffix, prefix }: DpidRequest): Promise<DpidResult> => {
static read = async ({ dpid }: DpidRequest): Promise<DpidResult> => {
const web3 = createAlchemyWeb3(RPC_URL);

const dpidRegistryContract =
Expand Down Expand Up @@ -172,7 +161,6 @@ export class DpidReader {
private static transformRaw = async (result: DpidResult, request: DpidRequest): Promise<string | DataResponse> => {
const { prefix, suffix, version } = request;
const hex = result.id16;
const output = { msg: `beta.dpid.org resolver`, params: request, hex };

const graphUrl = THE_GRAPH_RESOLVER_URL[prefix];
const graphResult: GraphResult = (await getIndexedResearchObjects(graphUrl, [hex])).researchObjects[0];
Expand All @@ -182,7 +170,7 @@ export class DpidReader {
// TODO: support version=v1 syntax in Nodes and we can get rid of cleanVersion logic and can pass the version identifier straight through
let cleanVersion: number | undefined = undefined;
if (version) {
cleanVersion = version?.substring(0, 1) == "v" ? parseInt(version!.substring(1)) - 1 : parseInt(version);
cleanVersion = version?.substring(0, 1) == "v" ? parseInt(version.substring(1)) - 1 : parseInt(version);
}

logger.debug({ cleanVersion, version }, "CLEAN VER");
Expand All @@ -193,7 +181,7 @@ export class DpidReader {
logger.debug({ cleanVersion }, "set clean ver");
}

const targetVersion = graphResult.versions[graphResult.versions.length - 1 - cleanVersion!];
const targetVersion = graphResult.versions[graphResult.versions.length - 1 - cleanVersion];

logger.info({ targetVersion }, "got target");

Expand All @@ -217,8 +205,9 @@ export class DpidReader {
) {
const res = await fetch(manifestLocation);
const researchObject: ResearchObjectV1 = await res.json();
const dataBucketCandidate: ResearchObjectV1Component = researchObject.components[0];
if (dataBucketCandidate && dataBucketCandidate.type === ResearchObjectComponentType.DATA_BUCKET) {
const dataBucketCandidate: ResearchObjectV1Component =
researchObject.components.find((a) => a.name == "root") || researchObject.components[0];
if (dataBucketCandidate) {
const dataBucket: DataBucketComponent = dataBucketCandidate as DataBucketComponent;
let dataSuffix;
if (versionAsData) {
Expand Down Expand Up @@ -249,11 +238,12 @@ export class DpidReader {
};
const defaultGateway = DEFAULT_IPFS_GATEWAY;
const selectedGateway =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(dataSuffix!.length > 0 ? CID_MAP[dataBucket.payload.cid] : defaultGateway) || defaultGateway;
const dagTestURl = `${selectedGateway.replace(/\/ipfs$/, "")}/api/v0/dag/get?arg=${arg}`;
console.log(dagTestURl, "dagTestURl");
try {
const { data } = await axios({ method: "POST", url: dagTestURl }); // , httpsAgent: agent });
const { data } = await axios({ method: "POST", url: dagTestURl });
console.log("posted");
if (!data.Data || data.Data["/"].bytes !== "CAE") {
return `${selectedGateway}/${arg}`;
Expand Down
1 change: 1 addition & 0 deletions src/dpid-reader/TheGraphResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const getAllResearchObjectsForDpidRegistrations = async (url: string, dpi
cid
researchObject {
id
owner
versions(orderBy: time) {
id
time
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ app.get("/*", async (req: Request, res: Response) => {

analytics.log({
dpid: parseInt(dpid),
version: parseInt(version!),
version: parseInt(version ?? "0"),
extra: dpidRequest,
eventType: LogEventType.DPID_GET,
});
Expand All @@ -84,9 +84,10 @@ app.get("/*", async (req: Request, res: Response) => {
)
);
} catch (err) {
const error = err as Error;
res.status(400).send({
error: (err as any).message,
detail: err,
error: error.message,
detail: error,
params: req.params,
query: req.query,
path: "/*",
Expand Down
3 changes: 2 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ const logger = pino({
});
export default logger;

/* eslint-disable @typescript-eslint/no-explicit-any */
function omitBuffer(array: any) {
return array.map((obj: any) => {
const { buffer, ...rest } = obj;
const { ...rest } = obj;
return rest;
});
}
Expand Down
79 changes: 0 additions & 79 deletions ssl/sealstorage-bundle.crt

This file was deleted.

0 comments on commit 059f180

Please sign in to comment.