Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed May 3, 2018
1 parent bbfe390 commit 70e4f91
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 191 deletions.
7 changes: 5 additions & 2 deletions shuffle/coin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def add_transaction_signatures(self, tx, signatures):
return tx

def check_double_spend(t):
"Double Spend Check should go here"
"""
Double Spend Check should go here
NOT IMPLEMENTED
"""
return true

def verify_tx_signature(self, sig, tx, vk):
Expand Down Expand Up @@ -107,4 +110,4 @@ def verify_signature(self, sig, message, vk):
pk, compressed = pubkey_from_signature(sig,Hash(msg_magic(message)))
address_from_signature = public_key_to_p2pkh(point_to_ser(pk.pubkey.point,compressed))
address_from_vk = self.address(vk)
return address_from_vk == address_from_signature
return address_from_signature == address_from_vk
307 changes: 204 additions & 103 deletions shuffle/coin_shuffle.py

Large diffs are not rendered by default.

13 changes: 2 additions & 11 deletions shuffle/commutator_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import select

class Channel(queue.Queue):
"""
simple Queue wrapper for using recv and send
"""
"""simple Queue wrapper for using recv and send"""
def __init__(self, switch_timeout = None):
queue.Queue.__init__(self)
self.switch_timeout = switch_timeout
Expand All @@ -29,9 +27,7 @@ def recv(self):
return self.get()

class Commutator(threading.Thread):
"""
Class for decoupling of send and recv ops.
"""
"""Class for decoupling of send and recv ops."""
def __init__(self, income, outcome, logger = ChannelWithPrint(), buffsize = 4096, timeout = 0, switch_timeout = 0.1, ssl = False):
super(Commutator, self).__init__()
self.income = income
Expand Down Expand Up @@ -75,23 +71,18 @@ def connect(self, host, port):
try:
bare_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
bare_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# bare_socket.settimeout(self.timeout)
if self.ssl:
self.socket = ssl.wrap_socket(bare_socket, ssl_version=ssl.PROTOCOL_TLSv1_2, ciphers="ECDHE-RSA-AES128-GCM-SHA256")
else:
self.socket = bare_socket
# self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
print(self.socket)
self.socket.connect((host, port))
# self.socket.settimeout(self.timeout)
self.debug('connected')
except IOError as e:
self.logger.put(str(e))
raise(e)

def _send(self, msg):
# print(msg)
message = msg + self.frame
self.socket.sendall(message)

Expand Down
11 changes: 6 additions & 5 deletions shuffle/messages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import message_pb2 as message_factory
# import message_pb2 as message_factory
# from electroncash_plugins.shuffle import message_pb2 as message_factory

from random import shuffle

