UNPKG

@vechain/vebetterdao-contracts

Version:

Open-source repository that houses the smart contracts powering the decentralized VeBetterDAO on the VeChain Thor blockchain.

71 lines (70 loc) 2.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadBlobToIPFS = uploadBlobToIPFS; exports.uploadDirectoryToIPFS = uploadDirectoryToIPFS; exports.toIPFSURL = toIPFSURL; const fs_1 = require("./fs"); const config_1 = require("@repo/config"); const axios_1 = __importDefault(require("axios")); async function uploadDirectoryToIPFS(pathToUpload, path) { try { const form = (0, fs_1.formData)(pathToUpload); const response = await axios_1.default.post((0, config_1.getConfig)().ipfsPinningService, form, { headers: { ...form.getHeaders(), }, }); // Extract the IPFS hash from the response const ipfsHash = response.data.IpfsHash; console.log("IPFS Hash:", ipfsHash); const files = await (0, fs_1.readFilesFromDirectory)(path); const folderName = (0, fs_1.getFolderName)(path); // Return the IPFS hash return [ipfsHash, files, folderName]; } catch (error) { console.error("Error uploading file:", error); throw new Error("Failed to upload directory to IPFS"); } } /** * Uploads a blob to IPFS. * @param blob The Blob object to upload. * @param filename A name for the file in the FormData payload. * @returns The IPFS hash of the uploaded blob. */ async function uploadBlobToIPFS(blob, filename) { try { const form = new FormData(); form.append("file", blob, filename); const response = await axios_1.default.post((0, config_1.getConfig)().ipfsPinningService, form); // Extract the IPFS hash from the response const ipfsHash = response.data.IpfsHash; console.log("IPFS Hash:", ipfsHash); return ipfsHash; } catch (error) { console.error("Error uploading blob:", error); throw new Error("Failed to upload blob to IPFS"); } } /** * Constructs an IPFS URL using a CID, and optionally a folder name and a file name. * @param cid - The CID to convert into an IPFS URL. * @param fileName - The name of the file to append to the URL. Optional. * @param folderName - The name of the folder to append to the URL. Optional. * @returns The IPFS URL in the format 'ipfs://{cid}/{folderName}/{fileName}'. */ function toIPFSURL(cid, fileName, folderName) { let url = `ipfs://${cid}`; if (folderName) { url += `/${folderName}`; } if (fileName) { url += `/${fileName}`; } return url; }