cryptpad/www/common/pinpad.js

109 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-03-13 09:56:08 +00:00
define([
'/common/rpc.js',
], function (Rpc) {
var create = function (network, proxy, cb) {
2017-04-12 14:02:42 +00:00
if (!network) {
window.setTimeout(function () {
cb('INVALID_NETWORK');
});
return;
}
if (!proxy) {
window.setTimeout(function () {
cb('INVALID_PROXY');
});
return;
}
2017-04-04 10:13:31 +00:00
var edPrivate = proxy.edPrivate;
var edPublic = proxy.edPublic;
2017-04-12 14:02:42 +00:00
if (!(edPrivate && edPublic)) {
window.setTimeout(function () {
cb('INVALID_KEYS');
});
return;
}
2017-04-04 10:13:31 +00:00
Rpc.create(network, edPrivate, edPublic, function (e, rpc) {
if (e) { return void cb(e); }
var exp = {};
// expose the supplied publicKey as an identifier
2017-04-04 10:13:31 +00:00
exp.publicKey = edPublic;
// expose the RPC module's raw 'send' command
2017-04-04 10:13:31 +00:00
exp.send = rpc.send;
// you can ask the server to pin a particular channel for you
2017-04-10 15:38:25 +00:00
exp.pin = function (channels, cb) {
// TODO use isArray if it's safe
if (!channels && channels.length) {
window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
2017-04-10 15:38:25 +00:00
rpc.send('PIN', channels, cb);
};
2017-04-05 15:28:04 +00:00
// you can also ask to unpin a particular channel
2017-04-10 15:38:25 +00:00
exp.unpin = function (channels, cb) {
// TODO use isArray if it's safe
if (!channels && channels.length) {
window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
2017-04-10 15:38:25 +00:00
rpc.send('UNPIN', channels, cb);
2017-04-04 10:13:31 +00:00
};
// ask the server what it thinks your hash is
2017-04-04 10:13:31 +00:00
exp.getServerHash = function (cb) {
rpc.send('GET_HASH', edPublic, function (e, hash) {
2017-04-10 15:38:25 +00:00
if (!(hash && hash[0])) {
return void cb('NO_HASH_RETURNED');
}
cb(e, hash[0]);
});
2017-04-04 10:13:31 +00:00
};
2017-03-13 09:56:08 +00:00
// if local and remote hashes don't match, send a reset
exp.reset = function (channels, cb) {
// TODO use isArray if it's safe
if (!channels && channels.length) {
window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
rpc.send('RESET', channels, function (e, response) {
cb(e, response[0]);
});
2017-04-05 15:28:04 +00:00
};
// get the total stored size of a channel's patches (in bytes)
exp.getFileSize = function (file, cb) {
rpc.send('GET_FILE_SIZE', file, cb);
2017-04-05 15:28:04 +00:00
};
// 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, function (e, response) {
if (e) { return void cb(e); }
if (response && response.length) {
cb(void 0, response[0]);
}
});
};
2017-04-05 15:28:04 +00:00
2017-04-04 10:13:31 +00:00
cb(e, exp);
});
2017-03-13 09:56:08 +00:00
};
return { create: create };
});