cryptpad/www/common/rpc.js

139 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-03-07 16:33:31 +00:00
define([
'/bower_components/tweetnacl/nacl-fast.min.js',
2017-03-27 16:15:15 +00:00
], function () {
var MAX_LAG_BEFORE_TIMEOUT = 30000;
2017-03-16 13:45:38 +00:00
var Nacl = window.nacl;
2017-03-07 16:33:31 +00:00
var uid = function () {
return Number(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))
.toString(32).replace(/\./g, '');
2017-03-07 16:33:31 +00:00
};
2017-03-27 16:15:15 +00:00
var signMsg = function (data, signKey) {
var buffer = Nacl.util.decodeUTF8(JSON.stringify(data));
return Nacl.util.encodeBase64(Nacl.sign.detached(buffer, signKey));
2017-03-16 13:45:38 +00:00
};
2017-03-27 16:15:15 +00:00
/*
types of messages:
pin -> hash
unpin -> hash
getHash -> hash
getTotalSize -> bytes
getFileSize -> bytes
*/
2017-03-27 16:15:15 +00:00
var sendMsg = function (ctx, data, cb) {
var network = ctx.network;
var hkn = network.historyKeeper;
var txid = uid();
ctx.pending[txid] = cb;
2017-03-27 16:15:15 +00:00
return network.sendto(hkn, JSON.stringify([txid, data]));
2017-03-07 16:33:31 +00:00
};
var parse = function (msg) {
try {
return JSON.parse(msg);
} catch (e) {
return null;
}
};
2017-03-07 16:33:31 +00:00
var onMsg = function (ctx, msg) {
var parsed = parse(msg);
if (!parsed) {
2017-03-27 16:15:15 +00:00
return void console.error(new Error('could not parse message: %s', msg));
}
var txid = parsed[0];
var cookie = parsed[1];
var pending = ctx.pending[txid];
2017-03-27 16:15:15 +00:00
if (!(parsed && parsed.slice)) {
2017-04-07 15:21:04 +00:00
// RPC responses are arrays. this message isn't meant for us.
return;
2017-03-27 16:15:15 +00:00
}
var response = parsed.slice(2);
if (typeof(pending) === 'function') {
if (parsed[1] === 'ERROR') {
return void pending(parsed[2]);
} else {
// update the cookie
if (/\|/.test(cookie)) {
if (ctx.cookie !== cookie) {
ctx.cookie = cookie;
}
}
}
pending(void 0, response);
}
2017-04-07 15:21:04 +00:00
//else { console.log("No callback provided"); }
2017-03-07 16:33:31 +00:00
};
2017-04-03 17:24:57 +00:00
var create = function (network, edPrivateKey, edPublicKey, cb) {
2017-03-27 16:15:15 +00:00
var signKey;
2017-03-07 16:33:31 +00:00
2017-03-16 13:45:38 +00:00
try {
2017-03-27 16:15:15 +00:00
signKey = Nacl.util.decodeBase64(edPrivateKey);
2017-03-16 13:45:38 +00:00
if (signKey.length !== 64) {
throw new Error('private key did not match expected length of 64');
}
2017-04-03 17:24:57 +00:00
} catch (err) {
return void cb(err);
}
2017-03-16 13:45:38 +00:00
2017-03-27 16:15:15 +00:00
var pubBuffer;
try {
pubBuffer = Nacl.util.decodeBase64(edPublicKey);
if (pubBuffer.length !== 32) {
2017-04-03 17:24:57 +00:00
return void cb('expected public key to be 32 uint');
2017-03-27 16:15:15 +00:00
}
2017-04-03 17:24:57 +00:00
} catch (err) {
return void cb(err);
}
2017-03-16 13:45:38 +00:00
2017-03-07 16:33:31 +00:00
var ctx = {
network: network,
timeouts: {}, // timeouts
pending: {}, // callbacks
2017-03-27 16:15:15 +00:00
cookie: null,
};
var send = function (type, msg, cb) {
2017-03-16 13:45:38 +00:00
// construct a signed message...
var data = [type, msg];
2017-04-03 17:24:57 +00:00
if (ctx.cookie && ctx.cookie.join) {
data.unshift(ctx.cookie.join('|'));
2017-04-03 17:24:57 +00:00
} else {
data.unshift(ctx.cookie);
}
var sig = signMsg(data, signKey);
2017-03-27 16:15:15 +00:00
data.unshift(edPublicKey);
data.unshift(sig);
// [sig, edPublicKey, cookie, type, msg]
return sendMsg(ctx, data, cb);
};
network.on('message', function (msg, sender) {
onMsg(ctx, msg);
});
2017-04-03 17:24:57 +00:00
send('COOKIE', "", function (e, msg) {
if (e) { return void cb(e); }
// callback to provide 'send' method to whatever needs it
cb(void 0, { send: send, });
2017-04-03 17:24:57 +00:00
});
2017-03-07 16:33:31 +00:00
};
return { create: create };
});