From 53c4092c78e1b5e933d4dfc27ec483e8cadf9ec4 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 14 Nov 2020 03:57:35 +0530 Subject: [PATCH 1/2] Edit comment feature Allow users to edit comment of the data sent by them. - Only data sent with comment field starting with 'EDIT:' can be updated. - new signature must be calculated based on the changed data (ie, existing other data content with updated comment) --- app/index.html | 70 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/app/index.html b/app/index.html index 6c55193..60b98b0 100644 --- a/app/index.html +++ b/app/index.html @@ -8064,6 +8064,8 @@ Bitcoin.Util = { processRequestFromUser(gid, uid, data); else if (data.message) processDataFromUser(gid, uid, data); + else if (data.edit) + processEditFromUser(gid, uid, data); else if (data.mark) processMarkFromUser(gid, uid, data); else if (data.delete) @@ -8081,7 +8083,6 @@ Bitcoin.Util = { } } - //Process request from users function processRequestFromUser(gid, uid, data) { let request = data.request; if (!floCrypto.validateAddr(request.receiverID)) @@ -8110,7 +8111,6 @@ Bitcoin.Util = { }) } - //Process data from users function processDataFromUser(gid, uid, data) { if (!floCrypto.validateAddr(data.receiverID)) throw Error("Invalid receiverID") @@ -8148,9 +8148,45 @@ Bitcoin.Util = { }) } + function processEditFromUser(gid, uid, data) { + if (!floCrypto.validateAddr(data.receiverID)) + throw Error("Invalid receiverID") + if (!floCrypto.validateAddr(data.requestorID)) + throw Error("Invalid requestorID") + let closeNode = floSupernode.kBucket.closestNode(data.receiverID) + if (!floGlobals.serveList.includes(closeNode)) + throw Error("Incorrect Supernode") + if (data.requestorID !== floCrypto.getFloID(data.pubKey)) + throw Error("Invalid pubKey") + let disk = data.application in floGlobals.appList ? data.application : floGlobals.defaultDisk; + compactIDB.readData(disk, data.edit.vectorClock, `SN_${closeNode}`).then(d => { + try { + if (!d.comment.startsWith("EDIT:")) + throw ("Data immutable"); + else if (d.senderID !== data.requestorID) + throw ("Invalid requestorID") + else { + d.sign = data.edit.sign; + d.comment = data.edit.comment; + d.time = data.edit.time = data.time; + let hashcontent = ["receiverID", "time", "application", "type", "message", "comment"] + .map(x => d[x]).join("|"); + if (!floCrypto.verifySign(hashcontent, data.edit.sign, data.pubKey)) + throw Error("Invalid signature") + compactIDB.writeData(disk, d, data.edit.vectorClock, `SN_${closeNode}`).then(r => { + floSupernode.supernodeClientWS.send(`@${uid}#${gid}:${JSON.stringify(true)}`) + sendBackupEdit(data.application, data.edit, closeNode) + }) + } + } catch (error) { + floSupernode.supernodeClientWS.send(`@${uid}#${gid}:${error.toString()}`) + } + }) + } + function processMarkFromUser(gid, uid, data) { if (!floCrypto.validateAddr(data.receiverID)) - throw Error("Invalid requestorID") + throw Error("Invalid receiverID") if (!(data.application in floGlobals.appList)) throw Error("Invalid application") if (!floCrypto.validateAddr(data.requestorID) || @@ -8753,7 +8789,7 @@ Bitcoin.Util = { console.info(result) resolve( `Updated Supernode Configuration` - ) + ) }).catch(error => reject(error)) }).catch(error => reject(error)) }).catch(error => reject(error)) @@ -8982,6 +9018,9 @@ Bitcoin.Util = { case "backupMark": markBackupData(data.sn_msg) break; + case "backupEdit": + editBackupData(data.sn_msg) + break; case "supernodeUp": nodeBackOnline(data.from) break; @@ -9086,6 +9125,17 @@ Bitcoin.Util = { sendToBackupNodes(sn_msg); } + function sendBackupEdit(application, edit, snID) { + var sn_msg = { + type: "backupEdit", + snID: snID, + time: Date.now(), + application: application, + edit: edit + } + sendToBackupNodes(sn_msg); + } + function sendStoredData(snID, receiver) { const sendObs = (obs, node) => new Promise((res, rej) => { compactIDB.readAllData(obs, `SN_${snID}`).then(result => { @@ -9190,6 +9240,18 @@ Bitcoin.Util = { } } + function editBackupData(data) { + if (floGlobals.storedList.includes(data.snID)) { + let disk = data.application in floGlobals.appList ? data.application : floGlobals.defaultDisk; + compactIDB.readData(disk, data.edit.vectorClock, `SN_${data.snID}`).then(d => { + d.sign = data.edit.sign; + d.time = data.edit.time; + d.comment = data.edit.comment; + compactIDB.writeData(disk, d, data.edit.vectorClock, `SN_${data.snID}`) + }) + } + } + function indicateSupernodeUp() { console.log("Indicating supernode is up") if (floGlobals.backupNodes.length) { From 6d3e28618c7145a9c2be3cc63eb8a6e0a7ba2b0d Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 19 Dec 2020 22:33:34 +0530 Subject: [PATCH 2/2] Use existing pubKey in EDIT feature --- app/index.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/index.html b/app/index.html index 60b98b0..9f68d03 100644 --- a/app/index.html +++ b/app/index.html @@ -8156,8 +8156,6 @@ Bitcoin.Util = { let closeNode = floSupernode.kBucket.closestNode(data.receiverID) if (!floGlobals.serveList.includes(closeNode)) throw Error("Incorrect Supernode") - if (data.requestorID !== floCrypto.getFloID(data.pubKey)) - throw Error("Invalid pubKey") let disk = data.application in floGlobals.appList ? data.application : floGlobals.defaultDisk; compactIDB.readData(disk, data.edit.vectorClock, `SN_${closeNode}`).then(d => { try { @@ -8171,7 +8169,7 @@ Bitcoin.Util = { d.time = data.edit.time = data.time; let hashcontent = ["receiverID", "time", "application", "type", "message", "comment"] .map(x => d[x]).join("|"); - if (!floCrypto.verifySign(hashcontent, data.edit.sign, data.pubKey)) + if (!floCrypto.verifySign(hashcontent, data.edit.sign, d.pubKey)) throw Error("Invalid signature") compactIDB.writeData(disk, d, data.edit.vectorClock, `SN_${closeNode}`).then(r => { floSupernode.supernodeClientWS.send(`@${uid}#${gid}:${JSON.stringify(true)}`)