From 9069ae092175c31df98d630f38eb873e90da8c54 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 7 Oct 2020 15:06:13 -0400 Subject: [PATCH] decryption for socket gun rpc --- src/sockets.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/sockets.js b/src/sockets.js index 787e176c..5a8b4890 100644 --- a/src/sockets.js +++ b/src/sockets.js @@ -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 { - socket.emit('$shock', data, key) + 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}`