UNPKG

@betacodd/lighthouse-package

Version:

NPM package and CLI tool to interact with lighthouse protocol

61 lines (60 loc) 2.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const lighthouse_config_1 = require("../../../../lighthouse.config"); const kavach_1 = require("@lighthouse-web3/kavach"); const encryptionNode_1 = require("../../encryptionNode"); exports.default = async (sourcePath, apiKey, publicKey, signed_message) => { try { const FormData = eval('require')('form-data'); const mime = eval('require')('mime-types'); const fs = eval('require')('fs-extra'); const token = 'Bearer ' + apiKey; const endpoint = lighthouse_config_1.lighthouseConfig.lighthouseNode + '/api/v0/add'; const stats = fs.lstatSync(sourcePath); if (stats.isFile()) { // Upload file const formDdata = new FormData(); const mimeType = mime.lookup(sourcePath); const { masterKey: fileEncryptionKey, keyShards } = await (0, kavach_1.generate)(); const fileData = fs.readFileSync(sourcePath); const encryptedData = await (0, encryptionNode_1.encryptFile)(fileData, fileEncryptionKey); formDdata.append('file', Buffer.from(encryptedData), sourcePath.replace(/^.*[\\/]/, '')); const response = await axios_1.default.post(endpoint, formDdata, { withCredentials: true, maxContentLength: Infinity, maxBodyLength: Infinity, headers: { 'Content-type': `multipart/form-data; boundary= ${formDdata.getBoundary()}`, Encryption: 'true', 'Mime-Type': mimeType ? mimeType : '', Authorization: token, }, }); const { error } = await (0, kavach_1.saveShards)(publicKey, response.data.Hash, signed_message, keyShards); if (error) { throw new Error('Error encrypting file'); } // return response /* { data: { Name: 'flow1.png', Hash: 'QmUHDKv3NNL1mrg4NTW4WwJqetzwZbGNitdjr2G6Z5Xe6s', Size: '31735' } } */ return { data: response.data }; } else { throw new Error('Directory currently not supported!!!'); } } catch (error) { throw new Error(error.message); } };