Skip to content

add: lighthouse, akave sdk #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@ethereum-attestation-service/eas-sdk": "2.6.0",
"@ethereumjs/rlp": "^5.0.2",
"@hookform/resolvers": "^3.9.0",
"@lighthouse-web3/kavach": "^0.1.9",
"@lighthouse-web3/sdk": "^0.3.7",
"@noble/secp256k1": "^2.1.0",
"@openzeppelin/merkle-tree": "^1.0.7",
"@radix-ui/colors": "^3.0.0",
Expand Down Expand Up @@ -67,10 +69,12 @@
"ethers": "^6.13.2",
"fast-querystring": "^1.1.2",
"fflate": "^0.8.2",
"got": "^14.4.5",
"graphology": "^0.25.4",
"graphql-request": "^7.1.0",
"jotai": "^2.10.0",
"jsdom": "^25.0.1",
"ky": "^1.7.2",
"lodash": "^4.17.21",
"lucide-react": "^0.446.0",
"micro-eth-signer": "^0.12.0",
Expand All @@ -81,8 +85,10 @@
"react-dom": "^18.3.1",
"react-dropzone": "^14.2.9",
"react-hook-form": "^7.53.0",
"readable-stream": "^4.5.2",
"sha256": "link:@noble/hashes/sha256",
"sigma": "3.0.0-beta.31",
"stream-browserify": "^3.0.0",
"tailwind-merge": "^2.5.2",
"tailwindcss": "^3.4.13",
"tailwindcss-animate": "^1.0.7",
Expand Down
8 changes: 6 additions & 2 deletions apps/storybook/src/components/ui/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from "@/components/ui/toast";
import { useToast } from "@/hooks/use-toast";

export function Toaster() {
export function Toaster({
className,
}: {
className?: string;
}) {
const { toasts } = useToast();

return (
Expand All @@ -27,7 +31,7 @@ export function Toaster() {
</Toast>
);
})}
<ToastViewport />
<ToastViewport className={className} />
</ToastProvider>
);
}
28 changes: 27 additions & 1 deletion apps/storybook/src/lib/blockscout/chainInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const BLOCKSCOUT_CHAINS_BY_ID = {
export const BLOCKSCOUT_CHAINS_BY_ID_IMPORTED = {
"1": {
name: "Ethereum",
description:
Expand Down Expand Up @@ -8943,3 +8943,29 @@ export const BLOCKSCOUT_CHAINS_BY_ID = {
],
},
} as Record<string, any>;

// Add Akave Fuji as not in chain Info yet

const BLOCKSCOUT_CHAINS_BY_ID_EXTENSION = {
"78963": {
name: "Akave Fuji",
description: "Akave Fuji",
logo: "",
ecosystem: "Ethereum",
isTestnet: false,
layer: 1,
rollupType: null,
website: "https://www.akave.ai/",
explorers: [
{
url: "http://explorer.akave.ai/",
hostedBy: "self",
},
],
},
};

export const BLOCKSCOUT_CHAINS_BY_ID = {
...BLOCKSCOUT_CHAINS_BY_ID_IMPORTED,
...BLOCKSCOUT_CHAINS_BY_ID_EXTENSION,
};
100 changes: 100 additions & 0 deletions apps/storybook/src/lib/filecoin/akave/client.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { faker } from "@faker-js/faker";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import {
AkaveBucket,
createBucket,
listBucketFiles,
listBuckets,
uploadFileObject,
uploadFileWithFormData,
} from "./client";

import fs from "fs";
import os from "os";
import path from "path";

describe(
"with file",
() => {
const AKAVE_ENDPOINT_URL = import.meta.env.VITE_AKAVE_ENDPOINT_URL;
const config = {
akaveEndpointUrl: AKAVE_ENDPOINT_URL,
};
const testBucketNameCreate = "test-bucket-create";
const testBucketNameExists = "test-bucket";

const tmpDir = os.tmpdir();
const fileName = "test.txt";
const filePath = path.join(tmpDir, fileName);

const testContent = faker.lorem.paragraphs(10);

beforeAll(() => {
fs.writeFileSync(filePath, testContent, {
encoding: "utf8",
});
});

test.skip("create bucket", async () => {
const results = await createBucket({
...config,
bucketName: testBucketNameCreate,
});

const { data, success } = results;

expect(success).toEqual(true);
expect(data?.ID).toBeDefined();
expect(data?.transactionHash).toBeDefined();
});

test("#listBuckets", async () => {
const results = await listBuckets(config);
console.log("results", results);
expect(
!!results.data!.find(
(bucket: any) => bucket.Name === testBucketNameExists,
),
).toEqual(true);
});

test("#listBucketFiles", async () => {
const files = await listBucketFiles({
...config,
bucketName: testBucketNameExists,
});
console.log("files", files);
// expect(!!files.length > 0).toEqual(true);
});

test("uploadFileObject", async () => {
const file = {
lorem: testContent,
};
const response = await uploadFileObject({
...config,
fileName,
file,
bucketName: testBucketNameExists,
});
expect(response).toHaveProperty("success", true);
// Add more assertions as needed
});

test.only("uploadFileWithFormData", async () => {
const file = new File([testContent], fileName, {
type: "text/plain",
});
const response = await uploadFileWithFormData({
...config,
fileName,
file,
bucketName: testBucketNameExists,
});
});

// TODO delete bucket
afterAll(async () => {});
},
60 * 1000,
);
163 changes: 163 additions & 0 deletions apps/storybook/src/lib/filecoin/akave/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import ky from "ky";

export interface AkaveFile {
Name: string;
RootCID: string;
Size: string;
CreatedAt: string;
}

export interface AkaveBucket {
ID: string;
Name: string;
CreatedAt: string;
files: File[];
}

/**
* Authentication and browser-compatability are to be confirmed
* TODO refactor akave config object
*/

const AKAVE_ENDPOINT_URL = process.env.AKAVE_ENDPOINT_URL || "localhost:3000";

export const getBucketMetadata = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
}: {
akaveEndpointUrl: string;
bucketName: string;
}) => {
const url = `${akaveEndpointUrl}/buckets/${bucketName}`;

return ky(url).then((response) => response.json());
};

