jackmallers
7 years ago
committed by
GitHub
26 changed files with 538 additions and 226 deletions
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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)) |
|||
) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
} |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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)) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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)) |
|||
) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
}) |
|||
}) |
|||
} |
@ -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) |
|||
} |
|||
}) |
|||
} |
Loading…
Reference in new issue