-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
259 lines (210 loc) · 7.94 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""The main blockchain server."""
import getpass
import os
import sys
sys.path.append('.') # noqa
# pylint: disable=C0413,C0411,W0611
from bottle import abort, error, get, post, request, response, route, run
from lib.blockchain import BlockChain
from lib.block import Block
from lib.crypto import ECDSAPublicKey, Hash512, BadSignatureError
from lib.errors import (BlockChainError, BlockNotFoundError,
BlockValidationError, TransactionNotFoundError,
TransactionValidationError)
from lib.transaction import Transaction
CHAIN = None
@get('/ping')
def ping():
"""Do basic test function."""
return"pong"
@get('/miner')
def mine_it():
"""Get info for mining a block."""
try:
txlist = CHAIN.q.get_pending_transactions()
num_blocks = CHAIN.q.get_num_blocks()
last_block = CHAIN.q.get_block(num_blocks - 1)
return{
'txlist': [x.serialise() for x in txlist],
'num_blocks': num_blocks,
'prev_hash': last_block.hash.serialise(),
'pow': CHAIN.blockdb.pow_difficulty
}
except BlockChainError:
abort(400, "Blockchain error")
@get('/block/<index>')
def get_block(index):
"""Get block at the specified index."""
try:
block = CHAIN.q.get_block(index)
response.content_type = "application/json"
return block.serialise()
except BlockNotFoundError:
abort(404, "Block not found")
except BlockChainError:
abort(500, "Blockchain error")
@get('/txid/<txid>')
def get_transaction(txid):
"""Get transaction by txid."""
try:
tx = CHAIN.q.get_transaction_from_blockchain(txid)
response.content_type = "application/json"
return tx.serialise()
except TransactionNotFoundError:
abort(404, "Transaction not found")
except BlockChainError:
abort(500, "Blockchain error")
@post('/address/utx')
def txids_for_address():
"""Get all (unspent) txids for this address."""
try:
if not request.json:
abort(400, "Invalid request")
data = dict(request.json)
pubkey_raw = data['pubkey']
sig_raw = data['sig']
assert pubkey_raw, "Invalid pubkey"
assert sig_raw, "Invalid sig"
pubkey = ECDSAPublicKey(Hash512.deserialise(pubkey_raw))
sig = Hash512.deserialise(sig_raw)
txids = CHAIN.q.get_txids_for_address(pubkey, sig)
txids_str = [x.serialise() for x in txids]
return {"data": txids_str}
except (AssertionError, KeyError):
abort(400, "Invalid request")
except PermissionError:
abort(403, "Permission denied")
except BadSignatureError:
abort(403, "Permission denied (Bad signature)")
except BlockChainError:
abort(500, "Blockchain error")
@post('/address/utxo')
def utxo_for_address_and_txid():
"""Get UTXO amount for the specified address and txid."""
try:
if not request.json:
abort(400, "Invalid request")
data = dict(request.json)
pubkey_raw = data['pubkey']
txid_raw = data['txid']
sig_raw = data['sig']
assert pubkey_raw, "Invalid pubkey"
assert txid_raw, "Invalid txid"
assert sig_raw, "Invalid sig"
pubkey = ECDSAPublicKey(Hash512.deserialise(pubkey_raw))
txid = Hash512.deserialise(txid_raw)
sig = Hash512.deserialise(sig_raw)
amount = CHAIN.q.get_utxo_private(pubkey, txid, sig)
return {"data": amount}
except (AssertionError, KeyError):
abort(400, "Invalid request")
except PermissionError:
abort(403, "Permission denied")
except BadSignatureError:
abort(403, "Permission denied (Bad signature)")
except BlockChainError:
abort(500, "Blockchain error")
@post('/address/balance')
def utxo_for_address():
"""Get total UTXO amount for the specified address."""
try:
if not request.json:
abort(400, "Invalid request")
data = dict(request.json)
pubkey_raw = data['pubkey']
sig_raw = data['sig']
assert pubkey_raw, "Invalid pubkey"
assert sig_raw, "Invalid sig"
pubkey = ECDSAPublicKey(Hash512.deserialise(pubkey_raw))
sig = Hash512.deserialise(sig_raw)
amount = 0
txids = CHAIN.q.get_txids_for_address(pubkey, sig)
address = pubkey.get_address()
for txid in txids:
amount += CHAIN.q.get_utxo(txid, address)
return {"data": amount}
except (AssertionError, KeyError) as exc:
abort(400, "Invalid request: {}".format(exc))
except PermissionError:
abort(403, "Permission denied")
except BadSignatureError:
abort(403, "Permission denied (Bad signature)")
except BlockChainError:
abort(500, "Blockchain error")
@post('/tx/submit')
def transaction_submit():
"""Submit a new transaction to the pool."""
try:
if not request.json:
abort(400, "Invalid transaction")
tx = Transaction.deserialise_dict(request.json)
tx.validate(CHAIN.q)
CHAIN.q.add_transaction_to_pending(tx)
except TransactionValidationError as exc:
abort(400, "Invalid transaction: {}".format(exc))
except BlockChainError:
abort(500, "Blockchain error")
@post('/block/submit')
def block_submit():
"""Submit a new block to the blockchain."""
try:
if not request.json:
abort(400, "Invalid block")
block = Block.deserialise_dict(request.json)
CHAIN.blockdb.write_new_block(block, CHAIN.q)
# Delete transactions from pending.
for tx in block.transactions:
CHAIN.transdb.delete_transaction(tx.txid)
return"SUCCESS"
except BlockValidationError as exc:
abort(400, "Invalid block: {}".format(exc))
except BlockChainError:
abort(500, "Blockchain error")
@error(500)
def handle_500(details):
"""Error handler for HTTP 500."""
return {'error': 'Unknown error: {}'.format(details)}
if __name__ == '__main__':
cur_dir = os.path.realpath(os.path.dirname(__file__))
base_dir = os.path.join(cur_dir, 'blockdata')
create_flag = False
password = ''
if not os.path.exists(base_dir):
# Blockchain not found. Create a new one.
print("Blockchain not found. A new blockchain will be created.")
try:
while True:
print("\nNOTE: Password will not be displayed while typing.")
password = getpass.getpass(
prompt="Please enter a password for the private key: ")
try:
assert len(password) >= 8, \
"Password must be at least 8 characters long"
password_repeat = getpass.getpass(
prompt="Please re-enter the password again: ")
if password_repeat == password:
break
print("Passwords do not match. Please try again.")
except AssertionError as exc:
print("Invalid password: {}".format(exc))
except KeyboardInterrupt:
print("\nCancelled.")
sys.exit(1)
os.makedirs(base_dir)
create_flag = True
CHAIN = BlockChain(base_dir=base_dir)
if create_flag and password and CHAIN.q.get_num_blocks() == 0:
CHAIN.create(passphrase=password)
print("Blockchain created successfully.")
elif CHAIN.q.get_num_blocks() < 1:
print("Blockchain is empty. Please delete the '{}' directory to "
"create a new one.".format(CHAIN.base_dir))
print("WARNING: Deleting an existing blockchain directory will "
"destroy the blockchain and any outstanding transactions!")
sys.exit(1)
try:
port = int(os.environ.get('BLOCKCHAIN_PORT')) or 5000
except (TypeError, ValueError):
port = 5000
print("Starting server on port {} ...\n".format(port))
run(host='localhost', port=port, debug=True)