cryptpad/www/common/pinpad.js

136 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-03-13 09:56:08 +00:00
define([
'/common/cryptpad-common.js',
'/common/rpc.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Cryptpad, Rpc) {
var Nacl = window.nacl;
2017-04-05 15:28:04 +00:00
var uniqueChannelList = function (list) {
list = list || Cryptpad.getUserChannelList();
return Cryptpad.deduplicateString(list).sort();
};
2017-04-04 10:13:31 +00:00
2017-04-05 15:28:04 +00:00
var localChannelsHash = function (fileList) {
var uniqueList = uniqueChannelList(fileList);
2017-04-04 10:13:31 +00:00
var hash = Nacl.util.encodeBase64(Nacl
.hash(Nacl.util.decodeUTF8( JSON.stringify(uniqueList) )));
return hash;
};
2017-04-05 08:42:24 +00:00
var getServerHash = function (rpc, edPublic, cb) {
2017-04-05 15:28:04 +00:00
rpc.send('GET_HASH', edPublic, function (e, hash) {
cb(e, hash[0]);
});
2017-04-04 10:13:31 +00:00
};
var getFileSize = function (rpc, file, cb) {
rpc.send('GET_FILE_SIZE', file, cb);
};
2017-04-04 10:13:31 +00:00
var getFileListSize = function (rpc, list, cb) {
var bytes = 0;
var left = list.length;
list.forEach(function (chan) {
getFileSize(rpc, chan, function (e, msg) {
if (e) {
if (e === 'ENOENT') {
// these channels no longer exists on the server
console.log(e, chan);
} else {
console.error(e);
}
} else if (msg && msg[0] && typeof(msg[0]) === 'number') {
bytes += msg[0];
//console.log(bytes);
} else {
console.log("returned message was not a number: ", msg);
}
--left;
if (left === 0) {
cb(void 0, bytes);
}
2017-03-13 09:56:08 +00:00
});
2017-04-04 10:13:31 +00:00
});
};
2017-03-13 09:56:08 +00:00
2017-04-05 15:28:04 +00:00
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) {
rpc.send('RESET', undefined, cb);
};
2017-03-13 09:56:08 +00:00
/*
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
*/
2017-04-04 10:13:31 +00:00
// 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 edPrivate = proxy.edPrivate;
var edPublic = proxy.edPublic;
if (!(edPrivate && edPublic)) { return void cb('INVALID_KEYS'); }
Rpc.create(network, edPrivate, edPublic, function (e, rpc) {
if (e) { return void cb(e); }
var exp = {};
exp.publicKey = edPublic;
exp.send = rpc.send;
2017-04-05 15:28:04 +00:00
exp.uniqueChannelList = uniqueChannelList;
2017-04-04 10:13:31 +00:00
exp.getFileSize = function (file, cb) {
getFileSize(rpc, file, cb);
};
exp.getFileListSize = function (list, cb) {
getFileListSize(rpc, list, cb);
};
exp.getServerHash = function (cb) {
getServerHash(rpc, edPublic, cb);
};
2017-03-13 09:56:08 +00:00
2017-04-05 15:28:04 +00:00
exp.pin = function (channel, cb) {
pinChannel(rpc, channel, cb);
};
exp.unpin = function (channel, cb) {
unpinChannel(rpc, channel, cb);
};
exp.reset = function (cb) {
reset(rpc, cb);
};
exp.localChannelsHash = localChannelsHash;
2017-04-04 10:13:31 +00:00
cb(e, exp);
});
2017-03-13 09:56:08 +00:00
};
return { create: create };
});