Browse Source

fix(lnd-ipc): refactor methods and pass lnd to functions

renovate/lint-staged-8.x
Jack Mallers 7 years ago
parent
commit
c751dce4bd
  1. 121
      app/lnd/index.js
  2. 3
      app/lnd/methods/allchannels.js
  3. 10
      app/lnd/methods/channelbalance.js
  4. 10
      app/lnd/methods/channels.js
  5. 10
      app/lnd/methods/createinvoice.js
  6. 74
      app/lnd/methods/index.js
  7. 10
      app/lnd/methods/info.js
  8. 8
      app/lnd/methods/invoice.js
  9. 10
      app/lnd/methods/invoices.js
  10. 10
      app/lnd/methods/payinvoice.js
  11. 10
      app/lnd/methods/payments.js
  12. 10
      app/lnd/methods/peers.js
  13. 10
      app/lnd/methods/pendingchannels.js
  14. 10
      app/lnd/methods/walletbalance.js
  15. 56
      app/main.dev.js
  16. 1
      app/reducers/invoice.js
  17. 5
      app/reducers/ipc.js
  18. 28
      app/reducers/payment.js

121
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)

3
app/lnd/methods/allchannels.js

@ -0,0 +1,3 @@
export default function(channels, pendingchannels) {
return Promise.all([channels, pendingchannels])
}

10
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)
})
})
}

10
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)
})
})
}

10
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)
})
})
}

74
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
}
}

10
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)
})
})
}

8
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))
})
}

10
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)
})
})
}

10
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)
})
})
}

10
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)
})
})
}

10
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)
})
})
}

10
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)
})
})
}

10
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)
})
})
}

56
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)
})

1
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

5
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

28
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

Loading…
Cancel
Save