UNPKG

@lighthouse-web3/sdk

Version:

NPM package and CLI tool to interact with lighthouse protocol

43 lines (42 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* istanbul ignore file */ const encryptionNode_1 = require("../../encryptionNode"); const kavach_1 = require("@lighthouse-web3/kavach"); const lighthouse_config_1 = require("../../../../lighthouse.config"); const util_1 = require("../../../utils/util"); exports.default = async (text, apiKey, publicKey, signedMessage, name) => { try { const token = 'Bearer ' + apiKey; const endpoint = lighthouse_config_1.lighthouseConfig.lighthouseNode + '/api/v0/add'; // Upload file const formData = new FormData(); const { masterKey: fileEncryptionKey, keyShards } = await (0, kavach_1.generate)(); const encryptedData = await (0, encryptionNode_1.encryptFile)(Buffer.from(text), fileEncryptionKey); const blob = new Blob([Buffer.from(encryptedData)]); formData.append('file', blob, name); const response = await (0, util_1.fetchWithTimeout)(endpoint, { method: 'POST', body: formData, credentials: 'include', timeout: 7200000, headers: { Encryption: 'true', 'Mime-Type': 'text/plain', Authorization: token, }, }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const responseData = (await response.json()); const { error } = await (0, kavach_1.saveShards)(publicKey, responseData[0].Hash, signedMessage, keyShards); if (error) { throw new Error('Error encrypting file'); } return { data: responseData }; } catch (error) { throw new Error(error.message); } };