From c751dce4bd81f3aa8ddd4909bfd74ebc4d82ba33 Mon Sep 17 00:00:00 2001 From: Jack Mallers Date: Mon, 21 Aug 2017 19:21:05 -0500 Subject: [PATCH] fix(lnd-ipc): refactor methods and pass lnd to functions --- app/lnd/index.js | 121 +---------------------------- app/lnd/methods/allchannels.js | 3 + app/lnd/methods/channelbalance.js | 10 +++ app/lnd/methods/channels.js | 10 +++ app/lnd/methods/createinvoice.js | 10 +++ app/lnd/methods/index.js | 74 ++++++++++++++++++ app/lnd/methods/info.js | 10 +++ app/lnd/methods/invoice.js | 8 ++ app/lnd/methods/invoices.js | 10 +++ app/lnd/methods/payinvoice.js | 10 +++ app/lnd/methods/payments.js | 10 +++ app/lnd/methods/peers.js | 10 +++ app/lnd/methods/pendingchannels.js | 10 +++ app/lnd/methods/walletbalance.js | 10 +++ app/main.dev.js | 56 +------------ app/reducers/invoice.js | 1 - app/reducers/ipc.js | 5 +- app/reducers/payment.js | 28 ++++--- 18 files changed, 209 insertions(+), 187 deletions(-) create mode 100644 app/lnd/methods/allchannels.js create mode 100644 app/lnd/methods/channelbalance.js create mode 100644 app/lnd/methods/channels.js create mode 100644 app/lnd/methods/createinvoice.js create mode 100644 app/lnd/methods/index.js create mode 100644 app/lnd/methods/info.js create mode 100644 app/lnd/methods/invoice.js create mode 100644 app/lnd/methods/invoices.js create mode 100644 app/lnd/methods/payinvoice.js create mode 100644 app/lnd/methods/payments.js create mode 100644 app/lnd/methods/peers.js create mode 100644 app/lnd/methods/pendingchannels.js create mode 100644 app/lnd/methods/walletbalance.js diff --git a/app/lnd/index.js b/app/lnd/index.js index 2f2b5dcb..c2a87208 100644 --- a/app/lnd/index.js +++ b/app/lnd/index.js @@ -1,124 +1,7 @@ import config from './config' import lightning from './lib/lightning' -import { decodeInvoice } from './utils' +import methods from './methods' const lnd = lightning(config.lightningRpc, config.lightningHost) -// LND Get Info -export function info() { - return new Promise((resolve, reject) => { - lnd.getInfo({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) - }) -} - -// LND List Peers -export function peers() { - return new Promise((resolve, reject) => { - lnd.listPeers({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) - }) -} - -// LND List Channels -const channels = new Promise((resolve, reject) => { - lnd.listChannels({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) -}) - -// LND List Pending Channels -const pendingChannels = new Promise((resolve, reject) => { - lnd.pendingChannels({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) -}) - -// LND Get All Channels -export function allChannels() { - return Promise.all([channels, pendingChannels]) -} - -// LND Get Payments -export function payments() { - return new Promise((resolve, reject) => { - lnd.listPayments({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) - }) -} - -// LND Get Invoices -export function invoices() { - return new Promise((resolve, reject) => { - lnd.listInvoices({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) - }) -} - -// LND Get Invoice -export function invoice(payreq) { - return new Promise((resolve, reject) => { - resolve(decodeInvoice(payreq)) - }) -} - -// LND Get Wallet Balance -const walletBalance = new Promise((resolve, reject) => { - lnd.walletBalance({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) -}) - -// LND Get Channel Balance -const channelBalance = new Promise((resolve, reject) => { - lnd.channelBalance({}, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) -}) - -// LND Get Wallet + Channel Balance -export function balance() { - return Promise.all([walletBalance, channelBalance]) -} - -// LND Get Wallet + Channel Balance -export function createInvoice({ memo, value }) { - return new Promise((resolve, reject) => { - lnd.addInvoice({ memo, value }, (err, data) => { - if (err) { reject(err) } - - resolve(data) - }) - }) -} - -export default { - info, - peers, - allChannels, - payments, - invoices, - invoice, - balance, - createInvoice -} +export default (event, msg, data) => methods(lnd, event, msg, data) \ No newline at end of file diff --git a/app/lnd/methods/allchannels.js b/app/lnd/methods/allchannels.js new file mode 100644 index 00000000..3754e973 --- /dev/null +++ b/app/lnd/methods/allchannels.js @@ -0,0 +1,3 @@ +export default function(channels, pendingchannels) { + return Promise.all([channels, pendingchannels]) +} \ No newline at end of file diff --git a/app/lnd/methods/channelbalance.js b/app/lnd/methods/channelbalance.js new file mode 100644 index 00000000..c3c65413 --- /dev/null +++ b/app/lnd/methods/channelbalance.js @@ -0,0 +1,10 @@ +// LND Get Channel Balance +export default function channelbalance(lnd) { + return new Promise((resolve, reject) => { + lnd.channelBalance({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/channels.js b/app/lnd/methods/channels.js new file mode 100644 index 00000000..6b274762 --- /dev/null +++ b/app/lnd/methods/channels.js @@ -0,0 +1,10 @@ +// LND List Channels +export default function channels(lnd) { + return new Promise((resolve, reject) => { + lnd.listChannels({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/createinvoice.js b/app/lnd/methods/createinvoice.js new file mode 100644 index 00000000..44e37e8a --- /dev/null +++ b/app/lnd/methods/createinvoice.js @@ -0,0 +1,10 @@ +// LND Create an invoice +export function createInvoice(lnd, { memo, value }) { + return new Promise((resolve, reject) => { + lnd.addInvoice({ memo, value }, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/index.js b/app/lnd/methods/index.js new file mode 100644 index 00000000..d047f041 --- /dev/null +++ b/app/lnd/methods/index.js @@ -0,0 +1,74 @@ +import allchannels from './allchannels' +import channelbalance from './channelbalance' +import channels from './channels' +import createinvoice from './createinvoice' +import info from './info' +import invoice from './invoice' +import invoices from './invoices' +import payinvoice from './payinvoice' +import payments from './payments' +import peers from './peers' +import pendingchannels from './pendingchannels' +import walletbalance from './walletbalance' + +export default function(lnd, event, msg, data) { + switch(msg) { + case 'info': + info(lnd) + .then(info =>event.sender.send('receiveInfo', info)) + .catch(error => console.log('info error: ', error)) + break + case 'peers': + // Data looks like { peers: [] } + peers(lnd) + .then(peers => event.sender.send('receivePeers', peers)) + .catch(error => console.log('peers error: ', error)) + break + case 'channels': + // Data looks like [ { channels: [channel, channel, channel] }, { total_limbo_balance: 0, pending_open_channels: [], pending_closing_channels: [], pending_force_closing_channels: [] } ] + Promise.all([channels, pendingchannels].map(func => func(lnd))) + .then(data => event.sender.send('receiveChannels', { channels: data[0].channels, pendingChannels: data[1] })) + .catch(error => console.log('channels error: ', error)) + break + case 'payments': + // Data looks like { payments: [] } + payments(lnd) + .then(payments => event.sender.send('receivePayments', payments)) + .catch(error => console.log('payments error: ', error)) + break + case 'invoices': + // Data looks like { invoices: [] } + invoices(lnd) + .then(invoices => event.sender.send('receiveInvoices', invoices)) + .catch(error => console.log('invoices error: ', error)) + break + case 'invoice': + // Data looks like { invoices: [] } + invoice(data.payreq) + .then(invoice => event.sender.send('receiveInvoice', invoice)) + .catch(error => console.log('invoice error: ', error)) + break + case 'balance': + // Balance looks like [ { balance: '129477456' }, { balance: '243914' } ] + Promise.all([walletbalance, channelbalance].map(func => func(lnd))) + .then(balance => event.sender.send('receiveBalance', { walletBalance: balance[0].balance, channelBalance: balance[1].balance })) + .catch(error => console.log('balance error: ', error)) + break + case 'createInvoice': + // Invoice looks like { r_hash: Buffer, payment_request: '' } + const { memo, value } = data + createInvoice(lnd, { memo, value }) + .then(invoice => event.sender.send('createdInvoice', Object.assign(invoice, { memo, value, r_hash: new Buffer(invoice.r_hash,'hex').toString('hex') }))) + .catch(error => console.log('createInvoice error: ', error)) + break + case 'sendPayment': + // Payment looks like { payment_preimage: Buffer, payment_route: Object } + const { paymentRequest } = data + sendPayment(lnd, { paymentRequest }) + .then(payment => event.sender.send('paymentSuccessful')) + .catch(error => console.log('sendPayment error: ', error)) + break + default: + return + } +} \ No newline at end of file diff --git a/app/lnd/methods/info.js b/app/lnd/methods/info.js new file mode 100644 index 00000000..8b704980 --- /dev/null +++ b/app/lnd/methods/info.js @@ -0,0 +1,10 @@ +// LND Get Info +export default function info(lnd) { + return new Promise((resolve, reject) => { + lnd.getInfo({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} diff --git a/app/lnd/methods/invoice.js b/app/lnd/methods/invoice.js new file mode 100644 index 00000000..b738b436 --- /dev/null +++ b/app/lnd/methods/invoice.js @@ -0,0 +1,8 @@ +import { decodeInvoice } from '../utils' + +// LND Get Invoice +export default function invoice(payreq) { + return new Promise((resolve, reject) => { + resolve(decodeInvoice(payreq)) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/invoices.js b/app/lnd/methods/invoices.js new file mode 100644 index 00000000..682aeb02 --- /dev/null +++ b/app/lnd/methods/invoices.js @@ -0,0 +1,10 @@ +// LND Get Invoices +export default function invoices(lnd) { + return new Promise((resolve, reject) => { + lnd.listInvoices({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/payinvoice.js b/app/lnd/methods/payinvoice.js new file mode 100644 index 00000000..8c514afc --- /dev/null +++ b/app/lnd/methods/payinvoice.js @@ -0,0 +1,10 @@ +// LND Pay an invoice +export function payinvoice(lnd, { paymentRequest }) { + return new Promise((resolve, reject) => { + lnd.sendPaymentSync({ payment_request: paymentRequest }, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/payments.js b/app/lnd/methods/payments.js new file mode 100644 index 00000000..2182b1dc --- /dev/null +++ b/app/lnd/methods/payments.js @@ -0,0 +1,10 @@ +// LND Get Payments +export default function payments(lnd) { + return new Promise((resolve, reject) => { + lnd.listPayments({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/peers.js b/app/lnd/methods/peers.js new file mode 100644 index 00000000..a7231ef0 --- /dev/null +++ b/app/lnd/methods/peers.js @@ -0,0 +1,10 @@ +// LND List Peers +export default function peers(lnd) { + return new Promise((resolve, reject) => { + lnd.listPeers({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/pendingchannels.js b/app/lnd/methods/pendingchannels.js new file mode 100644 index 00000000..cc7d4a15 --- /dev/null +++ b/app/lnd/methods/pendingchannels.js @@ -0,0 +1,10 @@ +// LND Get Pending Channels +export default function channels(lnd) { + return new Promise((resolve, reject) => { + lnd.pendingChannels({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/lnd/methods/walletbalance.js b/app/lnd/methods/walletbalance.js new file mode 100644 index 00000000..7a830747 --- /dev/null +++ b/app/lnd/methods/walletbalance.js @@ -0,0 +1,10 @@ +// LND Get Wallet Balance +export default function walletbalance(lnd) { + return new Promise((resolve, reject) => { + lnd.walletBalance({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} \ No newline at end of file diff --git a/app/main.dev.js b/app/main.dev.js index a2be9e38..bce530ae 100644 --- a/app/main.dev.js +++ b/app/main.dev.js @@ -93,60 +93,6 @@ app.on('ready', async () => { }); ipcMain.on('lnd', (event, { msg, data }) => { - switch(msg) { - case 'info': - lnd.info() - .then(info => event.sender.send('receiveInfo', info)) - .catch(error => console.log('info error: ', error)) - break - case 'peers': - // Data looks like { peers: [] } - lnd.peers() - .then(peers => event.sender.send('receivePeers', peers)) - .catch(error => console.log('info error: ', error)) - break - case 'channels': - // Data looks like [ { channels: [channel, channel, channel] }, { total_limbo_balance: 0, pending_open_channels: [], pending_closing_channels: [], pending_force_closing_channels: [] } ] - lnd.allChannels() - .then(data => event.sender.send('receiveChannels', { channels: data[0].channels, pendingChannels: data[1] })) - .catch(error => console.log('info error: ', error)) - break - case 'payments': - // Data looks like { payments: [] } - lnd.payments() - .then(payments => event.sender.send('receivePayments', payments)) - .catch(error => console.log('info error: ', error)) - break - case 'invoices': - // Data looks like { invoices: [] } - lnd.invoices() - .then(invoices => event.sender.send('receiveInvoices', invoices)) - .catch(error => console.log('info error: ', error)) - break - case 'invoice': - // Data looks like { invoices: [] } - lnd.invoice(data.payreq) - .then(invoice => { - console.log('invoice: ', invoice) - event.sender.send('receiveInvoice', invoice) - }) - .catch(error => console.log('info error: ', error)) - break - case 'balance': - // Balance looks like [ { balance: '129477456' }, { balance: '243914' } ] - lnd.balance() - .then(balance => event.sender.send('receiveBalance', { walletBalance: balance[0].balance, channelBalance: balance[1].balance })) - .catch(error => console.log('info error: ', error)) - break - case 'createInvoice': - // Balance looks like { r_hash: Buffer, payment_request: '' } - const { memo, value } = data - lnd.createInvoice({ memo, value }) - .then(invoice => event.sender.send('createdInvoice', Object.assign(invoice, { memo, value, r_hash: new Buffer(invoice.r_hash,'hex').toString('hex') }))) - .catch(error => console.log('info error: ', error)) - break - default: - return - } + lnd(event, msg, data) }) diff --git a/app/reducers/invoice.js b/app/reducers/invoice.js index 5c6c25f4..aceb5735 100644 --- a/app/reducers/invoice.js +++ b/app/reducers/invoice.js @@ -1,6 +1,5 @@ import { createSelector } from 'reselect' import { ipcRenderer } from 'electron' -import { callApi } from '../api' import { btc, usd } from '../utils' // ------------------------------------ // Constants diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index 97f06cee..bea6c641 100644 --- a/app/reducers/ipc.js +++ b/app/reducers/ipc.js @@ -2,7 +2,7 @@ import createIpc from 'redux-electron-ipc' import { receiveInfo } from './info' import { receivePeers } from './peers' import { receiveChannels } from './channels' -import { receivePayments } from './payment' +import { receivePayments, paymentSuccessful } from './payment' import { receiveInvoices, createdInvoice, receiveFormInvoice } from './invoice' import { receiveBalance } from './balance' @@ -15,7 +15,8 @@ const ipc = createIpc({ 'receiveInvoices': receiveInvoices, 'receiveInvoice': receiveFormInvoice, 'receiveBalance': receiveBalance, - 'createdInvoice': createdInvoice + 'createdInvoice': createdInvoice, + 'paymentSuccessful': paymentSuccessful }) export default ipc \ No newline at end of file diff --git a/app/reducers/payment.js b/app/reducers/payment.js index 107ed19f..547d5ffa 100644 --- a/app/reducers/payment.js +++ b/app/reducers/payment.js @@ -50,7 +50,7 @@ export function paymentFailed() { } // Send IPC event for payments -export const fetchPayments = () => async (dispatch) => { +export const fetchPayments = () => dispatch => { dispatch(getPayments()) ipcRenderer.send('lnd', { msg: 'payments' }) } @@ -58,19 +58,27 @@ export const fetchPayments = () => async (dispatch) => { // Receive IPC event for payments export const receivePayments = (event, { payments }) => dispatch => dispatch({ type: RECEIVE_PAYMENTS, payments }) -export const payInvoice = payment_request => async (dispatch) => { - dispatch(sendPayment()) - const payment = await callApi('sendpayment', 'post', { payment_request }) +// export const payInvoice = payment_request => async (dispatch) => { +// dispatch(sendPayment()) +// const payment = await callApi('sendpayment', 'post', { payment_request }) - if (payment) { - dispatch(fetchPayments()) - } else { - dispatch(paymentFailed()) - } +// if (payment) { +// dispatch(fetchPayments()) +// } else { +// dispatch(paymentFailed()) +// } + +// return payment +// } - return payment +export const payInvoice = paymentRequest => dispatch => { + dispatch(sendPayment()) + ipcRenderer.send('lnd', { msg: 'sendPayment', data: { paymentRequest } }) } +// Receive IPC event for successful payment +export const paymentSuccessful = () => fetchPayments() + // ------------------------------------ // Action Handlers