decryption for socket gun rpc

This commit is contained in:
Daniel Lugo 2020-10-07 15:06:13 -04:00
parent b8199660ca
commit 9069ae0921

View file

@ -12,7 +12,16 @@ const {
getUser,
isAuthenticated
} = require('../services/gunDB/Mediator')
const { deepDecryptIfNeeded } = require('../services/gunDB/rpc')
/**
* @typedef {import('../services/gunDB/Mediator').SimpleSocket} SimpleSocket
* @typedef {import('../services/gunDB/contact-api/SimpleGUN').ValidDataValue} ValidDataValue
*/
/**
* @param {SimpleSocket} socket
* @param {string} subID
*/
const onPing = (socket, subID) => {
logger.warn('Subscribing to pings socket...' + subID)
@ -299,7 +308,7 @@ module.exports = (
return
}
const { $shock } = socket.handshake.query
const { $shock, publicKeyForDecryption } = socket.handshake.query
const [root, path, method] = $shock.split('::')
@ -319,12 +328,21 @@ module.exports = (
}
/**
* @param {unknown} data
* @param {ValidDataValue} data
* @param {string} key
*/
const listener = (data, key) => {
const listener = async (data, key) => {
try {
if (publicKeyForDecryption) {
const decData = await deepDecryptIfNeeded(
data,
publicKeyForDecryption
)
socket.emit('$shock', decData, key)
} else {
socket.emit('$shock', data, key)
}
} catch (err) {
logger.error(
`Error for gun rpc socket, query ${$shock} -> ${err.message}`