From a2f692b9a3927fd9de5595182e9f1aa3e01f6997 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 7 Apr 2017 15:20:30 +0200 Subject: [PATCH] clean up serverside and clientside pinning logic --- rpc.js | 7 +-- www/common/pinpad.js | 113 +++++++++++++-------------------------- www/examples/pin/main.js | 15 ++++-- 3 files changed, 50 insertions(+), 85 deletions(-) diff --git a/rpc.js b/rpc.js index 7ad81acb3..e1901ea05 100644 --- a/rpc.js +++ b/rpc.js @@ -144,16 +144,11 @@ var getChannelList = function (store, publicKey, cb) { pins[pin] = false; }); - if (!parsed[1] || parsed[1].length) { - break; - } - else { + if (parsed[1] && parsed[1].length) { parsed[1].forEach(function (channel) { pins[channel] = true; }); - break; } - break; default: console.error('invalid message read from store'); diff --git a/www/common/pinpad.js b/www/common/pinpad.js index e40270311..420b65452 100644 --- a/www/common/pinpad.js +++ b/www/common/pinpad.js @@ -1,69 +1,12 @@ define([ - '/common/cryptpad-common.js', '/common/rpc.js', - '/bower_components/tweetnacl/nacl-fast.min.js' -], function (Cryptpad, Rpc) { +], function (Rpc) { var Nacl = window.nacl; - var uniqueChannelList = function (list) { - list = list || Cryptpad.getUserChannelList(); - return Cryptpad.deduplicateString(list).sort(); - }; - - var localChannelsHash = function (fileList) { - var uniqueList = uniqueChannelList(fileList); - var hash = Nacl.util.encodeBase64(Nacl - .hash(Nacl.util.decodeUTF8( JSON.stringify(uniqueList) ))); - return hash; - }; - - var getServerHash = function (rpc, edPublic, cb) { - rpc.send('GET_HASH', edPublic, function (e, hash) { - cb(e, hash[0]); - }); - }; - - var getFileSize = function (rpc, file, cb) { - rpc.send('GET_FILE_SIZE', file, cb); - }; - - var getFileListSize = function (rpc, cb) { - return rpc.send('GET_TOTAL_SIZE', undefined, cb); - }; - - var pinChannel = function (rpc, channel, cb) { - rpc.send('PIN', channel, cb); - }; - - var unpinChannel = function (rpc, channel, cb) { - rpc.send('UNPIN', channel, cb); - }; - - var reset = function (rpc, cb) { - var list = uniqueChannelList(); - rpc.send('RESET', list, cb); - }; - - /* -1. every time you want to pin or unpid a pad you send a message to the server -2. the server sends back a hash of the sorted list of your pinned pads -3. you hash your sorted list of pinned pads that you should have according to your drive -4. compare them, if same - AWESOME - if they are not - UNPIN all, send all - */ - - // Don't use create until Cryptpad is ready - // (use Cryptpad.ready) - var create = function (cb) { - // you will need to communicate with the server - // use an already established - var network = Cryptpad.getNetwork(); - - // your user proxy contains credentials you will need to make RPC calls - var proxy = Cryptpad.getStore().getProxy().proxy; + var create = function (network, proxy, cb) { + if (!network) { return void cb('INVALID_NETWORK'); } + if (!proxy) { return void cb('INVALID_PROXY'); } var edPrivate = proxy.edPrivate; var edPublic = proxy.edPublic; @@ -74,32 +17,52 @@ define([ if (e) { return void cb(e); } var exp = {}; + + // expose the supplied publicKey as an identifier exp.publicKey = edPublic; + + // expose the RPC module's raw 'send' command exp.send = rpc.send; - exp.uniqueChannelList = uniqueChannelList; + // you can ask the server to pin a particular channel for you + exp.pin = function (channel, cb) { + rpc.send('PIN', channel, cb); + }; - exp.getFileSize = function (file, cb) { - getFileSize(rpc, file, cb); + // you can also ask to unpin a particular channel + exp.unpin = function (channel, cb) { + rpc.send('UNPIN', channel, cb); }; - exp.getFileListSize = function (cb) { - getFileListSize(rpc, cb); + + // This implementation must match that on the server + // it's used for a checksum + exp.hashChannelList = function (list) { + return Nacl.util.encodeBase64(Nacl.hash(Nacl.util + .decodeUTF8(JSON.stringify(list)))); }; + + // ask the server what it thinks your hash is exp.getServerHash = function (cb) { - getServerHash(rpc, edPublic, cb); + rpc.send('GET_HASH', edPublic, function (e, hash) { + cb(e, hash[0]); + }); }; - exp.pin = function (channel, cb) { - pinChannel(rpc, channel, cb); - }; - exp.unpin = function (channel, cb) { - unpinChannel(rpc, channel, cb); + // if local and remote hashes don't match, send a reset + exp.reset = function (list, cb) { + rpc.send('RESET', list, cb); }; - exp.reset = function (cb) { - reset(rpc, cb); + + // get the total stored size of a channel's patches (in bytes) + exp.getFileSize = function (file, cb) { + rpc.send('GET_FILE_SIZE', file, cb); }; - exp.localChannelsHash = localChannelsHash; + // get the combined size of all channels (in bytes) for all the + // channels which the server has pinned for your publicKey + exp.getFileListSize = function (cb) { + rpc.send('GET_TOTAL_SIZE', undefined, cb); + }; cb(e, exp); }); diff --git a/www/examples/pin/main.js b/www/examples/pin/main.js index 0500f95b0..abb3ae73f 100644 --- a/www/examples/pin/main.js +++ b/www/examples/pin/main.js @@ -10,12 +10,16 @@ define([ }; var synchronize = function (call) { - var localHash = call.localChannelsHash(); + // provide a sorted list of unique channels + var list = Cryptpad.deduplicateString(Cryptpad.getUserChannelList()) + .sort(); + + var localHash = call.hashChannelList(list); var serverHash; call.getFileListSize(function (e, bytes) { if (e) { return void console.error(e); } - console.log("%s total bytes used", bytes); + console.log("total %sK bytes used", bytes / 1000); }); call.getServerHash(function (e, hash) { @@ -26,7 +30,7 @@ define([ return console.log("all your pads are pinned. There is nothing to do"); } - call.reset(function (e, response) { + call.reset(list, function (e, response) { if (e) { return console.error(e); } else { return console.log('reset pin list. new hash is [%s]', response); @@ -37,7 +41,10 @@ define([ $(function () { Cryptpad.ready(function (err, env) { - Pinpad.create(function (e, call) { + var network = Cryptpad.getNetwork(); + var proxy = Cryptpad.getStore().getProxy().proxy; + + Pinpad.create(network, proxy, function (e, call) { if (e) { return void console.error(e); } synchronize(call); });