2019-02-04 23:49:19 +00:00
|
|
|
var request = require('request');
|
2018-10-10 00:08:05 +00:00
|
|
|
var common = require('../common');
|
2019-01-13 22:55:25 +00:00
|
|
|
var logger = require('./logger');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2018-09-15 01:31:01 +00:00
|
|
|
|
|
|
|
exports.operateWallet = (req, res, next) => {
|
2019-04-06 22:42:09 +00:00
|
|
|
options = common.getOptions(1);
|
2019-02-04 23:49:19 +00:00
|
|
|
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') {
|
2019-04-06 22:42:09 +00:00
|
|
|
options.url = common.findNode(1).lnd_server_url + '/unlockwallet';
|
2018-09-15 01:31:01 +00:00
|
|
|
options.form = JSON.stringify(requestBody);
|
2019-02-04 23:49:19 +00:00
|
|
|
err_message = 'Unlocking wallet failed! Verify that lnd is running and the wallet is locked!';
|
2018-09-15 01:31:01 +00:00
|
|
|
} else {
|
2019-04-06 22:42:09 +00:00
|
|
|
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!';
|
|
|
|
}
|
2019-02-04 23:49:19 +00:00
|
|
|
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,
|
2019-02-04 23:49:19 +00:00
|
|
|
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,
|
2019-02-04 23:49:19 +00:00
|
|
|
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'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|