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