UNPKG

@lighthouse-web3/sdk

Version:

NPM package and CLI tool to interact with lighthouse protocol

104 lines (103 loc) 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* istanbul ignore file */ const kavach_1 = require("@lighthouse-web3/kavach"); const encryptionBrowser_1 = require("../../encryptionBrowser"); const lighthouse_config_1 = require("../../../../lighthouse.config"); const util_1 = require("../../../utils/util"); const readFileAsync = (file) => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { reader.result && resolve(reader.result); }; reader.onerror = reject; reader.readAsArrayBuffer(file); }); }; exports.default = async (files, apiKey, publicKey, auth_token, cidVersion, uploadProgressCallback) => { try { let keyMap = {}; let mimeType = null; if (files.length === 1) { mimeType = files[0].type; } const endpoint = lighthouse_config_1.lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false&cid-version=${cidVersion}`; const token = 'Bearer ' + apiKey; const fileArr = []; for (let i = 0; i < files.length; i++) { fileArr.push(files[i]); } (0, util_1.checkDuplicateFileNames)(fileArr); if (files.length > 1 && auth_token.startsWith('0x')) { throw new Error(JSON.stringify(`auth_token must be a JWT`)); } const formData = new FormData(); const filesParam = await Promise.all(fileArr.map(async (f) => { const { masterKey: fileEncryptionKey, keyShards } = await (0, kavach_1.generate)(); const fileData = await readFileAsync(f); const encryptedData = await (0, encryptionBrowser_1.encryptFile)(fileData, fileEncryptionKey); keyMap = { ...keyMap, [f.name]: keyShards }; return { data: new Blob([encryptedData], { type: f.type }), fileName: f.name, keyShards, }; })); filesParam.forEach(function (item_) { return formData.append('file', item_.data, item_.fileName ? item_.fileName : 'file'); }); const response = uploadProgressCallback ? await (0, util_1.fetchWithTimeout)(endpoint, { method: 'POST', body: formData, timeout: 7200000, headers: { Encryption: `${true}`, Authorization: token, }, onProgress: (progress) => { uploadProgressCallback({ progress: progress, }); }, }) : await (0, util_1.fetchWithTimeout)(endpoint, { method: 'POST', body: formData, timeout: 7200000, headers: { Encryption: `${true}`, Authorization: token, }, }); if (!response.ok) { const res = (await response.json()); throw new Error(res.error); } const responseText = await response.text(); const jsondata = JSON.parse(responseText); const savedKey = await Promise.all(jsondata.map(async (data) => { return (0, kavach_1.saveShards)(publicKey, data.Hash, auth_token, keyMap[data.Name]); })); savedKey.forEach((_savedKey) => { if (!_savedKey.isSuccess) { throw new Error(JSON.stringify(_savedKey)); } }); // return response /* { data: [{ Name: 'flow1.png', Hash: 'QmUHDKv3NNL1mrg4NTW4WwJqetzwZbGNitdjr2G6Z5Xe6s', Size: '31735' }] } */ return { data: jsondata }; } catch (error) { return error.message; } };