diff --git a/app/lnd/index.js b/app/lnd/index.js index 7cd730b4..572dee0c 100644 --- a/app/lnd/index.js +++ b/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 } diff --git a/app/main.dev.js b/app/main.dev.js index 98fa93ef..4a662192 100644 --- a/app/main.dev.js +++ b/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 } diff --git a/app/reducers/invoice.js b/app/reducers/invoice.js index 60988dac..f58f1bcc 100644 --- a/app/reducers/invoice.js +++ b/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 // ------------------------------------ diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index 98645466..45638d23 100644 --- a/app/reducers/ipc.js +++ b/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 \ No newline at end of file diff --git a/app/routes/app/components/components/Form/Form.js b/app/routes/app/components/components/Form/Form.js index 40c56eec..a646f7fd 100644 --- a/app/routes/app/components/components/Form/Form.js +++ b/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) => {