Merge pull request #324 from shocknet/bug/socket-args

Fixed socket emits not sending multiple arguments
This commit is contained in:
CapDog 2021-04-02 13:52:39 -04:00 committed by GitHub
commit d8102a3377
4 changed files with 14 additions and 9 deletions

View file

@ -226,6 +226,7 @@ const Config = require('../config')
/** /**
* @typedef {import('../contact-api/SimpleGUN').GUNNode} GUNNode * @typedef {import('../contact-api/SimpleGUN').GUNNode} GUNNode
* @typedef {import('../contact-api/SimpleGUN').UserGUNNode} UserGUNNode * @typedef {import('../contact-api/SimpleGUN').UserGUNNode} UserGUNNode
* @typedef {import('../contact-api/SimpleGUN').ValidDataValue} ValidDataValue
*/ */
// TO DO: move to common repo // TO DO: move to common repo
@ -254,7 +255,7 @@ const Config = require('../config')
// TO DO: move to common repo // TO DO: move to common repo
/** /**
* @typedef {object} SimpleSocket * @typedef {object} SimpleSocket
* @prop {(eventName: string, data?: Emission|EncryptedEmissionLegacy|EncryptedEmission) => void} emit * @prop {(eventName: string, data?: Emission|EncryptedEmissionLegacy|EncryptedEmission|ValidDataValue) => void} emit
* @prop {(eventName: string, handler: (data: any) => void) => void} on * @prop {(eventName: string, handler: (data: any) => void) => void} on
* @prop {{ query: { 'x-shockwallet-device-id': string, encryptionId: string }}} handshake * @prop {{ query: { 'x-shockwallet-device-id': string, encryptionId: string }}} handshake
*/ */

View file

@ -296,9 +296,9 @@ module.exports = (
publicKeyForDecryption publicKeyForDecryption
) )
emit('$shock', decData) emit('$shock', decData, key)
} else { } else {
emit('$shock', data) emit('$shock', data, key)
} }
} catch (err) { } catch (err) {
logger.error( logger.error(

View file

@ -83,10 +83,11 @@ const authorizeDevice = async ({ deviceId, publicKey }) => {
/** /**
* Encrypts the specified message using the specified deviceId's * Encrypts the specified message using the specified deviceId's
* public key * public key
* @param {{ deviceId: string, message: string }} arg0 * @param {{ deviceId: string, message: string | number | boolean }} arg0
* @returns {Promise<import('./crypto').EncryptedMessageResponse>} * @returns {Promise<import('./crypto').EncryptedMessageResponse>}
*/ */
const encryptMessage = async ({ message = '', deviceId }) => { const encryptMessage = async ({ message = '', deviceId }) => {
const parsedMessage = message.toString()
const publicKey = devicePublicKeys.get(deviceId) const publicKey = devicePublicKeys.get(deviceId)
if (!publicKey) { if (!publicKey) {
@ -97,7 +98,7 @@ const encryptMessage = async ({ message = '', deviceId }) => {
} }
const processedPublicKey = processKey(publicKey) const processedPublicKey = processKey(publicKey)
const messageBuffer = convertUTF8ToBuffer(message) const messageBuffer = convertUTF8ToBuffer(parsedMessage)
const encryptedMessage = await ECCrypto.encrypt( const encryptedMessage = await ECCrypto.encrypt(
processedPublicKey, processedPublicKey,
messageBuffer messageBuffer

View file

@ -19,6 +19,7 @@ const nonEncryptedEvents = [
* @typedef {import('../../services/gunDB/Mediator').Emission} Emission * @typedef {import('../../services/gunDB/Mediator').Emission} Emission
* @typedef {import('../../services/gunDB/Mediator').EncryptedEmission} EncryptedEmission * @typedef {import('../../services/gunDB/Mediator').EncryptedEmission} EncryptedEmission
* @typedef {import('../../services/gunDB/Mediator').EncryptedEmissionLegacy} EncryptedEmissionLegacy * @typedef {import('../../services/gunDB/Mediator').EncryptedEmissionLegacy} EncryptedEmissionLegacy
* @typedef {import('../../services/gunDB/contact-api/SimpleGUN').ValidDataValue} ValidDataValue
*/ */
/** /**
@ -28,7 +29,7 @@ const isNonEncrypted = eventName => nonEncryptedEvents.includes(eventName)
/** /**
* @param {SimpleSocket} socket * @param {SimpleSocket} socket
* @returns {(eventName: string, args?: Emission | EncryptedEmission | EncryptedEmissionLegacy) => Promise<void>} * @returns {(eventName: string, ...args: any[]) => Promise<void>}
*/ */
const encryptedEmit = socket => async (eventName, ...args) => { const encryptedEmit = socket => async (eventName, ...args) => {
try { try {
@ -55,19 +56,21 @@ const encryptedEmit = socket => async (eventName, ...args) => {
} }
const encryptedArgs = await Promise.all( const encryptedArgs = await Promise.all(
args.map(data => { args.map(async data => {
if (!data) { if (!data) {
return data return data
} }
return ECC.encryptMessage({ const encryptedMessage = await ECC.encryptMessage({
message: typeof data === 'object' ? JSON.stringify(data) : data, message: typeof data === 'object' ? JSON.stringify(data) : data,
deviceId deviceId
}) })
return encryptedMessage
}) })
) )
console.log('Encrypted args:', encryptedArgs) console.log('Encrypted args:', encryptedArgs, args)
return socket.emit(eventName, ...encryptedArgs) return socket.emit(eventName, ...encryptedArgs)
} catch (err) { } catch (err) {