diff --git a/src/routes.js b/src/routes.js index d0fb8359..19b24506 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1946,22 +1946,19 @@ module.exports = async ( ) }) - app.post('/api/lnd/listunspent', (req, res) => { - const { lightning } = LightningServices.services - const { minConfirmations = 3, maxConfirmations = 6 } = req.body - lightning.listUnspent( - { - min_confs: minConfirmations, - max_confs: maxConfirmations - }, - (err, unspent) => { - if (err) { - return handleError(res, err) - } - logger.debug('ListUnspent:', unspent) - res.json(unspent) - } - ) + app.post('/api/lnd/listunspent', async (req, res) => { + try { + return res.status(200).json({ + utxos: await LV2.listUnspent( + req.body.minConfirmations, + req.body.maxConfirmations + ) + }) + } catch (e) { + return res.status(500).json({ + errorMessage: e.message + }) + } }) app.get('/api/lnd/transactions', (req, res) => { diff --git a/utils/lightningServices/types.ts b/utils/lightningServices/types.ts index 4b8b5781..6e3c2f33 100644 --- a/utils/lightningServices/types.ts +++ b/utils/lightningServices/types.ts @@ -131,3 +131,35 @@ export interface Services { walletUnlocker: Record router: Record } + +export interface Utxo { + /** + * The type of address. + */ + address_type: unknown + + /** + * The address. + */ + address: string + + /** + * The value of the unspent coin in satoshis. + */ + amount_sat: number + + /** + * The pkscript in hex. + */ + pk_script: string + + /** + * The outpoint in format txid:n. + */ + outpoint: unknown + + /** + * The number of confirmations for the Utxo. + */ + confirmations: number +} diff --git a/utils/lightningServices/v2.js b/utils/lightningServices/v2.js index 89132e47..bda5f6ee 100644 --- a/utils/lightningServices/v2.js +++ b/utils/lightningServices/v2.js @@ -420,10 +420,39 @@ const newAddress = (type = 0) => { }) } +/** + * @typedef {import('./types').Utxo} Utxo + */ + +/** + * @param {number} minConfs + * @param {number} maxConfs + * @returns {Promise} + */ +const listUnspent = (minConfs = 3, maxConfs = 6) => + Common.makePromise((res, rej) => { + const { lightning } = lightningServices.getServices() + + lightning.listUnspent( + { + min_confs: minConfs, + max_confs: maxConfs + }, + (err, unspent) => { + if (err) { + rej(new Error(err.message)) + } else { + res(unspent.utxos) + } + } + ) + }) + module.exports = { sendPaymentV2Keysend, sendPaymentV2Invoice, listPayments, decodePayReq, - newAddress + newAddress, + listUnspent }