verificarPayload.js
536 Bytes
const createError = require('http-errors')
const EC = require('elliptic').ec
const ec = new EC('secp256k1')
const { clientPoint } = require('../keys')
const clientPub = ec.keyFromPublic(clientPoint)
const verificar = (nonce, firma) => clientPub.verify(nonce, new Buffer(firma, 'hex'))
module.exports = (req, res, next) => {
const { firma } = req.params
const payload = JSON.stringify(req.body)
const valido = verificar(payload, firma)
if (!valido ) return next(createError(403, ['La firma no es valida']))
return next()
}