Expand Down Expand Up @@ -55,6 +56,7 @@ def general_blame(self, reason, accused):
accused is a veryfikation key! of player who accused the Blame
reason is a reason why
"""
self.clear_packets()
# add new packet
packet = self.packets.packet.add()
# set blame resaon
Expand Down Expand Up @@ -208,7 +210,7 @@ def get_public_key(self):

@check_for_length
def get_decryption_key(self):
return self.packets.packet[-1].packet.message.blame.key.key
return self.packets.packet[-1].packet.message.blame.key.key

def get_signatures_and_packets(self):
return [ [packet.signature.signature, packet.packet.SerializeToString(), packet.packet.from_key.key] for packet in self.packets.packet]
Expand All @@ -219,9 +221,8 @@ def get_players(self):
def get_blame(self):
return [packet.packet.message for packet in self.packets.packet]

def get_strs(self):
return [packet.packet.message.str for packet in self.packets.packet]

def clear_packets(self):
self.__init__()

# z = Messages()
# z.blame_equivocation_failure('some key')
# z.packets.packet[-1].packet.message.blame.invalid.invalid = b"x"
37 changes: 2 additions & 35 deletions shuffle/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,7 @@
import PyQt5.QtGui as QtGui
from PyQt5.QtWidgets import (QVBoxLayout, QLabel, QGridLayout, QLineEdit, QHBoxLayout, QWidget, QCheckBox)

# from PyQt4.QtGui import *
# from PyQt4.QtCore import *
# import PyQt4.QtCore as QtCore
# import PyQt4.QtGui as QtGui
# # from PyQt4.QtWidgets import (QVBoxLayout, QLabel, QGridLayout, QLineEdit)
# from PyQt4.QtGui import (QVBoxLayout, QLabel, QGridLayout, QLineEdit)


from electroncash.plugins import BasePlugin, hook
# from electroncash.paymentrequest import PaymentRequest
from electroncash.i18n import _
from electroncash_gui.qt.util import EnterButton, Buttons, CloseButton
from electroncash_gui.qt.util import OkButton, WindowModalDialog
Expand All @@ -63,13 +54,8 @@ def is_available(self):

def __init__(self, parent, config, name):
BasePlugin.__init__(self, parent, config, name)
# self.server = self.config.get('coinshuffleserver', '')
# self.server_ssl = self.config.get('coinshufflessl', False)
self.window = None
self.tab = None
# will try to add parent functionality here
# self.username = self.config.get('user', '')
# self.password = self.config.get('password', '')

@hook
def init_qt(self, gui):
Expand All @@ -90,8 +76,6 @@ def on_close(self):

def update(self, window):
self.window = window
# add_optional_tab
# tab = window.shuffle_tab
self.tab = self.create_shuffle_tab()
self.set_coinshuffle_addrs()
icon = QIcon(":icons/tab_coins.png")
Expand All @@ -102,14 +86,6 @@ def update(self, window):
self.tab.tab_pos = len(self.window.tabs)
self.tab.tab_name = name
self.window.tabs.addTab(self.tab, icon, description.replace("&", ""))
# print(window.config.get('show_{}_tab'.format(name), False))
# #add_toggle_action
# is_shown = window.config.get('show_{}_tab'.format(tab.tab_name), False)
# item_name = (_("Hide") if is_shown else _("Show")) + " " + tab.tab_description
# menu_bar = window.menuBar()
# print(str(menu_bar.menus))
# tab.menu_action = menu_bar.addAction(item_name, lambda: window.toggle_tab(tab))
# window.setMenuBar(menu_bar)

def set_coinshuffle_addrs(self):
self.coinshuffle_servers.setItems()
Expand Down Expand Up @@ -161,6 +137,8 @@ def process_protocol_messages(self, message):
self.coinshuffle_text_output.setTextColor(QColor('red'))
if "insufficient" in message:
pass
elif "wrong hash" in message:
pass
else:
self.pThread.join()
self.enable_coinshuffle_settings()
Expand All @@ -169,7 +147,6 @@ def process_protocol_messages(self, message):
self.coinshuffle_text_output.setTextColor(QColor('black'))

def start_coinshuffle_protocol(self):
# print('protocol started')
from .client import protocolThread
from electroncash.bitcoin import (regenerate_key, deserialize_privkey)
from .shuffle import ConsoleLogger
Expand All @@ -187,14 +164,9 @@ def start_coinshuffle_protocol(self):
self.window.show_error(str(e), parent=parent)
continue
try:
# server_params = self.window.config.get('coinshuffleserver').split(":")
# server = server_params[0]
# port = int(server_params[1])
server_params = self.coinshuffle_servers.get_current_server()
server = server_params['server']
port = server_params['port']
# ssl = self.window.config.get('coinshufflessl')
# ssl = self.coinshuffle_use_ssl.isChecked()
ssl = server_params.get('ssl', False)
except:
self.coinshuffle_text_output.setText('Wrong server connection string')
Expand All @@ -209,11 +181,6 @@ def start_coinshuffle_protocol(self):
#disable inputs
self.disable_coinshuffle_settings()
self.coinshuffle_cancel_button.setEnabled(True)
# self.coinshuffle_start_button.setEnabled(False)
# self.coinshuffle_inputs.setEnabled(False)
# self.coinshuffle_changes.setEnabled(False)
# self.coinshuffle_outputs.setEnabled(False)
# self.coinshuffle_amount_radio.setEnabled(False)

amount = self.coinshuffle_amount_radio.get_amount()
fee = self.coinshuffle_fee_constant
Expand Down
1 change: 0 additions & 1 deletion shuffle/shuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from electroncash_gui.qt.util import *
from electroncash.i18n import _
from .client import protocolThread
# from electroncash_plugins.coinshuffle.client import protocolThread
from electroncash.bitcoin import regenerate_key
from electroncash.address import Address

Expand Down
20 changes: 0 additions & 20 deletions shuffle/tests/temp_test.py

This file was deleted.

28 changes: 14 additions & 14 deletions shuffle/tests/test_bad_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

class TestProtocol(TestProtocolCase):

def test_001_cheat_in_sending_different_keys(self):
protocolThreads = self.make_clients_threads(with_print = True, number_of_clients = self.number_of_players - 1)
bad_thread = self.make_bad_client(bad_client_wrong_broadcast, with_print = True)
protocolThreads.append(bad_thread)
# random.shuffle(protocolThreads)
self.start_protocols(protocolThreads)
done = False
while not done:
completes = [self.is_protocol_complete(p) for p in protocolThreads[:-1]]
done = all(completes)
self.stop_protocols(protocolThreads)
tx = protocolThreads[0].protocol.tx.raw
for pThread in protocolThreads[1:-1]:
self.assertEqual(tx, pThread.protocol.tx.raw)
# def test_001_cheat_in_sending_different_keys(self):
# protocolThreads = self.make_clients_threads(with_print = True, number_of_clients = self.number_of_players - 1)
# bad_thread = self.make_bad_client(bad_client_wrong_broadcast, with_print = True)
# protocolThreads.append(bad_thread)
# # random.shuffle(protocolThreads)
# self.start_protocols(protocolThreads)
# done = False
# while not done:
# completes = [self.is_protocol_complete(p) for p in protocolThreads[:-1]]
# done = all(completes)
# self.stop_protocols(protocolThreads)
# tx = protocolThreads[0].protocol.tx.raw
# for pThread in protocolThreads[1:-1]:
# self.assertEqual(tx, pThread.protocol.tx.raw)

def test_002_cheat_in_sending_different_outputs(self):
protocolThreads = self.make_clients_threads(with_print = True, number_of_clients = self.number_of_players - 1)
Expand Down

0 comments on commit 70e4f91

Please sign in to comment.