2019-05-27 18:55:09 +00:00
|
|
|
var request = require('request-promise');
|
2021-06-20 20:27:08 +00:00
|
|
|
var common = require('../../routes/common');
|
2019-05-27 18:55:09 +00:00
|
|
|
var atob = require('atob');
|
2021-02-21 19:02:31 +00:00
|
|
|
var logger = require('../shared/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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Generating Seed..'});
|
2019-05-27 18:55:09 +00:00
|
|
|
options = common.getOptions();
|
2020-03-10 17:25:52 +00:00
|
|
|
if ( req.params.passphrase) {
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/genseed?aezeed_passphrase=' + Buffer.from(atob(req.params.passphrase)).toString('base64');
|
2019-11-06 21:37:04 +00:00
|
|
|
} else {
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/genseed';
|
2019-05-27 18:55:09 +00:00
|
|
|
}
|
|
|
|
request(options).then((body) => {
|
2021-08-17 11:21:01 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Seed Generated'});
|
|
|
|
res.status(200).json(body);
|
2019-05-27 18:55:09 +00:00
|
|
|
})
|
2020-05-03 19:52:38 +00:00
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', 'Gen Seed Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2019-05-27 18:55:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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';
|
2020-03-10 17:25:52 +00:00
|
|
|
if (!req.params.operation || req.params.operation === 'unlockwallet') {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Unlocking Wallet..'});
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/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 {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Initializing Wallet..'});
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/initwallet';
|
2020-03-10 17:25:52 +00:00
|
|
|
if ( req.body.aezeed_passphrase && req.body.aezeed_passphrase !== '') {
|
2019-05-27 18:55:09 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'Wallet Response', data: body});
|
2020-03-10 17:25:52 +00:00
|
|
|
const body_str = (!body) ? '' : JSON.stringify(body);
|
|
|
|
const search_idx = (!body) ? -1 : body_str.search('Not Found');
|
2021-08-17 11:21:01 +00:00
|
|
|
if (!body) {
|
|
|
|
const errMsg = error ? error : err_message;
|
|
|
|
const err = common.handleError({ statusCode: 500, message: 'Wallet Error', error: errMsg }, 'Wallet', errMsg);
|
|
|
|
return res.status(err.statusCode).json({message: err.error, error: err.error});
|
|
|
|
} else if (search_idx > -1) {
|
|
|
|
const errMsg = err_message;
|
|
|
|
const err = common.handleError({ statusCode: 500, message: 'Wallet Error', error: errMsg }, 'Wallet', errMsg);
|
|
|
|
return res.status(err.statusCode).json({message: err.error, error: err.error});
|
|
|
|
} else if (body.error) {
|
|
|
|
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 {
|
2021-08-17 11:21:01 +00:00
|
|
|
const errMsg = (body.error && typeof body.error === 'object') ? JSON.stringify(body.error) : (body.error && typeof body.error === 'string') ? body.error : err_message;
|
|
|
|
const err = common.handleError({ statusCode: 500, message: 'Wallet Error', error: errMsg }, 'Wallet', errMsg);
|
|
|
|
return res.status(err.statusCode).json({message: err.error, error: err.error});
|
2018-09-16 06:29:21 +00:00
|
|
|
}
|
2018-09-15 01:31:01 +00:00
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Wallet Unlocked/Initialized'});
|
2019-05-27 18:55:09 +00:00
|
|
|
res.status(201).json('Successful');
|
2018-09-15 01:31:01 +00:00
|
|
|
}
|
2020-05-03 19:52:38 +00:00
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
if ((err.error.code === 1 && err.error.error === 'context canceled') || (err.error.code === 14 && err.error.error === 'transport is closing')) {
|
2019-05-27 19:24:09 +00:00
|
|
|
res.status(201).json('Successful');
|
|
|
|
} else {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', err_message);
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
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) => {
|
2020-05-03 19:52:38 +00:00
|
|
|
let response = common.updateSelectedNodeOptions();
|
|
|
|
res.status(response.status).json({updateMessage: response.message});
|
2019-11-06 21:37:04 +00:00
|
|
|
}
|
2020-10-12 22:43:16 +00:00
|
|
|
|
|
|
|
exports.getUTXOs = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Getting UTXOs..'});
|
2020-10-12 22:43:16 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/v2/wallet/utxos?max_confs=' + req.query.max_confs;
|
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'UTXO List Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'UTXOs Received'});
|
2020-10-12 22:43:16 +00:00
|
|
|
res.status(200).json(body.utxos ? body.utxos : []);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', 'List UTXOs Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-10-12 22:43:16 +00:00
|
|
|
});
|
|
|
|
}
|
2020-10-30 00:37:49 +00:00
|
|
|
|
|
|
|
exports.bumpFee = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Bumping Fee..'});
|
2020-10-30 00:37:49 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/v2/wallet/bumpfee';
|
|
|
|
options.form = {};
|
|
|
|
options.form.outpoint = {
|
|
|
|
txid_str: req.body.txid,
|
|
|
|
output_index: req.body.outputIndex
|
|
|
|
};
|
|
|
|
if (req.body.targetConf) {
|
|
|
|
options.form.target_conf = req.body.targetConf;
|
|
|
|
} else if (req.body.satPerByte) {
|
|
|
|
options.form.sat_per_byte = req.body.satPerByte;
|
|
|
|
}
|
|
|
|
options.form = JSON.stringify(options.form);
|
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'Bump Fee Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Fee Bumped'});
|
2020-10-30 00:37:49 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', 'Bump Fee Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-10-30 00:37:49 +00:00
|
|
|
});
|
|
|
|
}
|
2021-02-21 19:02:31 +00:00
|
|
|
|
|
|
|
exports.labelTransaction = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Labelling Transaction..'});
|
2021-02-21 19:02:31 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/v2/wallet/tx/label';
|
|
|
|
options.form = {};
|
|
|
|
options.form.txid = req.body.txid;
|
|
|
|
options.form.label = req.body.label;
|
|
|
|
options.form.overwrite = req.body.overwrite;
|
|
|
|
options.form = JSON.stringify(options.form);
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'Label Transaction Options', data: options.form});
|
2021-02-21 19:02:31 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'Label Transaction Post Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Transaction Labelled'});
|
2021-02-21 19:02:31 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', 'Label Transaction Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2021-02-21 19:02:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.leaseUTXO = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Leasing UTXO..'});
|
2021-02-21 19:02:31 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/v2/wallet/utxos/lease';
|
|
|
|
options.form = {};
|
|
|
|
options.form.id = req.body.txid;
|
|
|
|
options.form.outpoint = {
|
|
|
|
txid_bytes: req.body.txid,
|
|
|
|
output_index: req.body.outputIndex
|
|
|
|
};
|
|
|
|
options.form = JSON.stringify(options.form);
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'UTXO Lease Options', data: options.form});
|
2021-02-21 19:02:31 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'UTXO Lease Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'UTXO Leased'});
|
2021-02-21 19:02:31 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', 'Lease UTXO Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2021-02-21 19:02:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.releaseUTXO = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'Releasing UTXO..'});
|
2021-02-21 19:02:31 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/v2/wallet/utxos/release';
|
|
|
|
options.form = {};
|
|
|
|
options.form.id = req.body.txid;
|
|
|
|
options.form.outpoint = {
|
|
|
|
txid_bytes: req.body.txid,
|
|
|
|
output_index: req.body.outputIndex
|
|
|
|
};
|
|
|
|
options.form = JSON.stringify(options.form);
|
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Wallet', msg: 'UTXO Release Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Wallet', msg: 'UTXO Released'});
|
2021-02-21 19:02:31 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Wallet', 'Release UTXO Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2021-02-21 19:02:31 +00:00
|
|
|
});
|
|
|
|
}
|