2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-11 13:10:41 +00:00
RTL/controllers/wallet.js

58 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-01-01 16:26:51 +00:00
var request = require('request-promise');
2018-09-15 01:31:01 +00:00
var options = require("../connect");
var common = require('../common');
2018-09-15 01:31:01 +00:00
exports.operateWallet = (req, res, next) => {
var requestBody = {
wallet_password: Buffer.from(req.body.wallet_password).toString('base64')
};
if (undefined === req.params.operation || req.params.operation === 'unlock') {
options.url = common.lnd_server_url + '/unlockwallet';
2018-09-15 01:31:01 +00:00
options.form = JSON.stringify(requestBody);
err_message = 'Unlocking wallet failed! Verify that lnd is running!';
} else {
options.url = common.lnd_server_url + '/initwallet';
2018-09-15 01:31:01 +00:00
options.form = JSON.stringify(requestBody);
err_message = 'Initializing wallet failed!';
}
options.qs = req.query;
2019-01-01 16:26:51 +00:00
request.post(options).then((body) => {
2018-09-15 01:31:01 +00:00
console.log('\nUnlock Wallet Response: ');
console.log(body);
2018-09-16 06:29:21 +00:00
const body_str = (undefined === body) ? '' : JSON.stringify(body);
const search_idx = (undefined === body) ? -1 : body_str.search('Not Found');
if(undefined === body) {
2018-09-15 01:31:01 +00:00
res.status(500).json({
message: err_message,
2018-09-16 06:29:21 +00:00
error: 'Unlocking wallet failed! Verify that lnd is running!'
2018-09-15 01:31:01 +00:00
});
2018-09-16 06:29:21 +00:00
} else if(search_idx > -1) {
res.status(500).json({
message: err_message,
error: 'Unlocking wallet failed! Verify that lnd is running!'
});
} else if(body.error) {
2019-01-01 16:26:51 +00:00
if((body.code === 1 && body.error === 'context canceled') || (body.code === 14 && body.error === 'transport is closing')) {
2018-09-16 06:29:21 +00:00
res.status(201).json({wallet: 'successful'});
} else {
res.status(500).json({
message: err_message,
error: body.error
});
}
2018-09-15 01:31:01 +00:00
} else {
res.status(201).json({wallet: 'successful'});
}
2019-01-01 16:26:51 +00:00
})
.catch(function (err) {
if((err.error.code === 1 && err.error.error === 'context canceled') || (err.error.code === 14 && err.error.error === 'transport is closing')) {
res.status(201).json({wallet: 'successful'});
} else {
res.status(500).json({
message: err_message,
error: err.error.error
});
}
2018-09-15 01:31:01 +00:00
});
};