41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
|
|
const Nacl = require('tweetnacl/nacl-fast');
|
||
|
|
|
||
|
|
// TODO if this process is using too much CPU, we can use "cluster" to add load balancing to this code
|
||
|
|
|
||
|
|
process.on('message', function (data) {
|
||
|
|
console.log(+new Date(), "Message received by subprocess");
|
||
|
|
if (!data || !data.key || !data.msg || !data.txid) {
|
||
|
|
process.send({
|
||
|
|
error:'E_INVAL'
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const txid = data.txid;
|
||
|
|
|
||
|
|
const signedMsg = Nacl.util.decodeBase64(data.msg);
|
||
|
|
var validateKey;
|
||
|
|
try {
|
||
|
|
validateKey = Nacl.util.decodeBase64(data.key);
|
||
|
|
} catch (e) {
|
||
|
|
process.send({
|
||
|
|
txid: txid,
|
||
|
|
error:'E_BADKEY'
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// validate the message
|
||
|
|
const validated = Nacl.sign.open(signedMsg, validateKey);
|
||
|
|
if (!validated) {
|
||
|
|
process.send({
|
||
|
|
txid: txid,
|
||
|
|
error:'FAILED'
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
console.log(+new Date(), "Verification done in the subprocess");
|
||
|
|
process.send({
|
||
|
|
txid: txid,
|
||
|
|
success: true
|
||
|
|
});
|
||
|
|
});
|