better mySea

This commit is contained in:
Daniel Lugo 2020-02-25 17:37:46 -04:00
parent 128ff6d02a
commit 6655dbdfca

View file

@ -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,7 +147,9 @@ mySEA.secret = (recipientOrSenderEpub, recipientOrSenderSEA) => {
)}`
)
}
return SEAx.secret(recipientOrSenderEpub, recipientOrSenderSEA).then(sec => {
const sec = await SEAx.secret(recipientOrSenderEpub, recipientOrSenderSEA)
if (typeof sec !== 'string') {
throw new TypeError(
`Could not generate secret, args: ${JSON.stringify(
@ -139,7 +167,6 @@ mySEA.secret = (recipientOrSenderEpub, recipientOrSenderSEA) => {
}
return sec
})
}
const auth = require('../../auth/auth')