Fixed unhandled rejection error and added more logs

This commit is contained in:
emad-salah 2019-12-08 19:06:43 +01:00
parent 1896353ff3
commit a6d499bcbb

View file

@ -328,132 +328,156 @@ module.exports = (
}); });
app.post("/api/lnd/wallet", async (req, res) => { app.post("/api/lnd/wallet", async (req, res) => {
const { password, alias } = req.body; try {
const healthResponse = await checkHealth(); const { password, alias } = req.body;
if (!alias) { const healthResponse = await checkHealth();
return res.status(400).json({ if (!alias) {
field: "alias", return res.status(400).json({
errorMessage: "Please specify an alias for your new wallet" field: "alias",
}); errorMessage: "Please specify an alias for your new wallet"
} });
if (!password) {
return res.status(400).json({
field: "password",
errorMessage: "Please specify a password for your new wallet"
});
}
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"
});
}
walletUnlocker.genSeed({}, async (genSeedErr, genSeedResponse) => {
if (genSeedErr) {
logger.debug("GenSeed Error:", genSeedErr);
const healthResponse = await checkHealth();
if (healthResponse.LNDStatus.success) {
const message = genSeedErr.details;
return res
.status(400)
.send({ field: "GenSeed", errorMessage, success: false });
}
return res
.status(500)
.send({ field: "health", errorMessage: "LND is down", success: false });
} }
logger.debug("GenSeed:", genSeedResponse); if (!password) {
const mnemonicPhrase = genSeedResponse.cipher_seed_mnemonic; return res.status(400).json({
const walletArgs = { field: "password",
wallet_password: Buffer.from(password, "utf8"), errorMessage: "Please specify a password for your new wallet"
cipher_seed_mnemonic: mnemonicPhrase });
}; }
// Register user before creating wallet if (password.length < 8) {
const publicKey = await GunDB.register(alias, password); return res.status(400).json({
field: "password",
walletUnlocker.initWallet( errorMessage: "Please specify a password that's longer than 8 characters"
walletArgs, });
async (initWalletErr, initWalletResponse) => { }
if (initWalletErr) {
logger.error("initWallet Error:", initWalletErr.message); if (healthResponse.LNDStatus.service !== "walletUnlocker") {
return res.status(400).json({
field: "wallet",
errorMessage: "Wallet is already unlocked"
});
}
walletUnlocker.genSeed({}, async (genSeedErr, genSeedResponse) => {
try {
if (genSeedErr) {
logger.debug("GenSeed Error:", genSeedErr);
const healthResponse = await checkHealth(); const healthResponse = await checkHealth();
if (healthResponse.LNDStatus.success) { if (healthResponse.LNDStatus.success) {
const errorMessage = initWalletErr.details; const message = genSeedErr.details;
return res
return res.status(400).json({ .status(400)
field: "initWallet", .send({ field: "GenSeed", errorMessage: message, success: false });
errorMessage,
success: false
});
} }
return res.status(500).json({
field: "health", return res
errorMessage: "LND is down", .status(500)
success: false .send({ field: "health", errorMessage: "LND is down", success: false });
});
} }
logger.debug("initWallet:", initWalletResponse);
logger.debug("GenSeed:", genSeedResponse);
const waitUntilFileExists = seconds => { const mnemonicPhrase = genSeedResponse.cipher_seed_mnemonic;
logger.debug( const walletArgs = {
`Waiting for admin.macaroon to be created. Seconds passed: ${seconds}` wallet_password: Buffer.from(password, "utf8"),
); cipher_seed_mnemonic: mnemonicPhrase
setTimeout(async () => {
try {
const macaroonExists = await FS.access(
lnServicesData.macaroonPath
);
if (!macaroonExists) {
return waitUntilFileExists(seconds + 1);
}
logger.debug("admin.macaroon file created");
mySocketsEvents.emit("updateLightning");
const lnServices = await require("../services/lnd/lightning")(
lnServicesData.lndProto,
lnServicesData.lndHost,
lnServicesData.lndCertPath,
lnServicesData.macaroonPath
);
lightning = lnServices.lightning;
walletUnlocker = lnServices.walletUnlocker;
const token = await auth.generateToken();
return res.json({
mnemonicPhrase,
authorization: token,
user: {
alias,
publicKey
}
});
} catch (err) {
res.status(400).json({
field: "unknown",
errorMessage: sanitizeLNDError(err.message)
});
}
}, 1000);
}; };
waitUntilFileExists(1); // Register user before creating wallet
const publicKey = await GunDB.register(alias, password);
walletUnlocker.initWallet(
walletArgs,
async (initWalletErr, initWalletResponse) => {
try {
if (initWalletErr) {
logger.error("initWallet Error:", initWalletErr.message);
const healthResponse = await checkHealth();
if (healthResponse.LNDStatus.success) {
const errorMessage = initWalletErr.details;
return res.status(400).json({
field: "initWallet",
errorMessage,
success: false
});
}
return res.status(500).json({
field: "health",
errorMessage: "LND is down",
success: false
});
}
logger.debug("initWallet:", initWalletResponse);
const waitUntilFileExists = seconds => {
logger.debug(
`Waiting for admin.macaroon to be created. Seconds passed: ${seconds}`
);
setTimeout(async () => {
try {
const macaroonExists = await FS.access(
lnServicesData.macaroonPath
);
if (!macaroonExists) {
return waitUntilFileExists(seconds + 1);
}
logger.debug("admin.macaroon file created");
mySocketsEvents.emit("updateLightning");
const lnServices = await require("../services/lnd/lightning")(
lnServicesData.lndProto,
lnServicesData.lndHost,
lnServicesData.lndCertPath,
lnServicesData.macaroonPath
);
lightning = lnServices.lightning;
walletUnlocker = lnServices.walletUnlocker;
const token = await auth.generateToken();
return res.json({
mnemonicPhrase,
authorization: token,
user: {
alias,
publicKey
}
});
} catch (err) {
res.status(400).json({
field: "unknown",
errorMessage: sanitizeLNDError(err.message)
});
}
}, 1000);
};
waitUntilFileExists(1);
} catch (err) {
console.error(err);
return res.status(500).json({
field: "unknown",
errorMessage: err
})
}
}
);
} catch (err) {
console.error(err);
return res.status(500).json({
field: "unknown",
errorMessage: err
})
} }
); });
}); } catch (err) {
console.error(err);
return res.status(500).json({
field: "unknown",
errorMessage: err
})
}
}); });
app.post("/api/lnd/wallet/existing", async (req, res) => { app.post("/api/lnd/wallet/existing", async (req, res) => {