forked from f-prime/zCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzcoin.py
More file actions
121 lines (112 loc) · 4.27 KB
/
Copy pathzcoin.py
File metadata and controls
121 lines (112 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import socket
import json
from rsa import *
import random
import get_nodes
import get_db
import register
import sqlite3
import config
import time
import thread
import string
import get_difficulty
import check_coin
import send_coin
class zCoin:
def __init__(self):
self.cmds = {
"register":register.register,
"get_nodes":get_nodes.get_nodes,
"get_db":get_db.get_db,
"get_difficulty":get_difficulty.get_difficulty,
"get_raw_difficulty":get_difficulty.get_raw_difficulty,
"check_coin":check_coin.check_coin,
"confirm_coin":check_coin.confirm_coin,
"send_coin":send_coin.send_coin,
}
def first_run(self):
print "Generating address..."
pub, priv = newkeys(1024)
address = "Z"+''.join([random.choice(string.uppercase+string.lowercase+string.digits) for x in range(50)])
print "Your address is "+address
wallet = sqlite3.connect("wallet.db")
db = sqlite3.connect("db.db")
nodes = sqlite3.connect("nodes.db")
nodes.execute("CREATE TABLE IF NOT EXISTS data (address TEXT, relay INT, port INT, public TEXT, ip TEXT)")
db.execute("CREATE TABLE IF NOT EXISTS difficulty (level INT)")
db.execute("CREATE TABLE IF NOT EXISTS coins (starter TEXT, address TEXT, hash TEXT)")
db.execute("CREATE TABLE IF NOT EXISTS transactions (to_ TEXT, from_ TEXT, hash TEXT)")
db.execute("INSERT INTO difficulty (level) VALUES (7)")
db.commit()
wallet.execute("CREATE TABLE data (public TEXT, private TEXT, address TEXT)")
wallet.execute("INSERT INTO data (public, private, address) VALUES (?, ?, ?)", [str(pub), str(priv), address])
wallet.commit()
print "Registering with broker node..."
register.register_send(True)
print "Retreiving nodes db..."
get_nodes.get_nodes_send(True)
print "Retreiving coins db..."
get_db.get_db_send()
print "Syncing with network..."
register.register_send()
print "Done!"
def handle(self, obj, ip):
data = obj.recv(10240)
if data:
try:
data = json.loads(data)
except ValueError:
return
else:
print data
data['ip'] = ip
if "cmd" in data:
if data['cmd'] in self.cmds:
self.cmds[data['cmd']](obj, data)
obj.close()
def relay(self):
print "zCoin has started as a relay node"
nodes = sqlite3.connect("nodes.db")
cur = nodes.cursor()
cur.execute("SELECT * FROM data")
if not cur.fetchall(): #This must mean that it is the first node, thus it is a god node.
wallet = sqlite3.connect("wallet.db").cursor()
print "\nIt seems as though you are trying to run as the first God node in the network."
ip = raw_input("\nJust to be safe, give me the IP address of this node: ")
port = config.port
relay = 1
wallet.execute("SELECT address, public FROM data")
out = wallet.fetchall()
address = out[0][0]
public = out[0][1]
nodes.execute("INSERT INTO data (address, ip, port, public, relay) VALUES (?, ?, ?, ?, ?)", [address, ip, port, public, relay])
nodes.commit()
print "All good"
get_nodes.get_nodes_send()
get_db.get_db_send()
register.register_send()
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((config.host, config.port))
sock.listen(5)
while True:
obj, conn = sock.accept()
thread.start_new_thread(self.handle, (obj, conn[0]))
def non_relay(self):
while True:
get_nodes.get_nodes_send()
get_db.get_db_send()
time.sleep(60)
if __name__ == "__main__":
wallet = sqlite3.connect("wallet.db")
try:
wallet.execute("SELECT * FROM data")
except:
zCoin().first_run()
if config.relay:
zCoin().relay()
else:
print "zCoin has started as a normal node."
register.register_send()
zCoin().non_relay()