pokemon-showdown/verifier.js
Guangcong Luo 4dc62498f9 Fix TypeScript not finding config
It took me way too long to realize the Travis build error was a
TypeScript error, because TypeScript runs before PS generates a Config
file.

There are two fixes, we could either run mocha first, or we could
suppress the TypeScript errors. Suppressing TypeScript errors always
makes me feel bad, but I think it's more useful to everyone if they're
shown before running Mocha tests.
2018-01-18 04:55:15 -06:00

62 lines
1.6 KiB
JavaScript

/**
* Verifier process
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This is just an asynchronous implementation of a verifier for a
* signed key, because Node.js's crypto functions are synchronous,
* strangely, considering how everything else is asynchronous.
*
* I wrote this one day hoping it would help with performance, but
* I don't think it had any noticeable effect.
*
* @license MIT
*/
'use strict';
const crypto = require('crypto');
/*********************************************************
* Process manager
*********************************************************/
const QueryProcessManager = require('./lib/process-manager').QueryProcessManager;
const PM = new QueryProcessManager(module, async ({data, signature}) => {
let verifier = crypto.createVerify(Config.loginserverkeyalgo);
verifier.update(data);
let success = false;
try {
success = verifier.verify(Config.loginserverpublickey, signature, 'hex');
} catch (e) {}
return success;
});
if (!PM.isParentProcess) {
// This is a child process!
// @ts-ignore
global.Config = require('./config/config');
require('./lib/repl').start('verifier', /** @param {string} cmd */ cmd => eval(cmd));
} else {
PM.spawn(global.Config ? Config.verifierprocesses : 1);
}
/*********************************************************
* Exports
*********************************************************/
/**
* @param {string} data
* @param {string} signature
* @return {Promise<boolean>}
*/
function verify(data, signature) {
return PM.query({data, signature});
}
module.exports = {
verify,
PM,
};