@lighthouse-web3/sdk
Version:
NPM package and CLI tool to interact with lighthouse protocol
42 lines (41 loc) • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/* istanbul ignore file */
const encryptionBrowser_1 = require("../../encryptionBrowser");
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 encoder = new TextEncoder();
const encryptedData = await (0, encryptionBrowser_1.encryptFile)(encoder.encode(text).buffer, fileEncryptionKey);
formData.append('file', new Blob([encryptedData], { type: 'text/plain' }), name);
const response = await (0, util_1.fetchWithTimeout)(endpoint, {
method: 'POST',
body: formData,
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);
}
};