Browse Source

feature(lnd-ipc): ipc create invoice

renovate/lint-staged-8.x
Jack Mallers 8 years ago
parent
commit
6163187c30
  1. 14
      app/lnd/index.js
  2. 7
      app/main.dev.js
  3. 18
      app/reducers/invoice.js
  4. 5
      app/reducers/ipc.js
  5. 8
      app/routes/app/components/components/Form/Form.js

14
app/lnd/index.js

@ -93,11 +93,23 @@ 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,
balance
balance,
createInvoice
}

7
app/main.dev.js

@ -129,6 +129,13 @@ ipcMain.on('lnd', (event, { msg, data }) => {
.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
}

18
app/reducers/invoice.js

@ -96,7 +96,7 @@ export const fetchInvoice = payreq => async (dispatch) => {
}
// Send IPC event for invoices
export const fetchInvoices = () => async (dispatch) => {
export const fetchInvoices = () => dispatch => {
dispatch(getInvoices())
ipcRenderer.send('lnd', { msg: 'invoices' })
}
@ -104,19 +104,15 @@ export const fetchInvoices = () => async (dispatch) => {
// Receive IPC event for invoices
export const receiveInvoices = (event, { invoices }) => dispatch => dispatch({ type: RECEIVE_INVOICES, invoices })
export const createInvoice = (amount, memo, currency, rate) => async (dispatch) => {
// Send IPC event for creating an invoice
export const createInvoice = (amount, memo, currency, rate) => dispatch => {
const value = currency === 'btc' ? btc.btcToSatoshis(amount) : btc.btcToSatoshis(usd.usdToBtc(amount, rate))
dispatch(sendInvoice())
const invoice = await callApi('addinvoice', 'post', { value, memo })
if (invoice) {
dispatch(invoiceSuccessful({ memo, value, payment_request: invoice.data.payment_request }))
} else {
dispatch(invoiceFailed())
}
return invoice
ipcRenderer.send('lnd', { msg: 'createInvoice', data: { value, memo } })
}
// Receive IPC event for newly created invoice
export const createdInvoice = (event, invoice) => dispatch => dispatch({ type: INVOICE_SUCCESSFUL, invoice })
// ------------------------------------
// Action Handlers
// ------------------------------------

5
app/reducers/ipc.js

@ -3,7 +3,7 @@ import { receiveInfo } from './info'
import { receivePeers } from './peers'
import { receiveChannels } from './channels'
import { receivePayments } from './payment'
import { receiveInvoices } from './invoice'
import { receiveInvoices, createdInvoice } from './invoice'
import { receiveBalance } from './balance'
// Import all receiving IPC event handlers and pass them into createIpc
@ -13,7 +13,8 @@ const ipc = createIpc({
'receiveChannels': receiveChannels,
'receivePayments': receivePayments,
'receiveInvoices': receiveInvoices,
'receiveBalance': receiveBalance
'receiveBalance': receiveBalance,
'createdInvoice': createdInvoice
})
export default ipc

8
app/routes/app/components/components/Form/Form.js

@ -20,16 +20,12 @@ const Form = ({
}) => {
const requestClicked = () => {
createInvoice(amount, message, currency, btcTicker.price_usd)
.then((success) => {
if (success) { close() }
})
close()
}
const payClicked = () => {
payInvoice(payment_request)
.then((success) => {
if (success) { close() }
})
close()
}
const paymentRequestOnChange = (payreq) => {

Loading…
Cancel
Save