sairaj mote 2023-07-30 15:23:59 +05:30
commit a47b3abdf8
2 changed files with 113 additions and 64 deletions

View File

@ -1,4 +1,4 @@
(function (EXPORTS) { //floCloudAPI v2.4.3a (function (EXPORTS) { //floCloudAPI v2.4.5
/* FLO Cloud operations to send/request application data*/ /* FLO Cloud operations to send/request application data*/
'use strict'; 'use strict';
const floCloudAPI = EXPORTS; const floCloudAPI = EXPORTS;
@ -609,49 +609,39 @@
}) })
} }
*/ */
/*(NEEDS UPDATE) //edit comment of data in supernode cloud (sender only)
//edit comment of data in supernode cloud (mutable comments only) floCloudAPI.editApplicationData = function (vectorClock, comment_edit, options = {}) {
floCloudAPI.editApplicationData = function(vectorClock, newComment, oldData, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let p0 //request the data from cloud for resigning
if (!oldData) { let req_options = Object.assign({}, options);
options.atVectorClock = vectorClock; req_options.atVectorClock = vectorClock;
options.callback = false; requestApplicationData(undefined, req_options).then(result => {
p0 = requestApplicationData(false, options) if (!result.length)
} else return reject("Data not found");
p0 = Promise.resolve({ let data = result[0];
vectorClock: { if (data.senderID !== user.id)
...oldData return reject("Only sender can edit comment");
} data.comment = comment_edit;
}) let hashcontent = ["receiverID", "time", "application", "type", "message", "comment"]
p0.then(d => { .map(d => data[d]).join("|");
if (d.senderID != user.id) let re_sign = user.sign(hashcontent);
return reject("Invalid requestorID") var request = {
else if (!d.comment.startsWith("EDIT:")) receiverID: options.receiverID || DEFAULT.adminID,
return reject("Data immutable")
let data = {
requestorID: user.id, requestorID: user.id,
receiverID: d.receiverID, pubKey: user.public,
time: Date.now(), time: Date.now(),
application: d.application, vectorClock: vectorClock,
edit: { edit: comment_edit,
vectorClock: vectorClock, re_sign: re_sign
comment: newComment
}
} }
d.comment = data.edit.comment; let request_hash = ["time", "vectorClock", "edit", "re_sign"].map(d => request[d]).join("|");
let hashcontent = ["receiverID", "time", "application", "type", "message", request.sign = user.sign(request_hash);
"comment" singleRequest(request.receiverID, request)
] .then(result => resolve(result))
.map(x => d[x]).join("|")
data.edit.sign = user.sign(hashcontent)
singleRequest(data.receiverID, data)
.then(result => resolve("Data comment updated"))
.catch(error => reject(error)) .catch(error => reject(error))
}) }).catch(error => reject(error))
}) })
} }
*/
//tag data in supernode cloud (subAdmin access only) //tag data in supernode cloud (subAdmin access only)
floCloudAPI.tagApplicationData = function (vectorClock, tag, options = {}) { floCloudAPI.tagApplicationData = function (vectorClock, tag, options = {}) {
@ -811,6 +801,67 @@
}) })
} }
//upload file
floCloudAPI.uploadFile = function (fileBlob, type, options = {}) {
return new Promise((resolve, reject) => {
if (!(fileBlob instanceof File) && !(fileBlob instanceof Blob))
return reject("file must be instance of File/Blob");
fileBlob.arrayBuffer().then(arraybuf => {
let file_data = { type: fileBlob.type, name: fileBlob.name };
file_data.content = Crypto.util.bytesToBase64(new Uint8Array(arraybuf));
if (options.encrypt) {
let encryptionKey = options.encrypt === true ?
floGlobals.settings.encryptionKey : options.encrypt
file_data = floCrypto.encryptData(JSON.stringify(file_data), encryptionKey)
}
sendApplicationData(file_data, type, options)
.then(({ vectorClock, receiverID, type, application }) => resolve({ vectorClock, receiverID, type, application }))
.catch(error => reject(error))
}).catch(error => reject(error))
})
}
//download file
floCloudAPI.downloadFile = function (vectorClock, options = {}) {
return new Promise((resolve, reject) => {
options.atVectorClock = vectorClock;
requestApplicationData(options.type, options).then(result => {
if (!result.length)
return reject("File not found");
result = result[0];
try {
let file_data = decodeMessage(result.message);
//file is encrypted: decryption required
if (file_data instanceof Object && "secret" in file_data) {
if (!options.decrypt)
return reject("Data is encrypted");
let decryptionKey = (options.decrypt === true) ? Crypto.AES.decrypt(user_private, aes_key) : options.decrypt;
if (!Array.isArray(decryptionKey))
decryptionKey = [decryptionKey];
let flag = false;
for (let key of decryptionKey) {
try {
let tmp = floCrypto.decryptData(file_data, key);
file_data = JSON.parse(tmp);
flag = true;
break;
} catch (error) { }
}
if (!flag)
return reject("Unable to decrypt file: Invalid private key");
}
//reconstruct the file
let arraybuf = new Uint8Array(Crypto.util.base64ToBytes(file_data.content))
result.file = new File([arraybuf], file_data.name, { type: file_data.type });
resolve(result)
} catch (error) {
console.error(error);
reject("Data is not a file");
}
}).catch(error => reject(error))
})
}
/* /*
Functions: Functions:
findDiff(original, updatedObj) returns an object with the added, deleted and updated differences findDiff(original, updatedObj) returns an object with the added, deleted and updated differences

View File

@ -1,4 +1,4 @@
(function (EXPORTS) { //floDapps v2.4.0 (function (EXPORTS) { //floDapps v2.4.1
/* General functions for FLO Dapps*/ /* General functions for FLO Dapps*/
'use strict'; 'use strict';
const floDapps = EXPORTS; const floDapps = EXPORTS;
@ -172,12 +172,7 @@
//general //general
lastTx: {}, lastTx: {},
//supernode (cloud list) //supernode (cloud list)
supernodes: { supernodes: {}
indexes: {
uri: null,
pubKey: null
}
}
} }
var obs_a = { var obs_a = {
//login credentials //login credentials
@ -257,7 +252,8 @@
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!startUpOptions.cloud) if (!startUpOptions.cloud)
return resolve("No cloud for this app"); return resolve("No cloud for this app");
compactIDB.readData("lastTx", floCloudAPI.SNStorageID, DEFAULT.root).then(lastTx => { const CLOUD_KEY = "floCloudAPI#" + floCloudAPI.SNStorageID;
compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => {
var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName }; var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
if (typeof lastTx == 'number') //lastTx is tx count (*backward support) if (typeof lastTx == 'number') //lastTx is tx count (*backward support)
query_options.ignoreOld = lastTx; query_options.ignoreOld = lastTx;
@ -265,25 +261,27 @@
query_options.after = lastTx; query_options.after = lastTx;
//fetch data from flosight //fetch data from flosight
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => { floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
for (var i = result.data.length - 1; i >= 0; i--) { compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => {
var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName]; nodes = nodes || {};
for (let sn in content.removeNodes) for (var i = result.data.length - 1; i >= 0; i--) {
compactIDB.removeData("supernodes", sn, DEFAULT.root); var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
for (let sn in content.newNodes) for (let sn in content.removeNodes)
compactIDB.writeData("supernodes", content.newNodes[sn], sn, DEFAULT.root); delete nodes[sn];
for (let sn in content.updateNodes) for (let sn in content.newNodes)
compactIDB.readData("supernodes", sn, DEFAULT.root).then(r => { nodes[sn] = content.newNodes[sn];
r = r || {} for (let sn in content.updateNodes)
r.uri = content.updateNodes[sn]; if (sn in nodes) //check if node is listed
compactIDB.writeData("supernodes", r, sn, DEFAULT.root); nodes[sn].uri = content.updateNodes[sn];
}); }
} Promise.all([
compactIDB.writeData("lastTx", result.lastItem, floCloudAPI.SNStorageID, DEFAULT.root); compactIDB.writeData("lastTx", result.lastItem, CLOUD_KEY, DEFAULT.root),
compactIDB.readAllData("supernodes", DEFAULT.root).then(nodes => { compactIDB.writeData("supernodes", nodes, CLOUD_KEY, DEFAULT.root)
floCloudAPI.init(nodes) ]).then(_ => {
.then(result => resolve("Loaded Supernode list\n" + result)) floCloudAPI.init(nodes)
.catch(error => reject(error)) .then(result => resolve("Loaded Supernode list\n" + result))
}) .catch(error => reject(error))
}).catch(error => reject(error))
}).catch(error => reject(error))
}) })
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })