From 963b6446c5c627b3efe2fd2a0d27776af6d6e402 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 28 Apr 2021 10:57:36 -0400 Subject: [PATCH 01/11] Factor out for easier debugging --- services/gunDB/contact-api/events/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/events/index.js b/services/gunDB/contact-api/events/index.js index d046c7eb..8e5da91e 100644 --- a/services/gunDB/contact-api/events/index.js +++ b/services/gunDB/contact-api/events/index.js @@ -468,8 +468,9 @@ const processChats = debounce(() => { const pubToAvatar = Streams.getPubToAvatar() const pubToDn = Streams.getPubToDn() const pubToLastSeenApp = Streams.getPubToLastSeenApp() + const currentOutgoings = getCurrentOutgoings() const existingOutgoings = /** @type {[string, Outgoing][]} */ (Object.entries( - getCurrentOutgoings() + currentOutgoings ).filter(([_, o]) => o !== null)) const pubToFeed = Streams.getPubToFeed() From 14db1a3efd2eb60429d48459203bc14f395316d9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 28 Apr 2021 11:26:04 -0400 Subject: [PATCH 02/11] Let front-end handle user data subs --- services/gunDB/contact-api/events/index.js | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/services/gunDB/contact-api/events/index.js b/services/gunDB/contact-api/events/index.js index 8e5da91e..c7d3ba73 100644 --- a/services/gunDB/contact-api/events/index.js +++ b/services/gunDB/contact-api/events/index.js @@ -465,9 +465,6 @@ const notifyChatsListeners = () => { const processChats = debounce(() => { const Streams = require('../streams') - const pubToAvatar = Streams.getPubToAvatar() - const pubToDn = Streams.getPubToDn() - const pubToLastSeenApp = Streams.getPubToLastSeenApp() const currentOutgoings = getCurrentOutgoings() const existingOutgoings = /** @type {[string, Outgoing][]} */ (Object.entries( currentOutgoings @@ -478,19 +475,6 @@ const processChats = debounce(() => { const newChats = [] for (const [outID, out] of existingOutgoings) { - if (typeof pubToAvatar[out.with] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onAvatar(() => {}, out.with)() - } - if (typeof pubToDn[out.with] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onDisplayName(() => {}, out.with)() - } - if (typeof pubToLastSeenApp[out.with] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onPubToLastSeenApp(() => {}, out.with)() - } - /** @type {ChatMessage[]} */ let msgs = Object.entries(out.messages) .map(([mid, m]) => ({ @@ -516,7 +500,7 @@ const processChats = debounce(() => { messages: msgs, recipientAvatar: null, recipientDisplayName: null, - lastSeenApp: pubToLastSeenApp[out.with] || null + lastSeenApp: null } newChats.push(chat) @@ -548,10 +532,7 @@ const onChats = cb => { if (!onChatsSubbed) { const Streams = require('../streams') onOutgoing(processChats) - Streams.onAvatar(processChats) - Streams.onDisplayName(processChats) Streams.onPubToFeed(processChats) - Streams.onPubToLastSeenApp(processChats) onChatsSubbed = true } From 0ffbb6bbd689fc03179520898c124cbfb966337d Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 29 Apr 2021 15:54:35 -0400 Subject: [PATCH 03/11] Remove unnecessary gun subs (let front handle) --- services/gunDB/contact-api/events/onSentReqs.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/services/gunDB/contact-api/events/onSentReqs.js b/services/gunDB/contact-api/events/onSentReqs.js index 653ad3dc..f06518e1 100644 --- a/services/gunDB/contact-api/events/onSentReqs.js +++ b/services/gunDB/contact-api/events/onSentReqs.js @@ -51,10 +51,6 @@ const react = debounce(() => { // maps a pk to a feed, messages if subbed and pk is pubbing, null / // 'disconnected' otherwise const pubToFeed = Streams.getPubToFeed() - // pk to avatar - const pubToAvatar = Streams.getPubToAvatar() - // pk to display name - const pubToDN = Streams.getPubToDn() logger.info(`pubToLastSentREqID length: ${size(pubToLastSentReqID)}`) @@ -80,18 +76,6 @@ const react = debounce(() => { // eslint-disable-next-line no-empty-function Streams.onAddresses(() => {}, recipientPub)() } - // no avatar for this pk? let's ask the corresponding stream to sub to - // gun.user(pk).get('avatar') - if (typeof pubToAvatar[recipientPub] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onAvatar(() => {}, recipientPub)() - } - // no display name for this pk? let's ask the corresponding stream to sub to - // gun.user(pk).get('displayName') - if (typeof pubToDN[recipientPub] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onDisplayName(() => {}, recipientPub)() - } newReqs.push({ id: sentReqID, From 2be84451d1d1142dc1fac85b5f305a3456a44666 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 29 Apr 2021 15:59:26 -0400 Subject: [PATCH 04/11] Remove unnecessary gun subs (let front handle) --- services/gunDB/contact-api/events/onReceivedReqs.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/services/gunDB/contact-api/events/onReceivedReqs.js b/services/gunDB/contact-api/events/onReceivedReqs.js index 66048e83..83ef71af 100644 --- a/services/gunDB/contact-api/events/onReceivedReqs.js +++ b/services/gunDB/contact-api/events/onReceivedReqs.js @@ -44,22 +44,11 @@ const react = debounce(() => { const newReceivedReqsMap = {} const pubToFeed = Streams.getPubToFeed() - const pubToAvatar = Streams.getPubToAvatar() - const pubToDn = Streams.getPubToDn() for (const [id, req] of Object.entries(currAddressData)) { const inContact = Array.isArray(pubToFeed[req.from]) const isDisconnected = pubToFeed[req.from] === 'disconnected' - if (typeof pubToAvatar[req.from] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onAvatar(() => {}, req.from)() - } - if (typeof pubToDn[req.from] === 'undefined') { - // eslint-disable-next-line no-empty-function - Streams.onDisplayName(() => {}, req.from)() - } - if (!inContact && !isDisconnected) { newReceivedReqsMap[req.from] = { id, @@ -133,8 +122,6 @@ const onReceivedReqs = cb => { } }, user) - Streams.onAvatar(react) - Streams.onDisplayName(react) Streams.onPubToFeed(react) subbed = true From 19cac63baae022794975e6c6f55c943faa53de42 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 29 Apr 2021 18:26:17 -0400 Subject: [PATCH 05/11] Add "Reqs" to dictionary --- .vscode/settings.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 38a20598..91add6e5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,8 @@ "typescript.tsdk": "node_modules/typescript/lib", "debug.node.autoAttach": "on", "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode" + "editor.defaultFormatter": "esbenp.prettier-vscode", + "cSpell.words": [ + "Reqs" + ] } From 575bb14d4e7541bd1a23b1b75dfcda261be00512 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 29 Apr 2021 18:43:26 -0400 Subject: [PATCH 06/11] Remove unused subs (handled by RPC) --- services/gunDB/Mediator/index.js | 249 +----------------- services/gunDB/contact-api/events/index.js | 159 ----------- .../gunDB/contact-api/events/onSentReqs.js | 2 - services/gunDB/contact-api/streams/index.js | 98 ------- 4 files changed, 5 insertions(+), 503 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 9e8545e2..9464b6a5 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -385,12 +385,8 @@ const authenticate = async (alias, pass, __user) => { API.Jobs.onOrders(_user, gun, mySEA) API.Jobs.lastSeenNode(_user) - API.Events.onAvatar(() => {}, user)() - API.Events.onBio(() => {}, user) - API.Events.onBlacklist(() => {}, user) API.Events.onChats(() => {})() API.Events.onCurrentHandshakeAddress(() => {}, user)() - API.Events.onDisplayName(() => {}, user)() API.Events.onOutgoing(() => {})() API.Events.onSeedBackup(() => {}, user, mySEA) API.Events.onSimplerReceivedRequests(() => {})() @@ -443,12 +439,9 @@ const authenticate = async (alias, pass, __user) => { API.Jobs.onOrders(_user, gun, mySEA) API.Jobs.lastSeenNode(_user) - API.Events.onAvatar(() => {}, user)() - API.Events.onBio(() => {}, user) - API.Events.onBlacklist(() => {}, user) API.Events.onChats(() => {})() API.Events.onCurrentHandshakeAddress(() => {}, user)() - API.Events.onDisplayName(() => {}, user)() + API.Events.onOutgoing(() => {})() API.Events.onSeedBackup(() => {}, user, mySEA) API.Events.onSimplerReceivedRequests(() => {})() @@ -546,7 +539,6 @@ class Mediator { this.socket.on('disconnect', this.onDisconnect) this.socket.on(Action.ACCEPT_REQUEST, this.acceptRequest) - this.socket.on(Action.BLACKLIST, this.blacklist) this.socket.on( Action.GENERATE_NEW_HANDSHAKE_NODE, this.generateHandshakeNode @@ -559,26 +551,21 @@ class Mediator { this.sendHRWithInitialMsg ) this.socket.on(Action.SEND_MESSAGE, this.sendMessage) - this.socket.on(Action.SET_AVATAR, this.setAvatar) - this.socket.on(Action.SET_DISPLAY_NAME, this.setDisplayName) + this.socket.on(Action.SEND_PAYMENT, this.sendPayment) - this.socket.on(Action.SET_BIO, this.setBio) + this.socket.on(Action.DISCONNECT, this.disconnect) - this.socket.on(Event.ON_AVATAR, this.onAvatar) - this.socket.on(Event.ON_BLACKLIST, this.onBlacklist) this.socket.on(Event.ON_CHATS, this.onChats) - this.socket.on(Event.ON_DISPLAY_NAME, this.onDisplayName) + this.socket.on(Event.ON_HANDSHAKE_ADDRESS, this.onHandshakeAddress) this.socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) this.socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) - this.socket.on(Event.ON_BIO, this.onBio) + this.socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup) this.socket.on(Constants.Misc.IS_GUN_AUTH, this.isGunAuth) - this.socket.on(Action.SET_LAST_SEEN_APP, this.setLastSeenApp) - Object.values(Action).forEach(actionConstant => this.socket.on(actionConstant, this.setLastSeenApp) ) @@ -665,32 +652,6 @@ class Mediator { } } - /** - * @param {Readonly<{ publicKey: string , token: string }>} body - */ - blacklist = async body => { - try { - const { publicKey, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.blacklist(publicKey, user) - - this.socket.emit(Action.BLACKLIST, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.BLACKLIST, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - onDisconnect = () => { this.connected = false } @@ -919,124 +880,8 @@ class Mediator { } } - /** - * @param {Readonly<{ avatar: string|null , token: string }>} body - */ - setAvatar = async body => { - try { - const { avatar, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.setAvatar(avatar, user) - - this.socket.emit(Action.SET_AVATAR, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SET_AVATAR, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ displayName: string , token: string }>} body - */ - setDisplayName = async body => { - try { - const { displayName, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.setDisplayName(displayName, user) - - this.socket.emit(Action.SET_DISPLAY_NAME, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SET_DISPLAY_NAME, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - ////////////////////////////////////////////////////////////////////////////// - /** - * @param {Readonly<{ token: string }>} body - */ - onAvatar = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onAvatar(avatar => { - if (Config.SHOW_LOG) { - logger.info('---avatar---') - logger.info(avatar || 'null') - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_AVATAR, { - msg: avatar, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_AVATAR, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - onBlacklist = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onBlacklist(blacklist => { - if (Config.SHOW_LOG) { - logger.info('---blacklist---') - logger.info(blacklist.join(',')) - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_BLACKLIST, { - msg: blacklist, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_BLACKLIST, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - /** * @param {Readonly<{ token: string }>} body */ @@ -1071,38 +916,6 @@ class Mediator { } } - /** - * @param {Readonly<{ token: string }>} body - */ - onDisplayName = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onDisplayName(displayName => { - if (Config.SHOW_LOG) { - logger.info('---displayName---') - logger.info(displayName || 'null or empty string') - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_DISPLAY_NAME, { - msg: displayName, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_DISPLAY_NAME, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - /** * @param {Readonly<{ token: string }>} body */ @@ -1198,58 +1011,6 @@ class Mediator { } } - /** - * @param {Readonly<{ token: string }>} body - */ - onBio = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onBio(bio => { - this.socket.emit(Event.ON_BIO, { - msg: bio, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_BIO, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ bio: string|null , token: string }>} body - */ - setBio = async body => { - try { - const { bio, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.setBio(bio, user) - - this.socket.emit(Action.SET_BIO, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SET_BIO, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - /** * @param {Readonly<{ token: string }>} body */ diff --git a/services/gunDB/contact-api/events/index.js b/services/gunDB/contact-api/events/index.js index c7d3ba73..54e6725e 100644 --- a/services/gunDB/contact-api/events/index.js +++ b/services/gunDB/contact-api/events/index.js @@ -80,85 +80,6 @@ const __onUserToIncoming = (cb, user, SEA) => { }) } -/** @type {Set<(av: string|null) => void>} */ -const avatarListeners = new Set() - -/** @type {string|null} */ -let currentAvatar = null - -const getAvatar = () => currentAvatar - -/** @param {string|null} av */ -const setAvatar = av => { - currentAvatar = av - avatarListeners.forEach(l => l(currentAvatar)) -} - -let avatarSubbed = false - -/** - * @param {(avatar: string|null) => void} cb - * @param {UserGUNNode} user Pass only for testing purposes. - * @throws {Error} If user hasn't been auth. - * @returns {() => void} - */ -const onAvatar = (cb, user) => { - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - avatarListeners.add(cb) - - cb(currentAvatar) - - if (!avatarSubbed) { - avatarSubbed = true - user - .get(Key.PROFILE_BINARY) - .get(Key.AVATAR) - .on(avatar => { - if (typeof avatar === 'string' || avatar === null) { - setAvatar(avatar) - } - }) - } - - return () => { - avatarListeners.delete(cb) - } -} - -/** - * @param {(blacklist: string[]) => void} cb - * @param {UserGUNNode} user - * @returns {void} - */ -const onBlacklist = (cb, user) => { - /** @type {string[]} */ - const blacklist = [] - - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - const callb = debounce(cb, DEBOUNCE_WAIT_TIME) - - // Initial value if no items are in blacklist in gun - callb(blacklist) - - user - .get(Key.BLACKLIST) - .map() - .on(publicKey => { - if (typeof publicKey === 'string' && publicKey.length > 0) { - blacklist.push(publicKey) - callb(blacklist) - } else { - logger.warn('Invalid public key received for blacklist') - } - }) -} - /** @type {Set<(addr: string|null) => void>} */ const addressListeners = new Set() @@ -210,54 +131,6 @@ const onCurrentHandshakeAddress = (cb, user) => { } } -/** @type {Set<(dn: string|null) => void>} */ -const dnListeners = new Set() - -/** @type {string|null} */ -let currentDn = null - -const getDisplayName = () => currentDn - -/** @param {string|null} dn */ -const setDn = dn => { - currentDn = dn - dnListeners.forEach(l => l(currentDn)) -} - -let dnSubbed = false - -/** - * @param {(displayName: string|null) => void} cb - * @param {UserGUNNode} user Pass only for testing purposes. - * @throws {Error} If user hasn't been auth. - * @returns {() => void} - */ -const onDisplayName = (cb, user) => { - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - cb(currentDn) - - dnListeners.add(cb) - - if (!dnSubbed) { - dnSubbed = true - user - .get(Key.PROFILE) - .get(Key.DISPLAY_NAME) - .on(displayName => { - if (typeof displayName === 'string' || displayName === null) { - setDn(displayName) - } - }) - } - - return () => { - dnListeners.delete(cb) - } -} - /** * @param {(messages: Record) => void} cb * @param {string} userPK Public key of the user from whom the incoming @@ -543,32 +416,6 @@ const onChats = cb => { } } -/** @type {string|null} */ -let currentBio = null - -/** - * @param {(bio: string|null) => void} cb - * @param {UserGUNNode} user Pass only for testing purposes. - * @throws {Error} If user hasn't been auth. - * @returns {void}outgoingsListeners.forEach() - */ -const onBio = (cb, user) => { - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - const callb = debounce(cb, DEBOUNCE_WAIT_TIME) - // Initial value if avvatar is undefined in gun - callb(currentBio) - - user.get(Key.BIO).on(bio => { - if (typeof bio === 'string' || bio === null) { - currentBio = bio - callb(bio) - } - }) -} - /** @type {string|null} */ let currentSeedBackup = null @@ -599,10 +446,7 @@ const onSeedBackup = (cb, user, SEA) => { module.exports = { __onUserToIncoming, - onAvatar, - onBlacklist, onCurrentHandshakeAddress, - onDisplayName, onIncomingMessages, onOutgoing, getCurrentOutgoings, @@ -610,11 +454,8 @@ module.exports = { onSimplerSentRequests: require('./onSentReqs').onSentReqs, getCurrentSentReqs: require('./onSentReqs').getCurrentSentReqs, getCurrentReceivedReqs: require('./onReceivedReqs').getReceivedReqs, - onBio, onSeedBackup, onChats, - getAvatar, - getDisplayName, getHandshakeAddress, getChats } diff --git a/services/gunDB/contact-api/events/onSentReqs.js b/services/gunDB/contact-api/events/onSentReqs.js index f06518e1..3aefb6ef 100644 --- a/services/gunDB/contact-api/events/onSentReqs.js +++ b/services/gunDB/contact-api/events/onSentReqs.js @@ -113,8 +113,6 @@ const onSentReqs = cb => { Streams.onStoredReqs(react) Streams.onLastSentReqIDs(react) Streams.onPubToFeed(react) - Streams.onAvatar(react) - Streams.onDisplayName(react) subbed = true } diff --git a/services/gunDB/contact-api/streams/index.js b/services/gunDB/contact-api/streams/index.js index 0b211bcd..3f586954 100644 --- a/services/gunDB/contact-api/streams/index.js +++ b/services/gunDB/contact-api/streams/index.js @@ -3,99 +3,6 @@ const { Schema, Utils: CommonUtils } = require('shock-common') const Key = require('../key') const Utils = require('../utils') -/** - * @typedef {Record} Avatars - * @typedef {(avatars: Avatars) => void} AvatarListener - */ - -/** @type {Avatars} */ -const pubToAvatar = {} - -const getPubToAvatar = () => pubToAvatar - -/** @type {Set} */ -const avatarListeners = new Set() - -const notifyAvatarListeners = () => { - avatarListeners.forEach(l => l(pubToAvatar)) -} - -/** @type {Set} */ -const pubsWithAvatarListeners = new Set() - -/** - * @param {AvatarListener} cb - * @param {string=} pub - */ -const onAvatar = (cb, pub) => { - avatarListeners.add(cb) - cb(pubToAvatar) - if (pub && pubsWithAvatarListeners.add(pub)) { - require('../../Mediator') - .getGun() - .user(pub) - .get(Key.PROFILE_BINARY) - .get(Key.AVATAR) - .on(av => { - if (typeof av === 'string' || av === null) { - pubToAvatar[pub] = av || null - } else { - pubToAvatar[pub] = null - } - notifyAvatarListeners() - }) - } - return () => { - avatarListeners.delete(cb) - } -} - -/** - * @typedef {Record} DisplayNames - * @typedef {(avatars: Avatars) => void} DisplayNameListener - */ - -/** @type {DisplayNames} */ -const pubToDisplayName = {} - -const getPubToDn = () => pubToDisplayName - -/** @type {Set} */ -const displayNameListeners = new Set() - -const notifyDisplayNameListeners = () => { - displayNameListeners.forEach(l => l(pubToDisplayName)) -} - -/** @type {Set} */ -const pubsWithDisplayNameListeners = new Set() - -/** - * @param {DisplayNameListener} cb - * @param {string=} pub - */ -const onDisplayName = (cb, pub) => { - displayNameListeners.add(cb) - cb(pubToDisplayName) - if (pub && pubsWithDisplayNameListeners.add(pub)) { - require('../../Mediator') - .getGun() - .user(pub) - .get(Key.PROFILE) - .get(Key.DISPLAY_NAME) - .on(dn => { - if (typeof dn === 'string' || dn === null) { - pubToDisplayName[pub] = dn || null - } else { - pubToDisplayName[pub] = null - } - notifyDisplayNameListeners() - }) - } - return () => { - displayNameListeners.delete(cb) - } -} /** * @typedef {import('shock-common').Schema.StoredRequest} StoredRequest @@ -171,11 +78,6 @@ const onStoredReqs = cb => { } module.exports = { - onAvatar, - getPubToAvatar, - onDisplayName, - getPubToDn, - onPubToIncoming: require('./pubToIncoming').onPubToIncoming, getPubToIncoming: require('./pubToIncoming').getPubToIncoming, setPubToIncoming: require('./pubToIncoming').setPubToIncoming, From d5d48d574431367c15d66afb1d3fda598d4c446b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 29 Apr 2021 18:47:51 -0400 Subject: [PATCH 07/11] Add "ISEA" to dictionary --- .vscode/settings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 91add6e5..895f596f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "editor.formatOnSave": true, "editor.defaultFormatter": "esbenp.prettier-vscode", "cSpell.words": [ + "ISEA", "Reqs" ] } From d3c2d3e64513b0bd1815edaed1fd082f1eb97791 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 29 Apr 2021 18:48:26 -0400 Subject: [PATCH 08/11] Remove unused code (Mediator class) --- services/gunDB/Mediator/index.js | 595 ------------------------------- 1 file changed, 595 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 9464b6a5..9f6568a5 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -12,7 +12,6 @@ Gun.log = () => {} require('gun/lib/open') // @ts-ignore require('gun/lib/load') -const debounce = require('lodash/debounce') //@ts-ignore const { encryptedEmit, encryptedOn } = require('../../../utils/ECC/socket') const Key = require('../contact-api/key') @@ -215,14 +214,9 @@ mySEA.secret = async (recipientOrSenderEpub, recipientOrSenderSEA) => { return sec } -const auth = require('../../auth/auth') - const { Constants } = require('shock-common') -const { Action, Event } = Constants const API = require('../contact-api/index') -const Config = require('../config') -// const { nonEncryptedRoutes } = require('../../../utils/protectedRoutes') /** * @typedef {import('../contact-api/SimpleGUN').GUNNode} GUNNode @@ -492,579 +486,6 @@ const freshGun = () => { } } -/** - * @param {string} token - * @returns {Promise} - */ -const isValidToken = async token => { - const validation = await auth.validateToken(token) - - if (typeof validation !== 'object') { - return false - } - - if (validation === null) { - return false - } - - if (typeof validation.valid !== 'boolean') { - return false - } - - return validation.valid -} - -/** - * @param {string} token - * @throws {Error} If the token is invalid - * @returns {Promise} - */ -const throwOnInvalidToken = async token => { - const isValid = await isValidToken(token) - - if (!isValid) { - throw new Error('Token expired.') - } -} - -class Mediator { - /** - * @param {Readonly} socket - */ - constructor(socket) { - this.socket = this.encryptSocketInstance(socket) - - this.connected = true - - this.socket.on('disconnect', this.onDisconnect) - - this.socket.on(Action.ACCEPT_REQUEST, this.acceptRequest) - this.socket.on( - Action.GENERATE_NEW_HANDSHAKE_NODE, - this.generateHandshakeNode - ) - this.socket.on('GENERATE_ORDER_ADDRESS', this.generateOrderAddress) - this.socket.on('INIT_FEED_WALL', this.initWall) - this.socket.on(Action.SEND_HANDSHAKE_REQUEST, this.sendHandshakeRequest) - this.socket.on( - Action.SEND_HANDSHAKE_REQUEST_WITH_INITIAL_MSG, - this.sendHRWithInitialMsg - ) - this.socket.on(Action.SEND_MESSAGE, this.sendMessage) - - this.socket.on(Action.SEND_PAYMENT, this.sendPayment) - - this.socket.on(Action.DISCONNECT, this.disconnect) - - this.socket.on(Event.ON_CHATS, this.onChats) - - this.socket.on(Event.ON_HANDSHAKE_ADDRESS, this.onHandshakeAddress) - this.socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) - this.socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) - - this.socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup) - - this.socket.on(Constants.Misc.IS_GUN_AUTH, this.isGunAuth) - - Object.values(Action).forEach(actionConstant => - this.socket.on(actionConstant, this.setLastSeenApp) - ) - } - - /** @param {SimpleSocket} socket */ - encryptSocketInstance = socket => { - const emit = encryptedEmit(socket) - const on = encryptedOn(socket) - - return { - /** - * @type {SimpleSocket['on']} - */ - on, - /** @type {SimpleSocket['emit']} */ - emit - } - } - - /** @param {{ token: string }} body */ - setLastSeenApp = async body => { - logger.info('setLastSeen Called') - try { - await throwOnInvalidToken(body.token) - await API.Actions.setLastSeenApp() - this.socket.emit(Action.SET_LAST_SEEN_APP, { - ok: true, - msg: null, - origBody: body - }) - } catch (e) { - this.socket.emit(Action.SET_LAST_SEEN_APP, { - ok: false, - msg: e.message, - origBody: body - }) - } - } - - isGunAuth = () => { - try { - const isGunAuth = isAuthenticated() - - this.socket.emit(Constants.Misc.IS_GUN_AUTH, { - ok: true, - msg: { - isGunAuth - }, - origBody: {} - }) - } catch (err) { - this.socket.emit(Constants.Misc.IS_GUN_AUTH, { - ok: false, - msg: err.message, - origBody: {} - }) - } - } - - /** - * @param {Readonly<{ requestID: string , token: string }>} body - */ - acceptRequest = async body => { - try { - const { requestID, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.acceptRequest(requestID, gun, user, mySEA) - - this.socket.emit(Action.ACCEPT_REQUEST, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.ACCEPT_REQUEST, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - onDisconnect = () => { - this.connected = false - } - - /** - * @param {Readonly<{ token: string }>} body - */ - generateHandshakeNode = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - await API.Actions.generateHandshakeAddress() - - this.socket.emit(Action.GENERATE_NEW_HANDSHAKE_NODE, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.GENERATE_NEW_HANDSHAKE_NODE, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - generateOrderAddress = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - await API.Actions.generateOrderAddress(user) - - this.socket.emit('GENERATE_ORDER_ADDRESS', { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit('GENERATE_ORDER_ADDRESS', { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - initWall = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - await API.Actions.initWall() - - this.socket.emit('INIT_FEED_WALL', { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit('INIT_FEED_WALL', { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ recipientPublicKey: string , token: string }>} body - */ - sendHandshakeRequest = async body => { - try { - if (Config.SHOW_LOG) { - logger.info('\n') - logger.info('------------------------------') - logger.info('will now try to send a handshake request') - logger.info('------------------------------') - logger.info('\n') - } - - const { recipientPublicKey, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.sendHandshakeRequest( - recipientPublicKey, - gun, - user, - mySEA - ) - - if (Config.SHOW_LOG) { - logger.info('\n') - logger.info('------------------------------') - logger.info('handshake request successfuly sent') - logger.info('------------------------------') - logger.info('\n') - } - - this.socket.emit(Action.SEND_HANDSHAKE_REQUEST, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - if (Config.SHOW_LOG) { - logger.info('\n') - logger.info('------------------------------') - logger.info('handshake request send fail: ' + err.message) - logger.info('------------------------------') - logger.info('\n') - } - - this.socket.emit(Action.SEND_HANDSHAKE_REQUEST, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ initialMsg: string , recipientPublicKey: string , token: string }>} body - */ - sendHRWithInitialMsg = async body => { - try { - const { initialMsg, recipientPublicKey, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.sendHRWithInitialMsg( - initialMsg, - recipientPublicKey, - gun, - user, - mySEA - ) - - this.socket.emit(Action.SEND_HANDSHAKE_REQUEST_WITH_INITIAL_MSG, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SEND_HANDSHAKE_REQUEST_WITH_INITIAL_MSG, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ body: string , recipientPublicKey: string , token: string }>} reqBody - */ - sendMessage = async reqBody => { - try { - const { body, recipientPublicKey, token } = reqBody - - await throwOnInvalidToken(token) - - this.socket.emit(Action.SEND_MESSAGE, { - ok: true, - msg: await API.Actions.sendMessage( - recipientPublicKey, - body, - user, - mySEA - ), - origBody: reqBody - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SEND_MESSAGE, { - ok: false, - msg: err.message, - origBody: reqBody - }) - } - } - - /** - * @param {Readonly<{ uuid: string, recipientPub: string, amount: number, memo: string, token: string, feeLimit:number }>} reqBody - */ - sendPayment = async reqBody => { - try { - const { recipientPub, amount, memo, feeLimit, token } = reqBody - - await throwOnInvalidToken(token) - - const preimage = await API.Actions.sendPayment( - recipientPub, - amount, - memo, - feeLimit - ) - - this.socket.emit(Action.SEND_PAYMENT, { - ok: true, - msg: preimage, - origBody: reqBody - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SEND_PAYMENT, { - ok: false, - msg: err.message, - origBody: reqBody - }) - } - } - - ////////////////////////////////////////////////////////////////////////////// - - /** - * @param {Readonly<{ token: string }>} body - */ - onChats = async body => { - try { - const { token } = body - - // logger.info('ON_CHATS', body) - - await throwOnInvalidToken(token) - - API.Events.onChats(chats => { - if (Config.SHOW_LOG) { - logger.info('---chats---') - logger.info(JSON.stringify(chats)) - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_CHATS, { - msg: chats, - ok: true, - origBody: body - }) - }) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_CHATS, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - onHandshakeAddress = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onCurrentHandshakeAddress(addr => { - if (Config.SHOW_LOG) { - logger.info('---addr---') - logger.info(addr || 'null or empty string') - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_HANDSHAKE_ADDRESS, { - ok: true, - msg: addr, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_HANDSHAKE_ADDRESS, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - onReceivedRequests = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onSimplerReceivedRequests(receivedRequests => { - this.socket.emit(Event.ON_RECEIVED_REQUESTS, { - msg: receivedRequests, - ok: true, - origBody: body - }) - }) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_RECEIVED_REQUESTS, { - msg: err.message, - ok: false, - origBody: body - }) - } - } - - onSentRequestsSubbed = false - - /** - * @param {Readonly<{ token: string }>} body - */ - onSentRequests = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - if (!this.onSentRequestsSubbed) { - this.onSentRequestsSubbed = true - - API.Events.onSimplerSentRequests( - debounce(sentRequests => { - // logger.info( - // `new Reqss in mediator: ${JSON.stringify(sentRequests)}` - // ) - this.socket.emit(Event.ON_SENT_REQUESTS, { - msg: sentRequests, - ok: true, - origBody: body - }) - }, 1000) - ) - } - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_SENT_REQUESTS, { - msg: err.message, - ok: false, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - onSeedBackup = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - await API.Events.onSeedBackup( - seedBackup => { - this.socket.emit(Event.ON_SEED_BACKUP, { - ok: true, - msg: seedBackup, - origBody: body - }) - }, - user, - mySEA - ) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_SEED_BACKUP, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** @param {Readonly<{ pub: string, token: string }>} body */ - disconnect = async body => { - try { - const { pub, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.disconnect(pub) - - this.socket.emit(Action.DISCONNECT, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - this.socket.emit(Action.DISCONNECT, { - ok: false, - msg: err.message, - origBody: body - }) - } - } -} - /** * Creates an user for gun. Returns a promise containing the public key of the * newly created user. @@ -1183,25 +604,9 @@ const register = async (alias, pass) => { }) } -/** - * @param {SimpleSocket} socket - * @throws {Error} If gun is not authenticated or is in the process of - * authenticating. Use `isAuthenticating()` and `isAuthenticated()` to check for - * this first. - * @returns {Mediator} - */ -const createMediator = socket => { - // if (isAuthenticating() || !isAuthenticated()) { - // throw new Error("Gun must be authenticated to create a Mediator"); - // } - - return new Mediator(socket) -} - module.exports = { authenticate, logoff, - createMediator, isAuthenticated, isAuthenticating, isRegistering, From e7fe24a004239106da940ce2cc2b24686c4f2b00 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 30 Apr 2021 15:45:55 -0400 Subject: [PATCH 09/11] Test script for quik gun tests --- testcript.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 testcript.js diff --git a/testcript.js b/testcript.js new file mode 100644 index 00000000..ea41763b --- /dev/null +++ b/testcript.js @@ -0,0 +1,91 @@ +/** + * @format + */ +// @ts-check +const Gun = require('gun') + +require('gun/nts') +require('gun/lib/open') +require('gun/lib/load') + +const args = process.argv.slice(2) + +const [fileName, alias, pass, method, path] = args + +// @ts-expect-error +const gun = /** @type {import('./services/gunDB/contact-api/SimpleGUN').GUNNode} */ (Gun( + { + axe: false, + multicast: false, + peers: ['https://gun.shock.network:8765/gun'], + file: `TESTCRIPT-RADATA/${fileName}` + } +)) + +const user = gun.user() + +/** + * @param {any} data + * @param {string} key + */ +const cb = (data, key) => { + console.log('\n') + console.log(`key: ${key}`) + console.log('\n') + console.log(data) + console.log('\n') +} + +;(async () => { + try { + console.log(`Alias: ${alias}`) + console.log(`Pass: ${pass}`) + console.log('\n') + + // gun + // .get('handshakeNodes') + // .map() + // .once(cb) + + // wait for user data to be received + // await new Promise(res => setTimeout(res, 10000)) + + const ack = await new Promise(res => { + user.auth(alias, pass, _ack => { + res(_ack) + }) + }) + + if (typeof ack.err === 'string') { + throw new Error(ack.err) + } else if (typeof ack.sea === 'object') { + // clock skew + await new Promise(res => setTimeout(res, 2000)) + } else { + throw new Error('Unknown error.') + } + + const [root, ...keys] = path.split('.') + + let node = (() => { + if (root === 'gun') { + return gun + } + if (root === 'user') { + return user + } + return gun.user(root) + })() + + keys.forEach(key => (node = node.get(key))) + + if (method === 'once') node.once(cb) + if (method === 'load') node.load(cb) + if (method === 'on') node.on(cb) + if (method === 'map.once') node.map().once(cb) + if (method === 'map.on') node.map().on(cb) + } catch (e) { + console.log(`\nCaught error in app:\n`) + console.log(e) + } +})() From 4460de0747f403bf3227f10f1ca54138207d08d1 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 30 Apr 2021 15:46:09 -0400 Subject: [PATCH 10/11] Correct log --- services/gunDB/contact-api/events/onReceivedReqs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/events/onReceivedReqs.js b/services/gunDB/contact-api/events/onReceivedReqs.js index 83ef71af..8c5d3226 100644 --- a/services/gunDB/contact-api/events/onReceivedReqs.js +++ b/services/gunDB/contact-api/events/onReceivedReqs.js @@ -84,7 +84,7 @@ const listenerForAddr = addr => data => { } } - logger.info('data for address length: ' + size(addr)) + logger.info('data for address length: ' + size(currAddressData)) react() } From 785544c341de347af075385f847a7132bf2564a8 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 30 Apr 2021 15:46:19 -0400 Subject: [PATCH 11/11] Use map.on() over map.once() map.once() is eager and will serve empty/half-empty objects instead of waiting for the entirety of data to be wired in. --- services/gunDB/contact-api/jobs/onAcceptedRequests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/jobs/onAcceptedRequests.js b/services/gunDB/contact-api/jobs/onAcceptedRequests.js index 255cc107..b8a559f5 100644 --- a/services/gunDB/contact-api/jobs/onAcceptedRequests.js +++ b/services/gunDB/contact-api/jobs/onAcceptedRequests.js @@ -36,7 +36,7 @@ const onAcceptedRequests = (user, SEA) => { user .get(Key.STORED_REQS) .map() - .once(async (storedReq, id) => { + .on(async (storedReq, id) => { logger.info( `------------------------------------\nPROCID:${procid} (used for debugging memory leaks in jobs)\n---------------------------------------` )