2019-05-27 18:55:09 +00:00
|
|
|
var request = require('request-promise');
|
2019-09-01 17:01:38 +00:00
|
|
|
var common = require('../../common');
|
2019-05-27 18:55:09 +00:00
|
|
|
var atob = require('atob');
|
2019-09-01 17:01:38 +00:00
|
|
|
var logger = require('../logger');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2018-09-15 01:31:01 +00:00
|
|
|
|
2019-05-27 18:55:09 +00:00
|
|
|
exports.genSeed = (req, res, next) => {
|
|
|
|
options = common.getOptions();
|
|
|
|
if (undefined !== req.params.passphrase) {
|
2019-11-06 21:37:04 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/genseed?aezeed_passphrase=' + Buffer.from(atob(req.params.passphrase)).toString('base64');
|
|
|
|
} else {
|
|
|
|
options.url = common.getSelLNServerUrl() + '/genseed';
|
2019-05-27 18:55:09 +00:00
|
|
|
}
|
|
|
|
request(options).then((body) => {
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: "Genseed failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(200).json(body);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Genseed failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-15 01:31:01 +00:00
|
|
|
exports.operateWallet = (req, res, next) => {
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-05-27 18:55:09 +00:00
|
|
|
options.method = 'POST';
|
|
|
|
if (undefined === req.params.operation || req.params.operation === 'unlockwallet') {
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/unlockwallet';
|
2019-05-27 18:55:09 +00:00
|
|
|
options.form = JSON.stringify({
|
|
|
|
wallet_password: Buffer.from(atob(req.body.wallet_password)).toString('base64')
|
|
|
|
});
|
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-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/initwallet';
|
2019-05-27 18:55:09 +00:00
|
|
|
if (undefined !== req.body.aezeed_passphrase && req.body.aezeed_passphrase !== '') {
|
|
|
|
options.form = JSON.stringify({
|
|
|
|
wallet_password: Buffer.from(atob(req.body.wallet_password)).toString('base64'),
|
|
|
|
cipher_seed_mnemonic: req.body.cipher_seed_mnemonic,
|
|
|
|
aezeed_passphrase: Buffer.from(atob(req.body.aezeed_passphrase)).toString('base64')
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
options.form = JSON.stringify({
|
|
|
|
wallet_password: Buffer.from(atob(req.body.wallet_password)).toString('base64'),
|
|
|
|
cipher_seed_mnemonic: req.body.cipher_seed_mnemonic
|
|
|
|
});
|
|
|
|
}
|
2018-09-15 01:31:01 +00:00
|
|
|
err_message = 'Initializing wallet failed!';
|
|
|
|
}
|
2019-05-27 18:55:09 +00:00
|
|
|
request(options).then((body) => {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Wallet', msg: '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-05-27 18:55:09 +00:00
|
|
|
error: (error) ? 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')) {
|
2019-05-27 18:55:09 +00:00
|
|
|
res.status(201).json('Successful');
|
2018-09-16 06:29:21 +00:00
|
|
|
} else {
|
|
|
|
res.status(500).json({
|
|
|
|
message: err_message,
|
|
|
|
error: body.error
|
|
|
|
});
|
|
|
|
}
|
2018-09-15 01:31:01 +00:00
|
|
|
} else {
|
2019-05-27 18:55:09 +00:00
|
|
|
res.status(201).json('Successful');
|
2018-09-15 01:31:01 +00:00
|
|
|
}
|
2019-05-27 18:55:09 +00:00
|
|
|
}).catch(error => {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Wallet', lineNum: 83, msg: 'Wallet Response: ' + JSON.stringify(error.error)});
|
2019-05-27 19:24:09 +00:00
|
|
|
if((error.error.code === 1 && error.error.error === 'context canceled') || (error.error.code === 14 && error.error.error === 'transport is closing')) {
|
|
|
|
res.status(201).json('Successful');
|
|
|
|
} else {
|
|
|
|
res.status(500).json({
|
|
|
|
message: err_message,
|
2019-11-06 21:37:04 +00:00
|
|
|
error: error.error.message ? error.error.message : error.message
|
2019-05-27 19:24:09 +00:00
|
|
|
});
|
|
|
|
}
|
2018-09-15 01:31:01 +00:00
|
|
|
});
|
|
|
|
};
|
2019-11-06 21:37:04 +00:00
|
|
|
|
|
|
|
exports.updateSelNodeOptions = (req, res, next) => {
|
|
|
|
let response = common.updateSelectedNodeOptions();
|
|
|
|
res.status(response.status).json({updateMessage: response.message});
|
|
|
|
}
|