Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Max00355 committed Nov 17, 2013
0 parents commit 602defe
Show file tree
Hide file tree
Showing 30 changed files with 4,438 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.db
*.pyc
64 changes: 64 additions & 0 deletions check_coin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import get_difficulty
import socket
import json
from rsa import *
import hashlib
import base64
import sqlite3

def check_coin(obj, data):
"""
{"address":<addr>, "hash":<hash>, "starter":<starter>}
"""
node = sqlite3.connect("nodes.db").cursor()
c = node.execute("SELECT public FROM data WHERE address=?", [data['address']])
c = c.fetchall()
difficulty = get_difficulty.get_difficulty(None, None)
if c:
c = c[0]
if len(data['hash']) == 128:
if hashlib.sha512(str(data['starter'])).hexdigest() == data['hash'] and data['hash'].startswith("1"*difficulty['difficulty']):
if c[0].startswith("PublicKey(") and c[0].endswith(")"):
key = eval(c[0])

data['starter'] = base64.b64encode(encrypt(str(data['starter']), key))
obj.send(json.dumps({"response":"Coin Confirmed!"}))
try:
out = sqlite3.connect("db.db").cursor()
out.execute("SELECT * FROM coins")
data['difficulty'] = len(out.fetchall())/205000 + 7
except TypeError:
data['difficulty'] = 7
send_confirm(data)
else:
obj.send(json.dumps({"response":"Invalid Coin!"}))

def send_confirm(data):
nodes = sqlite3.connect("nodes.db").cursor()
nodes = nodes.execute("SELECT ip, port FROM data WHERE relay=1")
for x in nodes:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
continue
else:
data['cmd'] = "confirm_coin"
s.send(json.dumps(data))
s.close()

def confirm_coin(obj, data):
db_ = sqlite3.connect("db.db")
db = db_.cursor()
check = get_difficulty.get_difficulty(None, None)
db.execute("SELECT * FROM coins WHERE hash=?", [data['hash']])
if data['hash'].startswith("1"*check['difficulty']) and len(data['hash']) == 128 and not db.fetchall():
db_.execute("UPDATE difficulty SET level=? WHERE level=?", [data['difficulty'], check['difficulty']])
db_.execute("INSERT INTO coins (starter, hash, address) VALUES (?, ?, ?)", [data['starter'], data['hash'], data['address']])
db_.commit()
else:
return

7 changes: 7 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sqlite3

relay = 0 #1 is on 0 is off If 1 you must portforward port 6565
brokers = [{0:"74.63.229.249", 1:6565}, {0:"64.31.59.70", 1:6565}]
port = 6565
host = "0.0.0.0"

44 changes: 44 additions & 0 deletions get_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import socket
import json
import config
import random
import sqlite3

def get_db(obj, data):
db = sqlite3.connect("db.db").cursor()
db.execute("CREATE TABLE IF NOT EXISTS difficulty (level INT default 7)")
db.execute("CREATE TABLE IF NOT EXISTS coins (hash TEXT, address TEXT, starter TEXT)")
db.execute("CREATE TABLE IF NOT EXISTS transactions (to_ TEXT, from_ TEXT, hash TEXT)")
with open("db.db", 'rb') as file:
for x in file.readlines(1020):
obj.send(x)

def get_db_send():
node = sqlite3.connect("nodes.db")
cmd = {"cmd":"get_db"}
nodes = node.execute("SELECT ip, port FROM data WHERE relay=?", [True])
if not nodes:
return
nodes = nodes.fetchall()
random.shuffle(nodes)
for x in nodes:

s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps(cmd))
out = ""
while True:
data = s.recv(1024)
if data:
out = out + data
else:
break

with open("db.db", 'wb') as file:
file.write(out)
49 changes: 49 additions & 0 deletions get_difficulty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import socket
import json
import sqlite3

def get_difficulty(obj, data):
node = sqlite3.connect("nodes.db")
nodes = node.execute("SELECT ip, port FROM data WHERE relay=1")
nodes = nodes.fetchall()
difficulties = []
for x in nodes:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps({"cmd":"get_raw_difficulty"}))
data = s.recv(1024)
if not data:
s.close()
continue
try:
data = int(data)
except:
continue
else:
difficulties.append(data)
out = 0
for x in difficulties:
out += x
try:
out = out/len(difficulties)
if out < 7:
out = 7
except ZeroDivisionError:
out = 7
try: # Without this it will raise an error when check_coin is called.
obj.send(json.dumps({"difficulty":out}))
except:
pass
return {"difficulty":out}

