diff --git a/args/schema.sql b/args/schema.sql index fa5ab0d..9945813 100644 --- a/args/schema.sql +++ b/args/schema.sql @@ -113,6 +113,12 @@ PRIMARY KEY(id), FOREIGN KEY (floID) REFERENCES Users(floID) ); +CREATE TABLE lastTx( +floID CHAR(34) NOT NULL, +num INT, +PRIMARY KEY(floID) +); + CREATE TABLE nodeList( floID CHAR(34) NOT NULL, uri TINYTEXT, diff --git a/setup/reset-password.js b/setup/reset-password.js index 299494b..6583a88 100644 --- a/setup/reset-password.js +++ b/setup/reset-password.js @@ -10,10 +10,15 @@ console.log(__dirname); function validateKey(privKey) { return new Promise((resolve, reject) => { - if (floCrypto.verifyPrivKey(privKey, floGlobals.adminID)) + try { + if (!privKey || privKey === "") + throw 'Private Key cannot be empty!'; + let floID = floCrypto.getFloID(privKey); + if (!floID || !floCrypto.verifyPrivKey(privKey, floID)) + throw 'Invalid Private Key!'; return resolve(privKey); - else { - getInput.Text('Incorrect Private Key! Re-Enter: (Cancel)', 'Cancel').then(value => { + } catch (error) { + getInput.Text(error + ' Re-Enter: (Cancel)', 'Cancel').then(value => { if (value === 'Cancel') return reject(true); validateKey(value) @@ -51,7 +56,7 @@ function getPassword() { function resetPassword() { return new Promise((resolve, reject) => { - getInput.Text(`Enter private key for adminID (${floGlobals.adminID})`).then(value => { + getInput.Text(`Enter private key`).then(value => { validateKey(value).then(privKey => { getPassword().then(password => { let encrypted = Crypto.AES.encrypt(privKey, password); diff --git a/src/main.js b/src/main.js index 243b168..f95ca20 100644 --- a/src/main.js +++ b/src/main.js @@ -27,6 +27,7 @@ function refreshDataFromBlockchain() { return new Promise((resolve, reject) => { DB.query("SELECT num FROM lastTx WHERE floID=?", [floGlobals.adminID]).then(result => { let lastTx = result.length ? result[0].num : 0; + console.debug('lastTx', lastTx); floBlockchainAPI.readData(floGlobals.adminID, { ignoreOld: lastTx, sentOnly: true, @@ -37,6 +38,7 @@ function refreshDataFromBlockchain() { trusted_change = false; result.data.reverse().forEach(data => { var content = JSON.parse(data)[floGlobals.application]; + console.debug(content); //Node List if (content.Nodes) { nodes_change = true; @@ -45,7 +47,7 @@ function refreshDataFromBlockchain() { promises.push(DB.query("DELETE FROM nodeList WHERE floID=?", [n])); if (content.Nodes.add) for (let n in content.Nodes.add) - promises.push(DB.query("INSERT INTO nodeList (floID, uri) VALUE (?,?) ON DUPLICATE KEY UPDATE uri=NEW.uri", [n, content.Nodes.add[n]])); + promises.push(DB.query("INSERT INTO nodeList (floID, uri) VALUE (?,?) AS new ON DUPLICATE KEY UPDATE uri=new.uri", [n, content.Nodes.add[n]])); } //Trusted List if (content.Trusted) { @@ -55,7 +57,7 @@ function refreshDataFromBlockchain() { promises.push(DB.query("DELETE FROM trustedList WHERE floID=?", [id])); if (content.Trusted.add) for (let id of content.Trusted.add) - promises.push(DB.query("INSERT INTO trustedList (floID) VALUE (?) ON DUPLICATE KEY UPDATE floID=NEW.floID", [id])); + promises.push(DB.query("INSERT INTO trustedList (floID) VALUE (?) AS new ON DUPLICATE KEY UPDATE floID=new.floID", [id])); } //Tag List with priority and API if (content.Tag) { @@ -64,16 +66,17 @@ function refreshDataFromBlockchain() { promises.push(DB.query("DELETE FROM TagList WHERE tag=?", [t])); if (content.Tag.add) for (let t in content.Tag.add) - promises.push(DB.query("INSERT INTO TagList (tag, sellPriority, buyPriority, api) VALUE (?,?,?,?)", [t, content.Tag.add[t].sellPriority, content.Tag.add[t].buyPriority, content.Tag.add[t].api])); + promises.push(DB.query("INSERT INTO TagList (tag, sellPriority, buyPriority, api) VALUE (?,?,?,?) AS new ON DUPLICATE KEY UPDATE tag=new.tag", [t, content.Tag.add[t].sellPriority, content.Tag.add[t].buyPriority, content.Tag.add[t].api])); if (content.Tag.update) for (let t in content.Tag.update) for (let a in content.Tag.update[t]) promises.push(`UPDATE TagList WHERE tag=? SET ${a}=?`, [t, content.Tag.update[t][a]]); } }); - promises.push(DB.query("INSERT INTO lastTx (floID, num) VALUE (?, ?) ON DUPLICATE KEY UPDATE num=NEW.num", [floGlobals.adminID, result.totalTxs])); + promises.push(DB.query("INSERT INTO lastTx (floID, num) VALUE (?, ?) AS new ON DUPLICATE KEY UPDATE num=new.num", [floGlobals.adminID, result.totalTxs])); //Check if all save process were successful Promise.allSettled(promises).then(results => { + console.debug(results.filter(r => r.status === "rejected")); if (results.reduce((a, r) => r.status === "rejected" ? ++a : a, 0)) console.warn("Some data might not have been saved in database correctly"); }); @@ -101,7 +104,7 @@ function loadDataFromDB(changes, startup) { loadDataFromDB.nodeList = function() { return new Promise((resolve, reject) => { - DB.query("SELECT (floID, uri) FROM nodeList").then(result => { + DB.query("SELECT * FROM nodeList").then(result => { let nodes = {} for (let i in result) nodes[result[i].floID] = result[i].uri; @@ -140,15 +143,11 @@ module.exports = function startServer(public_dir) { console.error('Password not entered!'); process.exit(1); } - _tmp = Crypto.AES.decrypt(_tmp, _pass); - if (floCrypto.verifyPrivKey(_tmp, floGlobals.adminID)) { - global.myPrivKey = _tmp; - global.myPubKey = floCrypto.getPubKeyHex(global.myPrivKey); - global.myFloID = floCrypto.getFloID(global.myPubKey); - } else { - console.error('Loaded wrong private key!'); - process.exit(1); - } + global.myPrivKey = Crypto.AES.decrypt(_tmp, _pass); + global.myPubKey = floCrypto.getPubKeyHex(global.myPrivKey); + global.myFloID = floCrypto.getFloID(global.myPubKey); + if (!global.myFloID || !global.myPubKey || !global.myPrivKey) + throw "Invalid Keys"; } catch (error) { console.error('Unable to load private key!'); process.exit(1);