/
run.py
executable file
·221 lines (207 loc) · 8.49 KB
/
run.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
from __future__ import print_function
import argparse
import dill
import hashlib
import requests
import sys
import time
from getpass import getpass
from Cryptodome.Cipher import AES
import codecs
from crankycoin import config, logger
from crankycoin.node import FullNode
from crankycoin.wallet import Client
from crankycoin.miner import Miner
from crankycoin.repository.blockchain import Blockchain
from crankycoin.repository.mempool import Mempool
from crankycoin.repository.peers import Peers
from crankycoin.services.validator import Validator
from crankycoin.services.api_client import ApiClient
_PY3 = sys.version_info[0] > 2
if not _PY3:
input = raw_input
def client():
helptext = '''
Available commands:
===================
balance <public key (optional)>
send <destination> <amount> <fee>
publickey
privatekey
history <public key (optional)>
quit or exit
'''
peers = Peers()
api_client = ApiClient(peers)
encrypted = config['user']['encrypted_private_key']
if not encrypted.strip():
print("\n\nNo private key provided. A new wallet will be generated for you...\n\n")
wallet = Client(peers, api_client)
else:
passphrase = getpass("Enter passphrase: ")
encrypted = codecs.decode(encrypted, 'hex')
nonce = encrypted[0:16]
tag = encrypted[16:32]
ciphertext = encrypted[32:]
hashedpass = hashlib.sha256(passphrase.encode('utf-8')).digest()
cipher = AES.new(hashedpass, AES.MODE_EAX, nonce)
try:
private_key = cipher.decrypt_and_verify(ciphertext, tag)
wallet = Client(peers, api_client, private_key)
except ValueError as ve:
logger.warn('Invalid passphrase')
print("\n\nInvalid passphrase\n\n")
sys.exit(1)
while True:
cmd = input("{} ({}) wallet > ".format(config['network']['name'], config['network']['ticker_symbol']))
cmd_split = cmd.split()
try:
if cmd_split[0] == "balance":
if len(cmd_split) == 2:
print(wallet.get_balance(cmd_split[1]))
else:
print(wallet.get_balance())
elif cmd_split[0] == "send":
if len(cmd_split) == 4:
print(wallet.create_transaction(cmd_split[1], float(cmd_split[2]), float(cmd_split[3])))
else:
print("\nRequires destination, amount, fee\n")
elif cmd_split[0] == "publickey":
print(wallet.get_public_key())
elif cmd_split[0] == "privatekey":
print(wallet.get_private_key())
elif cmd_split[0] == "history":
if len(cmd_split) == 2:
print(wallet.get_transaction_history(cmd_split[1]))
else:
print(wallet.get_transaction_history())
elif cmd_split[0] in ("quit", "exit"):
sys.exit(0)
else: # help
print(helptext)
except IndexError:
pass
def full():
helptext = '''
Available commands:
===================
balance <public key (optional)>
history <public key (optional)>
getnodes
getblock <index (optional)>
getblocks <start index (optional)> <stop index (optional)>
mempoolcount
getmempool
getunconfirmedtx <tx hash>
mine <start | stop>
quit or exit
'''
peers = Peers()
api_client = ApiClient(peers)
blockchain = Blockchain()
mempool = Mempool()
validator = Validator()
ip = config['user']['ip']
public_key = config['user']['public_key']
if ip is None or public_key is None:
print("\n\npublic key and IP must be provided.\n\n")
sys.exit(1)
else:
print("\n\nfull node starting...\n\n")
full_node = FullNode(peers, api_client, blockchain, mempool, validator)
full_node.start()
miner = Miner(blockchain, mempool)
mining = False
while True:
cmd = input("{} ({}) full node > ".format(config['network']['name'], config['network']['ticker_symbol']))
cmd_split = cmd.split()
try:
if cmd_split[0] == "balance":
if len(cmd_split) == 2:
url = full_node.BALANCE_URL.format("localhost", full_node.FULL_NODE_PORT, cmd_split[1])
else:
url = full_node.BALANCE_URL.format("localhost", full_node.FULL_NODE_PORT, public_key)
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "history":
if len(cmd_split) == 2:
url = full_node.TRANSACTION_HISTORY_URL.format("localhost", full_node.FULL_NODE_PORT, cmd_split[1])
response = requests.get(url)
else:
url = full_node.TRANSACTION_HISTORY_URL.format("localhost", full_node.FULL_NODE_PORT, public_key)
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "getnodes":
url = full_node.NODES_URL.format("localhost", full_node.FULL_NODE_PORT)
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "getblock":
if len(cmd_split) == 2:
url = full_node.BLOCKS_URL.format("localhost", full_node.FULL_NODE_PORT, cmd_split[1])
else:
url = full_node.BLOCKS_URL.format("localhost", full_node.FULL_NODE_PORT, "latest")
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "getblocks":
if len(cmd_split) == 3:
url = full_node.BLOCKS_INV_URL.format("localhost", full_node.FULL_NODE_PORT, cmd_split[1], cmd_split[2])
else:
url = full_node.BLOCKS_URL.format("localhost", full_node.FULL_NODE_PORT, "")
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "mempoolcount":
url = full_node.UNCONFIRMED_TRANSACTIONS_URL.format("localhost", full_node.FULL_NODE_PORT, "count")
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "getmempool":
url = full_node.UNCONFIRMED_TRANSACTIONS_URL.format("localhost", full_node.FULL_NODE_PORT, "")
response = requests.get(url)
print(response.json())
elif cmd_split[0] == "getunconfirmedtx":
if len(cmd_split) == 2:
url = full_node.UNCONFIRMED_TRANSACTIONS_URL.format("localhost", full_node.FULL_NODE_PORT, cmd_split[1])
response = requests.get(url)
print(response.json())
else:
print("\nRequires tx hash\n")
elif cmd_split[0] == "mine":
if len(cmd_split) == 2:
if cmd_split[1] == "start":
if mining is False:
print("\n\nminer starting...\n\n")
mining = True
miner.start()
elif cmd_split[1] == "stop":
if mining is True:
print("\n\nminer shutting down...\n\n")
mining = False
miner.shutdown()
else:
print("\nRequires: start | stop")
else:
print("\nRequires: start | stop")
elif cmd_split[0] in ("quit", "exit"):
if mining is True:
print("\n\nminer shutting down...\n\n")
miner.shutdown()
time.sleep(2)
full_node.shutdown()
time.sleep(2)
sys.exit(0)
else: # help
print(helptext)
except IndexError:
pass
def main(argv):
parser = argparse.ArgumentParser(description='Starts a ' + config['network']['name'] + ' node')
parser.add_argument('mode', metavar='type', nargs='?', default=None, help='client | full')
args = parser.parse_args()
if args.mode == "client":
client()
elif args.mode == "full":
full()
else:
print("Node operation mode not specified")
if __name__ == "__main__":
main(sys.argv[1:])