forked from zack-bitcoin/basiccoin
/
gui.py
84 lines (76 loc) · 3.12 KB
/
gui.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
import networking, copy, tools, pt, os, blockchain, custom, http
# the easiest way to understand this file is to try it out and have a look at
# the html it creates. It creates a very simple page that allows you to spend
# money.
def spend(amount, pubkey, privkey, to_pubkey, DB):
amount = int(amount * (10 ** 5))
tx = {'type':'spend', 'id':pubkey, 'amount':amount, 'to':to_pubkey}
easy_add_transaction(tx, privkey, DB)
def easy_add_transaction(tx_orig, privkey, DB):
tx = copy.deepcopy(tx_orig)
pubkey = pt.privtopub(privkey)
try:
tx['count'] = blockchain.count(pubkey, DB)
except:
tx['count'] = 1
tx['signature'] = pt.ecdsa_sign(tools.det_hash(tx), privkey)
blockchain.add_tx(tx, DB)
submit_form = '''
<form name="first" action="{}" method="{}">
<input type="submit" value="{}">{}
</form> {}
'''
empty_page = '<html><body>{}</body></html>'
def easyForm(link, button_says, moreHtml = '', typee = 'post'):
a = submit_form.format(link, '{}', button_says, moreHtml, "{}")
if typee == 'get':
return a.format('get', '{}')
else:
return a.format('post', '{}')
linkHome = easyForm('/', 'HOME', '', 'get')
def page1(DB, brainwallet = custom.brainwallet):
out = empty_page
txt = '<input type="text" name="BrainWallet" value="{}">'
out = out.format(easyForm('/home', 'Play Go!', txt.format(brainwallet)))
return out.format('')
def home(DB, dic):
if 'BrainWallet' in dic:
dic['privkey'] = pt.sha256(dic['BrainWallet'])
elif 'privkey' not in dic:
return "<p>You didn't type in your brain wallet.</p>"
privkey = dic['privkey']
pubkey = pt.privtopub(dic['privkey'])
if 'do' in dic.keys():
if dic['do'] == 'spend':
spend(float(dic['amount']), pubkey, privkey, dic['to'], DB)
out = empty_page
out = out.format('<p>your address: ' + str(tools.pub2addr(pubkey)) + '</p>{}')
out = out.format('<p>current block: ' + str(DB['length']) + '</p>{}')
try:
balance = blockchain.db_get(pubkey, DB)
balance = balance['amount']
except:
balance = 0
for tx in DB['txs']:
if tx['type'] == 'spend' and tx['to'] == tools.pub2addr(pubkey):
balance += tx['amount']
if tx['type'] == 'spend' and tx['id'] == pubkey:
balance -= tx['amount']
out = out.format('<p>current balance is: ' + str(balance / 100000.0) + '</p>{}')
if balance > 0:
out = out.format(easyForm('/home', 'spend money', '''
<input type="hidden" name="do" value="spend">
<input type="text" name="to" value="address to give to">
<input type="text" name="amount" value="amount to spend">
<input type="hidden" name="privkey" value="{}">'''.format(privkey)))
txt = ''' <input type="hidden" name="privkey" value="{}">'''
s = easyForm('/home', 'Refresh', txt.format(privkey))
return out.format(s)
def hex2htmlPicture(string, size):
txt = '<img height="{}" src="data:image/png;base64,{}">{}'
return txt.format(str(size), string, '{}')
def main(port, brain_wallet, db):
global DB
DB = db
ip = ''
http.server(DB, port, page1, home)