Skip to content

Commit 40b1ba8

Browse files
committed
feat: Backend changes for SSS flow
1 parent 81abb36 commit 40b1ba8

4 files changed

Lines changed: 38 additions & 488 deletions

File tree

controllers/web3Controller.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pinata } from "../config/pinata.js";
22
import Web3Event from "../models/Web3Event.js";
3+
import CryptoJS from "crypto-js";
34

45
export const fetchFileInfo = async(req,res)=>{
56
try{
@@ -9,14 +10,22 @@ export const fetchFileInfo = async(req,res)=>{
910
return res.status(204).json({error:'No file information found.'});
1011

1112
const metaDatas=data.fileData.map((item)=>{
13+
let decryptedShare2="";
14+
if (item.share2) {
15+
const bytes = CryptoJS.AES.decrypt(item.share2, process.env.BACKEND_SECRET);
16+
decryptedShare2 = bytes.toString(CryptoJS.enc.Utf8);
17+
}
18+
1219
return {
13-
type:item.type,
14-
size:item.size,
15-
name:item.name,
16-
fileDBId:item._id,
17-
authTag:item.authTag,
18-
iv:item.iv,
19-
uploadedAt:item.uploadedAt
20+
fileDBId: item._id,
21+
share1: item.share1, // CID HASH
22+
share2: decryptedShare2, // DECRYPTED
23+
type: item.type,
24+
size: item.size,
25+
name: item.name,
26+
authTag: item.authTag,
27+
iv: item.iv,
28+
uploadedAt: item.uploadedAt
2029
}}
2130
);
2231
// console.log("MetaDatas:",metaDatas);
@@ -32,7 +41,7 @@ export const fetchFileInfo = async(req,res)=>{
3241
export const uploadToIPFS = async(req,res)=>{
3342
try{
3443
const file = req.file;
35-
const { iv, authTag } = req.body;
44+
const { iv, authTag, share1, share2 } = req.body;
3645

3746
if(!file)
3847
return res.status(404).json({ error: "No file provided."})
@@ -42,13 +51,18 @@ export const uploadToIPFS = async(req,res)=>{
4251
req.file.originalname ,
4352
{ type: req.file.mimetype}
4453
);
54+
// Encrypt share2 using BACKEND_SECRET
55+
const encryptedShare2 = CryptoJS.AES.encrypt(share2,process.env.BACKEND_SECRET).toString();
56+
4557
const response = await pinata.upload.public.file(fileObject);
4658
const fileMetadata={
59+
share1: share1,
60+
share2: encryptedShare2,
4761
type: req.file.mimetype,
4862
size: req.file.size,
4963
name: req.file.originalname,
5064
authTag: authTag,
51-
iv: iv
65+
iv: iv,
5266
};
5367

5468
const web3event= await Web3Event.findOneAndUpdate(
@@ -63,7 +77,11 @@ export const uploadToIPFS = async(req,res)=>{
6377

6478
console.log("Pinata Response:",response);
6579

66-
return res.status(200).json({uploadedFile:{...fileMetadata,fileDBId:newFile._id},message:"Uploaded to IPFS successfully!"});
80+
return res.status(200).json({uploadedFile:{
81+
...fileMetadata,
82+
share2: share2,
83+
fileDBId:newFile._id},
84+
message:"Uploaded to IPFS successfully!"});
6785
}
6886
catch(err)
6987
{

models/Web3Event.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const Web3EventSchema = new mongoose.Schema(
1010
},
1111
fileData:[
1212
{
13+
// Shares
14+
share1: {type:String, required:true}, // ipfs-cid
15+
share2: {type:String, required:true}, // encrypted using BACKEND_SECRET
1316
// Encryption Data
1417
authTag:{type:String, required:true},
1518
iv:{type:String, required:true},

0 commit comments

Comments
 (0)