diff --git a/app/lnd/index.js b/app/lnd/index.js index eaabff61..944cfe3e 100644 --- a/app/lnd/index.js +++ b/app/lnd/index.js @@ -3,6 +3,7 @@ import lightning from './lib/lightning' const lnd = lightning(config.lightningRpc, config.lightningHost) +// LND Get Info export function info() { return new Promise((resolve, reject) => { lnd.getInfo({}, (err, data) => { @@ -13,6 +14,7 @@ export function info() { }) } +// LND List Peers export function peers() { return new Promise((resolve, reject) => { lnd.listPeers({}, (err, data) => { @@ -23,7 +25,43 @@ export function peers() { }) } +// 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) + }) + }) +} + export default { info, - peers -} \ No newline at end of file + peers, + allChannels, + payments +} diff --git a/app/main.dev.js b/app/main.dev.js index 42dbb633..70c20b43 100644 --- a/app/main.dev.js +++ b/app/main.dev.js @@ -100,10 +100,23 @@ ipcMain.on('lnd', (event, { msg, data }) => { .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 default: return } diff --git a/app/reducers/channels.js b/app/reducers/channels.js index 20fef970..33cbd5f3 100644 --- a/app/reducers/channels.js +++ b/app/reducers/channels.js @@ -1,4 +1,5 @@ import { createSelector } from 'reselect' +import { ipcRenderer } from 'electron' import { callApi, callApis } from '../api' // ------------------------------------ // Constants @@ -38,14 +39,6 @@ export function getChannels() { } } -export function receiveChannels(channels) { - return { - type: RECEIVE_CHANNELS, - channels: channels[0].data.channels, - pendingChannels: channels[1].data - } -} - export function openingChannel() { return { type: OPENING_CHANNEL @@ -64,12 +57,15 @@ export function openingFailure() { } } +// Send IPC event for peers export const fetchChannels = () => async (dispatch) => { dispatch(getChannels()) - const channels = await callApis(['channels', 'pending_channels']) - dispatch(receiveChannels(channels)) + ipcRenderer.send('lnd', { msg: 'channels' }) } +// Receive IPC event for channels +export const receiveChannels = (event, { channels, pendingChannels }) => dispatch => dispatch({ type: RECEIVE_CHANNELS, channels, pendingChannels }) + export const openChannel = ({ pubkey, localamt, pushamt }) => async (dispatch) => { const payload = { pubkey, localamt, pushamt } dispatch(openingChannel()) diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index 02a68711..d750ce7f 100644 --- a/app/reducers/ipc.js +++ b/app/reducers/ipc.js @@ -1,11 +1,15 @@ import createIpc from 'redux-electron-ipc' import { receiveInfo } from './info' import { receivePeers } from './peers' +import { receiveChannels } from './channels' +import { receivePayments } from './payment' // Import all receiving IPC event handlers and pass them into createIpc const ipc = createIpc({ 'receiveInfo': receiveInfo, - 'receivePeers': receivePeers + 'receivePeers': receivePeers, + 'receiveChannels': receiveChannels, + 'receivePayments': receivePayments }) export default ipc \ No newline at end of file diff --git a/app/reducers/payment.js b/app/reducers/payment.js index 2bf3bc96..3d8a3f1e 100644 --- a/app/reducers/payment.js +++ b/app/reducers/payment.js @@ -1,4 +1,5 @@ import { createSelector } from 'reselect' +import { ipcRenderer } from 'electron' import { callApi } from '../api' // ------------------------------------ @@ -29,13 +30,6 @@ export function getPayments() { } } -export function receivePayments(data) { - return { - type: RECEIVE_PAYMENTS, - payments: data.payments.reverse() - } -} - export function sendPayment() { return { type: SEND_PAYMENT @@ -55,19 +49,15 @@ export function paymentFailed() { } } +// Send IPC event for peers export const fetchPayments = () => async (dispatch) => { dispatch(getPayments()) - const payments = await callApi('payments') - - if (payments) { - dispatch(receivePayments(payments.data)) - } else { - dispatch(paymentFailed()) - } - - return payments + ipcRenderer.send('lnd', { msg: 'payments' }) } +// Receive IPC event for peers +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 })