From 6655dbdfcaef7d06d5b6d4106f3de938951c49cd Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 25 Feb 2020 17:37:46 -0400 Subject: [PATCH] better mySea --- services/gunDB/Mediator/index.js | 63 +++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 94c34da1..2037ecf6 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -13,6 +13,8 @@ const logger = require('winston') /** @type {import('../contact-api/SimpleGUN').ISEA} */ // @ts-ignore const SEAx = require('gun/sea') +// @ts-ignore +SEAx.throw = true /** @type {import('../contact-api/SimpleGUN').ISEA} */ const mySEA = {} @@ -37,6 +39,20 @@ mySEA.encrypt = (msg, secret) => { ) } + if (typeof secret !== 'string') { + throw new TypeError( + `mySEA.encrypt() -> expected secret to be a an string, args: |msg| -- ${JSON.stringify( + secret + )}` + ) + } + + if (secret.length < 1) { + throw new TypeError( + `mySEA.encrypt() -> expected secret to be a populated string` + ) + } + // Avoid this: https://github.com/amark/gun/issues/804 and any other issues const sanitizedMsg = $$__SHOCKWALLET__MSG__ + msg @@ -88,7 +104,7 @@ mySEA.decrypt = (encMsg, secret) => { }) } -mySEA.secret = (recipientOrSenderEpub, recipientOrSenderSEA) => { +mySEA.secret = async (recipientOrSenderEpub, recipientOrSenderSEA) => { if (typeof recipientOrSenderEpub !== 'string') { throw new TypeError( 'epub has to be an string, args:' + @@ -113,6 +129,16 @@ mySEA.secret = (recipientOrSenderEpub, recipientOrSenderSEA) => { )}` ) } + + if (recipientOrSenderSEA === null) { + throw new TypeError( + 'sea has to be nont null, args: ' + + `${JSON.stringify(recipientOrSenderEpub)} -- ${JSON.stringify( + recipientOrSenderSEA + )}` + ) + } + if (recipientOrSenderEpub === recipientOrSenderSEA.pub) { throw new Error( 'Do not use pub for mysecret, args: ' + @@ -121,25 +147,26 @@ mySEA.secret = (recipientOrSenderEpub, recipientOrSenderSEA) => { )}` ) } - return SEAx.secret(recipientOrSenderEpub, recipientOrSenderSEA).then(sec => { - if (typeof sec !== 'string') { - throw new TypeError( - `Could not generate secret, args: ${JSON.stringify( - recipientOrSenderEpub - )} -- ${JSON.stringify(recipientOrSenderSEA)}` - ) - } - if (sec.length === 0) { - throw new TypeError( - `SEA.secret returned an empty string!, args: ${JSON.stringify( - recipientOrSenderEpub - )} -- ${JSON.stringify(recipientOrSenderSEA)}` - ) - } + const sec = await SEAx.secret(recipientOrSenderEpub, recipientOrSenderSEA) - return sec - }) + if (typeof sec !== 'string') { + throw new TypeError( + `Could not generate secret, args: ${JSON.stringify( + recipientOrSenderEpub + )} -- ${JSON.stringify(recipientOrSenderSEA)}` + ) + } + + if (sec.length === 0) { + throw new TypeError( + `SEA.secret returned an empty string!, args: ${JSON.stringify( + recipientOrSenderEpub + )} -- ${JSON.stringify(recipientOrSenderSEA)}` + ) + } + + return sec } const auth = require('../../auth/auth')