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

48 lines
1.8 KiB
JavaScript
Raw Normal View History

var request = require('request');
var common = require('../common');
2019-01-13 22:55:25 +00:00
var logger = require('./logger');
var options = {};
2018-09-15 01:31:01 +00:00
exports.operateWallet = (req, res, next) => {
options = common.getOptions(1);
var requestBody = {
2018-09-15 01:31:01 +00:00
wallet_password: Buffer.from(req.body.wallet_password).toString('base64')
};
if (undefined === req.params.operation || req.params.operation === 'unlock') {
options.url = common.findNode(1).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 and the wallet is locked!';
2018-09-15 01:31:01 +00:00
} else {
options.url = common.findNode(1).lnd_server_url + '/initwallet';
2018-09-15 01:31:01 +00:00
options.form = JSON.stringify(requestBody);
err_message = 'Initializing wallet failed!';
}
request.post(options, (error, response, body) => {
2019-01-13 22:55:25 +00:00
logger.info('\r\nWallet: 20: ' + JSON.stringify(Date.now()) + ': INFO: Unlock Wallet Response: ' + JSON.stringify(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,
error: err_message
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: err_message
2018-09-16 06:29:21 +00:00
});
} 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'});
}
});
};