Skip to content

Commit

Permalink
Fixed all those Storage bugs, jfc
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed Dec 20, 2023
1 parent 3139398 commit 57af1d8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 36 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"formdata-node": "^6.0.3",
"jest": "^27.5.1",
"prettier": "^2.6.2",
"ts-jest": "^27.1.4",
Expand Down
1 change: 1 addition & 0 deletions src/messages/store/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function Pin(spc: StorePinConfiguration): Promise<StoreMessage> {
channel: spc.channel,
fileHash: spc.fileHash,
APIServer: spc.APIServer || DEFAULT_API_V2,
inlineRequested: false,
storageEngine: ItemType.ipfs,
});
}
20 changes: 11 additions & 9 deletions src/messages/store/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MessageBuilder } from "../../utils/messageBuilder";
import { stripTrailingSlash } from "../../utils/url";
import FormData from "form-data";
import axios from "axios";
import { blobToBuffer, calculateSHA256Hash } from "./utils";
import { calculateSHA256Hash } from "./utils";

/**
* channel: The channel in which the message will be published.
Expand Down Expand Up @@ -62,7 +62,6 @@ export async function Publish({
buffer = await processFileObject(fileObject);
hash = await getHash(buffer, storageEngine, fileHash, APIServer);
}

const message = await createAndSendStoreMessage(
account,
channel,
Expand All @@ -73,7 +72,6 @@ export async function Publish({
sync,
buffer,
);

return message;
}

Expand Down Expand Up @@ -131,7 +129,7 @@ async function createAndSendStoreMessage(
APIServer,
});

if (ItemType.ipfs === message.item_type) {
if (ItemType.ipfs == message.item_type) {
await SignAndBroadcast({
message: message,
account,
Expand All @@ -140,7 +138,7 @@ async function createAndSendStoreMessage(
} else if (!fileObject) {
throw new Error("You need to specify a File to upload or a Hash to pin.");
} else {
return await sendMessage(
await sendMessage(
{
message: message,
account,
Expand All @@ -155,12 +153,16 @@ async function createAndSendStoreMessage(
}

async function processFileObject(fileObject: Blob | Buffer | null | undefined): Promise<Buffer> {
if (!fileObject) throw new Error("fileObject is null");
if (!fileObject) {
throw new Error("fileObject is null");
}

if (fileObject instanceof Blob) {
fileObject = await blobToBuffer(fileObject);
if (fileObject instanceof Buffer) {
return fileObject;
}
return fileObject;

const arrayBuf = await fileObject.arrayBuffer();
return Buffer.from(arrayBuf);
}

type SignAndBroadcastConfiguration = {
Expand Down
14 changes: 0 additions & 14 deletions src/messages/store/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import shajs from "sha.js";

export async function blobToBuffer(blob: Blob): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (reader.result instanceof ArrayBuffer) {
resolve(Buffer.from(reader.result));
} else {
reject("Failed to convert Blob to Buffer.");
}
};
reader.readAsArrayBuffer(blob);
});
}

export function calculateSHA256Hash(data: ArrayBuffer | Buffer): string {
const buffer = Buffer.from(data);
return new shajs.sha256().update(buffer).digest("hex");
Expand Down
45 changes: 32 additions & 13 deletions tests/messages/store/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ethereum, store } from "../../index";
import { DEFAULT_API_V2 } from "../../../src/global";
import { ItemType } from "../../../src/messages/message";
import { ItemType } from "../../../src/messages/types";
import fs, { readFileSync } from "fs";
import { EphAccountList } from "../../testAccount/entryPoint";
import { Blob as BlobPolyfill } from "formdata-node";
import { TextDecoder } from "util";

export function ArraybufferToString(ab: ArrayBuffer): string {
return new TextDecoder().decode(ab);
Expand All @@ -20,21 +21,19 @@ describe("Store message publish", () => {
throw Error("[Ephemeral Account Generation] - Generated Account corrupted");
});

it("should store a file and retrieve it correctly", async () => {
it("should store a file buffer and retrieve it correctly", async () => {
const { mnemonic } = ephemeralAccount.eth;
if (!mnemonic) throw Error("Can not retrieve mnemonic inside ephemeralAccount.json");
const account = ethereum.ImportAccountFromMnemonic(mnemonic);
const fileContent = readFileSync("./tests/messages/store/testFile.txt");

const hash = await store.Publish({
const message = await store.Publish({
channel: "TEST",
account: account,
fileObject: fileContent,
});

const response = await store.Get({
fileHash: hash.content.item_hash,
APIServer: DEFAULT_API_V2,
fileHash: message.content.item_hash,
});

const got = ArraybufferToString(response);
Expand All @@ -43,22 +42,43 @@ describe("Store message publish", () => {
expect(got).toBe(expected);
});

it("should store a file BLOB and retrieve it correctly", async () => {
const { mnemonic } = ephemeralAccount.eth;
if (!mnemonic) throw Error("Can not retrieve mnemonic inside ephemeralAccount.json");
const account = ethereum.ImportAccountFromMnemonic(mnemonic);
const fileContent = new BlobPolyfill(["n"]);

const message = await store.Publish({
channel: "TEST",
account: account,
fileObject: fileContent as unknown as Blob,
});
const response = await store.Get({
fileHash: message.content.item_hash,
});

const got = ArraybufferToString(response);
const expected = "n";

expect(got).toBe(expected);
});

it("should pin a file and retrieve it correctly", async () => {
const { mnemonic } = ephemeralAccount.eth;
if (!mnemonic) throw Error("Can not retrieve mnemonic inside ephemeralAccount.json");
const account = ethereum.ImportAccountFromMnemonic(mnemonic);
const helloWorldHash = "QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j";

const hash = await store.Pin({
const message = await store.Pin({
channel: "TEST",
account: account,
fileHash: helloWorldHash,
});

console.log(message);
const response = await store.Get({
fileHash: hash.content.item_hash,
APIServer: DEFAULT_API_V2,
fileHash: message.content.item_hash,
});
console.log(response);

const got = ArraybufferToString(response);
const expected = "hello world!";
Expand Down Expand Up @@ -91,14 +111,13 @@ describe("Store message publish", () => {
account: account,
fileHash: helloWorldHash,
}),
).rejects.toThrow("You must choose ipfs to pin file.");
).rejects.toThrow("You must choose ipfs to pin the file.");

await expect(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
store.Publish({
channel: "TEST",
APIServer: DEFAULT_API_V2,
account: account,
storageEngine: ItemType.storage,
}),
Expand Down

0 comments on commit 57af1d8

Please sign in to comment.