Jack Mallers
7 years ago
18 changed files with 209 additions and 187 deletions
@ -1,124 +1,7 @@ |
|||||
import config from './config' |
import config from './config' |
||||
import lightning from './lib/lightning' |
import lightning from './lib/lightning' |
||||
import { decodeInvoice } from './utils' |
import methods from './methods' |
||||
|
|
||||
const lnd = lightning(config.lightningRpc, config.lightningHost) |
const lnd = lightning(config.lightningRpc, config.lightningHost) |
||||
|
|
||||
// LND Get Info
|
export default (event, msg, data) => methods(lnd, event, msg, data) |
||||
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 |
|
||||
} |
|
@ -0,0 +1,3 @@ |
|||||
|
export default function(channels, pendingchannels) { |
||||
|
return Promise.all([channels, pendingchannels]) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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 |
||||
|
} |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
import { decodeInvoice } from '../utils' |
||||
|
|
||||
|
// LND Get Invoice
|
||||
|
export default function invoice(payreq) { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
resolve(decodeInvoice(payreq)) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
@ -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) |
||||
|
}) |
||||
|
}) |
||||
|
} |
Loading…
Reference in new issue