-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODEC_API.js
353 lines (279 loc) · 13.1 KB
/
CODEC_API.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
require ('dotenv').config()
const express = require("express");
const app = express();
const formidable = require('formidable');
const axios = require('axios');
const jwt = require('jsonwebtoken')
console.log ("Starting CODEC MAIN API...")
app.use(express.json())
let EncryptedObject = {};
const SEAL = require('node-seal/allows_js_node_umd');
var myArgs = process.argv.slice(2);
console.log('Listening on port: ', myArgs[0]);
console.log('==================================================================================================' );
//const SEAL = require('node-seal');
let BranchName = "Branch" + myArgs[0]
let AcctToLookUP;
async function GetBankAcctBalance(BranchAcctRes) {
console.log("@@@@@@@@@ Entered GetBankAcctBalance " );
let res = await axios.get('http://127.0.0.1:60000/BranchBal?AcctName=ABC123');
let data = res.data;
console.log("********** This is the data from BankAPI " + res);
BranchAcctRes = data;
}
app.post('/CodecAPI', (req, res, next) => {
const form = new formidable.IncomingForm();
form.parse(req, async function(err, fields, files) {
if (err) {
return res.status(400).json({ error: err.message });
}
let encObject = {};
AcctToLookUP = fields.AcctName;
encObject.cipherText = fields.cipherText;
encObject.publicKey = fields.publicKey;
// encObject.secretKey = fields.secretKey;
encObject.encryptor = fields.encryptor;
var NodeAddresses ;
// console.log( "RRRRRRRRRRRRRRRR: " +fields.NodeAdds[1]);
var NodeArray ;
var NodeNums = JSON.parse(fields.NodeNumbers)
let BranchAcctRes;
// console.log('RRRRRRRRRRRRRRRRR CodecAPI- 1 :: NodeNums ' + NodeNums );
if ( NodeNums > 0){
// console.log( "PPPPPPPPPPPPPPPP: " +JSON.parse(fields.NodeAdds));
NodeAddresses= JSON.parse(fields.NodeAdds)//.split(',');
// console.log( "PPPPPPPPPPPPPPPP: NodeAddresses 0: " +NodeAddresses[0]);
// console.log( "PPPPPPPPPPPPPPPP: NodeAddresses 1: " +NodeAddresses[1]);
NodeArray = JSON.parse(fields.NodeAdds);
}else{
NodeAddresses = '';
}
let promise7 = new Promise(function(resolve, reject) {
// GetBankAcctBalance(BranchAcctRes);
// console.log("1111111 This is the Acct To Look Up " + AcctToLookUP);
// BranchAcctRes = axios.get('127.0.0.1:60000/BranchBal?AcctName=ABC123');
let SQL_URL = 'http://127.0.0.1:60000/BranchBal?AcctName=' + AcctToLookUP +'&DataTable=' + BranchName;
// console.log("222222 Calling this URL " + SQL_URL);
axios.get(SQL_URL).then(response => {
// console.log("********** This is the response code: " + response.status);
// console.log("********** This is the response data: " + response.data.RetValue);
setTimeout(() => resolve(response), 200);
//setTimeout(() => resolve(BranchAcctRes), 500);
BranchAcctRes = response.data.RetValue
// return response.status;
})
.catch((error) => {
console.log('error 3 ' + error);
});
// console.log("2222222 Before the Call " );
//setTimeout(() => resolve(BranchAcctRes), 500);
});
promise7.then(
result => {
// console.log("44444444 This is the data from BankAPI " + BranchAcctRes);
let promise4 = new Promise(function(resolve, reject) {
// console.log( "Calling GetBankAcctBal: " );
AddEncryptInput(encObject, BranchAcctRes);
setTimeout(() => resolve(encObject), 1000);
});
promise4.then(
result => {
let promise3 = new Promise(function(resolve, reject) {
authenticateToken (fields.AccToken, fields.Auth_Serv_Add)
setTimeout(() => resolve(encObject), 1000);
});
promise3.then(
result => {
let promise6 = new Promise(function(resolve, reject) {
if (NodeNums < 1){ // If no more nodes, return
// console.log('CodecAPI- 1 ::Last NODE, Returning' );
res.json({ RetVal: encObject.TempcipherText });
return;
}
SendToNextNode (fields.AccToken, fields.Auth_Serv_Add, encObject, NodeNums, NodeArray);
TimeOut = (NodeNums+1 )* 6000;
console.log ("111111111111111111 AFTER SendToNextNode: Waiting to Receive in : "+ TimeOut )
setTimeout(() => resolve(encObject), TimeOut);
});
promise6.then(
result => {
console.log('CodecAPI- 1 :: Returning to the PREVIOUS Node' );
res.json({ RetVal: BranchName+','+encObject.TempcipherText });
}
);
}
);
}
);
});
//let data = res.data;
// console.log("33333333 Before the Call " );
// console.log('Completed promise7' );
}
);
});
function authenticateToken (AccToken, Auth_Serv_Add, res){
const token = AccToken && AccToken.split('.')[1]
//console.log ("authenticateToken: This is the token: "+ token)
if (Auth_Serv_Add == null ) return 402;
if (token == null ) return 401;
// console.log ("authenticateToken: This is the Auth Server: "+ Auth_Serv_Add)
// Verify the Access Token
const AuthStr = 'Bearer '.concat(AccToken);
// console.log("********** Before the call to Auth Server: " );
axios.get(Auth_Serv_Add, { headers: { Authorization: AuthStr } }).then(response => {
// console.log("********** This is the data: " + response.status);
return response.status;
})
.catch((error) => {
console.log('error 3 ' + error);
});
//eturn res.sendStatus(401);
}
//function SendToNextNode (AccToken, NodeAddr,Auth_Serv_Add, EncryptedObj, NodeNumb, NodeArray){
function SendToNextNode (AccToken,Auth_Serv_Add, EncryptedObj, NodeNumb, NodeArray){
/* console.log ("SendToNextNode: ------------------- CodecAPI/Post: This is all the Nodes: "+ NodeAddr )
console.log ("SendToNextNode++++++++++++++++++ CodecAPI/Post: This is next NodeAdress: "+ NodeAddr[1] )
*/
var FormData = require('form-data')
data = new FormData();
// console.log ("SendToNextNode: This is the NodeAdress 0: " )
data.append('Auth_Serv_Add', Auth_Serv_Add);
data.append('AcctName', AcctToLookUP);
// console.log ("SendToNextNode: This is the NodeAdress 1: " )
data.append('AccToken', AccToken);
data.append('NodeNumbers', JSON.stringify(NodeNumb-1))
// console.log ("SendToNextNode: This is the NodeAdress 2: " )
data.append('publicKey', EncryptedObj.publicKey);
data.append('cipherText', EncryptedObj.TempcipherText);
//console.log ("SendToNextNode: This is the NodeAdress 3: " )
//console.log ("SendToNextNode: This is the NodeAdress 1: "+ NodeAddr[0] )
//console.log ("SendToNextNode: This is the NodeAdress 4 NodeAddr[1]): " + NodeAddr[1])
// console.log ("SendToNextNode: This is the NodeAdress 5: " )
var NextNodeAddress;
if (NodeNumb > 0){
// data.append('NodeAdds', JSON.stringify(NodeAddr[1])) // Send the list starting with the next node address
// console.log ("999999999999999999 SendToNextNode: This is the NodeAdress 5: |" + NodeArray[0].toString() + "|")
NextNodeAddress= NodeArray[0].toString();
NodeArray.shift()
data.append('NodeAdds', JSON.stringify(NodeArray)) // Send the list starting with the next node address
}
else{
data.append('NodeAdds', 'Empty') // Send the list starting with the next node address
}
let promise2 = new Promise(function(resolve, reject) {
// console.log('--------------SendToNextNode- 1 :: promise2 . Sending this address to next node: |' + NodeAddr +"|" );
const formHeaders = data.getHeaders();
// console.log ("6666666666666666666 SendToNextNode: This is Number of Nodes left: |" + NodeNumb + "|")
//console.log ("777777777777777 SendToNextNode: These are the Nodes: |" + NodeAddr + "|")
/* if ( 1 == NodeNumb){
//NextNodeAddress= NodeAddr.toString();
}else{
NextNodeAddress= NodeAddr[0];
}
*/
axios.post(NextNodeAddress, data, {
//Authorization: 'Bearer '+AccToken,
headers: formHeaders
})
.then(response => {
// console.log('Got back from server ' + response.data.RetVal)
//console.log('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Got back from server ' + response.data.RetVal)
EncryptedObj.TempcipherText = response.data.RetVal;
//let Subtotal = ReturnBal(EncryptedObject);
}
)
.catch(error => {
console.log('Axios Error', error.message)
return;
})
// console.log ("101010101010 SendToNextNode: This is the NodeAdress 6: " )
// setTimeout(() => resolve(EncryptedObject), 1000);
});
promise2.then(
result => {
console.log("In SendToNextNode- 2 :: promise2 then .: " )
},
);
//eturn res.sendStatus(401);
}
function AddEncryptInput(EncrdObj, InputValue2) {
{
;(async () => {
const Morfix = await SEAL()
// Using CommonJS for RunKit
//const { Seal } = require('node-seal')
// const Morfix = await Seal()
// const Morfix = EncrdObj.Morfix;
const schemeType = Morfix.SchemeType.BFV
const securityLevel = Morfix.SecurityLevel.tc128
const polyModulusDegree = 4096
const bitSizes = [36, 36, 37]
const bitSize = 20
const parms = Morfix.EncryptionParameters(schemeType)
// Set the PolyModulusDegree
parms.setPolyModulusDegree(polyModulusDegree)
// console.log("AddEncryptInput:: Step 1: " ); /home/abinesh/CODEC/JWTAuth/CodecNetowrk/CODEC_Main_API_Temp_Partha.js
// Create a suitable set of CoeffModulus primes
parms.setCoeffModulus(
Morfix.CoeffModulus.Create(polyModulusDegree, Int32Array.from(bitSizes))
)
// console.log("AddEncryptInput:: Step 2: " );
// Set the PlainModulus to a prime of bitSize 20.
parms.setPlainModulus(
Morfix.PlainModulus.Batching(polyModulusDegree, bitSize)
)
// console.log("AddEncryptInput:: Step 3: " );
const context = Morfix.Context(
parms, // Encryption Parameters
true, // ExpandModChain
securityLevel // Enforce a security level
)
if (!context.parametersSet()) {
throw new Error(
'Could not set the parameters in the given context. Please try different encryption parameters.'
)
}
//-1- For testing Secret key uncomment me const UploadedSecretKey = Morfix.SecretKey()
//0- For testing Secret key uncomment me UploadedSecretKey.load(context, EncrdObj.secretKey)
//1- For testing Secret key uncomment me const decryptor = Morfix.Decryptor(context, UploadedSecretKey)
const UploadedPublicKey = Morfix.PublicKey()
UploadedPublicKey.load(context, EncrdObj.publicKey)
const uploadedCipherText = Morfix.CipherText()
uploadedCipherText.load(context, EncrdObj.cipherText)
const encoder = Morfix.BatchEncoder(context)
const keyGenerator = Morfix.KeyGenerator(context)
const encryptor = Morfix.Encryptor(context, UploadedPublicKey)
const evaluator = Morfix.Evaluator(context)
// Create data to be encrypted
const array = Int32Array.from([InputValue2])
// Encode the Array
const plainText = encoder.encode(array)
// Encrypt the PlainText
const cipherText = encryptor.encrypt(plainText)
evaluator.add(uploadedCipherText, cipherText, uploadedCipherText) // Op (A), Op (B), Op (Dest)
EncrdObj.cipherText = uploadedCipherText;
EncrdObj.context = context;
//2- For testing Secret key uncomment me const decryptedPlainText = decryptor.decrypt(EncrdObj.cipherText)
const TempcipherText = await EncrdObj.cipherText.save();
EncrdObj.TempcipherText = TempcipherText;
//console.log('AddEncryptInput:: EncrdObj.cipherText 2: ', TempcipherText)
// Decode the PlainText
//3- For testing Secret key uncomment me decodedArray = encoder.decode(decryptedPlainText)
//4- For testing Secret key uncomment me console.log('AddEncryptInput:: 3: ', decodedArray[0])
// This is not part of the object. I added it. AB
//5- For testing Secret key uncomment me EncrdObj.decryptedPlainText = decryptedPlainText;
// console.log ("AddEncryptInput: Before Returning TempcipherText...")
console.log('AddEncryptInput:: This is the value to add to the Total: ', array[0])
return TempcipherText;
})()
}
return new Promise(resolve => {
setTimeout(() => {
resolve(EncrdObj);
});
// }, 2000);
});
}
app.listen(myArgs[0]) //8000
console.log ("CODEC MAIN API started...")