Merge pull request #116 from shocknet/fix/signup-ux

Fix/signup ux
This commit is contained in:
Daniel Lugo 2020-07-17 15:51:06 -04:00 committed by GitHub
commit ae4dbe1940
6 changed files with 77 additions and 5 deletions

View file

@ -25,6 +25,8 @@ const parsePath = (filePath = "") => {
const lndDirectory = getLndDirectory(); const lndDirectory = getLndDirectory();
const SHOCK_SUPER_PEER = "http://gun.shock.network:8765/gun"
module.exports = (mainnet = false) => { module.exports = (mainnet = false) => {
const network = mainnet ? "mainnet" : "testnet"; const network = mainnet ? "mainnet" : "testnet";
@ -48,7 +50,7 @@ module.exports = (mainnet = false) => {
logfile: "shockapi.log", logfile: "shockapi.log",
lndLogFile: parsePath(`${lndDirectory}/logs/bitcoin/${network}/lnd.log`), lndLogFile: parsePath(`${lndDirectory}/logs/bitcoin/${network}/lnd.log`),
lndDirPath: lndDirectory, lndDirPath: lndDirectory,
peers: ["http://gun.shock.network:8765/gun"], peers: [SHOCK_SUPER_PEER],
useTLS: false, useTLS: false,
tokenExpirationMS: 4500000 tokenExpirationMS: 4500000
}; };

View file

@ -12,8 +12,8 @@ require('gun/lib/open')
// @ts-ignore // @ts-ignore
require('gun/lib/load') require('gun/lib/load')
const debounce = require('lodash/debounce') const debounce = require('lodash/debounce')
const Encryption = require('../../../utils/encryptionStore')
const Encryption = require('../../../utils/encryptionStore')
const Key = require('../contact-api/key') const Key = require('../contact-api/key')
/** @type {import('../contact-api/SimpleGUN').ISEA} */ /** @type {import('../contact-api/SimpleGUN').ISEA} */
@ -1275,6 +1275,56 @@ const register = async (alias, pass) => {
) )
} }
/**
* Peers provided to gun.
*/
const peers = Object.values(gun._.opt.peers)
const theresPeers = peers.length > 0
const atLeastOneIsConnected = peers.some(
p => p.wire && p.wire.readyState === 1
)
if (theresPeers && !atLeastOneIsConnected) {
throw new Error(
'No connected to any peers for checking of duplicate aliases'
)
}
if (theresPeers && atLeastOneIsConnected) {
// this import is done here to avoid circular dependency hell
const { timeout5 } = require('../contact-api/utils')
let userData = await timeout5(
new Promise(res => {
gun.get(`~@${alias}`).once(ud => res(ud))
})
)
if (userData) {
throw new Error(
'The given alias has been used before, use an unique alias instead.'
)
}
await new Promise(res => setTimeout(res, 300))
userData = await timeout5(
new Promise(res => {
gun.get(`~@${alias}`).once(ud => res(ud), {
// https://github.com/amark/gun/pull/971#issue-438630761
wait: 1500
})
})
)
if (userData) {
throw new Error(
'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)'
)
}
}
_isRegistering = true _isRegistering = true
/** @type {import('../contact-api/SimpleGUN').CreateAck} */ /** @type {import('../contact-api/SimpleGUN').CreateAck} */
@ -1282,6 +1332,13 @@ const register = async (alias, pass) => {
user.create(alias, pass, ack => res(ack)) user.create(alias, pass, ack => res(ack))
) )
// An empty ack object seems to be caused by a duplicate alias sign up
if ('{}' === JSON.stringify(ack)) {
throw new Error(
'The given alias has been used before, use an unique alias instead. (Empty ack)'
)
}
_isRegistering = false _isRegistering = false
if (typeof ack.err === 'string') { if (typeof ack.err === 'string') {

View file

@ -11,7 +11,9 @@ dotenv.config()
// @ts-ignore Let it crash if undefined // @ts-ignore Let it crash if undefined
exports.DATA_FILE_NAME = process.env.DATA_FILE_NAME || defaults.dataFileName exports.DATA_FILE_NAME = process.env.DATA_FILE_NAME || defaults.dataFileName
// @ts-ignore Let it crash if undefined /**
* @type {string[]}
*/
exports.PEERS = process.env.PEERS exports.PEERS = process.env.PEERS
? JSON.parse(process.env.PEERS) ? JSON.parse(process.env.PEERS)
: defaults.peers : defaults.peers

View file

@ -30,9 +30,20 @@ interface OpenListenerDataObj {
export type Listener = (data: ListenerData, key: string) => void export type Listener = (data: ListenerData, key: string) => void
export type Callback = (ack: Ack) => void export type Callback = (ack: Ack) => void
export interface Peer {
url: string
id: string
wire?: {
readyState: number
}
}
export interface Soul { export interface Soul {
get: string get: string
put: Primitive | null | object | undefined put: Primitive | null | object | undefined
opt: {
peers: Record<string, Peer>
}
} }
export type OpenListenerData = Primitive | null | OpenListenerDataObj export type OpenListenerData = Primitive | null | OpenListenerDataObj

View file

@ -713,7 +713,7 @@ module.exports = async (
logger.error(err); logger.error(err);
return res.status(500).json({ return res.status(500).json({
field: "unknown", field: "unknown",
errorMessage: err errorMessage: err.message || err
}) })
} }
}); });