Browse Source

Refactored LND API code. Added missing endpoints. Routes still missing for new endpoints.

renovate/lint-staged-8.x
João Almeida 8 years ago
parent
commit
0f7eb063b8
  1. 0
      app/lnd/config/.Rhistory
  2. 2
      app/lnd/config/index.js
  3. 121
      app/lnd/methods/channelController.js
  4. 10
      app/lnd/methods/channelbalance.js
  5. 10
      app/lnd/methods/channels.js
  6. 20
      app/lnd/methods/closechannel.js
  7. 10
      app/lnd/methods/connectpeer.js
  8. 10
      app/lnd/methods/createinvoice.js
  9. 10
      app/lnd/methods/disconnectpeer.js
  10. 85
      app/lnd/methods/index.js
  11. 10
      app/lnd/methods/info.js
  12. 12
      app/lnd/methods/invoice.js
  13. 10
      app/lnd/methods/invoices.js
  14. 52
      app/lnd/methods/invoicesController.js
  15. 86
      app/lnd/methods/networkController.js
  16. 10
      app/lnd/methods/newaddress.js
  17. 19
      app/lnd/methods/openchannel.js
  18. 10
      app/lnd/methods/payinvoice.js
  19. 10
      app/lnd/methods/payments.js
  20. 33
      app/lnd/methods/paymentsController.js
  21. 10
      app/lnd/methods/peers.js
  22. 52
      app/lnd/methods/peersController.js
  23. 10
      app/lnd/methods/pendingchannels.js
  24. 10
      app/lnd/methods/sendcoins.js
  25. 85
      app/lnd/methods/walletController.js
  26. 10
      app/lnd/methods/walletbalance.js

0
app/lnd/config/.Rhistory

2
app/lnd/config/index.js

@ -5,5 +5,5 @@
export default {
lightningRpc: `${__dirname}/rpc.proto`,
lightningHost: 'localhost:10009',
cert: '/Users/jmow/Library/Application Support/Lnd/tls.cert'
cert: '/Users/joaoalmeida/Library/Application\ Support/Lnd/tls.cert'
}

121
app/lnd/methods/channelController.js

