lightning: remove hub based approach, port qt gui to lnbase
This commit is contained in:
@@ -3,11 +3,11 @@ import binascii, base64
|
||||
from PyQt5 import QtCore, QtWidgets
|
||||
from collections import OrderedDict
|
||||
import logging
|
||||
from electrum.lightning import lightningCall
|
||||
import traceback
|
||||
|
||||
mapping = {0: "channel_point"}
|
||||
revMapp = {"channel_point": 0}
|
||||
# https://api.lightning.community/#listchannels
|
||||
mapping = {0: "chan_id"}
|
||||
revMapp = {"chan_id": 0}
|
||||
datatable = OrderedDict([])
|
||||
|
||||
class MyTableRow(QtWidgets.QTreeWidgetItem):
|
||||
@@ -29,78 +29,64 @@ class MyTableRow(QtWidgets.QTreeWidgetItem):
|
||||
|
||||
def addChannelRow(new):
|
||||
made = MyTableRow(new)
|
||||
datatable[new["channel_point"]] = made
|
||||
datatable.move_to_end(new["channel_point"], last=False)
|
||||
datatable[new["chan_id"]] = made
|
||||
datatable.move_to_end(new["chan_id"], last=False)
|
||||
return made
|
||||
|
||||
def clickHandler(nodeIdInput, local_amt_inp, push_amt_inp, lightningRpc):
|
||||
nodeId = nodeIdInput.text()
|
||||
print("creating channel with connstr {}".format(nodeId))
|
||||
lightningCall(lightningRpc, "openchannel")(str(nodeId), local_amt_inp.text(), push_amt_inp.text())
|
||||
|
||||
class LightningChannelsList(QtWidgets.QWidget):
|
||||
update_rows = QtCore.pyqtSignal(str, dict)
|
||||
update_rows = QtCore.pyqtSignal(dict)
|
||||
update_single_row = QtCore.pyqtSignal(dict)
|
||||
|
||||
def create_menu(self, position):
|
||||
menu = QtWidgets.QMenu()
|
||||
cur = self._tv.currentItem()
|
||||
channel_point = cur["channel_point"]
|
||||
def close():
|
||||
params = [str(channel_point)] + (["--force"] if not cur["active"] else []) # TODO test if force is being used correctly
|
||||
lightningCall(self.lightningRpc, "closechannel")(*params)
|
||||
menu.addAction("Close channel", close)
|
||||
menu.exec_(self._tv.viewport().mapToGlobal(position))
|
||||
def lightningWorkerHandler(self, sourceClassName, obj):
|
||||
new = {}
|
||||
for k, v in obj.items():
|
||||
try:
|
||||
v = binascii.hexlify(base64.b64decode(v)).decode("ascii")
|
||||
except:
|
||||
pass
|
||||
new[k] = v
|
||||
def clickHandler(self, nodeIdInput, local_amt_inp, push_amt_inp, lnworker):
|
||||
nodeId = nodeIdInput.text()
|
||||
print("creating channel with connstr {}".format(nodeId))
|
||||
local_amt = int(local_amt_inp.text())
|
||||
try:
|
||||
obj = datatable[new["channel_point"]]
|
||||
push_amt = int(push_amt_inp.text())
|
||||
except ValueError:
|
||||
push_amt = 0
|
||||
assert local_amt >= 200000
|
||||
assert local_amt >= push_amt
|
||||
obj = lnworker.open_channel_from_other_thread(node_id=str(nodeId), local_amt=local_amt, push_amt=push_amt, emit_function=self.update_rows.emit, get_password=self.main_window.password_dialog)
|
||||
|
||||
@QtCore.pyqtSlot(dict)
|
||||
def do_update_single_row(self, new):
|
||||
try:
|
||||
obj = datatable[new["chan_id"]]
|
||||
except KeyError:
|
||||
print("lightning channel_point {} unknown!".format(new["channel_point"]))
|
||||
print("lightning chan_id {} unknown!".format(new["chan_id"]))
|
||||
else:
|
||||
for k, v in new.items():
|
||||
try:
|
||||
if obj[k] != v: obj[k] = v
|
||||
except KeyError:
|
||||
obj[k] = v
|
||||
def lightningRpcHandler(self, methodName, obj):
|
||||
if isinstance(obj, Exception):
|
||||
try:
|
||||
raise obj
|
||||
except:
|
||||
traceback.print_exc()
|
||||
else:
|
||||
self.update_rows.emit(methodName, obj)
|
||||
|
||||
def do_update_rows(self, methodName, obj):
|
||||
if methodName != "listchannels":
|
||||
print("channel list ignoring reply {} to {}".format(obj, methodName))
|
||||
return
|
||||
def create_menu(self, position):
|
||||
menu = QtWidgets.QMenu()
|
||||
cur = self._tv.currentItem()
|
||||
def close():
|
||||
print("closechannel result", lnworker.close_channel_from_other_thread(cur.di))
|
||||
menu.addAction("Close channel", close)
|
||||
menu.exec_(self._tv.viewport().mapToGlobal(position))
|
||||
|
||||
@QtCore.pyqtSlot(dict)
|
||||
def do_update_rows(self, obj):
|
||||
self._tv.clear()
|
||||
for i in obj["channels"]:
|
||||
self._tv.insertTopLevelItem(0, addChannelRow(i))
|
||||
|
||||
|
||||
def __init__(self, parent, lightningWorker, lightningRpc):
|
||||
def __init__(self, parent, lnworker):
|
||||
QtWidgets.QWidget.__init__(self, parent)
|
||||
self.main_window = parent
|
||||
|
||||
self.update_rows.connect(self.do_update_rows)
|
||||
self.update_single_row.connect(self.do_update_single_row)
|
||||
|
||||
def tick():
|
||||
lightningCall(lightningRpc, "listchannels")()
|
||||
self.lnworker = lnworker
|
||||
|
||||
timer = QtCore.QTimer(self)
|
||||
timer.timeout.connect(tick)
|
||||
timer.start(5000)
|
||||
|
||||
lightningWorker.subscribe(self.lightningWorkerHandler)
|
||||
lightningRpc.subscribe(self.lightningRpcHandler)
|
||||
self.lightningRpc = lightningRpc
|
||||
lnworker.subscribe_channel_list_updates_from_other_thread(self.update_rows.emit)
|
||||
lnworker.subscribe_single_channel_update_from_other_thread(self.update_single_row.emit)
|
||||
|
||||
self._tv=QtWidgets.QTreeWidget(self)
|
||||
self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
|
||||
@@ -113,7 +99,7 @@ class LightningChannelsList(QtWidgets.QWidget):
|
||||
push_amt_inp = QtWidgets.QLineEdit(self)
|
||||
|
||||
button = QtWidgets.QPushButton('Open channel', self)
|
||||
button.clicked.connect(lambda: clickHandler(nodeid_inp, local_amt_inp, push_amt_inp, lightningRpc))
|
||||
button.clicked.connect(lambda: self.clickHandler(nodeid_inp, local_amt_inp, push_amt_inp, lnworker))
|
||||
|
||||
l=QtWidgets.QVBoxLayout(self)
|
||||
h=QtWidgets.QGridLayout(self)
|
||||
@@ -139,71 +125,3 @@ class LightningChannelsList(QtWidgets.QWidget):
|
||||
l.addWidget(self._tv)
|
||||
|
||||
self.resize(2500,1000)
|
||||
|
||||
class MockLightningWorker:
|
||||
def subscribe(self, handler):
|
||||
pass
|
||||
|
||||
if __name__=="__main__":
|
||||
import queue, threading, asyncio
|
||||
from sys import argv, exit
|
||||
import signal , traceback, os
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
|
||||
async def loopstop():
|
||||
loop.stop()
|
||||
|
||||
def signal_handler(signal, frame):
|
||||
asyncio.run_coroutine_threadsafe(loopstop(), loop)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
a=QtWidgets.QApplication(argv)
|
||||
|
||||
gotReplyHandlerLock = threading.Lock()
|
||||
gotReplyHandlerLock.acquire()
|
||||
replyHandler = None
|
||||
|
||||
class MockLightningRPC:
|
||||
def __init__(self, q):
|
||||
self.queue = q
|
||||
def subscribe(self, handler):
|
||||
global replyHandler
|
||||
replyHandler = handler
|
||||
gotReplyHandlerLock.release()
|
||||
|
||||
q = queue.Queue()
|
||||
w=LightningChannelsList(None, MockLightningWorker(), MockLightningRPC(q))
|
||||
w.show()
|
||||
w.raise_()
|
||||
|
||||
async def the_job():
|
||||
try:
|
||||
acquired_once = False
|
||||
while loop.is_running():
|
||||
try:
|
||||
cmd = q.get_nowait()
|
||||
except queue.Empty:
|
||||
await asyncio.sleep(1)
|
||||
continue
|
||||
if not acquired_once:
|
||||
gotReplyHandlerLock.acquire()
|
||||
acquired_once = True
|
||||
if cmd[0] == "listchannels":
|
||||
#replyHandler("listchannels", Exception("Test exception"))
|
||||
replyHandler("listchannels", {"channels": [{"channel_point": binascii.hexlify(os.urandom(32)).decode("ascii"), "active": True}]})
|
||||
elif cmd[0] == "openchannel":
|
||||
replyHandler("openchannel", {})
|
||||
else:
|
||||
print("mock rpc server ignoring", cmd[0])
|
||||
except:
|
||||
traceback.print_exc()
|
||||
|
||||
def asyncioThread():
|
||||
loop.create_task(the_job())
|
||||
loop.run_forever()
|
||||
|
||||
threading.Thread(target=asyncioThread).start()
|
||||
|
||||
exit(a.exec_())
|
||||
|
||||
@@ -4,8 +4,8 @@ import binascii
|
||||
from PyQt5 import QtCore, QtWidgets
|
||||
from collections import OrderedDict
|
||||
import logging
|
||||
from electrum.lightning import lightningCall
|
||||
from .qrcodewidget import QRDialog
|
||||
from PyQt5.QtCore import pyqtSignal, pyqtSlot
|
||||
|
||||
mapping = {0: "r_hash", 1: "pay_req", 2: "settled"}
|
||||
revMapp = {"r_hash": 0, "pay_req": 1, "settled": 2}
|
||||
@@ -38,23 +38,29 @@ def addInvoiceRow(new):
|
||||
datatable.move_to_end(new["r_hash"], last=False)
|
||||
return made
|
||||
|
||||
def clickHandler(numInput, treeView, lightningRpc):
|
||||
amt = numInput.value()
|
||||
if amt < 1:
|
||||
print("value too small")
|
||||
return
|
||||
print("creating invoice with value {}".format(amt))
|
||||
global idx
|
||||
#obj = {
|
||||
# "r_hash": binascii.hexlify((int.from_bytes(bytearray.fromhex("9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"), "big")+idx).to_bytes(byteorder="big", length=32)).decode("ascii"),
|
||||
# "pay_req": "lntb81920n1pdf258s" + str(idx),
|
||||
# "settled": False
|
||||
#}
|
||||
#treeView.insertTopLevelItem(0, addInvoiceRow(obj))
|
||||
idx += 1
|
||||
lightningCall(lightningRpc, "addinvoice")("--amt=" + str(amt))
|
||||
|
||||
class LightningInvoiceList(QtWidgets.QWidget):
|
||||
invoice_added_signal = QtCore.pyqtSignal(dict)
|
||||
|
||||
@QtCore.pyqtSlot(dict)
|
||||
def invoice_added_handler(self, di):
|
||||
self._tv.insertTopLevelItem(0, addInvoiceRow(invoice))
|
||||
|
||||
def clickHandler(self, numInput, treeView, lnworker):
|
||||
amt = numInput.value()
|
||||
if amt < 1:
|
||||
print("value too small")
|
||||
return
|
||||
print("creating invoice with value {}".format(amt))
|
||||
global idx
|
||||
#obj = {
|
||||
# "r_hash": binascii.hexlify((int.from_bytes(bytearray.fromhex("9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"), "big")+idx).to_bytes(byteorder="big", length=32)).decode("ascii"),
|
||||
# "pay_req": "lntb81920n1pdf258s" + str(idx),
|
||||
# "settled": False
|
||||
#}
|
||||
#treeView.insertTopLevelItem(0, addInvoiceRow(obj))
|
||||
idx += 1
|
||||
lnworker.add_invoice_from_other_thread(amt)
|
||||
|
||||
def create_menu(self, position):
|
||||
menu = QtWidgets.QMenu()
|
||||
pay_req = self._tv.currentItem()["pay_req"]
|
||||
@@ -68,14 +74,11 @@ class LightningInvoiceList(QtWidgets.QWidget):
|
||||
menu.addAction("Copy payment request", copy)
|
||||
menu.addAction("Show payment request as QR code", qr)
|
||||
menu.exec_(self._tv.viewport().mapToGlobal(position))
|
||||
def lightningWorkerHandler(self, sourceClassName, obj):
|
||||
new = {}
|
||||
for k, v in obj.items():
|
||||
try:
|
||||
v = binascii.hexlify(base64.b64decode(v)).decode("ascii")
|
||||
except:
|
||||
pass
|
||||
new[k] = v
|
||||
|
||||
payment_received_signal = pyqtSignal(dict)
|
||||
|
||||
@pyqtSlot(dict)
|
||||
def paymentReceived(self, new):
|
||||
try:
|
||||
obj = datatable[new["r_hash"]]
|
||||
except KeyError:
|
||||
@@ -86,17 +89,15 @@ class LightningInvoiceList(QtWidgets.QWidget):
|
||||
if obj[k] != v: obj[k] = v
|
||||
except KeyError:
|
||||
obj[k] = v
|
||||
def lightningRpcHandler(self, methodName, obj):
|
||||
if methodName != "addinvoice":
|
||||
print("ignoring reply {} to {}".format(obj, methodName))
|
||||
return
|
||||
self._tv.insertTopLevelItem(0, addInvoiceRow(obj))
|
||||
|
||||
def __init__(self, parent, lightningWorker, lightningRpc):
|
||||
|
||||
def __init__(self, parent, lnworker):
|
||||
QtWidgets.QWidget.__init__(self, parent)
|
||||
|
||||
lightningWorker.subscribe(self.lightningWorkerHandler)
|
||||
lightningRpc.subscribe(self.lightningRpcHandler)
|
||||
self.payment_received_signal.connect(self.paymentReceived)
|
||||
self.invoice_added_signal.connect(self.invoice_added_handler)
|
||||
|
||||
lnworker.subscribe_payment_received_from_other_thread(self.payment_received_signal.emit)
|
||||
lnworker.subscribe_invoice_added_from_other_thread(self.invoice_added_signal.emit)
|
||||
|
||||
self._tv=QtWidgets.QTreeWidget(self)
|
||||
self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
|
||||
@@ -108,12 +109,12 @@ class LightningInvoiceList(QtWidgets.QWidget):
|
||||
def keyPressEvent(self2, e):
|
||||
super(SatoshiCountSpinBox, self2).keyPressEvent(e)
|
||||
if QtCore.Qt.Key_Return == e.key():
|
||||
clickHandler(self2, self._tv, lightningRpc)
|
||||
self.clickHandler(self2, self._tv, lnworker)
|
||||
|
||||
numInput = SatoshiCountSpinBox(self)
|
||||
|
||||
button = QtWidgets.QPushButton('Add invoice', self)
|
||||
button.clicked.connect(lambda: clickHandler(numInput, self._tv, lightningRpc))
|
||||
button.clicked.connect(lambda: self.clickHandler(numInput, self._tv, lnworker))
|
||||
|
||||
l=QtWidgets.QVBoxLayout(self)
|
||||
h=QtWidgets.QGridLayout(self)
|
||||
|
||||
Reference in New Issue
Block a user