floCloudAPI_v2.0.2d | floDapps_v2.0.1b

floCloudAPI_v2.0.2d
- Fixed bug: syntax error in requestObjectData
 - Added closeRequest: closes the active live requests

floDapps_v2.0.1b
- minor improvements and alignment.
- Fixed: deleteAppData deletes the lastTx data of the adminID.
This commit is contained in:
sairajzero 2021-01-02 19:11:15 +05:30
parent 1a57f74704
commit b5532444fb

View File

@ -38,7 +38,7 @@
//floDapps.addStartUpFunction('Sample', Promised Function)
//floDapps.setAppObjectStores({sampleObs1:{}, sampleObs2:{options{autoIncrement:true, keyPath:'SampleKey'}, Indexes:{sampleIndex:{}}}})
//floDapps.setCustomPrivKeyInput( () => { FUNCTION BODY *must return private key* } )
//floDapps.setCustomPrivKeyInput( () => { FUNCTION BODY *must resolve private key* } )
floDapps.launchStartUp().then(result => {
console.log(result)
@ -7925,7 +7925,7 @@ Bitcoin.Util = {
}
}
</script>
<script id="floCloudAPI" version="2.0.2c">
<script id="floCloudAPI" version="2.0.2d">
/* FLO Cloud operations to send/request application data*/
const floCloudAPI = {
@ -8468,7 +8468,7 @@ Bitcoin.Util = {
}
delete options.callback;
}
this.requestApplicationData(objectName, options).then(data).then(dataSet => {
this.requestApplicationData(objectName, options).then(dataSet => {
this.util.updateObject(dataSet)
delete options.comment;
options.lowerVectorClock = floGlobals.lastVC[objectName] + 1
@ -8490,6 +8490,19 @@ Bitcoin.Util = {
})
},
closeRequest: function(requestID) {
return new Promise((resolve, reject) => {
let conn = this.util.liveRequest[requestID]
if (!conn)
return reject('Request not found')
conn.onclose = evt => {
delete this.util.liveRequest[requestID]
resolve('Request connection closed')
}
conn.close()
})
},
//reset or initialize an object and send it to cloud
resetObjectData: function(objectName, options = {}) {
return new Promise((resolve, reject) => {
@ -8700,7 +8713,7 @@ Bitcoin.Util = {
}
}
</script>
<script id="floDapps" version="2.0.1a">
<script id="floDapps" version="2.0.1b">
/* General functions for FLO Dapps*/
const floDapps = {
@ -8775,32 +8788,25 @@ Bitcoin.Util = {
readSupernodeListFromAPI: function() {
return new Promise((resolve, reject) => {
compactIDB.readData("lastTx", floGlobals.SNStorageID, "floDapps").then(
lastTx => {
compactIDB.readData("lastTx", floGlobals.SNStorageID, "floDapps").then(lastTx => {
floBlockchainAPI.readData(floGlobals.SNStorageID, {
ignoreOld: lastTx,
sentOnly: true,
pattern: "SuperNodeStorage"
}).then(result => {
for (var i = result.data.length - 1; i >= 0; i--) {
var content = JSON.parse(result.data[i])
.SuperNodeStorage;
var content = JSON.parse(result.data[i]).SuperNodeStorage;
for (sn in content.removeNodes)
compactIDB.removeData("supernodes", sn,
"floDapps");
compactIDB.removeData("supernodes", sn, "floDapps");
for (sn in content.newNodes)
compactIDB.writeData("supernodes", content
.newNodes[sn], sn, "floDapps");
compactIDB.writeData("supernodes", content.newNodes[sn], sn, "floDapps");
}
compactIDB.writeData("lastTx", result.totalTxs,
floGlobals.SNStorageID, "floDapps");
compactIDB.readAllData("supernodes", "floDapps").then(
result => {
compactIDB.writeData("lastTx", result.totalTxs, floGlobals.SNStorageID, "floDapps");
compactIDB.readAllData("supernodes", "floDapps").then(result => {
floGlobals.supernodes = result;
floCloudAPI.util.kBucket.launch()
.then(result => resolve(
"Loaded Supernode list\n" +
result))
.then(result => resolve("Loaded Supernode list\n" + result))
.catch(error => reject(error))
})
})
}).catch(error => reject(error))
@ -8809,42 +8815,32 @@ Bitcoin.Util = {
readAppConfigFromAPI: function() {
return new Promise((resolve, reject) => {
compactIDB.readData("lastTx", floGlobals.adminID, "floDapps").then(lastTx => {
compactIDB.readData("lastTx", `${floGlobals.application}|${floGlobals.adminID}`, "floDapps").then(lastTx => {
floBlockchainAPI.readData(floGlobals.adminID, {
ignoreOld: lastTx,
sentOnly: true,
pattern: floGlobals.application
}).then(result => {
for (var i = result.data.length - 1; i >= 0; i--) {
var content = JSON.parse(result.data[i])[floGlobals
.application];
var content = JSON.parse(result.data[i])[floGlobals.application];
if (!content || typeof content !== "object")
continue;
if (Array.isArray(content.removeSubAdmin))
for (var j = 0; j < content.removeSubAdmin
.length; j++)
compactIDB.removeData("subAdmins", content
.removeSubAdmin[j]);
for (var j = 0; j < content.removeSubAdmin.length; j++)
compactIDB.removeData("subAdmins", content.removeSubAdmin[j]);
if (Array.isArray(content.addSubAdmin))
for (var k = 0; k < content.addSubAdmin
.length; k++)
compactIDB.writeData("subAdmins", true,
content.addSubAdmin[k]);
for (var k = 0; k < content.addSubAdmin.length; k++)
compactIDB.writeData("subAdmins", true, content.addSubAdmin[k]);
if (content.settings)
for (let l in content.settings)
compactIDB.writeData("settings", content
.settings[l], l)
compactIDB.writeData("settings", content.settings[l], l)
}
compactIDB.writeData("lastTx", result.totalTxs,
floGlobals.adminID, "floDapps");
compactIDB.writeData("lastTx", result.totalTxs, `${floGlobals.application}|${floGlobals.adminID}`, "floDapps");
compactIDB.readAllData("subAdmins").then(result => {
floGlobals.subAdmins = Object.keys(result);
compactIDB.readAllData("settings").then(
result => {
compactIDB.readAllData("settings").then(result => {
floGlobals.settings = result;
resolve(
"Read app configuration from blockchain"
);
resolve("Read app configuration from blockchain");
})
})
})
@ -8897,7 +8893,7 @@ Bitcoin.Util = {
}
const writeSharesToIDB = function(shares, i = 0, resultIndexes = []) {
return new Promise((resolve, reject) => {
return new Promise(resolve => {
if (i >= shares.length)
return resolve(resultIndexes)
var n = floCrypto.randInt(0, 100000)
@ -8935,25 +8931,18 @@ Bitcoin.Util = {
if (!privKey)
return;
var threshold = floCrypto.randInt(10, 20)
writeSharesToIDB(floCrypto.createShamirsSecretShares(
privKey, threshold, threshold)).then(
resultIndexes => {
var shares = floCrypto.createShamirsSecretShares(privKey, threshold, threshold)
writeSharesToIDB(shares).then(resultIndexes => {
//store index keys in localStorage
localStorage.setItem(
`${floGlobals.application}#privKey`,
JSON.stringify(resultIndexes))
localStorage.setItem(`${floGlobals.application}#privKey`, JSON.stringify(resultIndexes))
//also add a dummy privatekey to the IDB
var randomPrivKey = floCrypto
.generateNewID().privKey
var randomThreshold = floCrypto.randInt(10,
20)
writeSharesToIDB(floCrypto
.createShamirsSecretShares(
randomPrivKey, randomThreshold,
randomThreshold))
var randomPrivKey = floCrypto.generateNewID().privKey
var randomThreshold = floCrypto.randInt(10, 20)
var randomShares = floCrypto.createShamirsSecretShares(randomPrivKey, randomThreshold, randomThreshold)
writeSharesToIDB(randomShares)
//resolve private Key
resolve(privKey)
}).catch(error => reject(error))
})
})
}
})
@ -8971,8 +8960,7 @@ Bitcoin.Util = {
} catch (error) {
reject("Access Denied: Incorrect PIN/Password")
}
}).catch(error => reject(
"Access Denied: PIN/Password required"))
}).catch(error => reject("Access Denied: PIN/Password required"))
}
})
}
@ -9005,15 +8993,11 @@ Bitcoin.Util = {
return new Promise((resolve, reject) => {
this.startUpFunctions[fname]().then(result => {
this.callStartUpFunction.completed += 1
this.startUpLog(true,
`${result}\nCompleted ${this.callStartUpFunction.completed}/${this.callStartUpFunction.total} Startup functions`
)
this.startUpLog(true, `${result}\nCompleted ${this.callStartUpFunction.completed}/${this.callStartUpFunction.total} Startup functions`)
resolve(true)
}).catch(error => {
this.callStartUpFunction.failed += 1
this.startUpLog(false,
`${error}\nFailed ${this.callStartUpFunction.failed}/${this.callStartUpFunction.total} Startup functions`
)
this.startUpLog(false, `${error}\nFailed ${this.callStartUpFunction.failed}/${this.callStartUpFunction.total} Startup functions`)
reject(false)
})
})
@ -9024,8 +9008,7 @@ Bitcoin.Util = {
return new Promise((resolve, reject) => {
this.util.initIndexedDB().then(log => {
console.log(log)
this.util.callStartUpFunction.total = Object.keys(this.util
.startUpFunctions).length
this.util.callStartUpFunction.total = Object.keys(this.util.startUpFunctions).length
this.util.callStartUpFunction.completed = 0
this.util.callStartUpFunction.failed = 0
var p0 = []
@ -9165,8 +9148,7 @@ Bitcoin.Util = {
return new Promise((resolve, reject) => {
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;
if (!settings || typeof settings !== "object" || !Object.keys(settings).length) settings = undefined;
if (!addList && !rmList && !settings)
return reject("No configuration change")
var floData = {
@ -9213,8 +9195,10 @@ Bitcoin.Util = {
compactIDB.deleteDB(floGlobals.application).then(result => {
localStorage.removeItem(`${floGlobals.application}#privKey`)
myPrivKey = myPubKey = myFloID = undefined;
resolve("App database(local) deleted")
})
compactIDB.removeData('lastTx', `${floGlobals.application}|${floGlobals.adminID}`, 'floDapps')
.then(result => resolve("App database(local) deleted"))
.catch(error => reject(error))
}).catch(error => reject(error))
})
},
@ -9308,9 +9292,8 @@ Bitcoin.Util = {
let vc = Object.keys(response).sort().pop()
let sync = JSON.parse(Crypto.AES.decrypt(response[vc].message, myPrivKey))
let promises = []
let store = (key, val, obs) =>
promises.push(compactIDB.writeData(obs, val, key, `floDapps#${floID}`))[
"contacts", "pubKeys", "messages"].forEach(c => {
let store = (key, val, obs) => promises.push(compactIDB.writeData(obs, val, key, `floDapps#${floID}`));
["contacts", "pubKeys", "messages"].forEach(c => {
for (let i in sync[c]) {
store(i, sync[c][i], c)
floGlobals[c][i] = sync[c][i]