encrypt initial msg
This commit is contained in:
parent
f118beac16
commit
70ac41edfa
2 changed files with 15 additions and 33 deletions
|
|
@ -29,14 +29,6 @@ const { isHandshakeRequest } = require('./schema')
|
||||||
*/
|
*/
|
||||||
const INITIAL_MSG = '$$__SHOCKWALLET__INITIAL__MESSAGE'
|
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
|
* 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
|
* 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 mySecret = require('../Mediator').getMySecret()
|
||||||
const encryptedForMeRecipientPub = await SEA.encrypt(withPublicKey, mySecret)
|
const encryptedForMeRecipientPub = await SEA.encrypt(withPublicKey, mySecret)
|
||||||
|
const ourSecret = await SEA.secret(
|
||||||
|
await Utils.pubToEpub(withPublicKey),
|
||||||
|
user._.sea
|
||||||
|
)
|
||||||
|
|
||||||
const maybeEncryptedForMeOutgoingFeedID = await Utils.tryAndWait(
|
const maybeEncryptedForMeOutgoingFeedID = await Utils.tryAndWait(
|
||||||
(_, user) =>
|
(_, user) =>
|
||||||
|
|
@ -99,12 +95,18 @@ const __createOutgoingFeed = async (withPublicKey, user, SEA) => {
|
||||||
throw new TypeError('typeof newOutgoingFeedID !== "string"')
|
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) => {
|
await new Promise((res, rej) => {
|
||||||
user
|
user
|
||||||
.get(Key.OUTGOINGS)
|
.get(Key.OUTGOINGS)
|
||||||
.get(newOutgoingFeedID)
|
.get(newOutgoingFeedID)
|
||||||
.get(Key.MESSAGES)
|
.get(Key.MESSAGES)
|
||||||
.set(__createInitialMessage(), ack => {
|
.set(initialMsg, ack => {
|
||||||
if (ack.err) {
|
if (ack.err) {
|
||||||
rej(new Error(ack.err))
|
rej(new Error(ack.err))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -118,12 +120,6 @@ const __createOutgoingFeed = async (withPublicKey, user, SEA) => {
|
||||||
mySecret
|
mySecret
|
||||||
)
|
)
|
||||||
|
|
||||||
if (typeof encryptedForMeNewOutgoingFeedID === 'undefined') {
|
|
||||||
throw new TypeError(
|
|
||||||
"typeof encryptedForMeNewOutgoingFeedID === 'undefined'"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
await new Promise((res, rej) => {
|
await new Promise((res, rej) => {
|
||||||
user
|
user
|
||||||
.get(Key.RECIPIENT_TO_OUTGOING)
|
.get(Key.RECIPIENT_TO_OUTGOING)
|
||||||
|
|
@ -1106,7 +1102,6 @@ const disconnect = async pub => {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
INITIAL_MSG,
|
|
||||||
__createOutgoingFeed,
|
__createOutgoingFeed,
|
||||||
acceptRequest,
|
acceptRequest,
|
||||||
authenticate,
|
authenticate,
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,9 @@ const DEBOUNCE_WAIT_TIME = 500
|
||||||
* @param {(userToIncoming: Record<string, string>) => void} cb
|
* @param {(userToIncoming: Record<string, string>) => void} cb
|
||||||
* @param {UserGUNNode} user Pass only for testing purposes.
|
* @param {UserGUNNode} user Pass only for testing purposes.
|
||||||
* @param {ISEA} SEA
|
* @param {ISEA} SEA
|
||||||
* @returns {Promise<void>}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const __onUserToIncoming = async (cb, user, SEA) => {
|
const __onUserToIncoming = (cb, user, SEA) => {
|
||||||
if (!user.is) {
|
if (!user.is) {
|
||||||
throw new Error(ErrorCode.NOT_AUTH)
|
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)
|
const secret = await SEA.secret(recipientEpub, user._.sea)
|
||||||
|
|
||||||
let { body } = data
|
let { body } = data
|
||||||
|
body = await SEA.decrypt(body, secret)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
messages[key] = {
|
messages[key] = {
|
||||||
body,
|
body,
|
||||||
|
|
@ -342,10 +332,7 @@ const onOutgoing = cb => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newOut.messages[mid] = {
|
newOut.messages[mid] = {
|
||||||
body:
|
body: await SEA.decrypt(msg.body, ourSec),
|
||||||
msg.body === Actions.INITIAL_MSG
|
|
||||||
? Actions.INITIAL_MSG
|
|
||||||
: await SEA.decrypt(msg.body, ourSec),
|
|
||||||
timestamp: msg.timestamp
|
timestamp: msg.timestamp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue