electrum-bitcoinprivate/plugins/cosigner_pool.py

204 lines
6.7 KiB
Python
Raw Normal View History

2014-08-21 10:35:51 -07:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2014 Thomas Voegtlin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import socket
import threading
import time
import xmlrpclib
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from electrum import bitcoin, util
from electrum import transaction
2014-08-31 02:42:40 -07:00
from electrum.plugins import BasePlugin, hook
2014-08-21 10:35:51 -07:00
from electrum.i18n import _
from electrum_gui.qt.transaction_dialog import show_transaction
2014-08-21 10:35:51 -07:00
import sys
import traceback
PORT = 12344
HOST = 'ecdsa.net'
server = xmlrpclib.ServerProxy('http://%s:%d'%(HOST,PORT), allow_none=True)
class Listener(util.DaemonThread):
2014-08-21 10:35:51 -07:00
def __init__(self, parent):
util.DaemonThread.__init__(self)
2014-08-21 10:35:51 -07:00
self.daemon = True
self.parent = parent
self.received = set()
self.keyhashes = []
2014-08-25 02:52:47 -07:00
def set_keyhashes(self, keyhashes):
self.keyhashes = keyhashes
2014-08-25 02:52:47 -07:00
def clear(self, keyhash):
server.delete(keyhash)
self.received.remove(keyhash)
2014-08-21 10:35:51 -07:00
def run(self):
while self.running:
if not self.keyhashes:
2014-08-21 10:35:51 -07:00
time.sleep(2)
continue
for keyhash in self.keyhashes:
if keyhash in self.received:
continue
2014-08-21 10:35:51 -07:00
try:
message = server.get(keyhash)
2014-08-21 10:35:51 -07:00
except Exception as e:
self.print_error("cannot contact cosigner pool")
2014-08-21 10:35:51 -07:00
time.sleep(30)
continue
if message:
self.received.add(keyhash)
self.print_error("received message for", keyhash)
self.parent.obj.emit(SIGNAL("cosigner:receive"), keyhash,
message)
2014-08-25 02:52:47 -07:00
# poll every 30 seconds
2014-08-21 10:35:51 -07:00
time.sleep(30)
class QtPlugin(BasePlugin):
2014-08-21 10:35:51 -07:00
def __init__(self, parent, config, name):
BasePlugin.__init__(self, parent, config, name)
self.listener = None
self.obj = QObject()
self.obj.connect(self.obj, SIGNAL('cosigner:receive'), self.on_receive)
self.keys = []
self.cosigner_list = []
2014-08-21 10:35:51 -07:00
@hook
def on_new_window(self, window):
self.update(window)
2014-08-21 10:35:51 -07:00
@hook
def on_close_window(self, window):
self.update(window)
def is_available(self):
return True
def update(self, window):
wallet = window.wallet
if wallet.wallet_type not in ['2of2', '2of3']:
return
if self.listener is None:
self.print_error("starting listener")
self.listener = Listener(self)
self.listener.start()
elif self.listener:
self.print_error("shutting down listener")
self.listener.stop()
self.listener = None
self.keys = []
2014-08-25 02:52:47 -07:00
self.cosigner_list = []
for key, xpub in wallet.master_public_keys.items():
K = bitcoin.deserialize_xkey(xpub)[-1].encode('hex')
_hash = bitcoin.Hash(K).encode('hex')
if wallet.master_private_keys.get(key):
self.keys.append((key, _hash, window))
else:
self.cosigner_list.append((window, xpub, K, _hash))
if self.listener:
self.listener.set_keyhashes([t[1] for t in self.keys])
2014-08-21 10:35:51 -07:00
2014-08-31 02:42:40 -07:00
@hook
2014-08-21 10:35:51 -07:00
def transaction_dialog(self, d):
d.cosigner_send_button = b = QPushButton(_("Send to cosigner"))
2014-08-21 10:35:51 -07:00
b.clicked.connect(lambda: self.do_send(d.tx))
d.buttons.insert(0, b)
2014-08-21 10:35:51 -07:00
self.transaction_dialog_update(d)
2014-08-31 02:42:40 -07:00
@hook
2014-08-21 10:35:51 -07: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 10:35:51 -07: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 02:52:47 -07:00
break
2014-08-21 10:35:51 -07:00
else:
d.cosigner_send_button.hide()
2014-08-21 10:35:51 -07:00
2014-08-25 02:52:47 -07:00
def cosigner_can_sign(self, tx, cosigner_xpub):
2014-08-21 10:35:51 -07:00
from electrum.transaction import x_to_xpub
xpub_set = set([])
for txin in tx.inputs:
for x_pubkey in txin['x_pubkeys']:
xpub = x_to_xpub(x_pubkey)
if xpub:
xpub_set.add(xpub)
2014-08-25 02:52:47 -07:00
return cosigner_xpub in xpub_set
2014-08-21 10:35:51 -07:00
def do_send(self, tx):
for window, xpub, K, _hash in self.cosigner_list:
2014-08-25 02:52:47 -07:00
if not self.cosigner_can_sign(tx, xpub):
continue
message = bitcoin.encrypt_message(tx.raw, K)
try:
server.put(_hash, message)
except Exception as e:
2015-06-12 01:44:49 -07:00
traceback.print_exc(file=sys.stdout)
window.show_message("Failed to send transaction to cosigning pool.")
2014-08-25 02:52:47 -07:00
return
window.show_message("Your transaction was sent to the cosigning pool.\nOpen your cosigner wallet to retrieve it.")
def on_receive(self, keyhash, message):
self.print_error("signal arrived for", keyhash)
for key, _hash, window in self.keys:
if _hash == keyhash:
break
else:
self.print_error("keyhash not found")
return
2014-08-21 10:35:51 -07:00
wallet = window.wallet
if wallet.use_encryption:
password = window.password_dialog('An encrypted transaction was retrieved from cosigning pool.\nPlease enter your password to decrypt it.')
2014-08-21 10:35:51 -07:00
if not password:
return
else:
password = None
if not window.question(_("An encrypted transaction was retrieved from cosigning pool.\nDo you want to open it now?")):
2014-09-14 11:28:21 -07:00
return
2014-08-21 10:35:51 -07:00
xprv = wallet.get_master_private_key(key, password)
2014-08-25 02:52:47 -07:00
if not xprv:
2014-08-21 10:35:51 -07:00
return
try:
2014-08-25 02:52:47 -07:00
k = bitcoin.deserialize_xkey(xprv)[-1].encode('hex')
2014-08-21 10:35:51 -07:00
EC = bitcoin.EC_KEY(k.decode('hex'))
message = EC.decrypt_message(message)
except Exception as e:
traceback.print_exc(file=sys.stdout)
window.show_message(str(e))
2014-08-21 10:35:51 -07:00
return
self.listener.clear(keyhash)
tx = transaction.Transaction(message)
show_transaction(tx, window, prompt_if_unsaved=True)