Files
pallectrum/electrum/plugins/cosigner_pool/qt.py

235 lines
8.5 KiB
Python
Raw Normal View History

2014-08-21 19:35:51 +02:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2014 Thomas Voegtlin
#
2016-02-23 11:36:42 +01:00
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
2014-08-21 19:35:51 +02:00
#
2016-02-23 11:36:42 +01:00
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
2014-08-21 19:35:51 +02:00
#
2016-02-23 11:36:42 +01:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2014-08-21 19:35:51 +02:00
import time
2017-02-05 13:38:44 +03:00
from xmlrpc.client import ServerProxy
2014-08-21 19:35:51 +02:00
from PyQt5.QtCore import QObject, pyqtSignal
2017-09-23 05:54:38 +02:00
from PyQt5.QtWidgets import QPushButton
2014-08-21 19:35:51 +02:00
from electrum import util, keystore, ecc, crypto
2014-08-21 19:35:51 +02:00
from electrum import transaction
from electrum.bip32 import BIP32Node
from electrum.plugin import BasePlugin, hook
2014-08-21 19:35:51 +02:00
from electrum.i18n import _
2016-12-20 13:23:55 +01:00
from electrum.wallet import Multisig_Wallet
2017-10-19 03:19:48 +02:00
from electrum.util import bh2u, bfh
2014-08-21 19:35:51 +02:00
from electrum.gui.qt.transaction_dialog import show_transaction
2018-08-03 20:53:56 +02:00
from electrum.gui.qt.util import WaitingDialog
2014-08-21 19:35:51 +02:00
import sys
import traceback
2018-03-08 21:55:24 +01:00
server = ServerProxy('https://cosigner.electrum.org/', allow_none=True)
2014-08-21 19:35:51 +02:00
class Listener(util.DaemonThread):
2014-08-21 19:35:51 +02:00
def __init__(self, parent):
util.DaemonThread.__init__(self)
2014-08-21 19:35:51 +02:00
self.daemon = True
self.parent = parent
self.received = set()
self.keyhashes = []
2014-08-25 11:52:47 +02:00
def set_keyhashes(self, keyhashes):
self.keyhashes = keyhashes
2014-08-25 11:52:47 +02:00
def clear(self, keyhash):
server.delete(keyhash)
self.received.remove(keyhash)
2014-08-21 19:35:51 +02:00
def run(self):
while self.running:
if not self.keyhashes:
2014-08-21 19:35:51 +02:00
time.sleep(2)
continue
for keyhash in self.keyhashes:
if keyhash in self.received:
continue
2014-08-21 19:35:51 +02:00
try:
message = server.get(keyhash)
2014-08-21 19:35:51 +02:00
except Exception as e:
2019-04-26 18:52:26 +02:00
self.logger.info("cannot contact cosigner pool")
2014-08-21 19:35:51 +02:00
time.sleep(30)
continue
if message:
self.received.add(keyhash)
2019-04-26 18:52:26 +02:00
self.logger.info(f"received message for {keyhash}")
2017-09-23 05:54:38 +02:00
self.parent.obj.cosigner_receive_signal.emit(
keyhash, message)
2014-08-25 11:52:47 +02:00
# poll every 30 seconds
2014-08-21 19:35:51 +02:00
time.sleep(30)
2017-09-23 05:54:38 +02:00
class QReceiveSignalObject(QObject):
cosigner_receive_signal = pyqtSignal(object, object)
class Plugin(BasePlugin):
2014-08-21 19:35:51 +02:00
def __init__(self, parent, config, name):
BasePlugin.__init__(self, parent, config, name)
self.listener = None
2017-09-23 05:54:38 +02:00
self.obj = QReceiveSignalObject()
self.obj.cosigner_receive_signal.connect(self.on_receive)
self.keys = []
self.cosigner_list = []
2014-08-21 19:35:51 +02:00
2016-02-25 09:39:01 +01:00
@hook
def init_qt(self, gui):
for window in gui.windows:
self.on_new_window(window)
@hook
def on_new_window(self, window):
self.update(window)
2014-08-21 19:35:51 +02:00
@hook
def on_close_window(self, window):
self.update(window)
def is_available(self):
return True
def update(self, window):
wallet = window.wallet
2016-12-20 13:23:55 +01:00
if type(wallet) != Multisig_Wallet:
return
if self.listener is None:
2019-04-26 18:52:26 +02:00
self.logger.info("starting listener")
self.listener = Listener(self)
self.listener.start()
elif self.listener:
2019-04-26 18:52:26 +02:00
self.logger.info("shutting down listener")
self.listener.stop()
self.listener = None
self.keys = []
2014-08-25 11:52:47 +02:00
self.cosigner_list = []
for key, keystore in wallet.keystores.items():
xpub = keystore.get_master_public_key()
pubkey = BIP32Node.from_xkey(xpub).eckey.get_public_key_bytes(compressed=True)
_hash = bh2u(crypto.sha256d(pubkey))
if not keystore.is_watching_only():
self.keys.append((key, _hash, window))
else:
self.cosigner_list.append((window, xpub, pubkey, _hash))
if self.listener:
self.listener.set_keyhashes([t[1] for t in self.keys])
2014-08-21 19:35:51 +02:00
2014-08-31 11:42:40 +02:00
@hook
2014-08-21 19:35:51 +02:00
def transaction_dialog(self, d):
d.cosigner_send_button = b = QPushButton(_("Send to cosigner"))
2014-08-21 19:35:51 +02:00
b.clicked.connect(lambda: self.do_send(d.tx))
d.buttons.insert(0, b)
2014-08-21 19:35:51 +02:00
self.transaction_dialog_update(d)
2014-08-31 11:42:40 +02:00
@hook
2014-08-21 19:35:51 +02:00
def transaction_dialog_update(self, d):
if d.tx.is_complete() or d.wallet.can_sign(d.tx):
d.cosigner_send_button.hide()
2014-08-21 19:35:51 +02:00
return
for window, xpub, K, _hash in self.cosigner_list:
if window.wallet == d.wallet and self.cosigner_can_sign(d.tx, xpub):
d.cosigner_send_button.show()
2014-08-25 11:52:47 +02:00
break
2014-08-21 19:35:51 +02:00
else:
d.cosigner_send_button.hide()
2014-08-21 19:35:51 +02:00
2014-08-25 11:52:47 +02:00
def cosigner_can_sign(self, tx, cosigner_xpub):
from electrum.keystore import is_xpubkey, parse_xpubkey
2014-08-21 19:35:51 +02:00
xpub_set = set([])
2016-02-25 09:39:01 +01:00
for txin in tx.inputs():
2014-08-21 19:35:51 +02:00
for x_pubkey in txin['x_pubkeys']:
if is_xpubkey(x_pubkey):
xpub, s = parse_xpubkey(x_pubkey)
2014-08-21 19:35:51 +02:00
xpub_set.add(xpub)
2014-08-25 11:52:47 +02:00
return cosigner_xpub in xpub_set
2014-08-21 19:35:51 +02:00
def do_send(self, tx):
2018-08-03 20:53:56 +02:00
def on_success(result):
window.show_message(_("Your transaction was sent to the cosigning pool.") + '\n' +
_("Open your cosigner wallet to retrieve it."))
def on_failure(exc_info):
e = exc_info[1]
2019-04-26 18:52:26 +02:00
try: self.logger.error("on_failure", exc_info=exc_info)
2018-08-03 20:53:56 +02:00
except OSError: pass
window.show_error(_("Failed to send transaction to cosigning pool") + ':\n' + repr(e))
2018-08-03 20:53:56 +02:00
for window, xpub, K, _hash in self.cosigner_list:
2014-08-25 11:52:47 +02:00
if not self.cosigner_can_sign(tx, xpub):
continue
2018-08-03 20:53:56 +02:00
# construct message
2018-03-12 21:23:37 +01:00
raw_tx_bytes = bfh(str(tx))
public_key = ecc.ECPubkey(K)
message = public_key.encrypt_message(raw_tx_bytes).decode('ascii')
2018-08-03 20:53:56 +02:00
# send message
task = lambda: server.put(_hash, message)
msg = _('Sending transaction to cosigning pool...')
WaitingDialog(window, msg, task, on_success, on_failure)
def on_receive(self, keyhash, message):
self.logger.info(f"signal arrived for {keyhash}")
for key, _hash, window in self.keys:
if _hash == keyhash:
break
else:
2019-04-26 18:52:26 +02:00
self.logger.info("keyhash not found")
return
2014-08-21 19:35:51 +02:00
wallet = window.wallet
if isinstance(wallet.keystore, keystore.Hardware_KeyStore):
window.show_warning(_('An encrypted transaction was retrieved from cosigning pool.') + '\n' +
_('However, hardware wallets do not support message decryption, '
'which makes them not compatible with the current design of cosigner pool.'))
return
elif wallet.has_keystore_encryption():
password = window.password_dialog(_('An encrypted transaction was retrieved from cosigning pool.') + '\n' +
_('Please enter your password to decrypt it.'))
2014-08-21 19:35:51 +02:00
if not password:
return
else:
password = None
if not window.question(_("An encrypted transaction was retrieved from cosigning pool.") + '\n' +
_("Do you want to open it now?")):
2014-09-14 20:28:21 +02:00
return
2014-08-21 19:35:51 +02:00
xprv = wallet.keystore.get_master_private_key(password)
2014-08-25 11:52:47 +02:00
if not xprv:
2014-08-21 19:35:51 +02:00
return
try:
privkey = BIP32Node.from_xkey(xprv).eckey
message = bh2u(privkey.decrypt_message(message))
2014-08-21 19:35:51 +02:00
except Exception as e:
2019-04-26 18:52:26 +02:00
self.logger.exception('')
window.show_error(_('Error decrypting message') + ':\n' + repr(e))
2014-08-21 19:35:51 +02:00
return
self.listener.clear(keyhash)
tx = transaction.Transaction(message)
show_transaction(tx, window, prompt_if_unsaved=True)