diff options
author | Ev Bogue <ev@evbogue.com> | 2019-09-07 11:41:14 -0500 |
---|---|---|
committer | Ev Bogue <ev@evbogue.com> | 2019-09-07 11:41:14 -0500 |
commit | f5498fe3581548902a72c6b4943a9717079198f5 (patch) | |
tree | 30411d557913a899cdc998e2c408aca8558f0780 | |
parent | 07c88bb0a329cff80d9d6acfaa292d2cc40a79cc (diff) |
and we have our first version of secure bogbook (sbb) working!
-rw-r--r-- | gossip.js | 87 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | server.js | 32 |
3 files changed, 102 insertions, 19 deletions
@@ -40,11 +40,11 @@ function sync (subs, keys) { var req = JSON.parse(message.data) console.log(req) unbox(req.box, req.requester, keys).then(unboxed => { - var nextreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) - console.log(nextreq) - if (nextreq.seq === 0) { - var feed = JSON.stringify(srclog) - box(feed, serverpub, keys).then(boxed => { + var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) + console.log(unboxedreq) + if (unboxedreq.seq === 0) { + var stringedfeed = JSON.stringify(srclog) + box(stringedfeed, serverpub, keys).then(boxed => { var obj = { requester: keys.publicKey, box: boxed @@ -54,6 +54,68 @@ function sync (subs, keys) { ws.send(JSON.stringify(obj)) }) } + + if (unboxedreq.seq > msg.seq) { + console.log('server feed is longer, requesting diff from server') + var reqdiff = JSON.stringify({author: unboxedreq.author, seq: msg.seq}) + box(reqdiff, serverpub, keys).then(boxed => { + var obj = { + requester: keys.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + }) + } + + if (unboxedreq.seq < msg.seq) { + console.log('server feed is shorter, sending diff to server') + var diff = JSON.stringify(srclog.slice(0, msg.seq - unboxedreq.seq)) + box(diff, serverpub, keys).then(boxed => { + var obj = { + requester: keys.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + }) + } + + if (Array.isArray(unboxedreq)) { + console.log('received diff from server') + open(unboxedreq[0]).then(msg => { + localforage.getItem(msg.author).then(feed => { + open(feed[0]).then(lastmsg => { + if (unboxedreq.length + lastmsg.seq === msg.seq) { + console.log('combinable feeds') + var newlog = unboxedreq.concat(feed) + localforage.setItem(msg.author, newlog).then(success => { + console.log('combined existing feed with diff and saved to client') + }) + localforage.getItem('log').then(log => { + if (!log) { + var log = [] + } + for (var i = unboxedreq.length -1; i >= 0; --i) { + open(unboxedreq[i]).then(opened => { + log.unshift(opened) + var scroller = document.getElementById('scroller') + scroller.insertBefore(render(opened, keys), scroller.childNodes[2]) + if (unboxedreq.length + lastmsg.seq === opened.seq) { + log.sort((a, b) => a.timestamp - b.timestamp) + var reversed = log.reverse() + localforage.setItem('log', reversed).then(success => { + console.log('saved log with ' + opened.author + ' prepended to localForage') + }) + } + }) + } + }) + } + }) + }) + }) + + } + }) } }) @@ -61,13 +123,11 @@ function sync (subs, keys) { console.log('NO LOG IN CLIENT') ws.onopen = function () { var reqwhole = JSON.stringify({author: sub, seq: 0}) - console.log(reqwhole) box(reqwhole, serverpub, keys).then(boxed => { var obj = { requester: keys.publicKey, box: boxed } - console.log(boxed) ws.send(JSON.stringify(obj)) }) } @@ -76,25 +136,26 @@ function sync (subs, keys) { console.log('received message from ' + req.requester) unbox(req.box, req.requester, keys).then(unboxed => { var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) - console.log(unboxedreq) if (Array.isArray(unboxedreq)) { open(unboxedreq[0]).then(msg => { - console.log(msg.seq) localforage.getItem(msg.author).then(feed => { if (!feed) { localforage.setItem(msg.author, unboxedreq).then(success => { console.log('saved log of ' + msg.author + ' to localforage') }) localforage.getItem('log').then(log => { + if (!log) { + var log = [] + } for (var i = unboxedreq.length -1; i >= 0; --i) { - console.log(i) open(unboxedreq[i]).then(opened => { log.unshift(opened) var scroller = document.getElementById('scroller') scroller.insertBefore(render(opened, keys), scroller.childNodes[2]) - console.log(opened) - if (opened.seq === unboxedreq.length) { - localforage.setItem('log', log).then(success => { + if (opened.seq === unboxedreq.length) { + log.sort((a, b) => a.timestamp - b.timestamp) + var reversed = log.reverse() + localforage.setItem('log', reversed).then(success => { console.log('saved log with ' + opened.author + ' prepended to localForage') }) } diff --git a/package.json b/package.json index 7d83efc..e80e800 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bogbook", - "version": "1.4.0", + "version": "1.5.0", "description": "secure blockchain logging (blogging, without the l) -- bogging", "main": "server.js", "scripts": { @@ -59,7 +59,7 @@ bog.keys().then(key => { if (unboxedreq.seq > msg.seq) { // right now the client is still sending the entire log, which works just fine but isn't optimal console.log('client feed is longer, requesting diff from client') - var reqdiff = JSON.stringify({author: unboxedreq.author, seq: 0}) + var reqdiff = JSON.stringify({author: unboxedreq.author, seq: msg.seq}) bog.box(reqdiff, req.requester, key).then(boxed => { var obj = { requester: key.publicKey, @@ -71,8 +71,15 @@ bog.keys().then(key => { if (unboxedreq.seq < msg.seq) { console.log('client feed is shorter, sending diff to client') - var diff = feed.slice(0, unboxedreq.seq) + var diff = JSON.stringify(feed.slice(0, msg.seq - unboxedreq.seq)) console.log(diff) + bog.box(diff, req.requester, key).then(boxed => { + var obj = { + requester: key.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + }) } }) } else { @@ -92,14 +99,29 @@ bog.keys().then(key => { } else if (Array.isArray(unboxedreq)) { // first check to make sure that we have an entire log bog.open(unboxedreq[0]).then(msg => { + console.log(msg) if (msg.seq === unboxedreq.length) { fs.writeFile(__dirname + '/bogs/' + msg.author, JSON.stringify(unboxedreq), 'UTF-8', function (err, success) { - // TODO this should check to make sure the sequence number is equal to the length of the array, otherwise we might have bunk feed console.log('Saved full log of ' + msg.author + ' sent by ' + req.requester) }) - } + } if (msg.seq > unboxedreq.length) { + fs.readFile(__dirname + '/bogs/' + msg.author, 'UTF-8', function (err, data) { + console.log('read existing feed from disk') + var feed = JSON.parse(data) + bog.open(feed[0]).then(lastmsg => { + console.log(msg.seq) + console.log(lastmsg.seq) + if (unboxedreq.length + lastmsg.seq === msg.seq) { + console.log('combinable feeds') + var newlog = unboxedreq.concat(feed) + fs.writeFile(__dirname + '/bogs/' + msg.author, JSON.stringify(newlog), 'UTF-8', function (err, success) { + console.log('combined existing feed with diff and saved to server') + }) + } + }) + }) + } }) - // TODO if we have a partial log then load the log we have, concat, and then save the log } }) }) |