diff --git a/app/lnd/config/index.js b/app/lnd/config/index.js index 3c0f7d2f..ddf42d90 100644 --- a/app/lnd/config/index.js +++ b/app/lnd/config/index.js @@ -2,8 +2,10 @@ // Mac OS X: /Users/user/Library/Application Support/Lnd/tls.cert // Linux: ~/.lnd/tls.cert // Windows: TODO find out where cert is located for windows machine +import { userInfo } from 'os' + export default { lightningRpc: `${__dirname}/rpc.proto`, lightningHost: 'localhost:10009', - cert: '/Users/jmow/Library/Application Support/Lnd/tls.cert' + cert: `/Users/${userInfo().username}/Library/Application\ Support/Lnd/tls.cert` } diff --git a/app/lnd/methods/channelController.js b/app/lnd/methods/channelController.js new file mode 100644 index 00000000..a508904b --- /dev/null +++ b/app/lnd/methods/channelController.js @@ -0,0 +1,116 @@ +import bitcore from 'bitcore-lib' +import pushopenchannel from '../push/openchannel' +import pushclosechannel from '../push/closechannel' + +const BufferUtil = bitcore.util.buffer + +/** + * Attempts to open a singly funded channel specified in the request to a remote peer. + * @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)) + ) +} + + +/** + * Returns the total funds available across all open channels in satoshis + * @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) + }) + }) +} + + +/** + * Returns a description of all the open channels that this node is a participant in + * @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) + }) + }) +} + + +/** + * Attempts to close an active channel identified by its channel outpoint (ChannelPoint) + * @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)) + ) +} + + +/** + * Returns a list of all the channels that are currently considered “pending" + * @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) + }) + }) +} + + +/** + * Returns the latest authenticated network announcement for the given channel + * @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) + }) + }) +} diff --git a/app/lnd/methods/channelbalance.js b/app/lnd/methods/channelbalance.js deleted file mode 100644 index 135a6b6e..00000000 --- a/app/lnd/methods/channelbalance.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/channels.js b/app/lnd/methods/channels.js deleted file mode 100644 index 512fec27..00000000 --- a/app/lnd/methods/channels.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/closechannel.js b/app/lnd/methods/closechannel.js deleted file mode 100644 index feb9abf7..00000000 --- a/app/lnd/methods/closechannel.js +++ /dev/null @@ -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)) - ) -} diff --git a/app/lnd/methods/connectpeer.js b/app/lnd/methods/connectpeer.js deleted file mode 100644 index 33608fc2..00000000 --- a/app/lnd/methods/connectpeer.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/createinvoice.js b/app/lnd/methods/createinvoice.js deleted file mode 100644 index c7fbba35..00000000 --- a/app/lnd/methods/createinvoice.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/disconnectpeer.js b/app/lnd/methods/disconnectpeer.js deleted file mode 100644 index e6a4bc88..00000000 --- a/app/lnd/methods/disconnectpeer.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/index.js b/app/lnd/methods/index.js index 1cc69a83..ab3129a8 100644 --- a/app/lnd/methods/index.js +++ b/app/lnd/methods/index.js @@ -1,27 +1,28 @@ /* 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 * 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 +//TODO - DecodePayReq +//TODO - SendPayment +//TODO - DeleteAllPayments + + 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 +31,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) + peersController.listPeers(lnd) .then(peersData => event.sender.send('receivePeers', 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 +52,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 +106,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 +116,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 +126,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 +136,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 }) diff --git a/app/lnd/methods/info.js b/app/lnd/methods/info.js deleted file mode 100644 index 8b704980..00000000 --- a/app/lnd/methods/info.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/invoice.js b/app/lnd/methods/invoice.js deleted file mode 100644 index 2c1f4efa..00000000 --- a/app/lnd/methods/invoice.js +++ /dev/null @@ -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) - } - }) -} diff --git a/app/lnd/methods/invoices.js b/app/lnd/methods/invoices.js deleted file mode 100644 index 9cad0746..00000000 --- a/app/lnd/methods/invoices.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/invoicesController.js b/app/lnd/methods/invoicesController.js new file mode 100644 index 00000000..0a2d1869 --- /dev/null +++ b/app/lnd/methods/invoicesController.js @@ -0,0 +1,80 @@ +import { decodeInvoice } from '../utils' +import pushinvoices from '../push/subscribeinvoice' + +/** + * Attempts to add a new invoice to the invoice database. + * @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) + }) + }) +} + +/** + * Returns a list of all the invoices currently stored within the database + * @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) + } + }) +} + + +/** + * Attemps to look up an invoice according to its payment hash + * @param {[type]} lnd [description] + * @param {[type]} rhash [description] + * @return {[type]} [description] + */ +export function lookupInvoice(lnd, { rhash }) { + return new Promise((resolve, reject) => { + lnd.lookupInvoice({ r_hash: rhash }, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} + + +/** + * Returns a uni-directional stream (server -> client) for notifying the client of newly added/settled invoices + * @param {[type]} lnd [description] + * @param {[type]} event [description] + * @return {[type]} [description] + */ +export function subscribeInvoices(lnd, event) { + return new Promise((resolve, reject) => { + pushinvoices(lnd, event) + .then(data => resolve(data)) + .catch(error => reject(error)) + }) +} diff --git a/app/lnd/methods/networkController.js b/app/lnd/methods/networkController.js new file mode 100644 index 00000000..8cdd4cbe --- /dev/null +++ b/app/lnd/methods/networkController.js @@ -0,0 +1,81 @@ +/** + * [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) + }) + }) +} + + +/** + * Returns general information concerning the lightning node + * @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) + }) + }) +} + + +/** + * Returns a description of the latest graph state from the point of view of the node + * @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) + }) + }) +} + + +/** + * Attempts to query the daemon’s Channel Router for a possible route to a target destination capable of carrying a specific amount of satoshis + * @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) + }) + }) +} + + +/** + * Returns some basic stats about the known channel graph from the point of view of the node + * @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) + }) + }) +} diff --git a/app/lnd/methods/newaddress.js b/app/lnd/methods/newaddress.js deleted file mode 100644 index d2a91d1f..00000000 --- a/app/lnd/methods/newaddress.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/openchannel.js b/app/lnd/methods/openchannel.js deleted file mode 100644 index 11058cfd..00000000 --- a/app/lnd/methods/openchannel.js +++ /dev/null @@ -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)) - ) -} diff --git a/app/lnd/methods/payinvoice.js b/app/lnd/methods/payinvoice.js deleted file mode 100644 index 43bfa3b8..00000000 --- a/app/lnd/methods/payinvoice.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/payments.js b/app/lnd/methods/payments.js deleted file mode 100644 index a276c3bd..00000000 --- a/app/lnd/methods/payments.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/paymentsController.js b/app/lnd/methods/paymentsController.js new file mode 100644 index 00000000..b59b64a7 --- /dev/null +++ b/app/lnd/methods/paymentsController.js @@ -0,0 +1,78 @@ +/** + * Dispatches a bi-directional streaming RPC for sending payments through the Lightning Network. + * @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) + }) + }) +} + + +/** + * Synchronous non-streaming version of SendPayment + * @param {[type]} lnd [description] + * @param {[type]} paymentRequest [description] + * @return {[type]} [description] + */ +export function sendPayment(lnd, { paymentRequest }) { + return new Promise((resolve, reject) => { + lnd.sendPayment({ payment_request: paymentRequest }, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} + +/** + * Returns a full description of the conditions encoded within the payment request + * @param {[type]} lnd [description] + * @param {[type]} payReq [description] + * @return {[type]} [description] + */ +export function decodePayReq(lnd, { payReq }) { + return new Promise((resolve, reject) => { + lnd.decodePayReq({ pay_req: payReq }, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} + +/** + * Returns a list of all outgoing payments + * @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) + }) + }) +} + +/** + * Deletes all outgoing payments from DB. + * @param {[type]} lnd [description] + * @return {[type]} [description] + */ +export function deleteAllPayments(lnd) { + return new Promise((resolve, reject) => { + lnd.deleteAllPayments({}, (err, data) => { + if (err) { reject(err) } + + resolve(data) + }) + }) +} diff --git a/app/lnd/methods/peers.js b/app/lnd/methods/peers.js deleted file mode 100644 index 1b604c1c..00000000 --- a/app/lnd/methods/peers.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/peersController.js b/app/lnd/methods/peersController.js new file mode 100644 index 00000000..5e557eef --- /dev/null +++ b/app/lnd/methods/peersController.js @@ -0,0 +1,49 @@ +/** + * Attempts to establish a connection to a remote peer + * @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) + }) + }) +} + + +/** + * Attempts to disconnect one peer from another + * @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) + }) + }) +} + + +/** + * Returns a verbose listing of all currently active peers + * @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) + }) + }) +} diff --git a/app/lnd/methods/pendingchannels.js b/app/lnd/methods/pendingchannels.js deleted file mode 100644 index 4408e750..00000000 --- a/app/lnd/methods/pendingchannels.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/sendcoins.js b/app/lnd/methods/sendcoins.js deleted file mode 100644 index eeabc8bb..00000000 --- a/app/lnd/methods/sendcoins.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/methods/walletController.js b/app/lnd/methods/walletController.js new file mode 100644 index 00000000..3053bbf1 --- /dev/null +++ b/app/lnd/methods/walletController.js @@ -0,0 +1,80 @@ +/** + * Returns the sum of all confirmed unspent outputs under control by the wallet + * @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) + }) + }) +} + + +/** + * Creates a new address under control of the local wallet + * @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) + }) + }) +} + +/** + * Creates a new witness address under control of the local wallet + * @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) + }) + }) +} + + +/** + * Returns a list describing all the known transactions relevant to the wallet + * @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) + }) + }) +} + + +/** + * Executes a request to send coins to a particular address + * @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) + }) + }) +} diff --git a/app/lnd/methods/walletbalance.js b/app/lnd/methods/walletbalance.js deleted file mode 100644 index e6db48a6..00000000 --- a/app/lnd/methods/walletbalance.js +++ /dev/null @@ -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) - }) - }) -} diff --git a/app/lnd/push/subscribeinvoice.js b/app/lnd/push/subscribeinvoice.js new file mode 100644 index 00000000..610e739d --- /dev/null +++ b/app/lnd/push/subscribeinvoice.js @@ -0,0 +1,16 @@ +export default function pushinvoices(lnd, event) { + return new Promise((resolve, reject) => { + try { + const call = lnd.subscribeInvoices({}) + + call.on('data', data => event.sender.send('pushinvoicesupdated', { data })) + call.on('end', () => event.sender.send('pushinvoicesend')) + call.on('error', error => event.sender.send('pushinvoiceserror', { error })) + call.on('status', status => event.sender.send('pushinvoicesstatus', { status })) + + resolve(null) + } catch (error) { + reject(error, null) + } + }) +}