-
Notifications
You must be signed in to change notification settings - Fork 15
/
genesis.py
97 lines (82 loc) · 3.45 KB
/
genesis.py
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
#WARNING: this file uses the old wallet structure, there is no need to update it at the moment
import hashlib
import socket
import re
import sqlite3
import os
import sys
import time
import base64
from Cryptodome.Hash import SHA256
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import PKCS1_v1_5
from Cryptodome.Hash import SHA
from Crypto import Random
if os.path.isfile("privkey.der"):
print("privkey.der found")
elif os.path.isfile("privkey_encrypted.der"):
print("privkey_encrypted.der found")
else:
# generate key pair and an address
key = RSA.generate(4096)
public_key = key.publickey()
private_key_readable = str(key.exportKey())
public_key_readable = str(key.publickey().exportKey())
address = hashlib.sha224(public_key_readable.encode("utf-8")).hexdigest() # hashed public key
# generate key pair and an address
print("Your address: {}".format(address))
print("Your private key:\n {}".format(private_key_readable))
print("Your public key:\n {}".format(public_key_readable))
with open("privkey.der", "a") as f:
f.write(str(private_key_readable))
with open("pubkey.der", "a") as f:
f.write(str(public_key_readable))
with open("address.txt", "a") as f:
f.write("{}\n".format(address))
# import keys
key = RSA.importKey(open('privkey.der').read())
public_key = key.publickey()
private_key_readable = str(key.exportKey())
public_key_readable = str(key.publickey().exportKey())
address = hashlib.sha224(public_key_readable.encode("utf-8")).hexdigest()
print("Your address: {}".format(address))
print("Your private key:\n {}".format(private_key_readable))
print("Your public key:\n {}".format(public_key_readable))
public_key_b64encoded = base64.b64encode(public_key_readable)
# import keys
timestamp = str(time.time())
print("Timestamp: {}".format(timestamp))
transaction = (timestamp, "genesis", address, str(float(100000000)), "genesis")
h = SHA.new(str(transaction))
signer = PKCS1_v1_5.new(key)
signature = signer.sign(h)
signature_enc = base64.b64encode(signature)
print("Encoded Signature: {}".format(signature_enc))
block_hash = hashlib.sha224(str((timestamp, transaction)).encode("utf-8")).hexdigest() # first hash is simplified
print ("Transaction Hash: {}".format(block_hash))
if os.path.isfile("static/ledger.db"):
print("You are beyond genesis")
else:
# transaction processing
cursor = None
mem_cur = None
try:
conn = sqlite3.connect('static/ledger.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE transactions (block_height INTEGER, timestamp, address, recipient, amount, signature, public_key, block_hash, fee, reward, operation, openfield)")
cursor.execute("INSERT INTO transactions VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", ("1", timestamp, 'genesis', address, '0', str(signature_enc), public_key_b64encoded, block_hash, 0, 1, 1, 'genesis')) # Insert a row of data
conn.commit() # Save (commit) the changes
mempool = sqlite3.connect('mempool.db')
mem_cur = mempool.cursor()
mem_cur.execute("CREATE TABLE transactions (timestamp, address, recipient, amount, signature, public_key, operation, openfield)")
mempool.commit()
mempool.close()
print("Genesis created.")
except sqlite3.Error as e:
print("Error %s:" % e.args[0])
sys.exit(1)
finally:
if cursor is not None:
cursor.close()
if mem_cur is not None:
mem_cur.close()