IPFS can be easily implemented as decentralized storage for a task. Using libraries such as web3.Storage, data can easily be stored, retrieved, and maintained on IPFS. The stored data will be referenced using IPFS content identifiers that are unique to the data, making your data completely portable and accessible from anywhere broadcasting data to the IPFS network, whether on a local or peer device or uploaded to web3.storage.
Import the library and create a new client in your executable file:
// Using Web3Storage to make uploading to IPFS easierconst { Web3Storage,getFilesFromPath } =require('web3.storage');// Create new clientconststorageClient=newWeb3Storage({ token:process.env.WEB3_STORAGE_KEY });
In the task function, store file and retrieve cid:
asyncfunctiontask() {constrandomJoke=getRandomJoke();// TODO sign data uploaded to IPFS to prove node uploaded itconstqodJSON=JSON.stringify(randomJoke);constsignedJSON=awaitnamespace.signData(qodJSON);fs.writeFileSync("qod.json", signedJSON);if (storageClient) {// Storing on IPFS through web3 storage as exampleconstfile=awaitgetFilesFromPath("./qod.json");constcid=awaitstorageClient.put(file);console.log("CID of Uploaded Data: ", cid);awaitnamespace.checkSubmissionAndUpdateRound(cid); } else {console.error("No web3 storage API key provided"); }}