Browse Source

feature(lnd-ipc): ipc form invoice

renovate/lint-staged-8.x
Jack Mallers 8 years ago
parent
commit
192d0e7b21
  1. 9
      app/lnd/index.js
  2. 37
      app/lnd/utils/index.js
  3. 9
      app/main.dev.js
  4. 29
      app/reducers/invoice.js
  5. 3
      app/reducers/ipc.js
  6. 3
      package.json
  7. 4
      yarn.lock

9
app/lnd/index.js

@ -1,5 +1,6 @@
import config from './config' import config from './config'
import lightning from './lib/lightning' import lightning from './lib/lightning'
import { decodeInvoice } from './utils'
const lnd = lightning(config.lightningRpc, config.lightningHost) const lnd = lightning(config.lightningRpc, config.lightningHost)
@ -70,6 +71,13 @@ export function invoices() {
}) })
} }
// LND Get Invoice
export function invoice(payreq) {
return new Promise((resolve, reject) => {
resolve(decodeInvoice(payreq))
})
}
// LND Get Wallet Balance // LND Get Wallet Balance
const walletBalance = new Promise((resolve, reject) => { const walletBalance = new Promise((resolve, reject) => {
lnd.walletBalance({}, (err, data) => { lnd.walletBalance({}, (err, data) => {
@ -110,6 +118,7 @@ export default {
allChannels, allChannels,
payments, payments,
invoices, invoices,
invoice,
balance, balance,
createInvoice createInvoice
} }

37
app/lnd/utils/index.js

@ -0,0 +1,37 @@
import zbase32 from 'zbase32'
function convertBigEndianBufferToLong(longBuffer) {
let longValue = 0
const byteArray = Buffer.from(longBuffer).swap64()
for (let i = byteArray.length - 1; i >= 0; i--) {
longValue = (longValue * 256) + byteArray[i]
}
return longValue
}
export function decodeInvoice(payreq) {
const payreqBase32 = zbase32.decode(payreq)
const bufferHexRotated = Buffer.from(payreqBase32).toString('hex')
const bufferHex = bufferHexRotated.substr(bufferHexRotated.length - 1, bufferHexRotated.length)
+ bufferHexRotated.substr(0, bufferHexRotated.length - 1)
const buffer = Buffer.from(bufferHex, 'hex')
const pubKeyBuffer = buffer.slice(0, 33)
const pubKeyHex = pubKeyBuffer.toString('hex')
const paymentHashBuffer = buffer.slice(33, 65)
const paymentHashHex = paymentHashBuffer.toString('hex')
const valueBuffer = buffer.slice(65, 73)
const amount = convertBigEndianBufferToLong(valueBuffer)
return {
payreq,
amount,
r_hash: paymentHashHex,
}
}

9
app/main.dev.js

@ -123,6 +123,15 @@ ipcMain.on('lnd', (event, { msg, data }) => {
.then(invoices => event.sender.send('receiveInvoices', invoices)) .then(invoices => event.sender.send('receiveInvoices', invoices))
.catch(error => console.log('info error: ', error)) .catch(error => console.log('info error: ', error))
break 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': case 'balance':
// Balance looks like [ { balance: '129477456' }, { balance: '243914' } ] // Balance looks like [ { balance: '129477456' }, { balance: '243914' } ]
lnd.balance() lnd.balance()

29
app/reducers/invoice.js

@ -51,13 +51,6 @@ export function receiveInvoice(invoice) {
} }
} }
export function receiveFormInvoice(formInvoice) {
return {
type: RECEIVE_FORM_INVOICE,
formInvoice
}
}
export function getInvoices() { export function getInvoices() {
return { return {
type: GET_INVOICES type: GET_INVOICES
@ -70,31 +63,21 @@ export function sendInvoice() {
} }
} }
export function invoiceSuccessful(invoice) {
return {
type: INVOICE_SUCCESSFUL,
invoice
}
}
export function invoiceFailed() { export function invoiceFailed() {
return { return {
type: INVOICE_FAILED type: INVOICE_FAILED
} }
} }
export const fetchInvoice = payreq => async (dispatch) => { // Send IPC event for a specific invoice
export const fetchInvoice = payreq => dispatch => {
dispatch(getInvoice()) dispatch(getInvoice())
const invoice = await callApi(`invoice/${payreq}`, 'get') ipcRenderer.send('lnd', { msg: 'invoice', data: { payreq } })
if (invoice) {
dispatch(receiveFormInvoice(invoice.data))
return true
}
dispatch(invoiceFailed())
return false
} }
// Receive IPC event for form invoice
export const receiveFormInvoice = (event, formInvoice) => dispatch => dispatch({ type: RECEIVE_FORM_INVOICE, formInvoice })
// Send IPC event for invoices // Send IPC event for invoices
export const fetchInvoices = () => dispatch => { export const fetchInvoices = () => dispatch => {
dispatch(getInvoices()) dispatch(getInvoices())

3
app/reducers/ipc.js

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

3
package.json

@ -211,7 +211,8 @@
"reselect": "^3.0.1", "reselect": "^3.0.1",
"satoshi-bitcoin": "^1.0.4", "satoshi-bitcoin": "^1.0.4",
"source-map-support": "^0.4.15", "source-map-support": "^0.4.15",
"xtend": "^4.0.1" "xtend": "^4.0.1",
"zbase32": "^0.0.2"
}, },
"devEngines": { "devEngines": {
"node": ">=7.x", "node": ">=7.x",

4
yarn.lock

@ -9271,6 +9271,10 @@ yauzl@2.4.1:
dependencies: dependencies:
fd-slicer "~1.0.1" fd-slicer "~1.0.1"
zbase32@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/zbase32/-/zbase32-0.0.2.tgz#169c6f2130a6c27a84247017538b56826a54b283"
zip-stream@^1.1.0: zip-stream@^1.1.0:
version "1.1.1" version "1.1.1"
resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-1.1.1.tgz#5216b48bbb4d2651f64d5c6e6f09eb4a7399d557" resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-1.1.1.tgz#5216b48bbb4d2651f64d5c6e6f09eb4a7399d557"

Loading…
Cancel
Save