From 6b6e7ce24010672cb6d71260b6cab522b051f8eb Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 24 Jan 2020 19:50:31 -0400 Subject: [PATCH 1/4] seed backup event --- services/gunDB/Mediator/index.js | 27 +++++++++++++++++++++++++++ services/gunDB/contact-api/events.js | 28 +++++++++++++++++++++++++++- services/gunDB/event-constants.js | 3 ++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 32ac03d5..2d765d67 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -309,6 +309,7 @@ class Mediator { socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) socket.on(Event.ON_BIO, this.onBio) + socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup) socket.on(IS_GUN_AUTH, this.isGunAuth) } @@ -943,6 +944,32 @@ class Mediator { }) } } + + /** + * @param {Readonly<{ token: string }>} body + */ + onSeedBackup = async body => { + try { + const { token } = body + + await throwOnInvalidToken(token) + + API.Events.onSeedBackup(seedBackup => { + this.socket.emit(Event.ON_SEED_BACKUP, { + ok: true, + msg: seedBackup, + origBody: body + }) + }, user) + } catch (err) { + console.log(err) + this.socket.emit(Event.ON_SEED_BACKUP, { + ok: false, + msg: err.message, + origBody: body + }) + } + } } /** diff --git a/services/gunDB/contact-api/events.js b/services/gunDB/contact-api/events.js index 6b5540ee..3c25ae60 100644 --- a/services/gunDB/contact-api/events.js +++ b/services/gunDB/contact-api/events.js @@ -1230,6 +1230,31 @@ const onBio = (cb, user) => { }) } +/** @type {string|null} */ +let currentSeedBackup = null + +/** + * @param {(seedBackup: string|null) => void} cb + * @param {UserGUNNode} user + * @throws {Error} If user hasn't been auth. + * @returns {void} + */ +const onSeedBackup = (cb, user) => { + if (!user.is) { + throw new Error(ErrorCode.NOT_AUTH) + } + + const callb = debounce(cb, DEBOUNCE_WAIT_TIME) + callb(currentSeedBackup) + + user.get(Key.SEED_BACKUP).on(seedBackup => { + if (typeof seedBackup === 'string' || seedBackup === null) { + currentSeedBackup = seedBackup + callb(seedBackup) + } + }) +} + module.exports = { __onSentRequestToUser, __onUserToIncoming, @@ -1242,5 +1267,6 @@ module.exports = { onChats, onSimplerReceivedRequests, onSimplerSentRequests, - onBio + onBio, + onSeedBackup } diff --git a/services/gunDB/event-constants.js b/services/gunDB/event-constants.js index 0c9c578f..c4ffdae4 100644 --- a/services/gunDB/event-constants.js +++ b/services/gunDB/event-constants.js @@ -6,7 +6,8 @@ const Events = { ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS", ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS", ON_SENT_REQUESTS: "ON_SENT_REQUESTS", - ON_BIO: "ON_BIO" + ON_BIO: "ON_BIO", + ON_SEED_BACKUP: "ON_SEED_BACKUP", }; module.exports = Events; From 07dc27e2b40ee8828292eb287c885e1456d554e0 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 24 Jan 2020 21:28:52 -0400 Subject: [PATCH 2/4] unencrypt seed backup --- services/gunDB/contact-api/events.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/services/gunDB/contact-api/events.js b/services/gunDB/contact-api/events.js index 3c25ae60..a0c14d9f 100644 --- a/services/gunDB/contact-api/events.js +++ b/services/gunDB/contact-api/events.js @@ -1236,21 +1236,24 @@ let currentSeedBackup = null /** * @param {(seedBackup: string|null) => void} cb * @param {UserGUNNode} user + * @param {ISEA} SEA * @throws {Error} If user hasn't been auth. - * @returns {void} + * @returns {Promise} */ -const onSeedBackup = (cb, user) => { +const onSeedBackup = async (cb, user, SEA) => { if (!user.is) { throw new Error(ErrorCode.NOT_AUTH) } + const mySecret = await SEA.secret(user._.sea.epub, user._.sea) + const callb = debounce(cb, DEBOUNCE_WAIT_TIME) callb(currentSeedBackup) - user.get(Key.SEED_BACKUP).on(seedBackup => { - if (typeof seedBackup === 'string' || seedBackup === null) { - currentSeedBackup = seedBackup - callb(seedBackup) + user.get(Key.SEED_BACKUP).on(async seedBackup => { + if (typeof seedBackup === 'string') { + currentSeedBackup = await SEA.decrypt(seedBackup, mySecret) + callb(currentSeedBackup) } }) } From b64b580d45b27bb0bdb590b0526b66e01a8befa3 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 24 Jan 2020 21:31:07 -0400 Subject: [PATCH 3/4] provide SEA --- services/gunDB/Mediator/index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 2d765d67..51f68b68 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -954,13 +954,17 @@ class Mediator { await throwOnInvalidToken(token) - API.Events.onSeedBackup(seedBackup => { - this.socket.emit(Event.ON_SEED_BACKUP, { - ok: true, - msg: seedBackup, - origBody: body - }) - }, user) + API.Events.onSeedBackup( + seedBackup => { + this.socket.emit(Event.ON_SEED_BACKUP, { + ok: true, + msg: seedBackup, + origBody: body + }) + }, + user, + mySEA + ) } catch (err) { console.log(err) this.socket.emit(Event.ON_SEED_BACKUP, { From 407fe7e493312c72914178b0cbbb6f9e7c94741b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 24 Jan 2020 21:33:10 -0400 Subject: [PATCH 4/4] await --- services/gunDB/Mediator/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 51f68b68..46735819 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -954,7 +954,7 @@ class Mediator { await throwOnInvalidToken(token) - API.Events.onSeedBackup( + await API.Events.onSeedBackup( seedBackup => { this.socket.emit(Event.ON_SEED_BACKUP, { ok: true,