Compare commits
No commits in common. "master" and "v3.0.1b-blockbook" have entirely different histories.
master
...
v3.0.1b-bl
809
btcOperator.js
809
btcOperator.js
File diff suppressed because it is too large
Load Diff
1
btcOperator.min.js
vendored
1
btcOperator.min.js
vendored
File diff suppressed because one or more lines are too long
1
compactIDB.min.js
vendored
1
compactIDB.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floBlockchainAPI v3.1.4
|
(function (EXPORTS) { //floBlockchainAPI v3.0.1b
|
||||||
/* FLO Blockchain Operator to send/receive data from blockchain using API calls via FLO Blockbook*/
|
/* FLO Blockchain Operator to send/receive data from blockchain using API calls via FLO Blockbook*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floBlockchainAPI = EXPORTS;
|
const floBlockchainAPI = EXPORTS;
|
||||||
@ -7,7 +7,7 @@
|
|||||||
blockchain: floGlobals.blockchain,
|
blockchain: floGlobals.blockchain,
|
||||||
apiURL: {
|
apiURL: {
|
||||||
FLO: ['https://blockbook.ranchimall.net/'],
|
FLO: ['https://blockbook.ranchimall.net/'],
|
||||||
FLO_TEST: ['https://blockbook-testnet.ranchimall.net/']
|
FLO_TEST: []
|
||||||
},
|
},
|
||||||
sendAmt: 0.0003,
|
sendAmt: 0.0003,
|
||||||
fee: 0.0002,
|
fee: 0.0002,
|
||||||
@ -15,29 +15,9 @@
|
|||||||
receiverID: floGlobals.adminID
|
receiverID: floGlobals.adminID
|
||||||
};
|
};
|
||||||
|
|
||||||
floBlockchainAPI.apiURL = DEFAULT.apiURL[DEFAULT.blockchain][0];
|
|
||||||
const SATOSHI_IN_BTC = 1e8;
|
const SATOSHI_IN_BTC = 1e8;
|
||||||
const isUndefined = val => typeof val === 'undefined';
|
const isUndefined = val => typeof val === 'undefined';
|
||||||
|
|
||||||
const checkIfTor = floBlockchainAPI.checkIfTor = () => {
|
|
||||||
return fetch('https://check.torproject.org/api/ip')
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(res => {
|
|
||||||
return res.IsTor
|
|
||||||
}).catch(e => {
|
|
||||||
console.error(e)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
let isTor = false;
|
|
||||||
checkIfTor().then(result => {
|
|
||||||
isTor = result
|
|
||||||
if (isTor) {
|
|
||||||
DEFAULT.apiURL.FLO.push('http://xge4kejxl6xs4cad3u3a7dnw7idndlkn3vmyo33t3a4ctk566y65eoad.onion/')
|
|
||||||
DEFAULT.apiURL.FLO_TEST.push('http://fdjrsde2qhfecvx6fkgmcidwkp34bdek7jo4y2fpqatrhzxtxkk6f4ad.onion/')
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const util = floBlockchainAPI.util = {};
|
const util = floBlockchainAPI.util = {};
|
||||||
|
|
||||||
util.Sat_to_FLO = value => parseFloat((value / SATOSHI_IN_BTC).toFixed(8));
|
util.Sat_to_FLO = value => parseFloat((value / SATOSHI_IN_BTC).toFixed(8));
|
||||||
|
|||||||
1
floBlockchainAPI.min.js
vendored
1
floBlockchainAPI.min.js
vendored
File diff suppressed because one or more lines are too long
304
floCloudAPI.js
304
floCloudAPI.js
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floCloudAPI v2.4.5a
|
(function (EXPORTS) { //floCloudAPI v2.4.3a
|
||||||
/* 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;
|
||||||
@ -195,24 +195,14 @@
|
|||||||
floCloudAPI.init = function startCloudProcess(nodes) {
|
floCloudAPI.init = function startCloudProcess(nodes) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
// accept only plain-ish objects
|
|
||||||
nodes = (nodes && typeof nodes === 'object' && !Array.isArray(nodes)) ? nodes : {};
|
|
||||||
|
|
||||||
supernodes = nodes;
|
supernodes = nodes;
|
||||||
|
|
||||||
// reset liveness bookkeeping for the new set
|
|
||||||
if (_inactive && typeof _inactive.clear === 'function') _inactive.clear();
|
|
||||||
|
|
||||||
// (re)build the bucket with current IDs
|
|
||||||
kBucket = new K_Bucket(DEFAULT.SNStorageID, Object.keys(supernodes));
|
kBucket = new K_Bucket(DEFAULT.SNStorageID, Object.keys(supernodes));
|
||||||
|
|
||||||
resolve('Cloud init successful');
|
resolve('Cloud init successful');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
Object.defineProperty(floCloudAPI, 'kBucket', {
|
Object.defineProperty(floCloudAPI, 'kBucket', {
|
||||||
get: () => kBucket
|
get: () => kBucket
|
||||||
@ -237,27 +227,24 @@
|
|||||||
|
|
||||||
function ws_activeConnect(snID, reverse = false) {
|
function ws_activeConnect(snID, reverse = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// Safe guard: uninitialized kBucket, empty list, or all inactive
|
if (_inactive.size === kBucket.list.length)
|
||||||
if (!kBucket || !kBucket.list || !kBucket.list.length || _inactive.size === kBucket.list.length)
|
|
||||||
return reject('Cloud offline');
|
return reject('Cloud offline');
|
||||||
|
if (!(snID in supernodes))
|
||||||
if (!(snID in supernodes)) {
|
snID = kBucket.closestNode(proxyID(snID));
|
||||||
var closest = kBucket.closestNode(proxyID(snID));
|
|
||||||
if (!closest) return reject('Cloud offline'); // no candidate to try
|
|
||||||
snID = closest;
|
|
||||||
}
|
|
||||||
|
|
||||||
ws_connect(snID)
|
ws_connect(snID)
|
||||||
.then(node => resolve(node))
|
.then(node => resolve(node))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
var nxtNode = reverse ? kBucket.prevNode(snID) : kBucket.nextNode(snID);
|
if (reverse)
|
||||||
if (!nxtNode || nxtNode === snID) return reject('Cloud offline'); // nothing else to try
|
var nxtNode = kBucket.prevNode(snID);
|
||||||
ws_activeConnect(nxtNode, reverse).then(resolve).catch(reject);
|
else
|
||||||
});
|
var nxtNode = kBucket.nextNode(snID);
|
||||||
});
|
ws_activeConnect(nxtNode, reverse)
|
||||||
|
.then(node => resolve(node))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function fetch_API(snID, data) {
|
function fetch_API(snID, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (_inactive.has(snID))
|
if (_inactive.has(snID))
|
||||||
@ -278,28 +265,25 @@
|
|||||||
|
|
||||||
function fetch_ActiveAPI(snID, data, reverse = false) {
|
function fetch_ActiveAPI(snID, data, reverse = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// Safe guard: uninitialized kBucket, empty list, or all inactive
|
if (_inactive.size === kBucket.list.length)
|
||||||
if (!kBucket || !kBucket.list || !kBucket.list.length || _inactive.size === kBucket.list.length)
|
|
||||||
return reject('Cloud offline');
|
return reject('Cloud offline');
|
||||||
|
if (!(snID in supernodes))
|
||||||
if (!(snID in supernodes)) {
|
snID = kBucket.closestNode(proxyID(snID));
|
||||||
var closest = kBucket.closestNode(proxyID(snID));
|
|
||||||
if (!closest) return reject('Cloud offline'); // no candidate available
|
|
||||||
snID = closest;
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch_API(snID, data)
|
fetch_API(snID, data)
|
||||||
.then(resolve)
|
.then(result => resolve(result))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
_inactive.add(snID);
|
_inactive.add(snID)
|
||||||
var nxtNode = reverse ? kBucket.prevNode(snID) : kBucket.nextNode(snID);
|
if (reverse)
|
||||||
if (!nxtNode || nxtNode === snID) return reject('Cloud offline'); // nothing else to try
|
var nxtNode = kBucket.prevNode(snID);
|
||||||
fetch_ActiveAPI(nxtNode, data, reverse).then(resolve).catch(reject);
|
else
|
||||||
});
|
var nxtNode = kBucket.nextNode(snID);
|
||||||
});
|
fetch_ActiveAPI(nxtNode, data, reverse)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function singleRequest(floID, data_obj, method = "POST") {
|
function singleRequest(floID, data_obj, method = "POST") {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data;
|
let data;
|
||||||
@ -390,31 +374,13 @@
|
|||||||
|
|
||||||
const util = floCloudAPI.util = {};
|
const util = floCloudAPI.util = {};
|
||||||
|
|
||||||
//Updating encoding/Decoding to modern standards
|
|
||||||
const encodeMessage = util.encodeMessage = function (message) {
|
const encodeMessage = util.encodeMessage = function (message) {
|
||||||
const bytes = new TextEncoder().encode(JSON.stringify(message));
|
return btoa(unescape(encodeURIComponent(JSON.stringify(message))))
|
||||||
let bin = "";
|
}
|
||||||
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
|
||||||
return btoa(bin);
|
|
||||||
};
|
|
||||||
|
|
||||||
const decodeMessage = util.decodeMessage = function (message) {
|
const decodeMessage = util.decodeMessage = function (message) {
|
||||||
try {
|
return JSON.parse(decodeURIComponent(escape(atob(message))))
|
||||||
// try modern decode first
|
}
|
||||||
const bin = atob(message);
|
|
||||||
const bytes = new Uint8Array(bin.length);
|
|
||||||
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
|
||||||
return JSON.parse(new TextDecoder().decode(bytes));
|
|
||||||
} catch (e1) {
|
|
||||||
try {
|
|
||||||
// fallback to legacy decode
|
|
||||||
return JSON.parse(decodeURIComponent(escape(atob(message))));
|
|
||||||
} catch (e2) {
|
|
||||||
// final fallback: return raw string to avoid hard crash
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const filterKey = util.filterKey = function (type, options = {}) {
|
const filterKey = util.filterKey = function (type, options = {}) {
|
||||||
return type + (options.comment ? ':' + options.comment : '') +
|
return type + (options.comment ? ':' + options.comment : '') +
|
||||||
@ -502,59 +468,21 @@
|
|||||||
|
|
||||||
function storeGeneral(fk, dataSet) {
|
function storeGeneral(fk, dataSet) {
|
||||||
try {
|
try {
|
||||||
if (!dataSet || typeof dataSet !== "object") return;
|
console.log(dataSet)
|
||||||
|
if (typeof generalData[fk] !== "object")
|
||||||
// Ensure containers exist
|
generalData[fk] = {}
|
||||||
if (typeof generalData[fk] !== "object" || generalData[fk] === null)
|
for (let vc in dataSet) {
|
||||||
generalData[fk] = {};
|
generalData[fk][vc] = dataSet[vc];
|
||||||
if (typeof lastVC[fk] !== "number")
|
if (dataSet[vc].log_time > lastVC[fk])
|
||||||
lastVC[fk] = 0;
|
lastVC[fk] = dataSet[vc].log_time;
|
||||||
|
|
||||||
// Merge data and track latest log_time
|
|
||||||
let updated = false;
|
|
||||||
let newLast = lastVC[fk];
|
|
||||||
|
|
||||||
for (const vc in dataSet) {
|
|
||||||
const rec = dataSet[vc];
|
|
||||||
// Skip bad records
|
|
||||||
if (!rec || typeof rec !== "object") continue;
|
|
||||||
|
|
||||||
// Assign only if changed reference (cheap check)
|
|
||||||
if (generalData[fk][vc] !== rec) {
|
|
||||||
generalData[fk][vc] = rec;
|
|
||||||
updated = true;
|
|
||||||
}
|
|
||||||
if (typeof rec.log_time === "number" && rec.log_time > newLast) {
|
|
||||||
newLast = rec.log_time;
|
|
||||||
updated = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
compactIDB.writeData("lastVC", lastVC[fk], fk)
|
||||||
if (!updated) return; // nothing new, avoid IDB writes
|
compactIDB.writeData("generalData", generalData[fk], fk)
|
||||||
|
|
||||||
lastVC[fk] = newLast;
|
|
||||||
|
|
||||||
// --- Debounce writes per fk to avoid IDB thrash ---
|
|
||||||
storeGeneral._pending = storeGeneral._pending || Object.create(null);
|
|
||||||
const pend = storeGeneral._pending;
|
|
||||||
|
|
||||||
clearTimeout(pend[fk]);
|
|
||||||
pend[fk] = setTimeout(() => {
|
|
||||||
try {
|
|
||||||
// Fire-and-forget; callers don’t wait on these
|
|
||||||
compactIDB.writeData("lastVC", lastVC[fk], fk);
|
|
||||||
compactIDB.writeData("generalData", generalData[fk], fk);
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
}, 50);
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function objectifier(data) {
|
function objectifier(data) {
|
||||||
if (!Array.isArray(data))
|
if (!Array.isArray(data))
|
||||||
data = [data];
|
data = [data];
|
||||||
@ -623,7 +551,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
//request any data from supernode cloud
|
//request any data from supernode cloud
|
||||||
const _requestApplicationData = function (type, options = {}) {
|
const requestApplicationData = floCloudAPI.requestApplicationData = function (type, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var request = {
|
var request = {
|
||||||
receiverID: options.receiverID || DEFAULT.adminID,
|
receiverID: options.receiverID || DEFAULT.adminID,
|
||||||
@ -654,17 +582,6 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
floCloudAPI.requestApplicationData = function (type, options = {}) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let single_request_mode = !(options.callback instanceof Function);
|
|
||||||
_requestApplicationData(type, options).then(data => {
|
|
||||||
if (single_request_mode)
|
|
||||||
resolve(objectifier(data))
|
|
||||||
else resolve(data);
|
|
||||||
}).catch(error => reject(error))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/*(NEEDS UPDATE)
|
/*(NEEDS UPDATE)
|
||||||
//delete data from supernode cloud (received only)
|
//delete data from supernode cloud (received only)
|
||||||
floCloudAPI.deleteApplicationData = function(vectorClocks, options = {}) {
|
floCloudAPI.deleteApplicationData = function(vectorClocks, options = {}) {
|
||||||
@ -692,39 +609,49 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//edit comment of data in supernode cloud (sender only)
|
/*(NEEDS UPDATE)
|
||||||
floCloudAPI.editApplicationData = function (vectorClock, comment_edit, options = {}) {
|
//edit comment of data in supernode cloud (mutable comments only)
|
||||||
|
floCloudAPI.editApplicationData = function(vectorClock, newComment, oldData, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
//request the data from cloud for resigning
|
let p0
|
||||||
let req_options = Object.assign({}, options);
|
if (!oldData) {
|
||||||
req_options.atVectorClock = vectorClock;
|
options.atVectorClock = vectorClock;
|
||||||
_requestApplicationData(undefined, req_options).then(result => {
|
options.callback = false;
|
||||||
if (!result.length)
|
p0 = requestApplicationData(false, options)
|
||||||
return reject("Data not found");
|
} else
|
||||||
let data = result[0];
|
p0 = Promise.resolve({
|
||||||
if (data.senderID !== user.id)
|
vectorClock: {
|
||||||
return reject("Only sender can edit comment");
|
...oldData
|
||||||
data.comment = comment_edit;
|
}
|
||||||
let hashcontent = ["receiverID", "time", "application", "type", "message", "comment"]
|
})
|
||||||
.map(d => data[d]).join("|");
|
p0.then(d => {
|
||||||
let re_sign = user.sign(hashcontent);
|
if (d.senderID != user.id)
|
||||||
var request = {
|
return reject("Invalid requestorID")
|
||||||
receiverID: options.receiverID || DEFAULT.adminID,
|
else if (!d.comment.startsWith("EDIT:"))
|
||||||
|
return reject("Data immutable")
|
||||||
|
let data = {
|
||||||
requestorID: user.id,
|
requestorID: user.id,
|
||||||
pubKey: user.public,
|
receiverID: d.receiverID,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
vectorClock: vectorClock,
|
application: d.application,
|
||||||
edit: comment_edit,
|
edit: {
|
||||||
re_sign: re_sign
|
vectorClock: vectorClock,
|
||||||
|
comment: newComment
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let request_hash = ["time", "vectorClock", "edit", "re_sign"].map(d => request[d]).join("|");
|
d.comment = data.edit.comment;
|
||||||
request.sign = user.sign(request_hash);
|
let hashcontent = ["receiverID", "time", "application", "type", "message",
|
||||||
singleRequest(request.receiverID, request)
|
"comment"
|
||||||
.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 = {}) {
|
||||||
@ -792,11 +719,11 @@
|
|||||||
storeGeneral(fk, d);
|
storeGeneral(fk, d);
|
||||||
options.callback(d, e)
|
options.callback(d, e)
|
||||||
}
|
}
|
||||||
_requestApplicationData(type, new_options)
|
requestApplicationData(type, new_options)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
} else {
|
} else {
|
||||||
_requestApplicationData(type, options).then(dataSet => {
|
requestApplicationData(type, options).then(dataSet => {
|
||||||
storeGeneral(fk, objectifier(dataSet))
|
storeGeneral(fk, objectifier(dataSet))
|
||||||
resolve(dataSet)
|
resolve(dataSet)
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
@ -821,7 +748,7 @@
|
|||||||
}
|
}
|
||||||
delete options.callback;
|
delete options.callback;
|
||||||
}
|
}
|
||||||
_requestApplicationData(objectName, options).then(dataSet => {
|
requestApplicationData(objectName, options).then(dataSet => {
|
||||||
updateObject(objectName, objectifier(dataSet));
|
updateObject(objectName, objectifier(dataSet));
|
||||||
delete options.comment;
|
delete options.comment;
|
||||||
options.lowerVectorClock = lastVC[objectName] + 1;
|
options.lowerVectorClock = lastVC[objectName] + 1;
|
||||||
@ -829,11 +756,11 @@
|
|||||||
if (callback) {
|
if (callback) {
|
||||||
let new_options = Object.create(options);
|
let new_options = Object.create(options);
|
||||||
new_options.callback = callback;
|
new_options.callback = callback;
|
||||||
_requestApplicationData(objectName, new_options)
|
requestApplicationData(objectName, new_options)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
} else {
|
} else {
|
||||||
_requestApplicationData(objectName, options).then(dataSet => {
|
requestApplicationData(objectName, options).then(dataSet => {
|
||||||
updateObject(objectName, objectifier(dataSet))
|
updateObject(objectName, objectifier(dataSet))
|
||||||
resolve(appObjects[objectName])
|
resolve(appObjects[objectName])
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
@ -884,67 +811,6 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//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
|
||||||
|
|||||||
1
floCloudAPI.min.js
vendored
1
floCloudAPI.min.js
vendored
File diff suppressed because one or more lines are too long
1
floCrypto.min.js
vendored
1
floCrypto.min.js
vendored
File diff suppressed because one or more lines are too long
204
floDapps.js
204
floDapps.js
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floDapps v2.4.1
|
(function (EXPORTS) { //floDapps v2.4.0
|
||||||
/* General functions for FLO Dapps*/
|
/* General functions for FLO Dapps*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floDapps = EXPORTS;
|
const floDapps = EXPORTS;
|
||||||
@ -144,7 +144,7 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var subAdmins = [], trustedIDs = [], settings = {};
|
var subAdmins, trustedIDs, settings;
|
||||||
Object.defineProperties(floGlobals, {
|
Object.defineProperties(floGlobals, {
|
||||||
subAdmins: {
|
subAdmins: {
|
||||||
get: () => subAdmins
|
get: () => subAdmins
|
||||||
@ -172,7 +172,12 @@
|
|||||||
//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
|
||||||
@ -249,154 +254,103 @@
|
|||||||
const startUpFunctions = [];
|
const startUpFunctions = [];
|
||||||
|
|
||||||
startUpFunctions.push(function readSupernodeListFromAPI() {
|
startUpFunctions.push(function readSupernodeListFromAPI() {
|
||||||
return new Promise((resolve) => {
|
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;
|
var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
|
||||||
|
if (typeof lastTx == 'number') //lastTx is tx count (*backward support)
|
||||||
// Fallback: init from cached nodes (never reject)
|
|
||||||
const initFromCache = (tag) =>
|
|
||||||
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root)
|
|
||||||
.then(nodes => {
|
|
||||||
nodes = nodes || {};
|
|
||||||
return floCloudAPI.init(nodes)
|
|
||||||
.then(r => resolve(`${tag} (from cache)\n${r}`))
|
|
||||||
.catch(() => resolve(`${tag} (cache present, init skipped)`));
|
|
||||||
})
|
|
||||||
.catch(() => resolve(`${tag} (no cache)`));
|
|
||||||
|
|
||||||
compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => {
|
|
||||||
const query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
|
|
||||||
if (typeof lastTx === 'number') // backward support (tx count)
|
|
||||||
query_options.ignoreOld = lastTx;
|
query_options.ignoreOld = lastTx;
|
||||||
else if (typeof lastTx === 'string') // last txid
|
else if (typeof lastTx == 'string') //lastTx is txid of last tx
|
||||||
query_options.after = lastTx;
|
query_options.after = lastTx;
|
||||||
|
//fetch data from flosight
|
||||||
// Try online; if it fails, fall back to cache
|
|
||||||
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
|
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
|
||||||
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => {
|
for (var i = result.data.length - 1; i >= 0; i--) {
|
||||||
nodes = nodes || {};
|
var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
|
||||||
for (let i = result.data.length - 1; i >= 0; i--) {
|
for (let sn in content.removeNodes)
|
||||||
const content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
|
compactIDB.removeData("supernodes", sn, DEFAULT.root);
|
||||||
if (!content || typeof content !== 'object') continue;
|
for (let sn in content.newNodes)
|
||||||
if (content.removeNodes)
|
compactIDB.writeData("supernodes", content.newNodes[sn], sn, DEFAULT.root);
|
||||||
for (let sn in content.removeNodes) delete nodes[sn];
|
for (let sn in content.updateNodes)
|
||||||
if (content.newNodes)
|
compactIDB.readData("supernodes", sn, DEFAULT.root).then(r => {
|
||||||
for (let sn in content.newNodes) nodes[sn] = content.newNodes[sn];
|
r = r || {}
|
||||||
if (content.updateNodes)
|
r.uri = content.updateNodes[sn];
|
||||||
for (let sn in content.updateNodes)
|
compactIDB.writeData("supernodes", r, sn, DEFAULT.root);
|
||||||
if (sn in nodes) 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(r => resolve("Loaded Supernode list\n" + r))
|
})
|
||||||
.catch(() => resolve("Loaded Supernode list (init deferred)"));
|
})
|
||||||
}).catch(() => resolve("Supernode list updated (persist partial)"));
|
}).catch(error => reject(error))
|
||||||
}).catch(() => initFromCache("Supernode list read failed"));
|
})
|
||||||
}).catch(() => initFromCache("Supernode network fetch failed"));
|
|
||||||
}).catch(() => initFromCache("Supernode lastTx read failed"));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
startUpFunctions.push(function readAppConfigFromAPI() {
|
startUpFunctions.push(function readAppConfigFromAPI() {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!startUpOptions.app_config)
|
if (!startUpOptions.app_config)
|
||||||
return resolve("No configs for this app");
|
return resolve("No configs for this app");
|
||||||
|
compactIDB.readData("lastTx", `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root).then(lastTx => {
|
||||||
// small helper: load cached directives into memory and resolve
|
var query_options = { sentOnly: true, pattern: DEFAULT.application };
|
||||||
const loadFromIDB = (msg) => Promise.all([
|
if (typeof lastTx == 'number') //lastTx is tx count (*backward support)
|
||||||
compactIDB.readAllData("subAdmins"),
|
query_options.ignoreOld = lastTx;
|
||||||
compactIDB.readAllData("trustedIDs"),
|
else if (typeof lastTx == 'string') //lastTx is txid of last tx
|
||||||
compactIDB.readAllData("settings")
|
query_options.after = lastTx;
|
||||||
]).then(([sub, trust, set]) => {
|
//fetch data from flosight
|
||||||
subAdmins = Object.keys(sub || {}); // arrays of IDs
|
|
||||||
trustedIDs = Object.keys(trust || {});
|
|
||||||
settings = set || {};
|
|
||||||
resolve(msg);
|
|
||||||
}).catch(() => {
|
|
||||||
// safe defaults if cache missing
|
|
||||||
subAdmins = []; trustedIDs = []; settings = {};
|
|
||||||
resolve(msg + " (no local cache)");
|
|
||||||
});
|
|
||||||
|
|
||||||
// If cloud is disabled, use cached config and move on
|
|
||||||
if (!startUpOptions.cloud)
|
|
||||||
return loadFromIDB("Read app configuration from local cache (offline)");
|
|
||||||
|
|
||||||
const lastKey = `${DEFAULT.application}|${DEFAULT.adminID}`;
|
|
||||||
|
|
||||||
// Try to read lastTx; on failure, just use cache (don’t block startup)
|
|
||||||
compactIDB.readData("lastTx", lastKey, DEFAULT.root).then(lastTx => {
|
|
||||||
const query_options = { sentOnly: true, pattern: DEFAULT.application };
|
|
||||||
if (typeof lastTx === 'number') query_options.ignoreOld = lastTx;
|
|
||||||
else if (typeof lastTx === 'string') query_options.after = lastTx;
|
|
||||||
|
|
||||||
// Fetch deltas from chain; on failure, fall back to cache
|
|
||||||
floBlockchainAPI.readData(DEFAULT.adminID, query_options).then(result => {
|
floBlockchainAPI.readData(DEFAULT.adminID, query_options).then(result => {
|
||||||
for (let i = result.data.length - 1; i >= 0; i--) {
|
for (var i = result.data.length - 1; i >= 0; i--) {
|
||||||
const content = JSON.parse(result.data[i])[DEFAULT.application];
|
var content = JSON.parse(result.data[i])[DEFAULT.application];
|
||||||
if (!content || typeof content !== "object") continue;
|
if (!content || typeof content !== "object")
|
||||||
|
continue;
|
||||||
if (Array.isArray(content.removeSubAdmin))
|
if (Array.isArray(content.removeSubAdmin))
|
||||||
for (let j = 0; j < content.removeSubAdmin.length; j++)
|
for (var j = 0; j < content.removeSubAdmin.length; j++)
|
||||||
compactIDB.removeData("subAdmins", content.removeSubAdmin[j]);
|
compactIDB.removeData("subAdmins", content.removeSubAdmin[j]);
|
||||||
|
|
||||||
if (Array.isArray(content.addSubAdmin))
|
if (Array.isArray(content.addSubAdmin))
|
||||||
for (let 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))
|
if (Array.isArray(content.removeTrustedID))
|
||||||
for (let j = 0; j < content.removeTrustedID.length; j++)
|
for (var j = 0; j < content.removeTrustedID.length; j++)
|
||||||
compactIDB.removeData("trustedIDs", content.removeTrustedID[j]);
|
compactIDB.removeData("trustedIDs", content.removeTrustedID[j]);
|
||||||
|
|
||||||
if (Array.isArray(content.addTrustedID))
|
if (Array.isArray(content.addTrustedID))
|
||||||
for (let k = 0; k < content.addTrustedID.length; k++)
|
for (var k = 0; k < content.addTrustedID.length; k++)
|
||||||
compactIDB.writeData("trustedIDs", true, content.addTrustedID[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)
|
||||||
}
|
}
|
||||||
|
compactIDB.writeData("lastTx", result.lastItem, `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root);
|
||||||
// persist last item marker (best effort)
|
compactIDB.readAllData("subAdmins").then(result => {
|
||||||
compactIDB.writeData("lastTx", result.lastItem, lastKey, DEFAULT.root).catch(() => {});
|
subAdmins = Object.keys(result);
|
||||||
|
compactIDB.readAllData("trustedIDs").then(result => {
|
||||||
// load fresh values from IDB into memory and finish
|
trustedIDs = Object.keys(result);
|
||||||
loadFromIDB("Read app configuration from blockchain");
|
compactIDB.readAllData("settings").then(result => {
|
||||||
}).catch(() => {
|
settings = result;
|
||||||
// network failed → boot from cache
|
resolve("Read app configuration from blockchain");
|
||||||
loadFromIDB("Read app configuration from local cache (network fail)");
|
})
|
||||||
});
|
})
|
||||||
}).catch(() => {
|
})
|
||||||
// couldn't read lastTx → still boot from cache
|
})
|
||||||
loadFromIDB("Read app configuration from local cache (no lastTx)");
|
}).catch(error => reject(error))
|
||||||
});
|
})
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
startUpFunctions.push(function loadDataFromAppIDB() {
|
startUpFunctions.push(function loadDataFromAppIDB() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const loadData = ["appObjects", "generalData", "lastVC"];
|
if (!startUpOptions.cloud)
|
||||||
|
return resolve("No cloud for this app");
|
||||||
// If cloud is disabled AND no IDB stores are expected, skip early
|
var loadData = ["appObjects", "generalData", "lastVC"]
|
||||||
if (!startUpOptions.cloud && (!initIndexedDB.appObs || Object.keys(initIndexedDB.appObs).length === 0))
|
var promises = []
|
||||||
return resolve("No cloud and no local data to load");
|
for (var i = 0; i < loadData.length; i++)
|
||||||
|
promises[i] = compactIDB.readAllData(loadData[i])
|
||||||
// Otherwise, read from IDB
|
Promise.all(promises).then(results => {
|
||||||
Promise.all(loadData.map(item => compactIDB.readAllData(item)))
|
for (var i = 0; i < loadData.length; i++)
|
||||||
.then(results => {
|
floGlobals[loadData[i]] = results[i]
|
||||||
for (let i = 0; i < loadData.length; i++)
|
resolve("Loaded Data from app IDB")
|
||||||
floGlobals[loadData[i]] = results[i];
|
}).catch(error => reject(error))
|
||||||
resolve("Loaded Data from app IDB");
|
})
|
||||||
})
|
|
||||||
.catch(error => reject(error));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var keyInput = type => new Promise((resolve, reject) => {
|
var keyInput = type => new Promise((resolve, reject) => {
|
||||||
|
|||||||
1
floDapps.min.js
vendored
1
floDapps.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
1
floTokenAPI.min.js
vendored
1
floTokenAPI.min.js
vendored
File diff suppressed because one or more lines are too long
16
index.html
16
index.html
@ -26,14 +26,14 @@
|
|||||||
|
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
<script src="lib.js" type="text/javascript"></script>
|
<script src="lib.js"></script>
|
||||||
<script src="floCrypto.js" type="text/javascript"></script>
|
<script src="floCrypto.js"></script>
|
||||||
<script src="btcOperator.js" type="text/javascript"></script>
|
<script src="btcOperator.js"></script>
|
||||||
<script src="floBlockchainAPI.js" type="text/javascript"></script>
|
<script src="floBlockchainAPI.js"></script>
|
||||||
<script src="floTokenAPI.js" type="text/javascript"></script>
|
<script src="floTokenAPI.js"></script>
|
||||||
<script src="compactIDB.js" type="text/javascript"></script>
|
<script src="compactIDB.js"></script>
|
||||||
<script src="floCloudAPI.js" type="text/javascript"></script>
|
<script src="floCloudAPI.js"></script>
|
||||||
<script src="floDapps.js" type="text/javascript"></script>
|
<script src="floDapps.js"></script>
|
||||||
<script id="onLoadStartUp">
|
<script id="onLoadStartUp">
|
||||||
function onLoadStartUp() {
|
function onLoadStartUp() {
|
||||||
if (window.quick) return;
|
if (window.quick) return;
|
||||||
|
|||||||
57
lib.min.js
vendored
57
lib.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user