-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
executable file
·110 lines (102 loc) · 3.13 KB
/
client.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
from sm3 import sm3
from sm2 import keygen
from utils import db_operate, get_balance, create_tx, parse_tx, mining
import getpass
info = {}
def login():
print '=== Welcome to C-moon coin system ==='
print 'login or register new account:'
print '1.login'
print '2.register'
print '3.exit'
choice = raw_input('choose:')
if choice == '1':
username = raw_input('Username:')
result = db_operate(choice=2, username=username)
if result == True:
print "The name does not exist!"
return False
password = getpass.getpass()
if sm3(password) != result[0]:
print 'Wrong password!'
return False
else:
info['username'] = username
info['pk'] = result[1]
info['sk'] = result[2]
return True
elif choice == '2':
username = raw_input('Username:')
result = db_operate(choice=2, username=username)
if result != True:
print "The name has been registered!"
return False
password = getpass.getpass()
pk, sk = keygen()
db_operate(choice=4, username=username,
password=sm3(password), key=[pk, sk])
print 'register successfully!\nyour pk:%s \nyour sk:%s' % (pk, sk)
info['username'] = username
info['pk'] = pk
info['sk'] = sk
return True
elif choice == '3':
exit()
else:
print 'Wrong option!'
return False
def wallet():
print '=== Wallet interface ==='
print 'choose an option:'
print '1.create a tx'
print '2.start mining'
print '3.get balance'
print '4.exit'
choice = raw_input('choose:')
if choice == '1':
dst_username = raw_input('Please input the other name:')
result = db_operate(choice=2, username=dst_username)
if result == True:
print '[!] User does not exist...'
return False
balance = get_balance(info['username'])
try:
value = int(raw_input('Please input the transfer value:'))
except Exception as e:
print '[!] Input format error...'
return False
if value > balance:
print '[!] You do not have this money...'
return False
src_pk = info['pk']
dst_pk = db_operate(choice=8, username=dst_username)
create_tx(src_pk=src_pk, dst_pk=dst_pk,
value=value, src_sk=info['sk'])
return True
elif choice == '2':
txs = []
tx1 = db_operate(choice=1)
if tx1 == None:
print '[!] No tx now...'
return False
txs.append(parse_tx(tx1.decode('hex')).get_tx())
mining(txs, info['pk'])
return True
elif choice == '3':
print '[*] Your balance:', get_balance(info['username'])
elif choice == '4':
exit()
else:
return False
if __name__ == '__main__':
try:
while True:
if login():
break
while True:
wallet()
except KeyboardInterrupt:
exit()