-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
165 lines (129 loc) · 6.69 KB
/
Copy pathnode.py
File metadata and controls
165 lines (129 loc) · 6.69 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
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
from hashlib import sha256
from datetime import datetime
from src.crypto import Crypto
import base58
import uuid
import os
import configparser
class Node:
def __init__(self, name : str):
"""
Initializes a new Node instance with a random private key and public key.
"""
# gets public and private key from PEM file
with open("config/key.pem", "rb") as key_file:
self.private_key = Crypto.PEM_to_private_key(key_file.read(), bytes("testpassword",'utf-8'))
self.public_key = self.private_key.public_key()
self.serialized_public_key = base58.b58encode(Crypto.serialize(self.public_key)).decode('utf-8')
self.name = name
self.id = str(uuid.uuid4())
self.vouches = [] # List to hold all vouches received by this Node
self.clock = 0 # Clock value to assign to new vouches
def vouch(self, recipient, vouch_type, comment):
"""
Creates a new vouch message and signs it with this Node's private key.
Args:
recipient (Node): The Node being vouched for.
vouch_type (str): The type of vouch being made (True or False).
comment (str): A comment on the vouch.
Returns:
dict: The new vouch message as a dictionary.
"""
# Create a new vouch message
self.clock += 1 # Increment the clock value
timestamp = datetime.now().timestamp() # Get the current timestamp
vouch_data = f"{self.serialized_public_key}{recipient.serialized_public_key}{vouch_type}{comment}{timestamp}{self.clock}" # Concatenate the vouch data
vouch_hash = sha256(vouch_data.encode()).hexdigest() # Hash the vouch data
vouch = {
"sender": self.serialized_public_key,
"recipient": recipient.serialized_public_key,
"vouch_type": vouch_type,
"comment": comment,
"timestamp": timestamp,
"hash": base58.b58encode(vouch_hash.encode()).decode(),
"clock": self.clock
}
# Sign the vouch message with this Node's private key
signature = Crypto.sign(self.private_key, vouch_hash.encode())
# Add the signature to the vouch message
vouch['signature'] = base58.b58encode(signature).decode()
# Add the vouch to our local vouch list
self.vouches.append(vouch)
# Return the new vouch message
return vouch
def unvouch(self, recipient_public_key, vouch_hash):
"""
Removes a vouch message from this Node's local vouch list.
Args:
recipient_public_key (str): The public key of the Node being vouched for.
vouch_hash (str): The hash of the vouch message to be removed.
Returns:
None.
"""
# Find the existing vouch in our local vouch list and remove it
for i, local_vouch in enumerate(self.vouches):
if base58.b58decode(local_vouch['recipient']).decode() == recipient_public_key and base58.b58decode(local_vouch['hash']).decode() == vouch_hash:
self.vouches.pop(i) # Remove the vouch message from the list
break
def update_vouch_type(self, recipient_public_key, vouch_hash, vouch_type):
"""
Updates an existing vouch message with a new vouch type and signature.
Args:
recipient_public_key (str): The public key of the Node being vouched for.
vouch_hash (str): The hash of the vouch message to be updated.
vouch_type (int): The new vouch_type of the vouch (-1, 0, or 1).
Returns:
dict: The updated vouch message.
"""
# Find the existing vouch in our local vouch list and update it with the new vouch_type and signature
for i, local_vouch in enumerate(self.vouches):
if local_vouch['recipient'] == recipient_public_key and local_vouch['hash'] == vouch_hash:
# Create the new vouch data string
vouch_data = f"{self.public_key}{self.serialized_public_key}{local_vouch['vouch_type']}{local_vouch['message']}{local_vouch['comment']}{local_vouch['timestamp']}{local_vouch['clock']}"
# Update the hash, vouch_type, and clock values in the vouch message
local_vouch['hash'] = sha256(vouch_data.encode()).hexdigest()
local_vouch['vouch_type'] = vouch_type
local_vouch['clock'] += 1
# Sign the updated vouch message with this Node's private key
signature = Crypto.sign(self.private_key, local_vouch['hash'].encode())
# Add the signature to the vouch message
local_vouch['signature'] = base58.b58encode(signature).decode()
# Update the vouch in our local vouch list
self.vouches[i] = local_vouch
# Return the updated vouch message
return local_vouch
def update_vouch_comment(self, recipient_public_key, vouch_hash, comment):
"""
Updates an existing vouch message with a new comment.
Args:
recipient_public_key (str): The public key of the Node being vouched for.
vouch_hash (str): The hash of the vouch message to be updated.
comment (str): The new comment to be added to the vouch message.
Returns:
dict: The updated vouch message.
"""
# Find the existing vouch in our local vouch list and update it with the new comment
for i, local_vouch in enumerate(self.vouches):
if local_vouch['recipient'] == recipient_public_key and local_vouch['hash'] == vouch_hash:
# Update the comment value in the vouch message
local_vouch['comment'] = comment
# Update the vouch in our local vouch list
self.vouches[i] = local_vouch
# Return the updated vouch message
return local_vouch
def get_vouches(self, recipient_public_key=None):
"""
Gets all vouch messages received by this Node, optionally filtered by recipient.
Args:
recipient_public_key (str): The public key of the Node being vouched for. Optional.
Returns:
list: A list of vouch messages as dictionaries.
"""
if recipient_public_key is None:
# Return all vouches
return self.vouches
else:
# Convert the recipient's public key to base58
recipient_public_key_base58 = base58.b58encode(recipient_public_key.encode()).decode()
# Return vouches for a specific recipient
return [vouch for vouch in self.vouches if vouch['recipient'] == recipient_public_key_base58]