diff --git a/index.html b/index.html index fac7914..2ba0dad 100644 --- a/index.html +++ b/index.html @@ -47,6 +47,8 @@

SuperNode Storage

@@ -6035,35 +6659,153 @@ } function onLoadStartUp() { - myPrivKey = prompt("Enter Private Key : ") - myPubKey = floCrypto.getPubKeyHex(myPrivKey) - myFloID = floCrypto.getFloIDfromPubkeyHex(myPubKey) initIndexedDBforSupernodeConfig().then(result => { console.log(result) refreshBlockchainData().then(result => { console.log(result) - if (myFloID in floGlobals.supernodes) { - initIndexedDBforSupernodeDataStorage(myFloID).then(result => { - console.log(result) - serverPwd = prompt("Enter Server Pass!") - setInterval(autoRefreshBlockchainData, floGlobals.refreshDelay); - floSupernode.initSupernode(serverPwd, myFloID).then(async result => { + getCredentials().then(result => { + if (myFloID in floGlobals.supernodes) { + initIndexedDBforSupernodeDataStorage(myFloID).then(result => { console.log(result) - floGlobals.serveList.push(myFloID) - floGlobals.storedList.push(myFloID) - await sleep(5000) - connectToAllBackupSupernode().then(async result => { + setInterval(autoRefreshBlockchainData, floGlobals.refreshDelay); + floSupernode.initSupernode(serverPwd, myFloID).then(async result => { console.log(result) - await sleep(2000) - reactor.dispatchEvent("indicate_supernode_up",myFloID) + floGlobals.serveList.push(myFloID) + floGlobals.storedList.push(myFloID) + await sleep(5000) + connectToAllBackupSupernode().then(async result => { + console.log(result) + await sleep(2000) + reactor.dispatchEvent("indicate_supernode_up",myFloID) + }).catch(error => console.log(error)) }).catch(error => console.log(error)) }).catch(error => console.log(error)) - }).catch(error => console.log(error)) - } + } + }).catch(error => console.log(error)) }).catch(error => console.log(error)) }).catch(error => console.log(error)) } + + function getCredentials(){ + return new Promise((resolve, reject) => { + getPrivateKeyCredentials().then(privKey => { + myPrivKey = privKey + myPubKey = floCrypto.getPubKeyHex(myPrivKey) + myFloID = floCrypto.getFloIDfromPubkeyHex(myPubKey) + getServerPasswordCredentials().then(pass => { + serverPwd = pass + resolve('Login Credentials loaded successful') + }).catch(error => reject(error)) + }).catch(error => reject(error)) + }) + } + + function getPrivateKeyCredentials(){ + return new Promise((resolve, reject) => { + var indexArr = localStorage.getItem("privKey") + if(indexArr){ + readSharesFromIDB(JSON.parse(indexArr)) + .then(result => resolve(result)) + .catch(error => reject(error)) + }else{ + try{ + var privKey = prompt("Enter Private Key: ") + if(!privKey) + return reject("Empty Private Key") + var floID = floCrypto.getFloIDfromPubkeyHex(floCrypto.getPubKeyHex(privKey)) + console.log(floID) + alert(`Supernode floID: ${floID}`) + }catch(error){ + console.log(error) + return reject("Invalid Private Key") + } + var threshold = floCrypto.randInt(10,20) + writeSharesToIDB(floCrypto.createShamirsSecretShares(privKey, threshold, threshold)).then(resultIndexes =>{ + //store index keys in localStorage + localStorage.setItem("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)) + //resolve private Key + resolve(privKey) + }).catch(error => reject(error)) + } + }) + } + + function getServerPasswordCredentials(){ + return new Promise((resolve, reject) => { + var indexArr = localStorage.getItem("serverPass") + if(indexArr){ + readSharesFromIDB(JSON.parse(indexArr)) + .then(result => resolve(result)) + .catch(error => reject(error)) + }else{ + var serverPass = prompt("Enter Server Password: ") + if(!serverPass) + return reject("Empty Server Password") + var threshold = floCrypto.randInt(10,20) + writeSharesToIDB(floCrypto.createShamirsSecretShares(serverPass, threshold, threshold)).then(resultIndexes =>{ + //store index keys in localStorage + localStorage.setItem("serverPass", JSON.stringify(resultIndexes)) + //also add a dummy string to the IDB + var randomLength = floCrypto.randInt(serverPass.length - 5, serverPass.length + 5) + var randomString = floCrypto.randString(randomLength) + var randomThreshold = floCrypto.randInt(10,20) + writeSharesToIDB(floCrypto.createShamirsSecretShares(randomPrivKey, randomThreshold, randomThreshold)) + //resolve private Key + resolve(serverPass) + }).catch(error => reject(error)) + } + }) + } + + function readSharesFromIDB(indexArr){ + return new Promise((resolve, reject) => { + var promises = [] + for(var i = 0; i < indexArr.length; i++) + promises.push(compactIDB.readData('credentials', indexArr[i])) + Promise.all(promises).then(shares => { + var secret = floCrypto.retrieveShamirSecret(shares) + if(secret) + resolve(secret) + else + reject("Shares are insufficient or incorrect") + }).catch(error => reject(error)) + }) + } + + function writeSharesToIDB(shares, i = 0, resultIndexes = []){ + return new Promise((resolve, reject) => { + if(i >= shares.length) + return resolve(resultIndexes) + var n = floCrypto.randInt(0, 100000) + compactIDB.addData("credentials", shares[i], n).then(res => { + resultIndexes.push(n) + writeSharesToIDB(shares, i+1, resultIndexes) + .then(result => resolve(resolve)) + }).catch(error => { + writeSharesToIDB(shares, i, resultIndexes) + .then(result => resolve(resolve)) + }) + }) + } + + function clearCredentials(type = 'Both'){ + if(type === 'Both') + return clearCredentials('privKey') + clearCredentials('serverPass') + else if(type === 'privKey' || type === 'serverPass'){ + var indexArr = localStorage.getItem(type) + if(!indexArr) + return `${type} credentials not found!\n` + indexArr = JSON.parse(indexArr) + indexArr.forEach(i => compactIDB.removeData('credentials', indexArr[i])) + localStorage.removeItem(type) + return `${type} credentials deleted!\n` + } + } function initIndexedDBforSupernodeConfig(){ return new Promise((resolve, reject) => { @@ -6071,6 +6813,7 @@ var snObj = { lastTx:{}, config:{}, + credentials:{}, supernodes:{ indexes:{ uri:null, pubKey:null } }, @@ -6079,8 +6822,8 @@ indexes:{ app:null, subAdminID:null } } } - compactIDB.setDefaultDB("SupernodeConfig") - compactIDB.initDB("SupernodeConfig", snObj) + compactIDB.setDefaultDB("SupernodeUtil") + compactIDB.initDB("SupernodeUtil", snObj) .then(result => resolve("Initiated supernode configuration IDB")) .catch(error => reject(error)); })