-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new interfaces thing: lightningd, eclair, ptarmigan.
- Loading branch information
Showing
11 changed files
with
465 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/** @format */ | ||
|
||
export function summary() { | ||
return Promise.all([ | ||
rpcCall('getinfo').then( | ||
({nodeId, alias, blockHeight, publicAddresses}) => ({ | ||
blockheight: blockheight, | ||
id: nodeId, | ||
alias, | ||
address: publicAddresses.length === 0 ? null : publicAddresses[0] | ||
}) | ||
), | ||
rpcCall('listinvoices'), | ||
rpcCall('listpendinginvoices'), | ||
rpcCall('listpayments'), | ||
rpcCall('channels').then( | ||
channels => | ||
channels.reduce( | ||
(acc, ch) => acc + ch.data.commitments.localCommit.spec.toLocalMsat, | ||
0 | ||
) / 1000 | ||
) | ||
]).then(([info, pendingInvoices, allInvoices, balance]) => { | ||
let received = allInvoices | ||
.filter(inv => { | ||
for (let i = 0; i < pendingInvoices.length; i++) { | ||
let pinv = pendingInvoices[i] | ||
if (inv.paymentHash === pinv.paymentHash) { | ||
return false | ||
} | ||
} | ||
return true | ||
}) | ||
.slice(0, 15) | ||
|
||
return { | ||
info, | ||
balance, | ||
transactions: received.map(({timestamp, amount, description}) => ({ | ||
date: timestamp, | ||
amount, | ||
description | ||
})) | ||
} | ||
}) | ||
} | ||
|
||
export function pay(bolt11, msatoshi = undefined, description = undefined) { | ||
return rpcCall('payinvoice', {invoice: bolt11, amountMsat: msatoshi}).then( | ||
callId => { | ||
return new Promise(resolve => { | ||
eventCallbacks[callId] = ({amount, feesPaid, paymentPreimage}) => | ||
resolve({ | ||
msatoshi_paid: amount, | ||
msatoshi_fees: feesPaid, | ||
preimage: paymentPreimage | ||
}) | ||
}) | ||
} | ||
) | ||
} | ||
|
||
export function decode(bolt11) { | ||
return rpcCall('parseinvoice', {}).then( | ||
({description, amount, nodeId, paymentHash, expiry, timestamp}) => ({ | ||
description, | ||
msatoshi: amount, | ||
nodeid: nodeId, | ||
hash: paymentHash, | ||
creation: timestamp, | ||
expiry | ||
}) | ||
) | ||
} | ||
|
||
export function makeInvoice(msatoshi, description) { | ||
return rpcCall('createinvoice', { | ||
amountMsat: msatoshi, | ||
description | ||
}).then(({serialized, paymentHash}) => ({ | ||
bolt11: serialized, | ||
hash: paymentHash | ||
})) | ||
} | ||
|
||
var eventCallbacks = {} | ||
|
||
export function listenForEvents(defaultCallback) { | ||
return getRpcParams().then(({endpoint, password}) => { | ||
const ws = new WebSocket(endpoint.trim().replace('://', '://:' + password)) | ||
|
||
ws.onmessage = ev => { | ||
var event | ||
try { | ||
event = JSON.parse(ev.data) | ||
} catch (e) { | ||
console.log('failed to parse websocket event', ev) | ||
return | ||
} | ||
|
||
// specific callbacks registered for this event | ||
if (eventCallbacks[event.id]) { | ||
eventCallbacks[event.id](event) | ||
delete eventCallbacks[event.id] | ||
} | ||
|
||
// here we send normalized data, not the raw event | ||
switch (event.type) { | ||
case 'payment-received': | ||
defaultCallback({}) | ||
} | ||
} | ||
|
||
ws.onclose = ev => { | ||
console.log('websocket closed', ev) | ||
listenForEvents(defaultCallback) | ||
} | ||
|
||
ws.onerror = ev => { | ||
console.log('error on websocket', err) | ||
listenForEvents(defaultCallback) | ||
} | ||
}) | ||
} | ||
|
||
function rpcCall(method, params = {}) { | ||
return getRpcParams().then(({endpoint, password}) => { | ||
let formData = new FormData() | ||
|
||
for (let k in params) { | ||
if (typeof params[k] === 'undefined') continue | ||
|
||
formData.append(k, params[k]) | ||
} | ||
|
||
return fetch(endpoint.trim() + '/' + method, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Accept: 'application/json', | ||
Authorization: 'Basic ' + window.btoa(':' + password) | ||
}, | ||
body: formData | ||
}) | ||
.then(r => r.json()) | ||
.then(res => { | ||
if (res.error) { | ||
throw new Error(res.error) | ||
} | ||
|
||
return res | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @format */ | ||
|
||
import * as lightningd_spark from './lightningd_spark' | ||
import * as eclair from './eclair' | ||
import * as ptarmigan from './ptarmigan' | ||
import {getRpcParams} from '../utils' | ||
|
||
const kinds = { | ||
lightningd_spark, | ||
eclair, | ||
ptarmigan | ||
} | ||
|
||
export default function handleRPC(rpcField = {}) { | ||
return getRpcParams().then(({kind}) => { | ||
for (let method in rpcField) { | ||
let args = rpcField[method] | ||
return kinds[kind][method].apply(null, args) | ||
} | ||
}) | ||
} |
Oops, something went wrong.