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