Simplify using GunSmith's new processes

This commit is contained in:
Daniel Lugo 2021-09-12 20:46:45 -04:00
parent 6386cfb18b
commit 289fe1c918

View file

@ -14,6 +14,8 @@ require('gun/lib/open')
require('gun/lib/load')
//@ts-ignore
const { encryptedEmit, encryptedOn } = require('../../../utils/ECC/socket')
const Key = require('../contact-api/key')
const Config = require('../config')
/** @type {import('../contact-api/SimpleGUN').ISEA} */
// @ts-ignore
@ -256,19 +258,16 @@ const API = require('../contact-api/index')
/* eslint-disable init-declarations */
/** @type {GUNNode} */
// @ts-ignore
let gun
const gun = Gun({
axe: false,
multicast: false,
peers: Config.PEERS
})
/** @type {UserGUNNode} */
let user
const user = gun.user()
/* eslint-enable init-declarations */
/** @type {string|null} */
let _currentAlias = null
/** @type {string|null} */
/** @type {string|null} */
let mySec = null
@ -298,10 +297,9 @@ const getUser = () => {
* Returns a promise containing the public key of the newly created user.
* @param {string} alias
* @param {string} pass
* @param {UserGUNNode=} __user
* @returns {Promise<string>}
*/
const authenticate = async (alias, pass, __user) => {
const authenticate = async (alias, pass) => {
if (!Common.isPopulatedString(alias)) {
throw new TypeError(
`Expected alias to be a populated string, instead got: ${alias}`
@ -312,45 +310,6 @@ const authenticate = async (alias, pass, __user) => {
`Expected pass to be a populated string, instead got: ${pass}`
)
}
const _user = __user || user
const isFreshGun = _user !== user
if (isFreshGun) {
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))
return ack.sea.pub
} else {
throw new Error('Unknown error.')
}
}
if (isAuthenticated()) {
if (alias !== _currentAlias) {
throw new Error(
`Tried to re-authenticate with an alias different to that of stored one, tried: ${alias} - stored: ${_currentAlias}, logoff first if need to change aliases.`
)
}
// clock skew
await new Promise(res => setTimeout(res, 2000))
// move this to a subscription; implement off() ? todo
API.Jobs.onOrders(_user, gun, mySEA)
API.Jobs.lastSeenNode(_user)
API.Events.onSeedBackup(() => {}, user, mySEA)
return _user._.sea.pub
}
if (isAuthenticating()) {
throw new Error(
@ -361,7 +320,7 @@ const authenticate = async (alias, pass, __user) => {
_isAuthenticating = true
const ack = await new Promise(res => {
_user.auth(alias, pass, _ack => {
user.auth(alias, pass, _ack => {
res(_ack)
})
})
@ -371,15 +330,36 @@ const authenticate = async (alias, pass, __user) => {
if (typeof ack.err === 'string') {
throw new Error(ack.err)
} else if (typeof ack.sea === 'object') {
mySec = await mySEA.secret(_user._.sea.epub, _user._.sea)
mySec = await mySEA.secret(user._.sea.epub, user._.sea)
// clock skew
await new Promise(res => setTimeout(res, 2000))
_currentAlias = alias
await new Promise(res => setTimeout(res, 5000))
API.Jobs.onOrders(_user, gun, mySEA)
API.Jobs.lastSeenNode(_user)
await /** @type {Promise<void>} */ (new Promise((res, rej) => {
user.get(Key.FOLLOWS).put(
{
unused: null
},
ack => {
if (ack.err && typeof ack.err !== 'number') {
rej(
new Error(
`Error initializing follows: ${JSON.stringify(
ack.err,
null,
4
)}`
)
)
} else {
res()
}
}
)
}))
// move this to a subscription; implement off() ? todo
API.Jobs.onOrders(user, gun, mySEA)
API.Jobs.lastSeenNode(user)
API.Events.onSeedBackup(() => {}, user, mySEA)
return ack.sea.pub
@ -392,37 +372,8 @@ const authenticate = async (alias, pass, __user) => {
}
}
const instantiateGun = () => {
const Config = require('../config')
// if (user) {
// user.leave()
// }
// @ts-ignore
user = null
if (gun) {
gun.off()
}
// @ts-ignore
gun = null
const _gun = /** @type {unknown} */ (Gun({
axe: false,
multicast: false,
peers: Config.PEERS
}))
gun = /** @type {GUNNode} */ (_gun)
user = gun.user()
}
instantiateGun()
const freshGun = () => {
return {
gun,
user
}
const logoff = () => {
user.leave()
}
/**
@ -512,22 +463,24 @@ const register = async (alias, pass) => {
// restart instances so write to user graph work, there's an issue with gun
// (at least on node) where after initial user creation, writes to user graph
// don't work
instantiateGun()
// instantiateGun()
logoff()
return authenticate(alias, pass)
}
module.exports = {
authenticate,
instantiateGun,
isAuthenticated,
isAuthenticating,
isRegistering,
gun,
user,
register,
getGun,
getUser,
mySEA,
getMySecret,
freshGun,
$$__SHOCKWALLET__ENCRYPTED__
}