replace ad-hoc response handler with Util.response

This commit is contained in:
ansuz
2020-03-17 16:52:41 -04:00
parent 9e85a1411e
commit 5467e1ffac
2 changed files with 55 additions and 44 deletions

View File

@@ -9,39 +9,43 @@ const Nacl = require('tweetnacl/nacl-fast');
console.log('New child process', process.pid);
process.on('message', function (data) {
console.log('In process', process.pid);
console.log(+new Date(), "Message received by subprocess");
//console.log('In process', process.pid);
//console.log(+new Date(), "Message received by subprocess");
if (!data || !data.key || !data.msg || !data.txid) {
process.send({
return void process.send({
error:'E_INVAL'
});
return;
}
const txid = data.txid;
const signedMsg = Nacl.util.decodeBase64(data.msg);
var signedMsg;
try {
signedMsg = Nacl.util.decodeBase64(data.msg);
} catch (e) {
return void process.send({
txid: txid,
error: 'E_BAD_MESSAGE',
});
}
var validateKey;
try {
validateKey = Nacl.util.decodeBase64(data.key);
} catch (e) {
process.send({
return void process.send({
txid: txid,
error:'E_BADKEY'
});
return;
}
// validate the message
const validated = Nacl.sign.open(signedMsg, validateKey);
if (!validated) {
process.send({
return void process.send({
txid: txid,
error:'FAILED'
});
return;
}
console.log(+new Date(), "Verification done in the subprocess");
process.send({
txid: txid,
success: true
});
});