export const listBucketFiles = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
}: {
akaveEndpointUrl: string;
bucketName: string;
}): Promise<AkaveFile[]> => {
const url = `${akaveEndpointUrl}/buckets/${bucketName}/files`;

return ky(url)
.then((response) => response.json<{ data: AkaveFile[] }>())
.then(({ data }) => {
return data || [];
});
};

type ListBucketRes = {
data?: Omit<AkaveBucket, "files">[];
success: boolean;
};
export const listBuckets = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
}: {
akaveEndpointUrl: string;
}) => {
const url = `${akaveEndpointUrl}/buckets`;

return ky(url).then((response) => response.json<ListBucketRes>());
};

type CreateBucketRes = {
data?: {
ID: string;
transactionHash: string;
};
success: boolean;
};

export const createBucket = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
}: {
akaveEndpointUrl: string;
bucketName: string;
}) => {
const url = `${akaveEndpointUrl}/buckets`;

return ky
.post(url, {
json: { bucketName },
})
.then((response) => response.json<CreateBucketRes>());
};

export const createDownloadUrl = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
fileName,
}: {
akaveEndpointUrl: string;
bucketName: string;
fileName: string;
}) => {
return `${akaveEndpointUrl}/buckets/${bucketName}/files/${fileName}/download`;
};

export type UploadParams = {
akaveEndpointUrl: string;
bucketName: string;
fileName: string;
file: File | Blob | Object;
};

const createUploadEndpoint = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
}: {
akaveEndpointUrl: string;
bucketName: string;
}) => {
return `${akaveEndpointUrl}/buckets/${bucketName}/files`;
};

export const uploadFileObject = ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
fileName,
file,
}: UploadParams) => {
const endpoint = createUploadEndpoint({
akaveEndpointUrl,
bucketName,
});

const options = typeof file === "string" ? { body: file } : { json: file };

return ky.post(endpoint, options).then((res) => res.json());
};

export const uploadFileWithFormData = async ({
akaveEndpointUrl = AKAVE_ENDPOINT_URL,
bucketName,
fileName,
file,
}: UploadParams): Promise<AkaveFile> => {
const endpoint = createUploadEndpoint({
akaveEndpointUrl,
bucketName,
});
const formData = new FormData();
// @ts-ignore
formData.append("file", file);

// Caused by: RequestContentLengthMismatchError: Request body length does not match content-length header
return ky
.post(endpoint, {
body: formData,
})
.then((res) => res.json())
.then((results: any) => {
const { success, data } = results;
if (success) {
return data;
}

throw new Error("Upload Failed");
});
};
1 change: 1 addition & 0 deletions apps/storybook/src/lib/filecoin/akave/fixture.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc
18 changes: 18 additions & 0 deletions apps/storybook/src/lib/filecoin/gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getLighthouseGatewayUrl } from "./lighthouse/isomorphic";

export enum FilecoinGateway {
Lighthouse = "lighthouse",
Akave = "akave",
}

export const getGatewayUrlWithCid = (
cid: string,
gateway: FilecoinGateway = FilecoinGateway.Lighthouse,
) => {
// TODO discuss on retrieveal, auth and optimization required
// if (gateway === FilecoinGateway.Akave) {
// return "";
// }

return getLighthouseGatewayUrl(cid);
};
Loading
Loading