Adding reload API data button
The reload API data button (top-right) refreshes the API for new data The refreshed API data are added to memory as well as stored in IDB. The kBucket is reloaded
This commit is contained in:
parent
174b9fddf6
commit
20c8f124e5
600
app/web/app.js
600
app/web/app.js
@ -148,7 +148,7 @@ var encrypt = {
|
|||||||
getPubKeyHex: function(privateKeyHex){
|
getPubKeyHex: function(privateKeyHex){
|
||||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
if(key.priv == null){
|
if(key.priv == null){
|
||||||
alert("Invalid Private key");
|
console.log("Invalid Private key");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
key.setCompressed(true);
|
key.setCompressed(true);
|
||||||
@ -287,9 +287,349 @@ function logout(){
|
|||||||
window.location.href = "index.html";
|
window.location.href = "index.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Refresh profile and superNode data from API */
|
||||||
|
function ajax(method, uri){
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
var url = `${api_url}/${uri}`
|
||||||
|
console.log(url)
|
||||||
|
var result;
|
||||||
|
request.open(method,url , false);
|
||||||
|
request.onload = function () {
|
||||||
|
if (request.readyState == 4 && request.status == 200)
|
||||||
|
result = this.response;
|
||||||
|
else {
|
||||||
|
console.log('error');
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
request.send();
|
||||||
|
console.log(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function reloadInitData(){
|
||||||
|
refreshAPIdata.then(result => {
|
||||||
|
console.log(result);
|
||||||
|
kBucketObj.launchKBucket().then(result => {
|
||||||
|
console.log(result)
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function refreshAPIdata(){
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
var addr = adminID;
|
||||||
|
var idb = indexedDB.open("FLO_Tweet");
|
||||||
|
idb.onerror = (event) => { reject("Error in opening IndexedDB!") };
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var lastTx = db.transaction('lastTx', "readwrite").objectStore('lastTx');
|
||||||
|
console.log(addr);
|
||||||
|
new Promise((res,rej) => {
|
||||||
|
var lastTxReq = lastTx.get(addr);
|
||||||
|
lastTxReq.onsuccess = (event) => {
|
||||||
|
var lasttx = event.target.result;
|
||||||
|
if(lasttx === undefined){
|
||||||
|
lasttx = 0;
|
||||||
|
}
|
||||||
|
res(lasttx);
|
||||||
|
}
|
||||||
|
}).then(lasttx => {
|
||||||
|
var response = ajax("GET",`api/addrs/${addr}/txs`);
|
||||||
|
var nRequired = JSON.parse(response).totalItems - lasttx;
|
||||||
|
console.log(nRequired);
|
||||||
|
while(true && nRequired){
|
||||||
|
var response = ajax("GET",`api/addrs/${addr}/txs?from=0&to=${nRequired}`);
|
||||||
|
response = JSON.parse(response);
|
||||||
|
if (nRequired + lasttx != response.totalItems ){
|
||||||
|
nRequired = response.totalItems - lasttx;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
response.items.reverse().forEach(tx => {
|
||||||
|
try {
|
||||||
|
if (tx.vin[0].addr == addr){
|
||||||
|
var data = JSON.parse(tx.floData).FLO_Tweet_SuperNode;
|
||||||
|
if(data !== undefined){
|
||||||
|
if(data.addNodes)
|
||||||
|
for(var i=0; i<data.addNodes.length; i++)
|
||||||
|
superNodeList.add(data.addNodes[i])
|
||||||
|
if(data.removeNodes)
|
||||||
|
for(var i=0; i<data.removeNodes.length; i++)
|
||||||
|
superNodeList.delete(data.removeNodes[i])
|
||||||
|
storeSuperNodeData(data).then(function (response) {
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
var data = JSON.parse(tx.floData).FLO_Tweet;
|
||||||
|
if(data !== undefined){
|
||||||
|
if(encrypt.getFLOIDfromPubkeyHex(data.pubKey)!=tx.vin[0].addr)
|
||||||
|
throw("PublicKey doesnot match with floID")
|
||||||
|
data = {floID : tx.vin[0].addr, onionAddr : data.onionAddr, name : data.name, pubKey:data.pubKey};
|
||||||
|
profiles[data.floID] = {onionAddr : data.onionAddr, name : data.name, pubKey : data.pubKey};
|
||||||
|
storeProfile(data).then(function (response) {
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
//console.log(error)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var obs = db.transaction('lastTx', "readwrite").objectStore('lastTx');
|
||||||
|
obs.put(response.totalItems,addr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
db.close();
|
||||||
|
resolve('retrived data from API');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Common IDB functions */
|
||||||
|
function storeProfile(data){
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
var idb = indexedDB.open("FLO_Tweet");
|
||||||
|
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction('profiles', "readwrite").objectStore('profiles');
|
||||||
|
objectRequest = obs.put(data);
|
||||||
|
objectRequest.onerror = (event) => { reject(Error('Error occured: Unable to store data'))};
|
||||||
|
objectRequest.onsuccess = (event) => { resolve('Data saved OK') };
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function storeSuperNodeData(data){
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
var idb = indexedDB.open("FLO_Tweet");
|
||||||
|
idb.onerror = (event) => { reject("Error in opening IndexedDB!"); };
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction('superNodes', "readwrite").objectStore('superNodes');
|
||||||
|
if(data.addNodes)
|
||||||
|
for(var i=0; i<data.addNodes.length; i++)
|
||||||
|
obs.add(true,data.addNodes[i])
|
||||||
|
if(data.removeNodes)
|
||||||
|
for(var i=0; i<data.removeNodes.length; i++)
|
||||||
|
obs.delete(data.removeNodes[i])
|
||||||
|
db.close();
|
||||||
|
resolve('Updated superNodes list in IDB');
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function storeTweet(data,tid){
|
||||||
|
var idb = indexedDB.open("FLO_Tweet");
|
||||||
|
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction("tweets", "readwrite").objectStore("tweets");
|
||||||
|
data.tweetID = `${data.time}_${data.floID}`;
|
||||||
|
data.tid = tid;
|
||||||
|
obs.add(data);
|
||||||
|
var obsL = db.transaction("lastTweet", "readwrite").objectStore("lastTweet");
|
||||||
|
obsL.put(tid,data.floID);
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function storeMsg(data){
|
||||||
|
var idb = indexedDB.open("FLO_Tweet");
|
||||||
|
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction("messages", "readwrite").objectStore("messages");
|
||||||
|
data.msgID = `${data.time}_${data.floID}`;
|
||||||
|
obs.add(data);
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SuperNode functions */
|
||||||
|
function sendDataToSuperNode(floID,data){
|
||||||
|
kBucketObj.determineClosestSupernode(floID).then(result=>{
|
||||||
|
var superNodeWS = new WebSocket("ws://"+profiles[result[0].floID].onionAddr+"/ws");
|
||||||
|
superNodeWS.onopen = function(ev){
|
||||||
|
console.log(`Connected to ${floID}'s SuperNode!`);
|
||||||
|
superNodeWS.send(data);
|
||||||
|
};
|
||||||
|
superNodeWS.onerror = function(ev) {console.log(`${floid}'s SuperNode is offline!`);};
|
||||||
|
superNodeWS.onclose = function(ev) {console.log(`Disconnected from ${floid}'s SuperNode!`);};
|
||||||
|
}).catch(e => {
|
||||||
|
console.log(e.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function superNodeMode(data){
|
||||||
|
if(data.reqNewTweets){
|
||||||
|
kBucketObj.determineClosestSupernode(data.floID).then(result => {
|
||||||
|
if(result[0].floID == selfID)
|
||||||
|
SuperNode_sendTweetsFromIDB(data.floID,data.tid,data.requestor);
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}else if(data.newSuperNodeTweet){
|
||||||
|
kBucketObj.determineClosestSupernode(data.floID).then(result => {
|
||||||
|
if(result[0].floID == selfID)
|
||||||
|
SuperNode_storeSuperNodeTweet(data.data,data.tid);
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}else if(data.viaSuperNodeMsg){
|
||||||
|
kBucketObj.determineClosestSupernode(data.to).then(result => {
|
||||||
|
if(result[0].floID == selfID)
|
||||||
|
SuperNode_storeViaSuperNodeMsg(data.from,data.to,data.data);
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}else if(data.viaMsgreq){
|
||||||
|
kBucketObj.determineClosestSupernode(data.floID).then(result => {
|
||||||
|
if(result[0].floID == selfID)
|
||||||
|
SuperNode_sendviaMsgFromIDB(data.floID);
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function SuperNode_sendTweetsFromIDB(floID,tid,requestor){
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
var requestorWS = new WebSocket("ws://"+profiles[requestor].onionAddr+"/ws");
|
||||||
|
requestorWS.onopen = (event) => {
|
||||||
|
console.log(`sending ${floID} tweets to ${requestor} Server!`);
|
||||||
|
var idb = indexedDB.open("FLO_Tweet",2);
|
||||||
|
idb.onerror = (event) => { reject("Error in opening IndexedDB!") };
|
||||||
|
idb.onupgradeneeded = (event) => {
|
||||||
|
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
||||||
|
objectStore1.createIndex('floID', 'floID', { unique: false });
|
||||||
|
objectStore1.createIndex('tid', 'tid', { unique: false });
|
||||||
|
objectStore1.createIndex('data', 'data', { unique: false });
|
||||||
|
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement:true });
|
||||||
|
objectStore2.createIndex('from', 'from', { unique: false });
|
||||||
|
objectStore2.createIndex('to', 'to', { unique: false });
|
||||||
|
objectStore2.createIndex('data', 'data', { unique: false });
|
||||||
|
}
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction("superNodeTweet", "readwrite").objectStore("superNodeTweet");
|
||||||
|
var curReq = obs.openCursor();
|
||||||
|
curReq.onsuccess = (event) => {
|
||||||
|
var cursor = event.target.result;
|
||||||
|
if(cursor) {
|
||||||
|
if(cursor.value.floID == floID && cursor.value.tid > tid){
|
||||||
|
data = JSON.stringify({fromSuperNode:true, floID:cursor.value.floID,tid:cursor.value.tid,data:cursor.value.data})
|
||||||
|
requestorWS.send(data);
|
||||||
|
}
|
||||||
|
cursor.continue();
|
||||||
|
}else{
|
||||||
|
resolve("Displayed Tweets from IDB!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
curReq.onerror = (event) => { reject("Error in Reading tweets from IDB!") }
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
requestorWS.onerror = (event) => { console.log(`${requestor} Server is offline!`) };
|
||||||
|
requestorWS.onclose = (event) => { console.log(`Disconnected from ${requestor} Server!`) };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function SuperNode_storeSuperNodeTweet(data,tid){
|
||||||
|
var idb = indexedDB.open("FLO_Tweet",2);
|
||||||
|
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
||||||
|
idb.onupgradeneeded = (event) => {
|
||||||
|
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
||||||
|
objectStore1.createIndex('floID', 'floID', { unique: false });
|
||||||
|
objectStore1.createIndex('tid', 'tid', { unique: false });
|
||||||
|
objectStore1.createIndex('data', 'data', { unique: false });
|
||||||
|
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement:true });
|
||||||
|
objectStore2.createIndex('from', 'from', { unique: false });
|
||||||
|
objectStore2.createIndex('to', 'to', { unique: false });
|
||||||
|
objectStore2.createIndex('data', 'data', { unique: false });
|
||||||
|
}
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction("superNodeTweet", "readwrite").objectStore("superNodeTweet");
|
||||||
|
var parsedData = JSON.parse(data);
|
||||||
|
var tweetID = ''+parsedData.floID+'_'+parsedData.time;
|
||||||
|
obs.add({tweetID:tweetID,floID:parsedData.floID,tid:tid,data:data});
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function SuperNode_storeViaSuperNodeMsg(from,to,data){
|
||||||
|
var idb = indexedDB.open("FLO_Tweet",2);
|
||||||
|
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
||||||
|
idb.onupgradeneeded = (event) => {
|
||||||
|
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
||||||
|
objectStore1.createIndex('floID', 'floID', { unique: false });
|
||||||
|
objectStore1.createIndex('tid', 'tid', { unique: false });
|
||||||
|
objectStore1.createIndex('data', 'data', { unique: false });
|
||||||
|
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement :true });
|
||||||
|
objectStore2.createIndex('from', 'from', { unique: false });
|
||||||
|
objectStore2.createIndex('to', 'to', { unique: false });
|
||||||
|
objectStore2.createIndex('data', 'data', { unique: false });
|
||||||
|
}
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction("viaSuperNodeMsg", "readwrite").objectStore("viaSuperNodeMsg");
|
||||||
|
obs.add({from:from,to:to,data:data});
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function SuperNode_sendviaMsgFromIDB(floID){
|
||||||
|
var receiverWS = new WebSocket("ws://"+profiles[floID].onionAddr+"/ws");
|
||||||
|
receiverWS.onopen = (event) => {
|
||||||
|
var idb = indexedDB.open("FLO_Tweet",2);
|
||||||
|
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
||||||
|
idb.onupgradeneeded = (event) => {
|
||||||
|
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
||||||
|
objectStore1.createIndex('floID', 'floID', { unique: false });
|
||||||
|
objectStore1.createIndex('tid', 'tid', { unique: false });
|
||||||
|
objectStore1.createIndex('data', 'data', { unique: false });
|
||||||
|
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement:true });
|
||||||
|
objectStore2.createIndex('from', 'from', { unique: false });
|
||||||
|
objectStore2.createIndex('to', 'to', { unique: false });
|
||||||
|
objectStore2.createIndex('data', 'data', { unique: false });
|
||||||
|
};
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
var obs = db.transaction("viaSuperNodeMsg", "readwrite").objectStore("viaSuperNodeMsg");
|
||||||
|
obs.openCursor().onsuccess = (event) => {
|
||||||
|
var cursor = event.target.result;
|
||||||
|
if(cursor) {
|
||||||
|
if(cursor.value.to == floID){
|
||||||
|
receiverWS.send(cursor.value.data);
|
||||||
|
cursor.delete();
|
||||||
|
}
|
||||||
|
cursor.continue();
|
||||||
|
}else{
|
||||||
|
console.log('Sent All messages to '+floID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
db.close();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
receiverWS.onerror = (event) => { console.log('Connection Error to '+floID) };
|
||||||
|
receiverWS.onclose = (event) => { console.log('Disconnected from '+floID) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---end of app.js --- */
|
||||||
|
|
||||||
/*Kademlia DHT K-bucket implementation as a binary tree.*/
|
/*Kademlia DHT K-bucket implementation as a binary tree.*/
|
||||||
|
|
||||||
if (typeof reactor == "undefined" || !reactor) {
|
if (typeof reactor == "undefined" || !reactor) {
|
||||||
(function () {
|
(function () {
|
||||||
|
|
||||||
function Event(name) {
|
function Event(name) {
|
||||||
@ -761,7 +1101,7 @@ reactor.addEventListener('bucket_full', function (someObject) {
|
|||||||
|
|
||||||
kBucketObj = {
|
kBucketObj = {
|
||||||
|
|
||||||
decodeBase58Address: function (blockchain, address) {
|
decodeBase58Address: function (address) {
|
||||||
let k = bitjs.Base58.decode(address)
|
let k = bitjs.Base58.decode(address)
|
||||||
k.shift()
|
k.shift()
|
||||||
k.splice(-4, 4)
|
k.splice(-4, 4)
|
||||||
@ -772,12 +1112,12 @@ kBucketObj = {
|
|||||||
try {
|
try {
|
||||||
//const master_flo_pubKey = localbitcoinplusplus.master_configurations.masterFLOPubKey;
|
//const master_flo_pubKey = localbitcoinplusplus.master_configurations.masterFLOPubKey;
|
||||||
const master_flo_addr = adminID;
|
const master_flo_addr = adminID;
|
||||||
const SuKBucketId = this.floIdToKbucketId(crypto, master_flo_addr);
|
const SuKBucketId = this.floIdToKbucketId(master_flo_addr);
|
||||||
const SukbOptions = { localNodeId: SuKBucketId }
|
const SukbOptions = { localNodeId: SuKBucketId }
|
||||||
supernodeKBucket = new BuildKBucket(SukbOptions);
|
supernodeKBucket = new BuildKBucket(SukbOptions);
|
||||||
for(var i=0; i<superNodeList.length ; i++){
|
var SNArray = Array.from(superNodeList);
|
||||||
this.addNewUserNodeInKbucket(crypto,superNodeList[i],supernodeKBucket)
|
for(var i=0; i<SNArray.length ; i++)
|
||||||
}
|
this.addNewUserNodeInKbucket(SNArray[i],supernodeKBucket)
|
||||||
resolve('SuperNode KBucket formed');
|
resolve('SuperNode KBucket formed');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
@ -791,7 +1131,7 @@ kBucketObj = {
|
|||||||
try {
|
try {
|
||||||
let flo_id = bitjs.pubkey2address(pubKey);
|
let flo_id = bitjs.pubkey2address(pubKey);
|
||||||
let kname = `SKBucket_${pubKey}`;
|
let kname = `SKBucket_${pubKey}`;
|
||||||
const KBucketId = this.floIdToKbucketId(crypto, flo_id)
|
const KBucketId = this.floIdToKbucketId(flo_id)
|
||||||
const kbOptions = { localNodeId: KBucketId }
|
const kbOptions = { localNodeId: KBucketId }
|
||||||
window[kname] = new BuildKBucket(kbOptions);
|
window[kname] = new BuildKBucket(kbOptions);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
@ -808,18 +1148,18 @@ kBucketObj = {
|
|||||||
};
|
};
|
||||||
KB.add(contact)
|
KB.add(contact)
|
||||||
},
|
},
|
||||||
addNewUserNodeInKbucket: function(blockchain, address, KB=supernodeKBucket) {
|
addNewUserNodeInKbucket: function(address, KB=supernodeKBucket) {
|
||||||
let decodedId = address;
|
let decodedId = address;
|
||||||
try {
|
try {
|
||||||
decodedId = this.floIdToKbucketId(blockchain, address);
|
decodedId = this.floIdToKbucketId(address);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
decodedId = address;
|
decodedId = address;
|
||||||
}
|
}
|
||||||
const addNewUserNode = this.addContact(decodedId, address, KB);
|
const addNewUserNode = this.addContact(decodedId, address, KB);
|
||||||
return {decodedId:decodedId, address:address};
|
return {decodedId:decodedId, address:address};
|
||||||
},
|
},
|
||||||
floIdToKbucketId: function (blockchain, address) {
|
floIdToKbucketId: function (address) {
|
||||||
const decodedId = this.decodeBase58Address(blockchain, address);
|
const decodedId = this.decodeBase58Address(address);
|
||||||
const nodeIdBigInt = new BigInteger(decodedId, 16);
|
const nodeIdBigInt = new BigInteger(decodedId, 16);
|
||||||
const nodeIdBytes = nodeIdBigInt.toByteArrayUnsigned();
|
const nodeIdBytes = nodeIdBigInt.toByteArrayUnsigned();
|
||||||
const nodeIdNewInt8Array = new Uint8Array(nodeIdBytes);
|
const nodeIdNewInt8Array = new Uint8Array(nodeIdBytes);
|
||||||
@ -843,7 +1183,7 @@ kBucketObj = {
|
|||||||
let pubKeyBytes = Crypto.util.hexToBytes(pubKey);
|
let pubKeyBytes = Crypto.util.hexToBytes(pubKey);
|
||||||
return Crypto.util.bytesToBase64(pubKeyBytes);
|
return Crypto.util.bytesToBase64(pubKeyBytes);
|
||||||
},
|
},
|
||||||
restoreKbucket: function(flo_addr, blockchain="FLO_TEST", KB=KBucket) {
|
restoreKbucket: function(flo_addr, KB=KBucket) {
|
||||||
return new Promise((resolve, reject)=>{
|
return new Promise((resolve, reject)=>{
|
||||||
readAllDB('kBucketStore')
|
readAllDB('kBucketStore')
|
||||||
.then(dbObject => {
|
.then(dbObject => {
|
||||||
@ -854,7 +1194,7 @@ kBucketObj = {
|
|||||||
dbObject
|
dbObject
|
||||||
.filter(f=>!su_flo_addr_array.includes(f.data.id))
|
.filter(f=>!su_flo_addr_array.includes(f.data.id))
|
||||||
.map(dbObj=>{
|
.map(dbObj=>{
|
||||||
this.addNewUserNodeInKbucket(blockchain, flo_addr, dbObj.data, KB);
|
this.addNewUserNodeInKbucket(flo_addr, dbObj.data, KB);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
reject(`Failed to restore kBucket.`);
|
reject(`Failed to restore kBucket.`);
|
||||||
@ -870,7 +1210,7 @@ kBucketObj = {
|
|||||||
let supernodeSeedsObj = JSON.parse(supernodeSeeds);
|
let supernodeSeedsObj = JSON.parse(supernodeSeeds);
|
||||||
|
|
||||||
Object.entries(supernodeSeedsObj).map(seedObj=>{
|
Object.entries(supernodeSeedsObj).map(seedObj=>{
|
||||||
let kbuck = this.addNewUserNodeInKbucket(crypto, seedObj[1].kbucketId,
|
let kbuck = this.addNewUserNodeInKbucket(seedObj[1].kbucketId,
|
||||||
{ id: seedObj[1].kbucketId }, supernodeKBucket);
|
{ id: seedObj[1].kbucketId }, supernodeKBucket);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -948,7 +1288,7 @@ kBucketObj = {
|
|||||||
}
|
}
|
||||||
let isFloIdUint8 = flo_addr instanceof Uint8Array;
|
let isFloIdUint8 = flo_addr instanceof Uint8Array;
|
||||||
if (!isFloIdUint8) {
|
if (!isFloIdUint8) {
|
||||||
flo_addr = this.floIdToKbucketId(crypto, flo_addr);
|
flo_addr = this.floIdToKbucketId(flo_addr);
|
||||||
}
|
}
|
||||||
const closestSupernode = supernodeKBucket.closest(flo_addr, n);
|
const closestSupernode = supernodeKBucket.closest(flo_addr, n);
|
||||||
resolve(closestSupernode);
|
resolve(closestSupernode);
|
||||||
@ -959,233 +1299,5 @@ kBucketObj = {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
|
||||||
|
|
||||||
addClosestSupernodeInDB: function(flo_addr, KB=KBucket) {
|
|
||||||
return new Promise(async (resolve, reject)=>{
|
|
||||||
const supernodeSeeds = localbitcoinplusplus.master_configurations.supernodeSeeds;
|
|
||||||
if (typeof supernodeSeeds !== "object") reject("Failed to get supernode seeds.");
|
|
||||||
let supernodeSeedsObj = JSON.parse(supernodeSeeds);
|
|
||||||
|
|
||||||
Object.entries(supernodeSeedsObj).map(seedObj=>{
|
|
||||||
console.log(seedObj);
|
|
||||||
this.addNewUserNodeInKbucketAndDB(
|
|
||||||
crypto, seedObj[1].kbucketId,
|
|
||||||
{ id: seedObj[1].kbucketId });
|
|
||||||
});
|
|
||||||
|
|
||||||
let primarySu = await this.determineClosestSupernode(flo_addr);
|
|
||||||
let nearestSupernode = await this.determineClosestSupernode(flo_addr="", n=1, supernodeKBucket, primarySu[0].data.id);
|
|
||||||
let nearestSupernodeIds = nearestSupernode.map(f=>f.data.id);
|
|
||||||
let supernodeSeedsArray = Object.values(supernodeSeedsObj)
|
|
||||||
.filter(seed=>nearestSupernodeIds.includes(seed.kbucketId))
|
|
||||||
.sort(function(a, b){
|
|
||||||
return nearestSupernodeIds.indexOf(a.kbucketId) - nearestSupernodeIds.indexOf(b.kbucketId);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (supernodeSeedsArray.length>0) {
|
|
||||||
resolve(supernodeSeedsArray);
|
|
||||||
} else {
|
|
||||||
reject(false);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Common IDB functions */
|
|
||||||
function storeTweet(data,tid){
|
|
||||||
var idb = indexedDB.open("FLO_Tweet");
|
|
||||||
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction("tweets", "readwrite").objectStore("tweets");
|
|
||||||
data.tweetID = `${data.time}_${data.floID}`;
|
|
||||||
data.tid = tid;
|
|
||||||
obs.add(data);
|
|
||||||
var obsL = db.transaction("lastTweet", "readwrite").objectStore("lastTweet");
|
|
||||||
obsL.put(tid,data.floID);
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function storeMsg(data){
|
|
||||||
var idb = indexedDB.open("FLO_Tweet");
|
|
||||||
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction("messages", "readwrite").objectStore("messages");
|
|
||||||
data.msgID = `${data.time}_${data.floID}`;
|
|
||||||
obs.add(data);
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/* SuperNode functions */
|
|
||||||
function sendDataToSuperNode(floID,data){
|
|
||||||
kBucketObj.determineClosestSupernode(floID).then(result=>{
|
|
||||||
var superNodeWS = new WebSocket("ws://"+profiles[result[0].floID].onionAddr+"/ws");
|
|
||||||
superNodeWS.onopen = function(ev){
|
|
||||||
console.log(`Connected to ${floID}'s SuperNode!`);
|
|
||||||
superNodeWS.send(data);
|
|
||||||
};
|
|
||||||
superNodeWS.onerror = function(ev) {console.log(`${floid}'s SuperNode is offline!`);};
|
|
||||||
superNodeWS.onclose = function(ev) {console.log(`Disconnected from ${floid}'s SuperNode!`);};
|
|
||||||
}).catch(e => {
|
|
||||||
console.log(e.message);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function superNodeMode(data){
|
|
||||||
if(data.reqNewTweets){
|
|
||||||
kBucketObj.determineClosestSupernode(data.floID).then(result => {
|
|
||||||
if(result[0].floID == selfID)
|
|
||||||
SuperNode_sendTweetsFromIDB(data.floID,data.tid,data.requestor);
|
|
||||||
}).catch(error => {
|
|
||||||
console.log(error);
|
|
||||||
});
|
|
||||||
}else if(data.newSuperNodeTweet){
|
|
||||||
kBucketObj.determineClosestSupernode(data.floID).then(result => {
|
|
||||||
if(result[0].floID == selfID)
|
|
||||||
SuperNode_storeSuperNodeTweet(data.data,data.tid);
|
|
||||||
}).catch(error => {
|
|
||||||
console.log(error);
|
|
||||||
});
|
|
||||||
}else if(data.viaSuperNodeMsg){
|
|
||||||
kBucketObj.determineClosestSupernode(data.to).then(result => {
|
|
||||||
if(result[0].floID == selfID)
|
|
||||||
SuperNode_storeViaSuperNodeMsg(data.from,data.to,data.data);
|
|
||||||
}).catch(error => {
|
|
||||||
console.log(error);
|
|
||||||
});
|
|
||||||
}else if(data.viaMsgreq){
|
|
||||||
kBucketObj.determineClosestSupernode(data.floID).then(result => {
|
|
||||||
if(result[0].floID == selfID)
|
|
||||||
SuperNode_sendviaMsgFromIDB(data.floID);
|
|
||||||
}).catch(error => {
|
|
||||||
console.log(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function SuperNode_sendTweetsFromIDB(floID,tid,requestor){
|
|
||||||
return new Promise((resolve,reject) => {
|
|
||||||
var requestorWS = new WebSocket("ws://"+profiles[requestor].onionAddr+"/ws");
|
|
||||||
requestorWS.onopen = (event) => {
|
|
||||||
console.log(`sending ${floID} tweets to ${requestor} Server!`);
|
|
||||||
var idb = indexedDB.open("FLO_Tweet",2);
|
|
||||||
idb.onerror = (event) => { reject("Error in opening IndexedDB!") };
|
|
||||||
idb.onupgradeneeded = (event) => {
|
|
||||||
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
|
||||||
objectStore1.createIndex('floID', 'floID', { unique: false });
|
|
||||||
objectStore1.createIndex('tid', 'tid', { unique: false });
|
|
||||||
objectStore1.createIndex('data', 'data', { unique: false });
|
|
||||||
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement:true });
|
|
||||||
objectStore2.createIndex('from', 'from', { unique: false });
|
|
||||||
objectStore2.createIndex('to', 'to', { unique: false });
|
|
||||||
objectStore2.createIndex('data', 'data', { unique: false });
|
|
||||||
}
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction("superNodeTweet", "readwrite").objectStore("superNodeTweet");
|
|
||||||
var curReq = obs.openCursor();
|
|
||||||
curReq.onsuccess = (event) => {
|
|
||||||
var cursor = event.target.result;
|
|
||||||
if(cursor) {
|
|
||||||
if(cursor.value.floID == floID && cursor.value.tid > tid){
|
|
||||||
data = JSON.stringify({fromSuperNode:true, floID:cursor.value.floID,tid:cursor.value.tid,data:cursor.value.data})
|
|
||||||
requestorWS.send(data);
|
|
||||||
}
|
|
||||||
cursor.continue();
|
|
||||||
}else{
|
|
||||||
resolve("Displayed Tweets from IDB!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
curReq.onerror = (event) => { reject("Error in Reading tweets from IDB!") }
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
requestorWS.onerror = (event) => { console.log(`${requestor} Server is offline!`) };
|
|
||||||
requestorWS.onclose = (event) => { console.log(`Disconnected from ${requestor} Server!`) };
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function SuperNode_storeSuperNodeTweet(data,tid){
|
|
||||||
var idb = indexedDB.open("FLO_Tweet",2);
|
|
||||||
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
|
||||||
idb.onupgradeneeded = (event) => {
|
|
||||||
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
|
||||||
objectStore1.createIndex('floID', 'floID', { unique: false });
|
|
||||||
objectStore1.createIndex('tid', 'tid', { unique: false });
|
|
||||||
objectStore1.createIndex('data', 'data', { unique: false });
|
|
||||||
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement:true });
|
|
||||||
objectStore2.createIndex('from', 'from', { unique: false });
|
|
||||||
objectStore2.createIndex('to', 'to', { unique: false });
|
|
||||||
objectStore2.createIndex('data', 'data', { unique: false });
|
|
||||||
}
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction("superNodeTweet", "readwrite").objectStore("superNodeTweet");
|
|
||||||
var parsedData = JSON.parse(data);
|
|
||||||
var tweetID = ''+parsedData.floID+'_'+parsedData.time;
|
|
||||||
obs.add({tweetID:tweetID,floID:parsedData.floID,tid:tid,data:data});
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function SuperNode_storeViaSuperNodeMsg(from,to,data){
|
|
||||||
var idb = indexedDB.open("FLO_Tweet",2);
|
|
||||||
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
|
||||||
idb.onupgradeneeded = (event) => {
|
|
||||||
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
|
||||||
objectStore1.createIndex('floID', 'floID', { unique: false });
|
|
||||||
objectStore1.createIndex('tid', 'tid', { unique: false });
|
|
||||||
objectStore1.createIndex('data', 'data', { unique: false });
|
|
||||||
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement :true });
|
|
||||||
objectStore2.createIndex('from', 'from', { unique: false });
|
|
||||||
objectStore2.createIndex('to', 'to', { unique: false });
|
|
||||||
objectStore2.createIndex('data', 'data', { unique: false });
|
|
||||||
}
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction("viaSuperNodeMsg", "readwrite").objectStore("viaSuperNodeMsg");
|
|
||||||
obs.add({from:from,to:to,data:data});
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function SuperNode_sendviaMsgFromIDB(floID){
|
|
||||||
var receiverWS = new WebSocket("ws://"+profiles[floID].onionAddr+"/ws");
|
|
||||||
receiverWS.onopen = (event) => {
|
|
||||||
var idb = indexedDB.open("FLO_Tweet",2);
|
|
||||||
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
|
||||||
idb.onupgradeneeded = (event) => {
|
|
||||||
var objectStore1 = event.target.result.createObjectStore("superNodeTweet",{ keyPath: 'tweetID' });
|
|
||||||
objectStore1.createIndex('floID', 'floID', { unique: false });
|
|
||||||
objectStore1.createIndex('tid', 'tid', { unique: false });
|
|
||||||
objectStore1.createIndex('data', 'data', { unique: false });
|
|
||||||
var objectStore2 = event.target.result.createObjectStore("viaSuperNodeMsg",{ keyPath: 'id',autoIncrement:true });
|
|
||||||
objectStore2.createIndex('from', 'from', { unique: false });
|
|
||||||
objectStore2.createIndex('to', 'to', { unique: false });
|
|
||||||
objectStore2.createIndex('data', 'data', { unique: false });
|
|
||||||
};
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction("viaSuperNodeMsg", "readwrite").objectStore("viaSuperNodeMsg");
|
|
||||||
obs.openCursor().onsuccess = (event) => {
|
|
||||||
var cursor = event.target.result;
|
|
||||||
if(cursor) {
|
|
||||||
if(cursor.value.to == floID){
|
|
||||||
receiverWS.send(cursor.value.data);
|
|
||||||
cursor.delete();
|
|
||||||
}
|
|
||||||
cursor.continue();
|
|
||||||
}else{
|
|
||||||
console.log('Sent All messages to '+floID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
receiverWS.onerror = (event) => { console.log('Connection Error to '+floID) };
|
|
||||||
receiverWS.onclose = (event) => { console.log('Disconnected from '+floID) };
|
|
||||||
}
|
}
|
||||||
@ -35,10 +35,13 @@
|
|||||||
<input type="text" class="form-control-nav" id="search" aria-describedby="search1">
|
<input type="text" class="form-control-nav" id="search" aria-describedby="search1">
|
||||||
<span class="glyphicon glyphicon-search form-control-feedback" aria-hidden="true"></span>
|
<span class="glyphicon glyphicon-search form-control-feedback" aria-hidden="true"></span>
|
||||||
</div>
|
</div>
|
||||||
|
<button class="btn btn-default btn-sm" onclick="reloadInitData()">
|
||||||
|
<span class="glyphicon glyphicon-refresh"> </span>
|
||||||
|
</button>
|
||||||
<button class="btn btn-default btn-sm" onclick="logout()">
|
<button class="btn btn-default btn-sm" onclick="logout()">
|
||||||
<span class="glyphicon glyphicon-log-out"> </span> Log out
|
<span class="glyphicon glyphicon-log-out"> </span> Log out
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -12,10 +12,10 @@ function viewHome(){
|
|||||||
console.log(profiles);
|
console.log(profiles);
|
||||||
privKey = encrypt.retrieveShamirSecret(JSON.parse(sessionStorage.privKey));
|
privKey = encrypt.retrieveShamirSecret(JSON.parse(sessionStorage.privKey));
|
||||||
selfID = sessionStorage.selfID;
|
selfID = sessionStorage.selfID;
|
||||||
superNodeList = JSON.parse(sessionStorage.superNodeList);
|
superNodeList = new Set(JSON.parse(sessionStorage.superNodeList));
|
||||||
if(superNodeList.includes(selfID))
|
if(superNodeList.has(selfID))
|
||||||
modSuperNode = true;
|
modSuperNode = true;
|
||||||
kBucketObj.launchKBucket().then(function(result){
|
kBucketObj.launchKBucket().then(result => {
|
||||||
console.log(result);
|
console.log(result);
|
||||||
initselfWebSocket();
|
initselfWebSocket();
|
||||||
listProfiles();
|
listProfiles();
|
||||||
@ -33,8 +33,8 @@ function viewHome(){
|
|||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
})
|
})
|
||||||
}).catch(function(error){
|
}).catch(error => {
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ function initselfWebSocket(){
|
|||||||
superNodeMode(data);
|
superNodeMode(data);
|
||||||
}
|
}
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
selfWebsocket.onerror = (event) => { console.log(event) };
|
selfWebsocket.onerror = (event) => { console.log(event) };
|
||||||
@ -214,7 +214,7 @@ function connectToAllFollowing(){
|
|||||||
storeTweet({floID:data.floID,time:data.time,data:data.tweet},id);
|
storeTweet({floID:data.floID,time:data.time,data:data.tweet},id);
|
||||||
createTweetElement(data.floID,data.time,data.tweet);
|
createTweetElement(data.floID,data.time,data.tweet);
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@ -129,7 +129,7 @@ label span {
|
|||||||
<script type="text/javascript" src="login.js"></script>
|
<script type="text/javascript" src="login.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body onload="refreshAPIdata();">
|
<body onload="initAPIdata();">
|
||||||
<div id="alert-container"></div>
|
<div id="alert-container"></div>
|
||||||
<div class="nav">
|
<div class="nav">
|
||||||
<form id='serverConnect'>
|
<form id='serverConnect'>
|
||||||
|
|||||||
@ -15,9 +15,9 @@ const fee = 0.0005;
|
|||||||
var username,privKey,floID;
|
var username,privKey,floID;
|
||||||
var profiles;
|
var profiles;
|
||||||
|
|
||||||
function refreshAPIdata(){
|
function initAPIdata(){
|
||||||
return new Promise((resolve,reject) => {
|
return new Promise((resolve,reject) => {
|
||||||
console.log("refreshAPIdata");
|
console.log("initAPIdata");
|
||||||
getDatafromAPI().then(result => {
|
getDatafromAPI().then(result => {
|
||||||
console.log(result);
|
console.log(result);
|
||||||
getProfilesfromIDB().then(result => {
|
getProfilesfromIDB().then(result => {
|
||||||
@ -26,8 +26,8 @@ function refreshAPIdata(){
|
|||||||
sessionStorage.profiles = JSON.stringify(profiles);
|
sessionStorage.profiles = JSON.stringify(profiles);
|
||||||
getSuperNodeListfromIDB().then(result => {
|
getSuperNodeListfromIDB().then(result => {
|
||||||
console.log(result)
|
console.log(result)
|
||||||
superNodeList = result;
|
superNodeList = new Set(result);
|
||||||
sessionStorage.superNodeList = JSON.stringify(superNodeList);
|
sessionStorage.superNodeList = JSON.stringify(Array.from(superNodeList));
|
||||||
resolve('Refresh API data Successful!')
|
resolve('Refresh API data Successful!')
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
@ -224,40 +224,6 @@ function getDatafromAPI(){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeProfile(data){
|
|
||||||
return new Promise((resolve,reject) => {
|
|
||||||
var idb = indexedDB.open("FLO_Tweet");
|
|
||||||
idb.onerror = (event) => { console.log("Error in opening IndexedDB!") };
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction('profiles', "readwrite").objectStore('profiles');
|
|
||||||
objectRequest = obs.put(data);
|
|
||||||
objectRequest.onerror = (event) => { reject(Error('Error occured: Unable to store data'))};
|
|
||||||
objectRequest.onsuccess = (event) => { resolve('Data saved OK') };
|
|
||||||
db.close();
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function storeSuperNodeData(data){
|
|
||||||
return new Promise((resolve,reject) => {
|
|
||||||
var idb = indexedDB.open("FLO_Tweet");
|
|
||||||
idb.onerror = (event) => { reject("Error in opening IndexedDB!"); };
|
|
||||||
idb.onsuccess = (event) => {
|
|
||||||
var db = event.target.result;
|
|
||||||
var obs = db.transaction('superNodes', "readwrite").objectStore('superNodes');
|
|
||||||
if(data.addNodes)
|
|
||||||
for(var i=0; i<data.addNodes.length; i++)
|
|
||||||
obs.add(true,data.addNodes[i])
|
|
||||||
if(data.removeNodes)
|
|
||||||
for(var i=0; i<data.removeNodes.length; i++)
|
|
||||||
obs.delete(data.removeNodes[i])
|
|
||||||
db.close();
|
|
||||||
resolve('Updated superNodes list in IDB');
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getProfilesfromIDB(){
|
function getProfilesfromIDB(){
|
||||||
return new Promise((resolve,reject) => {
|
return new Promise((resolve,reject) => {
|
||||||
var idb = indexedDB.open("FLO_Tweet");
|
var idb = indexedDB.open("FLO_Tweet");
|
||||||
@ -307,10 +273,6 @@ function initselfWebSocket(serverPass){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Script for AJAX, and register functions
|
|
||||||
|
|
||||||
function registerID(sender,onionAddr,wif,pubkey,username) {
|
function registerID(sender,onionAddr,wif,pubkey,username) {
|
||||||
return new Promise((resolve,reject) => {
|
return new Promise((resolve,reject) => {
|
||||||
var trx = bitjs.transaction();
|
var trx = bitjs.transaction();
|
||||||
|
|||||||
@ -12,8 +12,8 @@ function initMsgs(){
|
|||||||
console.log(profiles);
|
console.log(profiles);
|
||||||
privKey = encrypt.retrieveShamirSecret(JSON.parse(sessionStorage.privKey));
|
privKey = encrypt.retrieveShamirSecret(JSON.parse(sessionStorage.privKey));
|
||||||
selfID = sessionStorage.selfID;
|
selfID = sessionStorage.selfID;
|
||||||
superNodeList = JSON.parse(sessionStorage.superNodeList);
|
superNodeList = new Set(JSON.parse(sessionStorage.superNodeList));
|
||||||
if(superNodeList.includes(selfID))
|
if(superNodeList.has(selfID))
|
||||||
modSuperNode = true;
|
modSuperNode = true;
|
||||||
kBucketObj.launchKBucket().then(result => {
|
kBucketObj.launchKBucket().then(result => {
|
||||||
console.log(result)
|
console.log(result)
|
||||||
@ -93,7 +93,7 @@ function initselfWebSocket(){
|
|||||||
superNodeMode(data);
|
superNodeMode(data);
|
||||||
}
|
}
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.log(error.message)
|
console.log(error)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
selfWebsocket.onerror = (event) => { console.log(event) };
|
selfWebsocket.onerror = (event) => { console.log(event) };
|
||||||
|
|||||||
@ -15,15 +15,15 @@ function viewProfile(){
|
|||||||
selfID = sessionStorage.selfID;
|
selfID = sessionStorage.selfID;
|
||||||
var url = new URL(window.location.href);
|
var url = new URL(window.location.href);
|
||||||
profileID = url.searchParams.get("floID");
|
profileID = url.searchParams.get("floID");
|
||||||
superNodeList = JSON.parse(sessionStorage.superNodeList);
|
superNodeList = new Set(JSON.parse(sessionStorage.superNodeList));
|
||||||
if(superNodeList.includes(selfID))
|
if(superNodeList.has(selfID))
|
||||||
modSuperNode = true;
|
modSuperNode = true;
|
||||||
kBucketObj.launchKBucket().then(result => {
|
kBucketObj.launchKBucket().then(result => {
|
||||||
console.log(result)
|
console.log(result)
|
||||||
listProfiles();
|
listProfiles();
|
||||||
displayProfile(profileID);
|
displayProfile(profileID);
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ function displayProfile(profileID){
|
|||||||
profileServerStatus = false;
|
profileServerStatus = false;
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ function connectToProfileServer(floID){
|
|||||||
storeTweet({floID:floID,time:data.time,data:data.tweet},id);
|
storeTweet({floID:floID,time:data.time,data:data.tweet},id);
|
||||||
createTweetElement(floID,data.time,data.tweet);
|
createTweetElement(floID,data.time,data.tweet);
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -158,7 +158,7 @@ function pingSuperNodeforNewTweets(floID){
|
|||||||
var data = JSON.stringify({reqNewTweets:true,floID:floID,tid:result,requestor:selfID})
|
var data = JSON.stringify({reqNewTweets:true,floID:floID,tid:result,requestor:selfID})
|
||||||
sendDataToSuperNode(floID,data);
|
sendDataToSuperNode(floID,data);
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +169,7 @@ function pingProfileServerforNewTweets(floID){
|
|||||||
console.log("sent");
|
console.log("sent");
|
||||||
resolve('Sent New tweet request to user server!');
|
resolve('Sent New tweet request to user server!');
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(error.message);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,7 +239,7 @@ function initselfWebSocket(){
|
|||||||
superNodeMode(data);
|
superNodeMode(data);
|
||||||
}
|
}
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.log(error.message)
|
console.log(error)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
selfWebsocket.onerror = (event) => { console.log(event) };
|
selfWebsocket.onerror = (event) => { console.log(event) };
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user