-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.py
More file actions
47 lines (41 loc) · 1.33 KB
/
wallet.py
File metadata and controls
47 lines (41 loc) · 1.33 KB
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
'''
Wallet that can make transactions in the blockchain.
Classes:
Wallet
'''
import utils
class Wallet:
'''
Single coin wallet in the Blockchain.
Args:
username (str): username of the wallet.
password (str): password to make transactions.
Attributes:
username (str): username of the wallet.
public_key (bytes): public key used in the encryption.
'''
def __init__(self, username: str, password: str):
if not username:
self.username = ''
self.public_key = utils.generate_key_pair('','', False)
elif not username.isalnum(): # username has to be alphanumeric
raise ValueError('Username must only contain alphanumeric values.')
else:
self.username = username
self.public_key = utils.generate_key_pair(username, password)
self.balance = 0
def calculate_hash(self):
'''
Gets hash of the wallet.
Hash is calculated with concatenation of:
username+ public key.
Returns:
hash (bytes): hash of the transaction in bytes.
'''
return utils.sha256(
bytes(self.username, 'utf-8'),
self.public_key.public_bytes(
utils.DER_ENCODING,
utils.PUBLIC_KEY_FORMAT
)
)