From 70ac41edfad71cbe06505774803ebe3dff48ad0b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 12 Feb 2020 14:36:01 -0400 Subject: [PATCH] encrypt initial msg --- services/gunDB/contact-api/actions.js | 27 +++++++++------------- services/gunDB/contact-api/events/index.js | 21 ++++------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 4df52296..b9a11a54 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -29,14 +29,6 @@ const { isHandshakeRequest } = require('./schema') */ const INITIAL_MSG = '$$__SHOCKWALLET__INITIAL__MESSAGE' -/** - * @returns {Message} - */ -const __createInitialMessage = () => ({ - body: INITIAL_MSG, - timestamp: Date.now() -}) - /** * Create a an outgoing feed. The feed will have an initial special acceptance * message. Returns a promise that resolves to the id of the newly-created @@ -60,6 +52,10 @@ const __createOutgoingFeed = async (withPublicKey, user, SEA) => { const mySecret = require('../Mediator').getMySecret() const encryptedForMeRecipientPub = await SEA.encrypt(withPublicKey, mySecret) + const ourSecret = await SEA.secret( + await Utils.pubToEpub(withPublicKey), + user._.sea + ) const maybeEncryptedForMeOutgoingFeedID = await Utils.tryAndWait( (_, user) => @@ -99,12 +95,18 @@ const __createOutgoingFeed = async (withPublicKey, user, SEA) => { throw new TypeError('typeof newOutgoingFeedID !== "string"') } + /** @type {Message} */ + const initialMsg = { + body: await SEA.encrypt(INITIAL_MSG, ourSecret), + timestamp: Date.now() + } + await new Promise((res, rej) => { user .get(Key.OUTGOINGS) .get(newOutgoingFeedID) .get(Key.MESSAGES) - .set(__createInitialMessage(), ack => { + .set(initialMsg, ack => { if (ack.err) { rej(new Error(ack.err)) } else { @@ -118,12 +120,6 @@ const __createOutgoingFeed = async (withPublicKey, user, SEA) => { mySecret ) - if (typeof encryptedForMeNewOutgoingFeedID === 'undefined') { - throw new TypeError( - "typeof encryptedForMeNewOutgoingFeedID === 'undefined'" - ) - } - await new Promise((res, rej) => { user .get(Key.RECIPIENT_TO_OUTGOING) @@ -1106,7 +1102,6 @@ const disconnect = async pub => { } module.exports = { - INITIAL_MSG, __createOutgoingFeed, acceptRequest, authenticate, diff --git a/services/gunDB/contact-api/events/index.js b/services/gunDB/contact-api/events/index.js index 2e4c98c2..83648c2e 100644 --- a/services/gunDB/contact-api/events/index.js +++ b/services/gunDB/contact-api/events/index.js @@ -30,9 +30,9 @@ const DEBOUNCE_WAIT_TIME = 500 * @param {(userToIncoming: Record) => void} cb * @param {UserGUNNode} user Pass only for testing purposes. * @param {ISEA} SEA - * @returns {Promise} + * @returns {void} */ -const __onUserToIncoming = async (cb, user, SEA) => { +const __onUserToIncoming = (cb, user, SEA) => { if (!user.is) { throw new Error(ErrorCode.NOT_AUTH) } @@ -233,17 +233,7 @@ const onIncomingMessages = (cb, userPK, incomingFeedID, gun, user, SEA) => { const secret = await SEA.secret(recipientEpub, user._.sea) let { body } = data - - if (body !== Actions.INITIAL_MSG) { - const decrypted = await SEA.decrypt(body, secret) - - if (typeof decrypted !== 'string') { - console.log("onIncommingMessages() -> typeof decrypted !== 'string'") - return - } - - body = decrypted - } + body = await SEA.decrypt(body, secret) messages[key] = { body, @@ -342,10 +332,7 @@ const onOutgoing = cb => { return } newOut.messages[mid] = { - body: - msg.body === Actions.INITIAL_MSG - ? Actions.INITIAL_MSG - : await SEA.decrypt(msg.body, ourSec), + body: await SEA.decrypt(msg.body, ourSec), timestamp: msg.timestamp } }