Browse Source

and we have our first version of secure bogbook (sbb) working!

english
Ev Bogue 3 years ago
parent
commit
f5498fe358
  1. 87
      gossip.js
  2. 2
      package.json
  3. 32
      server.js

87
gossip.js

@ -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')
})
}

2
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": {

32
server.js

@ -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
}
})
})

Loading…
Cancel
Save