lightning: march 2018 rebase, without integration
This commit is contained in:
49
gui/kivy/uix/dialogs/lightning_channels.py
Normal file
49
gui/kivy/uix/dialogs/lightning_channels.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from kivy.lang import Builder
|
||||
from kivy.factory import Factory
|
||||
|
||||
Builder.load_string('''
|
||||
<LightningChannelItem@CardItem>
|
||||
channelId: '<channelId not set>'
|
||||
Label:
|
||||
text: root.channelId
|
||||
|
||||
<LightningChannelsDialog@Popup>:
|
||||
name: 'lightning_channels'
|
||||
BoxLayout:
|
||||
orientation: 'vertical'
|
||||
spacing: '1dp'
|
||||
ScrollView:
|
||||
GridLayout:
|
||||
cols: 1
|
||||
id: lightning_channels_container
|
||||
size_hint: 1, None
|
||||
height: self.minimum_height
|
||||
spacing: '2dp'
|
||||
padding: '12dp'
|
||||
''')
|
||||
|
||||
class LightningChannelsDialog(Factory.Popup):
|
||||
def __init__(self, app):
|
||||
super(LightningChannelsDialog, self).__init__()
|
||||
self.clocks = []
|
||||
self.app = app
|
||||
def open(self, *args, **kwargs):
|
||||
super(LightningChannelsDialog, self).open(*args, **kwargs)
|
||||
for i in self.clocks: i.cancel()
|
||||
self.clocks.append(Clock.schedule_interval(self.fetch_channels, 10))
|
||||
self.app.wallet.lightning.subscribe(self.rpc_result_handler)
|
||||
def dismiss(self, *args, **kwargs):
|
||||
super(LightningChannelsDialog, self).dismiss(*args, **kwargs)
|
||||
self.app.wallet.lightning.clearSubscribers()
|
||||
def fetch_channels(self, dw):
|
||||
lightning.lightningCall(self.app.wallet.lightning, "listchannels")()
|
||||
def rpc_result_handler(self, res):
|
||||
if isinstance(res, Exception):
|
||||
raise res
|
||||
channel_cards = self.ids.lightning_channels_container
|
||||
channels_cards.clear_widgets()
|
||||
for i in res["channels"]:
|
||||
item = Factory.LightningChannelItem()
|
||||
item.screen = self
|
||||
item.channelId = i.channelId
|
||||
channel_cards.add_widget(item)
|
||||
68
gui/kivy/uix/dialogs/lightning_payer.py
Normal file
68
gui/kivy/uix/dialogs/lightning_payer.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from kivy.lang import Builder
|
||||
from kivy.factory import Factory
|
||||
from electrum_gui.kivy.i18n import _
|
||||
|
||||
Builder.load_string('''
|
||||
<LightningPayerDialog@Popup>
|
||||
id: s
|
||||
name: 'lightning_payer'
|
||||
invoice_data: ''
|
||||
BoxLayout:
|
||||
orientation: "vertical"
|
||||
BlueButton:
|
||||
text: s.invoice_data if s.invoice_data else _('Lightning invoice')
|
||||
shorten: True
|
||||
on_release: Clock.schedule_once(lambda dt: app.show_info(_('Copy and paste the lightning invoice using the Paste button, or use the camera to scan a QR code.')))
|
||||
GridLayout:
|
||||
cols: 4
|
||||
size_hint: 1, None
|
||||
height: '48dp'
|
||||
IconButton:
|
||||
id: qr
|
||||
on_release: Clock.schedule_once(lambda dt: app.scan_qr(on_complete=s.on_lightning_qr))
|
||||
icon: 'atlas://gui/kivy/theming/light/camera'
|
||||
Button:
|
||||
text: _('Paste')
|
||||
on_release: s.do_paste()
|
||||
Button:
|
||||
text: _('Paste sample')
|
||||
on_release: s.do_paste_sample()
|
||||
Button:
|
||||
text: _('Clear')
|
||||
on_release: s.do_clear()
|
||||
Button:
|
||||
size_hint: 1, None
|
||||
height: '48dp'
|
||||
text: _('Pay pasted/scanned invoice')
|
||||
on_release: s.do_pay()
|
||||
''')
|
||||
|
||||
class LightningPayerDialog(Factory.Popup):
|
||||
def __init__(self, app):
|
||||
super(LightningPayerDialog, self).__init__()
|
||||
self.app = app
|
||||
def open(self, *args, **kwargs):
|
||||
super(LightningPayerDialog, self).open(*args, **kwargs)
|
||||
class FakeQtSignal:
|
||||
def emit(self2, data):
|
||||
self.app.show_info(data)
|
||||
class MyConsole:
|
||||
newResult = FakeQtSignal()
|
||||
self.app.wallet.lightning.setConsole(MyConsole())
|
||||
def dismiss(self, *args, **kwargs):
|
||||
super(LightningPayerDialog, self).dismiss(*args, **kwargs)
|
||||
self.app.wallet.lightning.setConsole(None)
|
||||
def do_paste_sample(self):
|
||||
self.invoice_data = "lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w"
|
||||
def do_paste(self):
|
||||
contents = self.app._clipboard.paste()
|
||||
if not contents:
|
||||
self.app.show_info(_("Clipboard is empty"))
|
||||
return
|
||||
self.invoice_data = contents
|
||||
def do_clear(self):
|
||||
self.invoice_data = ""
|
||||
def do_pay(self):
|
||||
lightning.lightningCall(self.app.wallet.lightning, "sendpayment")("--pay_req=" + self.invoice_data)
|
||||
def on_lightning_qr(self):
|
||||
self.app.show_info("Lightning Invoice QR scanning not implemented") #TODO
|
||||
147
gui/qt/lightning_invoice_list.py
Normal file
147
gui/qt/lightning_invoice_list.py
Normal file
@@ -0,0 +1,147 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import base64
|
||||
import binascii
|
||||
from PyQt5 import QtCore, QtWidgets
|
||||
from collections import OrderedDict
|
||||
import logging
|
||||
from electrum.lightning import lightningCall
|
||||
|
||||
mapping = {0: "r_hash", 1: "pay_req", 2: "settled"}
|
||||
revMapp = {"r_hash": 0, "pay_req": 1, "settled": 2}
|
||||
datatable = OrderedDict([])
|
||||
idx = 0
|
||||
|
||||
class MyTableRow(QtWidgets.QTreeWidgetItem):
|
||||
def __init__(self, di):
|
||||
if "settled" not in di:
|
||||
di["settled"] = False
|
||||
strs = [str(di[mapping[key]]) for key in range(len(mapping))]
|
||||
print(strs)
|
||||
super(MyTableRow, self).__init__(strs)
|
||||
assert isinstance(di, dict)
|
||||
self.di = di
|
||||
def __getitem__(self, idx):
|
||||
return self.di[idx]
|
||||
def __setitem__(self, idx, val):
|
||||
self.di[idx] = val
|
||||
try:
|
||||
self.setData(revMapp[idx], QtCore.Qt.DisplayRole, '{0}'.format(val))
|
||||
except KeyError:
|
||||
logging.warning("Lightning Invoice field %s unknown", idx)
|
||||
def __str__(self):
|
||||
return str(self.di)
|
||||
|
||||
def addInvoiceRow(new):
|
||||
made = MyTableRow(new)
|
||||
datatable[new["r_hash"]] = made
|
||||
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):
|
||||
def create_menu(self, position):
|
||||
menu = QtWidgets.QMenu()
|
||||
pay_req = self._tv.currentItem()["pay_req"]
|
||||
cb = QtWidgets.QApplication.instance().clipboard()
|
||||
def copy():
|
||||
print(pay_req)
|
||||
cb.setText(pay_req)
|
||||
menu.addAction("Copy payment request", copy)
|
||||
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
|
||||
try:
|
||||
obj = datatable[new["r_hash"]]
|
||||
except KeyError:
|
||||
print("lightning payment invoice r_hash {} unknown!".format(new["r_hash"]))
|
||||
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 methodName != "addinvoice":
|
||||
print("ignoring reply {} to {}".format(obj, methodName))
|
||||
return
|
||||
self._tv.insertTopLevelItem(0, addInvoiceRow(obj))
|
||||
|
||||
def __init__(self, parent, lightningWorker, lightningRpc):
|
||||
QtWidgets.QWidget.__init__(self, parent)
|
||||
|
||||
lightningWorker.subscribe(self.lightningWorkerHandler)
|
||||
lightningRpc.subscribe(self.lightningRpcHandler)
|
||||
|
||||
self._tv=QtWidgets.QTreeWidget(self)
|
||||
self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
|
||||
self._tv.setColumnCount(len(mapping))
|
||||
self._tv.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
|
||||
self._tv.customContextMenuRequested.connect(self.create_menu)
|
||||
|
||||
class SatoshiCountSpinBox(QtWidgets.QSpinBox):
|
||||
def keyPressEvent(self2, e):
|
||||
super(SatoshiCountSpinBox, self2).keyPressEvent(e)
|
||||
if QtCore.Qt.Key_Return == e.key():
|
||||
clickHandler(self2, self._tv, lightningRpc)
|
||||
|
||||
numInput = SatoshiCountSpinBox(self)
|
||||
|
||||
button = QtWidgets.QPushButton('Add invoice', self)
|
||||
button.clicked.connect(lambda: clickHandler(numInput, self._tv, lightningRpc))
|
||||
|
||||
l=QtWidgets.QVBoxLayout(self)
|
||||
h=QtWidgets.QGridLayout(self)
|
||||
h.addWidget(numInput, 0, 0)
|
||||
h.addWidget(button, 0, 1)
|
||||
#h.addItem(QtWidgets.QSpacerItem(100, 200, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred), 0, 2)
|
||||
#h.setSizePolicy(
|
||||
h.setColumnStretch(0, 1)
|
||||
h.setColumnStretch(1, 1)
|
||||
h.setColumnStretch(2, 2)
|
||||
l.addLayout(h)
|
||||
l.addWidget(self._tv)
|
||||
|
||||
self.resize(2500,1000)
|
||||
|
||||
def tick():
|
||||
key = "9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"
|
||||
if not key in datatable:
|
||||
return
|
||||
row = datatable[key]
|
||||
row["settled"] = not row["settled"]
|
||||
print("data changed")
|
||||
|
||||
if __name__=="__main__":
|
||||
from sys import argv, exit
|
||||
|
||||
a=QtWidgets.QApplication(argv)
|
||||
|
||||
w=LightningInvoiceList()
|
||||
w.show()
|
||||
w.raise_()
|
||||
|
||||
timer = QtCore.QTimer()
|
||||
timer.timeout.connect(tick)
|
||||
timer.start(1000)
|
||||
exit(a.exec_())
|
||||
Reference in New Issue
Block a user