Check signature for history keeper in a different process

This commit is contained in:
yflory
2020-03-17 13:29:53 +01:00
parent ab6ccfe1f6
commit 0d636dabc9
3 changed files with 81 additions and 13 deletions

40
lib/check-signature.js Normal file
View File

@@ -0,0 +1,40 @@
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
});
});