def get_raw_difficulty(obj, data):
db = sqlite3.connect("db.db")
check = db.execute("SELECT level FROM difficulty")
check = check.fetchall()[0]
obj.send(str(check[0]))
53 changes: 53 additions & 0 deletions get_nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import socket
import json
import config
import random
import sqlite3

"""
Added: 11/16/13 12:08 AM
This is the code for the "get_nodes" command, a command that will send or retrieve the global database of nodes.
{"cmd":"get_nodes"}
"""

def get_nodes(obj, data):
with open("nodes.db", 'rb') as file:
for x in file.readlines(1020):
obj.send(x)

def get_nodes_send(god=False):
node = sqlite3.connect("nodes.db")
cmd = {"cmd":"get_nodes"}
nodes = node.execute("SELECT ip, port FROM data WHERE relay=?", [True])
nodes = nodes.fetchall()
if god:
nodes = config.brokers
if not nodes:
return
random.shuffle(nodes)
for x in nodes:

s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps(cmd))
out = ""
while True:
data = s.recv(1024)
if data:
out = out + data
else:
break


with open("nodes.db", 'wb') as file:
file.write(out)
70 changes: 70 additions & 0 deletions miner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import socket
import hashlib
import json
import random
import string
import sqlite3

def check_difficulty():
db = sqlite3.connect("nodes.db")
find = db.execute("SELECT ip, port FROM data WHERE relay=?", [True])
find = find.fetchall()
random.shuffle(find)
for x in find:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps({"cmd":"get_difficulty"}))
data = s.recv(1024)
print data
try:
data = json.loads(data)
except:
continue
s.close()
return data
s.close()

def check_coin(data):
node = sqlite3.connect("nodes.db")
node = node.execute("SELECT ip, port FROM data WHERE relay=?", [True])
node = node.fetchall()
random.shuffle(node)
for x in node:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
data['cmd'] = "check_coin"
print data
s.send(json.dumps(data))
s.close()

def mine():
while True:
check = check_difficulty()
starter = ''.join([random.choice(string.uppercase+string.lowercase+string.digits) for x in range(5)])
on = 0
while True:
print starter+str(on)
c = hashlib.sha512(starter+str(on)).hexdigest()
startswith = "1"*check['difficulty']
if c.startswith(startswith):
print c
wall = sqlite3.connect("wallet.db")
address = wall.execute("SELECT address FROM data")
address = address.fetchall()[0][0]
check_coin({"starter":starter+str(on), "hash":c, "address":address})
break
else:
on += 1
mine()
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
zCoin
=====

zCoin is a cryptocurrency that looks to approach the idea of an online currency at a different angle than that of Bitcoin.

First Run
=========

If this is your first time running zCoin you will need Python 2.7 installed, then you will simply need to run zcoin.py

Then run the shell.py file in order to access zCoin commands.

Relay Node
==========

If you would like to help out the zCoin network you will first need to open port 6565 then open config.py and set relay to 1.

Mining
======

zCoin comes with a basic miner on install. To mine zCoin make sure you have your node running and simply run miner.py

40 changes: 40 additions & 0 deletions register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json
import socket
import config
import random
import sqlite3

def register(obj, data):
check = sqlite3.connect("nodes.db")
c = check.execute("SELECT * FROM data WHERE address=?", [data['address']])
if c.fetchall():
check.execute("UPDATE data SET relay=?, ip=?, port=?, public=? WHERE address=?", [data['relay'], data['ip'], data['port'], data['address'], data['public']])
else:
check.execute("INSERT INTO data (address, ip, port, relay, public) VALUES (?, ?, ?, ?, ?)", [data['address'], data['ip'], data['port'], data['relay'], data['public']])
check.commit()

def register_send(god=False):
node = sqlite3.connect("nodes.db")
wallet = sqlite3.connect("wallet.db")
data = wallet.execute("SELECT address, public FROM data")
data = data.fetchall()[0]
cmd = {"cmd":"register", "address":data[0], "public":data[1], "port":config.port, "relay":config.relay}
nodes = node.execute("SELECT ip, port FROM data WHERE relay=?", [True])
nodes = nodes.fetchall()
if god:
nodes = config.brokers
if not nodes:
return
random.shuffle(nodes)
for x in nodes:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps(cmd))
s.close()

Loading

0 comments on commit 602defe

Please sign in to comment.