From 529454bd0800b328a19690d39601b51dddba2116 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 10 Mar 2020 15:47:45 -0400 Subject: [PATCH 1/5] catch all errors --- src/routes.js | 122 ++++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 58 deletions(-) diff --git a/src/routes.js b/src/routes.js index c3ae2385..5a6cef90 100644 --- a/src/routes.js +++ b/src/routes.js @@ -640,66 +640,72 @@ module.exports = async ( }); app.post("/api/lnd/wallet/existing", async (req, res) => { - const { password, alias } = req.body; - const healthResponse = await checkHealth(); - const exists = await walletExists(); - if (!exists) { - return res.status(500).json({ - field: "wallet", - errorMessage: "LND wallet does not exist, please create a new one" - }); - } - - if (!alias) { - return res.status(400).json({ - field: "alias", - errorMessage: "Please specify an alias for your wallet" - }); - } - - if (!password) { - return res.status(400).json({ - field: "password", - errorMessage: "Please specify a password for your wallet alias" - }); - } - - if (password.length < 8) { - return res.status(400).json({ - field: "password", - errorMessage: "Please specify a password that's longer than 8 characters" - }); - } - - if (healthResponse.LNDStatus.service !== "walletUnlocker") { - return res.status(400).json({ - field: "wallet", - errorMessage: "Wallet is already unlocked. Please restart your LND instance and try again." - }); - } - try { - await unlockWallet(password); - } catch(err) { - return res.status(401).json({ - field: "wallet", - errorMessage: "Invalid LND wallet password" - }); - } - - // Register user after verifying wallet password - const publicKey = await GunDB.register(alias, password); - - // Generate Access Token - const token = await auth.generateToken(); - - res.json({ - authorization: token, - user: { - alias, - publicKey + const { password, alias } = req.body; + const healthResponse = await checkHealth(); + const exists = await walletExists(); + if (!exists) { + return res.status(500).json({ + field: "wallet", + errorMessage: "LND wallet does not exist, please create a new one" + }); } - }) + + if (!alias) { + return res.status(400).json({ + field: "alias", + errorMessage: "Please specify an alias for your wallet" + }); + } + + if (!password) { + return res.status(400).json({ + field: "password", + errorMessage: "Please specify a password for your wallet alias" + }); + } + + if (password.length < 8) { + return res.status(400).json({ + field: "password", + errorMessage: "Please specify a password that's longer than 8 characters" + }); + } + + if (healthResponse.LNDStatus.service !== "walletUnlocker") { + return res.status(400).json({ + field: "wallet", + errorMessage: "Wallet is already unlocked. Please restart your LND instance and try again." + }); + } + + try { + await unlockWallet(password); + } catch(err) { + return res.status(401).json({ + field: "wallet", + errorMessage: "Invalid LND wallet password" + }); + } + + // Register user after verifying wallet password + const publicKey = await GunDB.register(alias, password); + + // Generate Access Token + const token = await auth.generateToken(); + + res.json({ + authorization: token, + user: { + alias, + publicKey + } + }) + } catch (err) { + return res.status(500).json({ + errorMessage: err.message, + }) + } }); // get lnd info From 5c649b46928fc61e5456c3556ba1c8370d8e4820 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 10 Mar 2020 16:57:25 -0400 Subject: [PATCH 2/5] remove unused endpoint --- src/routes.js | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/src/routes.js b/src/routes.js index 5a6cef90..1097258e 100644 --- a/src/routes.js +++ b/src/routes.js @@ -443,47 +443,6 @@ module.exports = async ( } }); - app.post("/api/lnd/connect", (req, res) => { - const { lightning, walletUnlocker } = LightningServices.services; - const args = { - wallet_password: Buffer.from(req.body.password, "utf-8") - }; - - lightning.getInfo({}, async err => { - if (err) { - // try to unlock wallet - await recreateLnServices(); - return walletUnlocker.unlockWallet(args, async unlockErr => { - if (unlockErr) { - unlockErr.error = unlockErr.message; - logger.error("Unlock Error:", unlockErr); - const health = await checkHealth(); - if (health.LNDStatus.success) { - res.status(400); - res.json({ field: "WalletUnlocker", errorMessage: unlockErr.message }); - } else { - res.status(500); - res.json({ errorMessage: "LND is down" }); - } - } else { - await recreateLnServices(); - mySocketsEvents.emit("updateLightning"); - const token = await auth.generateToken(); - res.json({ - authorization: token - }); - } - }); - } - - const token = await auth.generateToken(); - - return res.json({ - authorization: token - }); - }); - }); - app.post("/api/lnd/wallet", async (req, res) => { try { const { walletUnlocker } = LightningServices.services; From dfca4d9bbd3fa8a9389a527dcf7eeeaf61ed60b1 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 10 Mar 2020 18:09:53 -0400 Subject: [PATCH 3/5] partial stack trace for unhandled promise rejections --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fb4a12d4..3e7c59b3 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "src/server.js", "scripts": { "start": "node --experimental-modules src/server.js", - "dev": "node main.js -h 0.0.0.0", + "dev": "node --trace-warnings main.js -h 0.0.0.0", "dev:watch": "nodemon main.js -- -h 0.0.0.0", "test": "jest --no-cache", "test:watch": "jest --no-cache --watch", From 67efc4e995278fc6454931689c5303ab2944afdb Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 10 Mar 2020 18:11:11 -0400 Subject: [PATCH 4/5] no SEA throw (gun bug) --- services/gunDB/Mediator/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 29c30cff..5b8cda26 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -13,8 +13,9 @@ const Encryption = require('../../../utils/encryptionStore') /** @type {import('../contact-api/SimpleGUN').ISEA} */ // @ts-ignore const SEAx = require('gun/sea') -// @ts-ignore -SEAx.throw = true +// Re-enable in the future, when SEA errors inside user.auth/etc actually +// propagate up. +// SEAx.throw = true /** @type {import('../contact-api/SimpleGUN').ISEA} */ const mySEA = {} From 83868ff10fb1271281c44d96f654fcab58f6f289 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 10 Mar 2020 18:38:57 -0400 Subject: [PATCH 5/5] log error, better error message --- services/gunDB/Mediator/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 5b8cda26..263da404 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -316,7 +316,10 @@ const authenticate = async (alias, pass, __user) => { return ack.sea.pub } else { - throw new Error('Unknown error.') + logger.error( + `Unknown error, wrong password? Ack looks like: ${JSON.stringify(ack)}` + ) + throw new Error(`Didn't work, bad password?`) } }