@ -0,0 +1,121 @@
import bitcore from 'bitcore-lib'
import pushopenchannel from '../push/openchannel'
import pushclosechannel from '../push/closechannel'
const BufferUtil = bitcore.util.buffer
/**
* @param {[type]} lnd [description]
* @param {[type]} event [description]
* @param {[type]} payload [description]
* @return {[type]} [description]
*/
export function openChannel(lnd, event, payload) {
const { pubkey, localamt, pushamt } = payload
const res = {
node_pubkey: BufferUtil.hexToBuffer(pubkey),
local_funding_amount: Number(localamt),
push_sat: Number(pushamt)
}
return new Promise((resolve, reject) =>
pushopenchannel(lnd, event, res)
.then(data => resolve(data))
.catch(error => reject(error))
)
}
/**
* [channelBalance description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function channelBalance(lnd) {
return new Promise((resolve, reject) => {
lnd.channelBalance({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [listChannels description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function listChannels(lnd) {
return new Promise((resolve, reject) => {
lnd.listChannels({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [closechannel description]
* @param {[type]} lnd [description]
* @param {[type]} event [description]
* @param {[type]} payload [description]
* @return {[type]} [description]
*/
export function closeChannel(lnd, event, payload) {
const tx = payload.channel_point.funding_txid.match(/.{2}/g).reverse().join('')
const res = {
channel_point: {
funding_txid: BufferUtil.hexToBuffer(tx),
output_index: Number(payload.channel_point.output_index)
}
}
return new Promise((resolve, reject) =>
pushclosechannel(lnd, event, res)
.then(data => resolve(data))
.catch(error => reject(error))
)
}
/**
* [pendingChannels description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function pendingChannels(lnd) {
return new Promise((resolve, reject) => {
lnd.pendingChannels({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [getChanInfo description]
* @param {[type]} lnd [description]
* @param {[type]} channelId [description]
* @return {[type]} [description]
*/
export function getChanInfo(lnd, { chanId }){
return new Promise((resolve, reject) => {
lnd.getChanInfo({ chan_id: chanId }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/channelbalance.js

@ -1,10 +0,0 @@
// 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

@ -1,10 +0,0 @@
// LND List Channels
export default function channels(lnd) {
return new Promise((resolve, reject) => {
lnd.listChannels({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

20
app/lnd/methods/closechannel.js

@ -1,20 +0,0 @@
import bitcore from 'bitcore-lib'
import pushclosechannel from '../push/closechannel'
const BufferUtil = bitcore.util.buffer
export default function closechannel(lnd, event, payload) {
const tx = payload.channel_point.funding_txid.match(/.{2}/g).reverse().join('')
const res = {
channel_point: {
funding_txid: BufferUtil.hexToBuffer(tx),
output_index: Number(payload.channel_point.output_index)
}
}
return new Promise((resolve, reject) =>
pushclosechannel(lnd, event, res)
.then(data => resolve(data))
.catch(error => reject(error))
)
}

10
app/lnd/methods/connectpeer.js

@ -1,10 +0,0 @@
// LND Connect to a peer
export default function connectpeer(lnd, { pubkey, host }) {
return new Promise((resolve, reject) => {
lnd.connectPeer({ addr: { pubkey, host }, perm: true }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/createinvoice.js

@ -1,10 +0,0 @@
// LND Create an invoice
export default function createInvoice(lnd, { memo, value }) {
return new Promise((resolve, reject) => {
lnd.addInvoice({ memo, value }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/disconnectpeer.js

@ -1,10 +0,0 @@
// LND Disconnect from a peer
export default function disconnectpeer(lnd, { pubkey }) {
return new Promise((resolve, reject) => {
lnd.disconnectPeer({ pub_key: pubkey }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

85
app/lnd/methods/index.js

@ -1,27 +1,42 @@
/* eslint no-console: 0 */ // --> OFF
import channelbalance from './channelbalance'
import channels from './channels'
import closechannel from './closechannel'
import connectpeer from './connectpeer'
import createinvoice from './createinvoice'
import disconnectpeer from './disconnectpeer'
import info from './info'
import invoice from './invoice'
import invoices from './invoices'
import newaddress from './newaddress'
import openchannel from './openchannel'
import payinvoice from './payinvoice'
import payments from './payments'
import peers from './peers'
import pendingchannels from './pendingchannels'
import sendcoins from './sendcoins'
import walletbalance from './walletbalance'
//import channelbalance from './channelbalance'
//import channels from './channels'
//import closechannel from './closechannel'
//import connectpeer from './connectpeer'
//import createinvoice from './createinvoice'
//import disconnectpeer from './disconnectpeer'
//import info from './info'
//import invoice from './invoice'
//import invoices from './invoices'
//import newaddress from './newaddress'
//import openchannel from './openchannel'
//import payinvoice from './payinvoice'
//import payments from './payments'
//import peers from './peers'
//import pendingchannels from './pendingchannels'
//import sendcoins from './sendcoins'
//import walletbalance from './walletbalance'
import * as invoicesController from './invoicesController'
import * as channelController from './channelController'
import * as walletController from './walletController'
import * as peersController from './peersController'
import * as paymentsController from './paymentsController'
import * as networkController from './networkController'
//TODO - GetChanInfo
//TODO - GetTransactions
//TODO - GetNodeInfo
//TODO - DescribeGraph
//TODO - GetNetworkInfo
//TODO - QueryRoutes
export default function (lnd, event, msg, data) {
switch (msg) {
case 'info':
info(lnd)
networkController.getInfo(lnd)
.then((infoData) => {
event.sender.send('receiveInfo', infoData)
event.sender.send('receiveCryptocurrency', infoData.chains[0])
@ -30,20 +45,20 @@ export default function (lnd, event, msg, data) {
break
case 'newaddress':
// Data looks like { address: '' }
newaddress(lnd, data.type)
walletController.newAddress(lnd, data.type)
.then(({ address }) => event.sender.send('receiveAddress', address))
.catch(error => console.log('newaddress error: ', error))
break
case 'peers':
// Data looks like { peers: [] }
peers(lnd)
.then(peersData => event.sender.send('receivePeers', peersData))
peersController.listPeers(lnd)
.then(peersData => event.sender.send('listPeers', peersData))
.catch(error => console.log('peers error: ', error))
break
case 'channels':
// Data looks like
// Data looks like
// [ { channels: [] }, { total_limbo_balance: 0, pending_open_channels: [], pending_closing_channels: [], pending_force_closing_channels: [] } ]
Promise.all([channels, pendingchannels].map(func => func(lnd)))
Promise.all([channelController.listChannels, channelController.pendingChannels].map(func => func(lnd)))
.then(channelsData =>
event.sender.send('receiveChannels', { channels: channelsData[0].channels, pendingChannels: channelsData[1] })
)
@ -51,51 +66,51 @@ export default function (lnd, event, msg, data) {
break
case 'payments':
// Data looks like { payments: [] }
payments(lnd)
paymentsController.listPayments(lnd)
.then(paymentsData => event.sender.send('receivePayments', paymentsData))
.catch(error => console.log('payments error: ', error))
break
case 'invoices':
// Data looks like { invoices: [] }
invoices(lnd)
invoicesController.listInvoices(lnd)
.then(invoicesData => event.sender.send('receiveInvoices', invoicesData))
.catch(error => console.log('invoices error: ', error))
break
case 'invoice':
// Data looks like { invoices: [] }
invoice(data.payreq)
invoicesController.getInvoice(data.payreq)
.then(invoiceData => event.sender.send('receiveInvoice', invoiceData))
.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)))
Promise.all([walletController.walletBalance, channelController.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: '' }
// { memo, value } = data
createinvoice(lnd, data)
invoicesController.addInvoice(lnd, data)
.then(newinvoice =>
event.sender.send(
'createdInvoice',
Object.assign(newinvoice, { memo: data.memo, value: data.value, r_hash: new Buffer(newinvoice.r_hash, 'hex').toString('hex') })
)
)
.catch(error => console.log('createInvoice error: ', error))
.catch(error => console.log('addInvoice error: ', error))
break
case 'sendPayment':
// Payment looks like { payment_preimage: Buffer, payment_route: Object }
// { paymentRequest } = data
payinvoice(lnd, data)
paymentsController.sendPaymentSync(lnd, data)
.then(({ payment_route }) => event.sender.send('paymentSuccessful', Object.assign(data, { payment_route })))
.catch(error => console.log('payinvoice error: ', error))
break
case 'sendCoins':
// Transaction looks like { txid: String }
// { addr, amount } = data
sendcoins(lnd, data)
walletController.sendCoins(lnd, data)
.then((transaction) => {
console.log('transaction: ', transaction)
event.sender.send('sendSuccessful', { transaction })
@ -105,7 +120,7 @@ export default function (lnd, event, msg, data) {
case 'openChannel':
// Response is empty. Streaming updates on channel status and updates
// { pubkey, localamt, pushamt } = data
openchannel(lnd, event, data)
channelController.openChannel(lnd, event, data)
.then((channel) => {
console.log('CHANNEL: ', channel)
event.sender.send('channelSuccessful', { channel })
@ -115,7 +130,7 @@ export default function (lnd, event, msg, data) {
case 'closeChannel':
// Response is empty. Streaming updates on channel status and updates
// { channel_point, force } = data
closechannel(lnd, event, data)
channelController.closeChannel(lnd, event, data)
.then((result) => {
console.log('CLOSE CHANNEL: ', result)
event.sender.send('closeChannelSuccessful')
@ -125,7 +140,7 @@ export default function (lnd, event, msg, data) {
case 'connectPeer':
// Returns a peer_id. Pass the pubkey, host and peer_id so we can add a new peer to the list
// { pubkey, host } = data
connectpeer(lnd, data)
peersController.connectPeer(lnd, data)
.then(({ peer_id }) => {
console.log('peer_id: ', peer_id)
event.sender.send('connectSuccess', { pub_key: data.pubkey, address: data.host, peer_id })
@ -135,7 +150,7 @@ export default function (lnd, event, msg, data) {
case 'disconnectPeer':
// Empty response. Pass back pubkey on success to remove it from the peers list
// { pubkey } = data
disconnectpeer(lnd, data)
peersController.disconnectPeer(lnd, data)
.then(() => {
console.log('pubkey: ', data.pubkey)
event.sender.send('disconnectSuccess', { pubkey: data.pubkey })

10
app/lnd/methods/info.js

@ -1,10 +0,0 @@
// LND Get Info
export default function info(lnd) {
return new Promise((resolve, reject) => {
lnd.getInfo({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

12
app/lnd/methods/invoice.js

@ -1,12 +0,0 @@
import { decodeInvoice } from '../utils'
// LND Get Invoice
export default function invoice(payreq) {
return new Promise((resolve, reject) => {
try {
resolve(decodeInvoice(payreq))
} catch (error) {
reject(error)
}
})
}

10
app/lnd/methods/invoices.js

@ -1,10 +0,0 @@
// LND Get Invoices
export default function invoices(lnd) {
return new Promise((resolve, reject) => {
lnd.listInvoices({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

52
app/lnd/methods/invoicesController.js

@ -0,0 +1,52 @@
import { decodeInvoice } from '../utils'
/**
* [createInvoice description]
* @param lnd [description]
* @param memo [description]
* @param value [description]
* @return [description]
*/
export function addInvoice (lnd, { memo, value }) {
return new Promise((resolve, reject) => {
lnd.addInvoice({ memo, value }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [listInvoices description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function listInvoices(lnd) {
return new Promise((resolve, reject) => {
lnd.listInvoices({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* @param {[type]} payreq [description]
* @return {[type]} [description]
*/
export function getInvoice(payreq) {
return new Promise((resolve, reject) => {
try {
resolve(decodeInvoice(payreq))
} catch (error) {
reject(error)
}
})
}
//TODO - SubscribeInvoice

86
app/lnd/methods/networkController.js

@ -0,0 +1,86 @@
/**
* [info description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function getInfo(lnd) {
return new Promise((resolve, reject) => {
lnd.getInfo({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [getNodeInfo description]
* @param {[type]} lnd [description]
* @param {[type]} pubkey [description]
* @return {[type]} [description]
*/
export function getNodeInfo(lnd, { pubkey }) {
return new Promise ((resolve, reject) => {
lnd.getNodeInfo({ pub_key: pubkey}, (err, data) => {
if(err) { reject (err) }
resolve(data)
})
})
}
/**
* [describeGraph description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function describeGraph(lnd) {
return new Promise ((resolve, reject) => {
lnd.describeGraph({}, (err, data) => {
if(err) { reject (err) }
resolve(data)
})
})
}
/**
* [queryRoutes description]
* @param {[type]} lnd [description]
* @param {[type]} pubkey [description]
* @param {[type]} amount [description]
* @return {[type]} [description]
*/
export function queryRoutes(lnd, { pubkey, amount }) {
return new Promise ((resolve, reject) => {
lnd.queryRoutes({pub_key: pubkey, amt: amount }, (err, data) => {
if(err) {reject (err) }
resolve(data)
})
})
}
/**
* [getNetworkInfo description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function getNetworkInfo(lnd) {
return new Promise((resolve, reject) => {
lnd.getNetworkInfo({}, (err, data) => {
if(err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/newaddress.js

@ -1,10 +0,0 @@
// LND Generate New Address
export default function info(lnd, type) {
return new Promise((resolve, reject) => {
lnd.newAddress({ type }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

19
app/lnd/methods/openchannel.js

@ -1,19 +0,0 @@
import bitcore from 'bitcore-lib'
import pushopenchannel from '../push/openchannel'
const BufferUtil = bitcore.util.buffer
export default function openchannel(lnd, event, payload) {
const { pubkey, localamt, pushamt } = payload
const res = {
node_pubkey: BufferUtil.hexToBuffer(pubkey),
local_funding_amount: Number(localamt),
push_sat: Number(pushamt)
}
return new Promise((resolve, reject) =>
pushopenchannel(lnd, event, res)
.then(data => resolve(data))
.catch(error => reject(error))
)
}

10
app/lnd/methods/payinvoice.js

@ -1,10 +0,0 @@
// LND Pay an invoice
export default 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

@ -1,10 +0,0 @@
// LND Get Payments
export default function payments(lnd) {
return new Promise((resolve, reject) => {
lnd.listPayments({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

33
app/lnd/methods/paymentsController.js

@ -0,0 +1,33 @@
/**
* [sendPaymentSync description]
* @param {[type]} lnd [description]
* @param {[type]} paymentRequest [description]
* @return {[type]} [description]
*/
export function sendPaymentSync(lnd, { paymentRequest }) {
return new Promise((resolve, reject) => {
lnd.sendPaymentSync({ payment_request: paymentRequest }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [listPayments description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function listPayments(lnd) {
return new Promise((resolve, reject) => {
lnd.listPayments({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/peers.js

@ -1,10 +0,0 @@
// LND List Peers
export default function peers(lnd) {
return new Promise((resolve, reject) => {
lnd.listPeers({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

52
app/lnd/methods/peersController.js

@ -0,0 +1,52 @@
/**
* [connectpeer description]
* @param {[type]} lnd [description]
* @param {[type]} pubkey [description]
* @param {[type]} host [description]
* @return {[type]} [description]
*/
export function connectPeer(lnd, { pubkey, host }) {
return new Promise((resolve, reject) => {
lnd.connectPeer({ addr: { pubkey, host }, perm: true }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [disconnectpeer description]
* @param {[type]} lnd [description]
* @param {[type]} pubkey [description]
* @return {[type]} [description]
*/
export function disconnectPeer(lnd, { pubkey }) {
return new Promise((resolve, reject) => {
lnd.disconnectPeer({ pub_key: pubkey }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [peers description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function listPeers(lnd) {
return new Promise((resolve, reject) => {
lnd.listPeers({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/pendingchannels.js

@ -1,10 +0,0 @@
// 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/sendcoins.js

@ -1,10 +0,0 @@
// LND send coins on chain
export default function sendcoins(lnd, { addr, amount }) {
return new Promise((resolve, reject) => {
lnd.sendCoins({ addr, amount }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

85
app/lnd/methods/walletController.js

@ -0,0 +1,85 @@
/**
* [walletbalance description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function walletBalance(lnd) {
return new Promise((resolve, reject) => {
lnd.walletBalance({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [newAddress description]
* @param {[type]} lnd [description]
* @param {[type]} type [description]
* @return {[type]} [description]
*/
export function newAddress(lnd, type) {
return new Promise((resolve, reject) => {
lnd.newAddress({type}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [newWitnessAddress description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function newWitnessAddress(lnd, { addr }) {
return new Promise((resolve, reject) => {
lnd.newWitnessAddress({ address: addr}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [getTransactions description]
* @param {[type]} lnd [description]
* @return {[type]} [description]
*/
export function getTransactions(lnd) {
return new Promise((resolve, reject) => {
lnd.getTransactions({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
/**
* [sendcoins description]
* @param {[type]} lnd [description]
* @param {[type]} addr [description]
* @param {[type]} amount [description]
* @return {[type]} [description]
*/
export function sendCoins(lnd, { addr, amount }) {
return new Promise((resolve, reject) => {
lnd.sendCoins({ addr, amount }, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}

10
app/lnd/methods/walletbalance.js

@ -1,10 +0,0 @@
// LND Get Wallet Balance
export default function walletbalance(lnd) {
return new Promise((resolve, reject) => {
lnd.walletBalance({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
Loading…
Cancel
Save