diff --git a/scripts/floDapps.js b/scripts/floDapps.js index 38ee0b4..8924153 100644 --- a/scripts/floDapps.js +++ b/scripts/floDapps.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //floDapps v2.3.2d +(function (EXPORTS) { //floDapps v2.3.5 /* General functions for FLO Dapps*/ 'use strict'; const floDapps = EXPORTS; @@ -144,11 +144,14 @@ } }); - var subAdmins, settings + var subAdmins, trustedIDs, settings; Object.defineProperties(floGlobals, { subAdmins: { get: () => subAdmins }, + trustedIDs: { + get: () => trustedIDs + }, settings: { get: () => settings }, @@ -181,6 +184,7 @@ credentials: {}, //for Dapps subAdmins: {}, + trustedIDs: {}, settings: {}, appObjects: {}, generalData: {}, @@ -228,10 +232,31 @@ }) } + const startUpOptions = { + cloud: true, + app_config: true, + } + + floDapps.startUpOptions = { + set app_config(val) { + if (val === true || val === false) + startUpOptions.app_config = val; + }, + get app_config() { return startUpOptions.app_config }, + + set cloud(val) { + if (val === true || val === false) + startUpOptions.cloud = val; + }, + get cloud() { return startUpOptions.cloud }, + } + const startUpFunctions = []; startUpFunctions.push(function readSupernodeListFromAPI() { return new Promise((resolve, reject) => { + if (!startUpOptions.cloud) + return resolve("No cloud for this app"); compactIDB.readData("lastTx", floCloudAPI.SNStorageID, DEFAULT.root).then(lastTx => { floBlockchainAPI.readData(floCloudAPI.SNStorageID, { ignoreOld: lastTx, @@ -244,6 +269,12 @@ compactIDB.removeData("supernodes", sn, DEFAULT.root); for (let sn in content.newNodes) compactIDB.writeData("supernodes", content.newNodes[sn], sn, DEFAULT.root); + for (let sn in content.updateNodes) + compactIDB.readData("supernodes", sn, DEFAULT.root).then(r => { + r = r || {} + r.uri = content.updateNodes[sn]; + compactIDB.writeData("supernodes", r, sn, DEFAULT.root); + }); } compactIDB.writeData("lastTx", result.totalTxs, floCloudAPI.SNStorageID, DEFAULT.root); compactIDB.readAllData("supernodes", DEFAULT.root).then(nodes => { @@ -258,6 +289,8 @@ startUpFunctions.push(function readAppConfigFromAPI() { return new Promise((resolve, reject) => { + if (!startUpOptions.app_config) + return resolve("No configs for this app"); compactIDB.readData("lastTx", `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root).then(lastTx => { floBlockchainAPI.readData(DEFAULT.adminID, { ignoreOld: lastTx, @@ -274,6 +307,12 @@ if (Array.isArray(content.addSubAdmin)) for (var k = 0; k < content.addSubAdmin.length; k++) compactIDB.writeData("subAdmins", true, content.addSubAdmin[k]); + if (Array.isArray(content.removeTrustedID)) + for (var j = 0; j < content.removeTrustedID.length; j++) + compactIDB.removeData("trustedIDs", content.removeTrustedID[j]); + if (Array.isArray(content.addTrustedID)) + for (var k = 0; k < content.addTrustedID.length; k++) + compactIDB.writeData("trustedIDs", true, content.addTrustedID[k]); if (content.settings) for (let l in content.settings) compactIDB.writeData("settings", content.settings[l], l) @@ -281,9 +320,12 @@ compactIDB.writeData("lastTx", result.totalTxs, `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root); compactIDB.readAllData("subAdmins").then(result => { subAdmins = Object.keys(result); - compactIDB.readAllData("settings").then(result => { - settings = result; - resolve("Read app configuration from blockchain"); + compactIDB.readAllData("trustedIDs").then(result => { + trustedIDs = Object.keys(result); + compactIDB.readAllData("settings").then(result => { + settings = result; + resolve("Read app configuration from blockchain"); + }) }) }) }) @@ -293,6 +335,8 @@ startUpFunctions.push(function loadDataFromAppIDB() { return new Promise((resolve, reject) => { + if (!startUpOptions.cloud) + return resolve("No cloud for this app"); var loadData = ["appObjects", "generalData", "lastVC"] var promises = [] for (var i = 0; i < loadData.length; i++) @@ -404,7 +448,8 @@ try { user_public = floCrypto.getPubKeyHex(privKey); user_id = floCrypto.getAddress(privKey); - floCloudAPI.user(user_id, privKey); //Set user for floCloudAPI + if (startUpOptions.cloud) + floCloudAPI.user(user_id, privKey); //Set user for floCloudAPI user_priv_wrap = () => checkIfPinRequired(key); let n = floCrypto.randInt(12, 20); aes_key = floCrypto.randString(n); @@ -454,7 +499,7 @@ }) }); - floDapps.launchStartUp = function() { + floDapps.launchStartUp = function () { return new Promise((resolve, reject) => { initIndexedDB().then(log => { console.log(log) @@ -497,7 +542,7 @@ floDapps.setAppObjectStores = appObs => initIndexedDB.appObs = appObs; - floDapps.storeContact = function(floID, name) { + floDapps.storeContact = function (floID, name) { return new Promise((resolve, reject) => { if (!floCrypto.validateAddr(floID)) return reject("Invalid floID!") @@ -508,7 +553,7 @@ }); } - floDapps.storePubKey = function(floID, pubKey) { + floDapps.storePubKey = function (floID, pubKey) { return new Promise((resolve, reject) => { if (floID in user.pubKeys) return resolve("pubKey already stored") @@ -523,7 +568,7 @@ }); } - floDapps.sendMessage = function(floID, message) { + floDapps.sendMessage = function (floID, message) { return new Promise((resolve, reject) => { let options = { receiverID: floID, @@ -538,7 +583,7 @@ }) } - floDapps.requestInbox = function(callback) { + floDapps.requestInbox = function (callback) { return new Promise((resolve, reject) => { let lastVC = Object.keys(user.messages).sort().pop() let options = { @@ -552,7 +597,7 @@ try { if (d[v].message instanceof Object && "secret" in d[v].message) d[v].message = floCrypto.decryptData(d[v].message, privKey) - } catch (error) {} + } catch (error) { } compactIDB.writeData("messages", d[v], v, user.db_name) user.messages[v] = d[v] } @@ -565,8 +610,10 @@ }) } - floDapps.manageAppConfig = function(adminPrivKey, addList, rmList, settings) { + floDapps.manageAppConfig = function (adminPrivKey, addList, rmList, settings) { return new Promise((resolve, reject) => { + if (!startUpOptions.app_config) + return reject("No configs for this app"); if (!Array.isArray(addList) || !addList.length) addList = undefined; if (!Array.isArray(rmList) || !rmList.length) rmList = undefined; if (!settings || typeof settings !== "object" || !Object.keys(settings).length) settings = undefined; @@ -584,12 +631,36 @@ reject('Access Denied for Admin privilege') else floBlockchainAPI.writeData(floID, JSON.stringify(floData), adminPrivKey) - .then(result => resolve(['Updated App Configuration', result])) - .catch(error => reject(error)) + .then(result => resolve(['Updated App Configuration', result])) + .catch(error => reject(error)) }) } - const clearCredentials = floDapps.clearCredentials = function() { + floDapps.manageAppTrustedIDs = function (adminPrivKey, addList, rmList) { + return new Promise((resolve, reject) => { + if (!startUpOptions.app_config) + return reject("No configs for this app"); + if (!Array.isArray(addList) || !addList.length) addList = undefined; + if (!Array.isArray(rmList) || !rmList.length) rmList = undefined; + if (!addList && !rmList) + return reject("No change in list") + var floData = { + [DEFAULT.application]: { + addTrustedID: addList, + removeTrustedID: rmList + } + } + var floID = floCrypto.getFloID(adminPrivKey) + if (floID != DEFAULT.adminID) + reject('Access Denied for Admin privilege') + else + floBlockchainAPI.writeData(floID, JSON.stringify(floData), adminPrivKey) + .then(result => resolve(['Updated App Configuration', result])) + .catch(error => reject(error)) + }) + } + + const clearCredentials = floDapps.clearCredentials = function () { return new Promise((resolve, reject) => { compactIDB.clearData('credentials', DEFAULT.application).then(result => { localStorage.removeItem(`${DEFAULT.application}#privKey`); @@ -599,7 +670,7 @@ }) } - floDapps.deleteUserData = function(credentials = false) { + floDapps.deleteUserData = function (credentials = false) { return new Promise((resolve, reject) => { let p = [] p.push(compactIDB.deleteDB(user.db_name)) @@ -611,7 +682,7 @@ }) } - floDapps.deleteAppData = function() { + floDapps.deleteAppData = function () { return new Promise((resolve, reject) => { compactIDB.deleteDB(DEFAULT.application).then(result => { localStorage.removeItem(`${DEFAULT.application}#privKey`) @@ -623,7 +694,7 @@ }) } - floDapps.securePrivKey = function(pwd) { + floDapps.securePrivKey = function (pwd) { return new Promise(async (resolve, reject) => { let indexArr = localStorage.getItem(`${DEFAULT.application}#privKey`) if (!indexArr) @@ -643,8 +714,8 @@ }) } - floDapps.verifyPin = function(pin = null) { - const readSharesFromIDB = function(indexArr) { + floDapps.verifyPin = function (pin = null) { + const readSharesFromIDB = function (indexArr) { return new Promise((resolve, reject) => { var promises = [] for (var i = 0; i < indexArr.length; i++) @@ -687,7 +758,7 @@ }) } - const getNextGeneralData = floDapps.getNextGeneralData = function(type, vectorClock = null, options = {}) { + const getNextGeneralData = floDapps.getNextGeneralData = function (type, vectorClock = null, options = {}) { var fk = floCloudAPI.util.filterKey(type, options) vectorClock = vectorClock || getNextGeneralData[fk] || '0'; var filteredResult = {} @@ -716,10 +787,10 @@ let tmp = floCrypto.decryptData(data.message, key) data.message = JSON.parse(tmp) break; - } catch (error) {} + } catch (error) { } } } - } catch (error) {} + } catch (error) { } } } getNextGeneralData[fk] = Object.keys(filteredResult).sort().pop();