2017-03-11 01:03:15 +08:00
|
|
|
/* Use Nacl for checking signatures of messages
|
|
|
|
|
|
|
|
*/
|
|
|
|
var Nacl = require("tweetnacl");
|
|
|
|
|
|
|
|
var RPC = module.exports;
|
|
|
|
|
|
|
|
var pin = function (ctx, cb) { };
|
|
|
|
var unpin = function (ctx, cb) { };
|
|
|
|
var getHash = function (ctx, cb) { };
|
|
|
|
var getTotalSize = function (ctx, cb) { };
|
|
|
|
var getFileSize = function (ctx, cb) { };
|
|
|
|
|
2017-03-15 22:55:25 +08:00
|
|
|
var isValidChannel = function (chan) {
|
|
|
|
return /^[a-fA-F0-9]/.test(chan);
|
|
|
|
};
|
|
|
|
|
2017-03-16 20:07:42 +08:00
|
|
|
var checkSignature = function (signedMsg, publicKey) {
|
|
|
|
if (!(signedMsg && publicKey)) { return null; }
|
|
|
|
|
|
|
|
var signedBuffer = Nacl.util.decodeBase64(signedMsg);
|
|
|
|
var pubBuffer = Nacl.util.decodeBase64(publicKey);
|
|
|
|
|
|
|
|
var opened = Nacl.sign.open(signedBuffer, pubBuffer);
|
|
|
|
|
|
|
|
if (opened) {
|
|
|
|
var decoded = Nacl.util.encodeUTF8(opened);
|
|
|
|
try {
|
|
|
|
return JSON.parse(decoded);
|
|
|
|
} catch (e) { } // fall through to return
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-03-11 01:03:15 +08:00
|
|
|
RPC.create = function (config, cb) {
|
|
|
|
// load pin-store...
|
|
|
|
|
|
|
|
console.log('loading rpc module...');
|
2017-03-16 20:07:42 +08:00
|
|
|
var rpc = function (ctx, args, respond) {
|
|
|
|
if (args.length < 2) {
|
|
|
|
return void respond("INSUFFICIENT_ARGS");
|
|
|
|
}
|
|
|
|
|
|
|
|
var signed = args[0];
|
|
|
|
var publicKey = args[1];
|
|
|
|
|
|
|
|
var msg = checkSignature(signed, publicKey);
|
|
|
|
if (!msg) {
|
|
|
|
return void respond("INVALID_SIGNATURE");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(msg) !== 'object') {
|
|
|
|
return void respond('INVALID_MSG');
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:03:15 +08:00
|
|
|
switch (msg[0]) {
|
|
|
|
case 'ECHO':
|
2017-03-16 21:43:05 +08:00
|
|
|
return void respond(void 0, msg);
|
|
|
|
case 'RESET':
|
|
|
|
return void respond('NOT_IMPLEMENTED', msg);
|
2017-03-11 01:03:15 +08:00
|
|
|
case 'PIN':
|
2017-03-16 21:43:05 +08:00
|
|
|
return void respond('NOT_IMPLEMENTED', msg);
|
2017-03-11 01:03:15 +08:00
|
|
|
case 'UNPIN':
|
2017-03-16 21:43:05 +08:00
|
|
|
return void respond('NOT_IMPLEMENTED', msg);
|
2017-03-11 01:03:15 +08:00
|
|
|
case 'GET_HASH':
|
2017-03-16 21:43:05 +08:00
|
|
|
return void respond('NOT_IMPLEMENTED', msg);
|
2017-03-11 01:03:15 +08:00
|
|
|
case 'GET_TOTAL_SIZE':
|
2017-03-16 21:43:05 +08:00
|
|
|
return void respond('NOT_IMPLEMENTED', msg);
|
2017-03-11 01:03:15 +08:00
|
|
|
case 'GET_FILE_SIZE':
|
2017-03-15 22:55:25 +08:00
|
|
|
if (!isValidChannel(msg[1])) {
|
|
|
|
return void respond('INVALID_CHAN');
|
|
|
|
}
|
|
|
|
|
|
|
|
return void ctx.store.getChannelSize(msg[1], function (e, size) {
|
|
|
|
if (e) { return void respond(e.code); }
|
|
|
|
respond(void 0, size);
|
|
|
|
});
|
2017-03-11 01:03:15 +08:00
|
|
|
default:
|
2017-03-16 21:43:05 +08:00
|
|
|
return void respond('UNSUPPORTED_RPC_CALL', msg);
|
2017-03-11 01:03:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cb(void 0, rpc);
|
|
|
|
};
|
|
|
|
|