Permalink
Browse files

Converted from python 2 to python 3

Cleansed dataTypes,
parse_ip now works even on windows ( ipv6 parsing not done yet )
  • Loading branch information...
gallexis committed Jul 4, 2016
1 parent c7ab2a9 commit c4e689e24a45698cf677620b68ce190c2b877763
View
@@ -1,16 +1,16 @@
import Tkinter
import tkinter
from Manager import core_manager
root = Tkinter.Tk()
root = tkinter.Tk()
top_label = Tkinter.Label(root, text="Created by Shlomi Zeltsinger and Alexis Gallepe")
top_label = tkinter.Label(root, text="Created by Shlomi Zeltsinger and Alexis Gallepe")
top_label.grid(row=0)
send_menu = Tkinter.Menu(root)
send_menu = tkinter.Menu(root)
root.config(menu=send_menu)
send_menu_sub = Tkinter.Menu(send_menu)
send_menu_sub = tkinter.Menu(send_menu)
send_menu.add_cascade(label="Choose package to send", menu=send_menu_sub)
send_menu_sub.add_command(label="0-version", command=core_manager.send_version_msg())
send_menu_sub.add_command(label="1-verAck", command=core_manager.send_verack_msg())
View
@@ -37,36 +37,37 @@ def run(self):
self.manager(parsedHeader, payloadStream)
except Exception as e:
print e
print(e)
break
print "Exit receiver Thread"
print("Exit receiver Thread")
def manager(self, parsedHeader, payloadStream):
self.log(parsedHeader.to_string())
command = parsedHeader.command.decode("utf-8")
if parsedHeader.command.startswith('ping'):
if command.startswith('ping'):
ping = Ping.DecodePing(payloadStream)
pong = Pong.EncodePong(ping.nonce)
packet = PacketCreator(pong)
self.senderQueue.put(packet.forge_packet())
elif parsedHeader.command.startswith('inv'):
elif command.startswith('inv'):
inv = Inv.DecodeInv(payloadStream)
self.log(inv.get_decoded_info())
elif parsedHeader.command.startswith('addr'):
elif command.startswith('addr'):
addr = Addr.DecodeAddr(payloadStream)
self.log(addr.get_decoded_info())
elif parsedHeader.command.startswith('pong'):
elif command.startswith('pong'):
pong = Pong.DecodePong(payloadStream)
self.log(pong.get_decoded_info())
elif parsedHeader.command.startswith('version'):
elif command.startswith('version'):
version = Version.DecodeVersion(payloadStream)
self.log(version.get_decoded_info())
View
@@ -15,4 +15,4 @@ def run(self):
order = self.queue.get()
self.sock.sendall(order)
print "Exit sender Thread"
print("Exit sender Thread")
View
@@ -17,18 +17,18 @@ def get_order_client(self):
self.order(0)
self.order(1)
print "Version & VerAck sent, connected to node."
print("Version & VerAck sent, connected to node.")
while True:
print "Enter your command number:"
print "11: Exit"
print "(0: Version)"
print "(1: verack)"
print "2: getAddr"
print "3: Ping"
print "4: GetBlocks (block hash already defined)"
cmd = input(">")
print("Enter your command number:")
print("11: Exit")
print("(0: Version)")
print("(1: verack)")
print("2: getAddr")
print("3: Ping")
print("4: GetBlocks (block hash already defined)")
cmd = int(input(">"))
self.order(cmd)
def order(self, cmd):
@@ -54,11 +54,11 @@ def order(self, cmd):
packet = PacketCreator(ping).forge_packet()
elif cmd == 4:
print "Enter your block Hash(es): (you can write as many blocks as you want, separated by a coma)"
print "i.e: 5c3e6403d40837110a2e8afb602b1c01714bda7ce23bea0a0000000000000000,951b7b286867a7e074865cd08a1ed99783bdfb189c90e6400000000000000000"
hashes = raw_input(">")
print("Enter your block Hash(es): (you can write as many blocks as you want, separated by a coma)")
print("i.e: 5c3e6403d40837110a2e8afb602b1c01714bda7ce23bea0a0000000000000000,951b7b286867a7e074865cd08a1ed99783bdfb189c90e6400000000000000000")
hashes = input(">")
hashes = [ hash.strip() for hash in hashes.split(',') ]
print hashes
print(hashes)
getBlocks = GetBlocks.EncodeGetblocks(hashes)
packet = PacketCreator(getBlocks).forge_packet()
View
@@ -16,7 +16,7 @@ def connect():
try:
sock.connect((HOST, PORT))
except Exception as e:
print e
print(e)
sys.exit(0)
return sock
View
@@ -9,7 +9,7 @@ def __init__(self, block): # Packets is a stream
self.magic = read_hexa(block.read(4))
self.command = block.read(12)
self.payload_size = read_uint32(block.read(4))
self.checksum = hash_to_string(block.read(4))
self.checksum = read_hexa(block.read(4))
self.header_size = 4 + 12 + 4 + 4
View
@@ -1,25 +1,30 @@
import codecs
import hashlib
from Utils.dataTypes import *
class PacketCreator:
def __init__(self, payload):
self.payload = payload.forge() # The message payload forged
# create the header
self.magic = to_hexa(
"F9BEB4D9") # The Magic number of the Main network -> This message will be accepted by the main network
try:
self.payload = str.encode(payload.forge()) # The message payload forged
except:
self.payload = payload.forge()
# Create the header
self.magic = to_hexa("F9BEB4D9") # The Magic number of the Main network -> This message will be accepted by the main network
self.command = self.command_padding(payload.command_name)
self.length = to_uint32(len(self.payload))
self.checksum = self.get_checksum()
def command_padding(self, command): # The message command should be padded to be 12 bytes long.
command += (12 - len(command)) * "\00"
return command
# The message command should be padded to be 12 bytes long.
def command_padding(self, cmd):
command = str(cmd)
command = command.ljust(12, '\00')
return str.encode(command)
def get_checksum(self):
check = hashlib.sha256(hashlib.sha256(self.payload).digest()).digest()[:4]
return check
return hashlib.sha256(hashlib.sha256( self.payload ).digest()).digest()[:4]
def forge_header(self):
return self.magic + self.command + self.length + self.checksum
@@ -22,7 +22,7 @@ class DecodeFilterLoad:
def __init__(self):
pass
bf = BloomFilter(1000,0.1)
print bf.size
print bf.hash_count
if "__main__" == __name__:
bf = BloomFilter(1000,0.1)
print(bf.size)
print(bf.hash_count)
@@ -10,14 +10,14 @@ def __init__(self):
self.version = to_int32(version_number)
self.services = to_uint64(0)
self.timestamp = to_int64(time.time())
self.timestamp = to_int64(int(time.time()))
self.addr_recv_services = to_uint64(0)
self.addr_recv_ip = to_big_endian_16char("127.0.0.1")
self.addr_recv_ip = to_big_endian_16char(b"127.0.0.1")
self.addr_recv_port = to_big_endian_uint16(8333)
self.addr_trans_services = to_uint64(0)
self.addr_trans_ip = to_big_endian_16char("127.0.0.1")
self.addr_trans_ip = to_big_endian_16char(b"127.0.0.1")
self.addr_trans_port = to_big_endian_uint16(8333)
self.nonce = to_uint64(random.getrandbits(64))
@@ -50,7 +50,7 @@ def __init__(self, payload):
self.nonce = read_uint64(payload.read(8))
self.user_agent_bytes = read_compactSize_uint(payload)
self.user_agent = read_char(payload.read(self.user_agent_bytes), self.user_agent_bytes)
self.user_agent = read_chars(payload.read(self.user_agent_bytes), self.user_agent_bytes)
self.starting_height = read_int32(payload.read(4))
self.relay = read_bool(payload.read(1))
@@ -9,7 +9,6 @@ class EncodeBlock:
def __init__(self):
self.command_name = "block"
def forge(self):
# TODO
return ""
@@ -20,8 +19,8 @@ def __init__(self, payload):
# Block header
self.version = read_uint32(payload.read(4))
self.previous_block_header_hash = read_32char(payload.read(32))
self.merkle_root_hash = read_32char(payload.read(32))
self.previous_block_header_hash = read_chars(payload.read(32))
self.merkle_root_hash = read_chars(payload.read(32))
self.time = read_uint32(payload.read(4))
self.bits = read_uint32(payload.read(4))
self.nounce = read_uint32(payload.read(4))
@@ -67,11 +66,11 @@ def get_txIn(self,payload,tx_in_count):
for _ in range(tx_in_count):
# previous_output
hash = read_32char(payload.read(32))
hash = read_chars(payload.read(32))
index = read_uint32(payload.read(4))
script_bytes = read_compactSize_uint(payload)
signature_script = read_char(payload,script_bytes)
signature_script = read_chars(payload, script_bytes)
sequence = read_uint32(payload.read(4))
@@ -93,7 +92,7 @@ def get_txOut(self,payload,tx_out_count):
value = read_uint64(payload.read(8))
pub_key_script_bytes = read_compactSize_uint(payload)
pk_script = read_char(payload, pub_key_script_bytes)
pk_script = read_chars(payload, pub_key_script_bytes)
tx_out.append({
"value": value,
@@ -13,30 +13,27 @@ def __init__(self, hashes):
self.version = to_uint32(version_number)
self.hash_count = to_compactSize_uint(length_hashes)
self.hashes = ''.join(to_32char(to_hexa(e)) for e in hashes)
self.stop_hash = to_32char(to_hexa("0" * 32))
self.hashes = b''.join(to_chars(to_hexa(e)) for e in hashes)
self.stop_hash = to_chars(to_hexa("00" * 32))
def forge(self):
return self.version + self.hash_count + self.hashes + self.stop_hash
class DecodeGetblocks:
def __init__(self, payload):
self.version = read_uint32(payload.read(4))
self.hash_count = read_compactSize_uint(payload)
self.hashes = self.parse_hashes(payload)
def parse_hashes(self,payload):
hashes = []
for _ in range(self.hash_count):
hashes.append(read_32char(payload.read(32)))
hashes.append(read_chars(payload.read(32)))
return hashes
def get_decoded_info(self):
display = "\n-----GetBlocks-----"
display += "\nVersion:\t %s" % self.version
@@ -20,7 +20,7 @@ def encode_inventories(self, inventories):
for inv in inventories:
encodeInventories += to_uint32(inv["type"])
encodeInventories += to_32char(inv["hash"])
encodeInventories += to_chars(inv["hash"])
return encodeInventories
View
@@ -1,4 +1,6 @@
magic_number = "F9BEB4D9".decode("hex") # The Main network magic number
from Utils.dataTypes import *
magic_number = to_hexa("F9BEB4D9") # The Main network magic number
version_number = 70012
latest_known_block = 416419 # june 2016
Oops, something went wrong.

0 comments on commit c4e689e

Please sign in to comment.