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