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
|
2022-11-06 04:36:56 +00:00
|
|
|
from typing import TYPE_CHECKING, Union, List, Tuple, Dict
|
2020-06-29 02:15:01 +02:00
|
|
|
import ssl
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2019-02-11 20:21:24 +01:00
|
|
|
from PyQt5.QtCore import QObject, pyqtSignal
|
2017-09-23 05:54:38 +02:00
|
|
|
from PyQt5.QtWidgets import QPushButton
|
2020-06-29 02:15:01 +02:00
|
|
|
import certifi
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2019-02-21 22:17:06 +01:00
|
|
|
from electrum import util, keystore, ecc, crypto
|
2014-08-21 19:35:51 +02:00
|
|
|
from electrum import transaction
|
2021-03-27 01:44:18 +01:00
|
|
|
from electrum.transaction import Transaction, PartialTransaction, tx_from_any, SerializationError
|
2019-02-21 22:17:06 +01:00
|
|
|
from electrum.bip32 import BIP32Node
|
2018-07-11 17:38:47 +02:00
|
|
|
from electrum.plugin import BasePlugin, hook
|
2014-08-21 19:35:51 +02:00
|
|
|
from electrum.i18n import _
|
2020-12-08 10:33:43 +01:00
|
|
|
from electrum.wallet import Multisig_Wallet, Abstract_Wallet
|
2023-02-17 11:35:03 +00:00
|
|
|
from electrum.util import bfh
|
2022-11-06 04:36:56 +00:00
|
|
|
from electrum.logging import Logger
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2019-10-23 17:09:41 +02:00
|
|
|
from electrum.gui.qt.transaction_dialog import show_transaction, TxDialog
|
2018-08-03 20:53:56 +02:00
|
|
|
from electrum.gui.qt.util import WaitingDialog
|
2015-03-25 10:46:15 +01:00
|
|
|
|
2019-10-23 17:09:41 +02:00
|
|
|
if TYPE_CHECKING:
|
2020-05-25 17:31:13 +02:00
|
|
|
from electrum.gui.qt import ElectrumGui
|
2019-10-23 17:09:41 +02:00
|
|
|
from electrum.gui.qt.main_window import ElectrumWindow
|
2014-08-21 19:35:51 +02:00
|
|
|
|
|
|
|
|
|
2020-06-29 02:15:01 +02:00
|
|
|
ca_path = certifi.where()
|
|
|
|
|
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=ca_path)
|
|
|
|
|
server = ServerProxy('https://cosigner.electrum.org/', allow_none=True, context=ssl_context)
|
2014-08-21 19:35:51 +02:00
|
|
|
|
|
|
|
|
|
2015-09-04 12:36:25 +09:00
|
|
|
class Listener(util.DaemonThread):
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2022-11-06 04:36:56 +00:00
|
|
|
def __init__(self, cw: 'CosignerWallet'):
|
2015-09-04 12:36:25 +09:00
|
|
|
util.DaemonThread.__init__(self)
|
2014-08-21 19:35:51 +02:00
|
|
|
self.daemon = True
|
2022-11-06 04:36:56 +00:00
|
|
|
self.cw = cw
|
2015-09-04 12:36:25 +09:00
|
|
|
self.received = set()
|
|
|
|
|
self.keyhashes = []
|
2014-08-25 11:52:47 +02:00
|
|
|
|
2015-09-04 12:36:25 +09:00
|
|
|
def set_keyhashes(self, keyhashes):
|
|
|
|
|
self.keyhashes = keyhashes
|
2014-08-25 11:52:47 +02:00
|
|
|
|
2015-09-04 12:36:25 +09:00
|
|
|
def clear(self, keyhash):
|
|
|
|
|
server.delete(keyhash)
|
|
|
|
|
self.received.remove(keyhash)
|
2014-08-21 19:35:51 +02:00
|
|
|
|
|
|
|
|
def run(self):
|
2021-03-17 19:34:57 +01:00
|
|
|
while self.is_running():
|
2015-09-04 12:36:25 +09:00
|
|
|
if not self.keyhashes:
|
2014-08-21 19:35:51 +02:00
|
|
|
time.sleep(2)
|
|
|
|
|
continue
|
2015-09-04 12:36:25 +09:00
|
|
|
for keyhash in self.keyhashes:
|
|
|
|
|
if keyhash in self.received:
|
|
|
|
|
continue
|
2014-08-21 19:35:51 +02:00
|
|
|
try:
|
2015-09-04 12:36:25 +09:00
|
|
|
message = server.get(keyhash)
|
2014-08-21 19:35:51 +02:00
|
|
|
except Exception as e:
|
2022-11-06 04:36:56 +00:00
|
|
|
self.logger.info(f"cannot contact cosigner pool. exc: {e!r}")
|
2014-08-21 19:35:51 +02:00
|
|
|
time.sleep(30)
|
|
|
|
|
continue
|
2015-09-04 12:36:25 +09:00
|
|
|
if message:
|
|
|
|
|
self.received.add(keyhash)
|
2019-04-26 18:52:26 +02:00
|
|
|
self.logger.info(f"received message for {keyhash}")
|
2022-11-06 04:36:56 +00:00
|
|
|
self.cw.obj.cosigner_receive_signal.emit(
|
2017-09-23 05:54:38 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2015-11-23 19:38:48 +01:00
|
|
|
class Plugin(BasePlugin):
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2015-09-04 12:36:25 +09:00
|
|
|
def __init__(self, parent, config, name):
|
|
|
|
|
BasePlugin.__init__(self, parent, config, name)
|
2020-05-25 17:31:13 +02:00
|
|
|
self._init_qt_received = False
|
2022-11-06 04:36:56 +00:00
|
|
|
self.cosigner_wallets = {} # type: Dict[Abstract_Wallet, CosignerWallet]
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2016-02-25 09:39:01 +01:00
|
|
|
@hook
|
2020-05-25 17:31:13 +02:00
|
|
|
def init_qt(self, gui: 'ElectrumGui'):
|
|
|
|
|
if self._init_qt_received: # only need/want the first signal
|
|
|
|
|
return
|
|
|
|
|
self._init_qt_received = True
|
2016-02-25 09:39:01 +01:00
|
|
|
for window in gui.windows:
|
2020-12-08 10:33:43 +01:00
|
|
|
self.load_wallet(window.wallet, window)
|
2016-02-25 09:39:01 +01:00
|
|
|
|
2015-11-21 15:24:38 +01:00
|
|
|
@hook
|
2020-12-08 10:33:43 +01:00
|
|
|
def load_wallet(self, wallet: 'Abstract_Wallet', window: 'ElectrumWindow'):
|
2022-11-06 04:36:56 +00:00
|
|
|
if type(wallet) != Multisig_Wallet:
|
|
|
|
|
return
|
|
|
|
|
self.cosigner_wallets[wallet] = CosignerWallet(wallet, window)
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2015-11-21 15:24:38 +01:00
|
|
|
@hook
|
2015-09-04 12:36:25 +09:00
|
|
|
def on_close_window(self, window):
|
2022-11-06 04:36:56 +00:00
|
|
|
wallet = window.wallet
|
|
|
|
|
if cw := self.cosigner_wallets.get(wallet):
|
|
|
|
|
cw.close()
|
|
|
|
|
self.cosigner_wallets.pop(wallet)
|
2015-09-04 12:36:25 +09:00
|
|
|
|
|
|
|
|
def is_available(self):
|
2015-11-21 15:24:38 +01:00
|
|
|
return True
|
|
|
|
|
|
2022-11-06 04:36:56 +00:00
|
|
|
@hook
|
|
|
|
|
def transaction_dialog(self, d: 'TxDialog'):
|
|
|
|
|
if cw := self.cosigner_wallets.get(d.wallet):
|
|
|
|
|
cw.hook_transaction_dialog(d)
|
|
|
|
|
|
|
|
|
|
@hook
|
|
|
|
|
def transaction_dialog_update(self, d: 'TxDialog'):
|
|
|
|
|
if cw := self.cosigner_wallets.get(d.wallet):
|
|
|
|
|
cw.hook_transaction_dialog_update(d)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CosignerWallet(Logger):
|
|
|
|
|
# one for each open window
|
|
|
|
|
|
|
|
|
|
def __init__(self, wallet: 'Multisig_Wallet', window: 'ElectrumWindow'):
|
|
|
|
|
assert isinstance(wallet, Multisig_Wallet)
|
|
|
|
|
self.wallet = wallet
|
|
|
|
|
self.window = window
|
|
|
|
|
Logger.__init__(self)
|
|
|
|
|
self.obj = QReceiveSignalObject()
|
|
|
|
|
self.obj.cosigner_receive_signal.connect(self.on_receive)
|
|
|
|
|
|
|
|
|
|
self.keys = [] # type: List[Tuple[str, str]]
|
|
|
|
|
self.cosigner_list = [] # type: List[Tuple[str, bytes, str]]
|
2016-07-02 08:58:56 +02:00
|
|
|
for key, keystore in wallet.keystores.items():
|
2019-10-23 17:09:41 +02:00
|
|
|
xpub = keystore.get_master_public_key() # type: str
|
2019-02-21 22:17:06 +01:00
|
|
|
pubkey = BIP32Node.from_xkey(xpub).eckey.get_public_key_bytes(compressed=True)
|
2023-02-17 11:35:03 +00:00
|
|
|
_hash = crypto.sha256d(pubkey).hex()
|
2016-12-20 11:12:22 +01:00
|
|
|
if not keystore.is_watching_only():
|
2022-11-06 04:36:56 +00:00
|
|
|
self.keys.append((key, _hash))
|
2015-11-21 15:24:38 +01:00
|
|
|
else:
|
2022-11-06 04:36:56 +00:00
|
|
|
self.cosigner_list.append((xpub, pubkey, _hash))
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2022-11-06 04:36:56 +00:00
|
|
|
self.logger.info("starting listener")
|
|
|
|
|
self.listener = Listener(self)
|
|
|
|
|
self.listener.start()
|
|
|
|
|
self.listener.set_keyhashes([t[1] for t in self.keys])
|
|
|
|
|
|
|
|
|
|
def diagnostic_name(self):
|
|
|
|
|
return self.wallet.diagnostic_name()
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
self.logger.info("shutting down listener")
|
|
|
|
|
self.listener.stop()
|
|
|
|
|
self.listener = None
|
|
|
|
|
|
|
|
|
|
def hook_transaction_dialog(self, d: 'TxDialog'):
|
2015-09-04 12:36:25 +09:00
|
|
|
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))
|
2015-06-27 11:48:27 +09:00
|
|
|
d.buttons.insert(0, b)
|
2019-11-12 23:02:02 +01:00
|
|
|
b.setVisible(False)
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2022-11-06 04:36:56 +00:00
|
|
|
def hook_transaction_dialog_update(self, d: 'TxDialog'):
|
|
|
|
|
assert self.wallet == d.wallet
|
2023-02-21 13:15:06 +01:00
|
|
|
if d.tx.is_complete() or d.wallet.can_sign(d.tx):
|
2019-11-12 23:02:02 +01:00
|
|
|
d.cosigner_send_button.setVisible(False)
|
2014-08-21 19:35:51 +02:00
|
|
|
return
|
2022-11-06 04:36:56 +00:00
|
|
|
for xpub, K, _hash in self.cosigner_list:
|
|
|
|
|
if self.cosigner_can_sign(d.tx, xpub):
|
2019-11-12 23:02:02 +01:00
|
|
|
d.cosigner_send_button.setVisible(True)
|
2014-08-25 11:52:47 +02:00
|
|
|
break
|
2014-08-21 19:35:51 +02:00
|
|
|
else:
|
2019-11-12 23:02:02 +01:00
|
|
|
d.cosigner_send_button.setVisible(False)
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2019-10-23 17:09:41 +02:00
|
|
|
def cosigner_can_sign(self, tx: Transaction, cosigner_xpub: str) -> bool:
|
2020-02-07 20:09:40 +01:00
|
|
|
# TODO implement this properly:
|
|
|
|
|
# should return True iff cosigner (with given xpub) can sign and has not yet signed.
|
|
|
|
|
# note that tx could also be unrelated from wallet?... (not ismine inputs)
|
|
|
|
|
return True
|
2019-10-23 17:09:41 +02:00
|
|
|
|
|
|
|
|
def do_send(self, tx: Union[Transaction, PartialTransaction]):
|
2018-08-03 20:53:56 +02:00
|
|
|
def on_success(result):
|
2022-11-06 04:36:56 +00:00
|
|
|
self.window.show_message(_("Your transaction was sent to the cosigning pool.") + '\n' +
|
2018-08-03 20:53:56 +02:00
|
|
|
_("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
|
2022-11-06 04:36:56 +00:00
|
|
|
self.window.show_error(_("Failed to send transaction to cosigning pool") + ':\n' + repr(e))
|
2018-08-03 20:53:56 +02:00
|
|
|
|
cosigner pool: use single thread to send messages
ServerProxy does not seem to be thread-safe.
For e.g. a 2of3 multisig wallet, which would send two messages,
one msg would get sent but the other might error out. See trace:
E | plugins.cosigner_pool.qt.Plugin | on_failure
Traceback (most recent call last):
File "...\electrum\electrum\gui\qt\util.py", line 832, in run
result = task.task()
File "...\electrum\electrum\plugins\cosigner_pool\qt.py", line 199, in <lambda>
task = lambda: server.put(_hash, message)
File "...\Python38\lib\xmlrpc\client.py", line 1109, in __call__
return self.__send(self.__name, args)
File "...\Python38\lib\xmlrpc\client.py", line 1450, in __request
response = self.__transport.request(
File "...\Python38\lib\xmlrpc\client.py", line 1153, in request
return self.single_request(host, handler, request_body, verbose)
File "...\Python38\lib\xmlrpc\client.py", line 1165, in single_request
http_conn = self.send_request(host, handler, request_body, verbose)
File "...\Python38\lib\xmlrpc\client.py", line 1271, in send_request
connection.putrequest("POST", handler, skip_accept_encoding=True)
File "...\Python38\lib\http\client.py", line 1088, in putrequest
raise CannotSendRequest(self.__state)
http.client.CannotSendRequest: Request-sent
2020-06-29 02:19:03 +02:00
|
|
|
buffer = []
|
|
|
|
|
# construct messages
|
2022-11-06 04:36:56 +00:00
|
|
|
for xpub, K, _hash in self.cosigner_list:
|
2014-08-25 11:52:47 +02:00
|
|
|
if not self.cosigner_can_sign(tx, xpub):
|
|
|
|
|
continue
|
2019-10-23 17:09:41 +02:00
|
|
|
raw_tx_bytes = tx.serialize_as_bytes()
|
2018-05-24 18:57:13 +02:00
|
|
|
public_key = ecc.ECPubkey(K)
|
|
|
|
|
message = public_key.encrypt_message(raw_tx_bytes).decode('ascii')
|
cosigner pool: use single thread to send messages
ServerProxy does not seem to be thread-safe.
For e.g. a 2of3 multisig wallet, which would send two messages,
one msg would get sent but the other might error out. See trace:
E | plugins.cosigner_pool.qt.Plugin | on_failure
Traceback (most recent call last):
File "...\electrum\electrum\gui\qt\util.py", line 832, in run
result = task.task()
File "...\electrum\electrum\plugins\cosigner_pool\qt.py", line 199, in <lambda>
task = lambda: server.put(_hash, message)
File "...\Python38\lib\xmlrpc\client.py", line 1109, in __call__
return self.__send(self.__name, args)
File "...\Python38\lib\xmlrpc\client.py", line 1450, in __request
response = self.__transport.request(
File "...\Python38\lib\xmlrpc\client.py", line 1153, in request
return self.single_request(host, handler, request_body, verbose)
File "...\Python38\lib\xmlrpc\client.py", line 1165, in single_request
http_conn = self.send_request(host, handler, request_body, verbose)
File "...\Python38\lib\xmlrpc\client.py", line 1271, in send_request
connection.putrequest("POST", handler, skip_accept_encoding=True)
File "...\Python38\lib\http\client.py", line 1088, in putrequest
raise CannotSendRequest(self.__state)
http.client.CannotSendRequest: Request-sent
2020-06-29 02:19:03 +02:00
|
|
|
buffer.append((_hash, message))
|
|
|
|
|
if not buffer:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# send messages
|
|
|
|
|
# note: we send all messages sequentially on the same thread
|
|
|
|
|
def send_messages_task():
|
|
|
|
|
for _hash, message in buffer:
|
|
|
|
|
server.put(_hash, message)
|
|
|
|
|
msg = _('Sending transaction to cosigning pool...')
|
2022-11-06 04:36:56 +00:00
|
|
|
WaitingDialog(self.window, msg, send_messages_task, on_success, on_failure)
|
2015-09-04 12:36:25 +09:00
|
|
|
|
|
|
|
|
def on_receive(self, keyhash, message):
|
2019-05-07 01:41:41 +02:00
|
|
|
self.logger.info(f"signal arrived for {keyhash}")
|
2022-11-06 04:36:56 +00:00
|
|
|
for key, _hash in self.keys:
|
2015-09-04 12:36:25 +09:00
|
|
|
if _hash == keyhash:
|
|
|
|
|
break
|
|
|
|
|
else:
|
2019-04-26 18:52:26 +02:00
|
|
|
self.logger.info("keyhash not found")
|
2015-09-04 12:36:25 +09:00
|
|
|
return
|
2014-08-21 19:35:51 +02:00
|
|
|
|
2022-11-06 04:36:56 +00:00
|
|
|
window = self.window
|
|
|
|
|
wallet = self.wallet
|
2018-04-17 16:20:08 +02:00
|
|
|
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
|
2018-04-17 16:20:08 +02:00
|
|
|
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
|
|
|
|
2016-12-20 11:12:22 +01: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:
|
2019-02-21 22:17:06 +01:00
|
|
|
privkey = BIP32Node.from_xkey(xprv).eckey
|
2019-10-23 17:09:41 +02:00
|
|
|
message = 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('')
|
2019-07-17 20:12:52 +02:00
|
|
|
window.show_error(_('Error decrypting message') + ':\n' + repr(e))
|
2014-08-21 19:35:51 +02:00
|
|
|
return
|
|
|
|
|
|
2015-09-04 12:36:25 +09:00
|
|
|
self.listener.clear(keyhash)
|
2021-03-27 01:44:18 +01:00
|
|
|
try:
|
|
|
|
|
tx = tx_from_any(message)
|
|
|
|
|
except SerializationError as e:
|
|
|
|
|
window.show_error(_("Electrum was unable to deserialize the transaction:") + "\n" + str(e))
|
|
|
|
|
return
|
2019-10-23 17:09:41 +02:00
|
|
|
show_transaction(tx, parent=window, prompt_if_unsaved=True)
|