encrypt initial msg

This commit is contained in:
Daniel Lugo 2020-02-12 14:36:01 -04:00
parent f118beac16
commit 70ac41edfa
2 changed files with 15 additions and 33 deletions

View file

@ -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,

View file

@ -30,9 +30,9 @@ const DEBOUNCE_WAIT_TIME = 500
* @param {(userToIncoming: Record<string, string>) => void} cb
* @param {UserGUNNode} user Pass only for testing purposes.
* @param {ISEA} SEA
* @returns {Promise<void>}
* @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
}
}