Skip to content

Commit 2b28e8a

Browse files
committed
added code sharing logic
1 parent cbd6578 commit 2b28e8a

1 file changed

Lines changed: 9 additions & 19 deletions

File tree

Framework/deploy_handler/long_poll_handler.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ def on_message(self, message) -> bool:
5757
return False
5858

5959
elif message.startswith(b'{"command":"KEY_REQUEST"'):
60-
# Handle key request from server
6160
self.handle_key_request(message)
6261
return False
6362

6463
elif message.startswith(b'{"command":"PRIVATE_KEY"'):
65-
# Handle private key received from server
6664
self.handle_private_key(message)
6765
return False
6866

@@ -85,14 +83,11 @@ def handle_key_request(self, message: bytes) -> None:
8583
request_id = payload["request_id"]
8684
public_key_pem = payload["public_key"]
8785
receiver_node_ids = payload["receiver_node_ids"]
88-
8986
print(f"[key-request] Received request {request_id} to share key with {receiver_node_ids}")
9087

91-
# Find the private key that matches the provided public key
9288
private_key_pem = self.get_private_key_matching_public_key(public_key_pem)
9389

9490
if private_key_pem:
95-
# Respond to the key request immediately - no need for separate distribute call!
9691
self.respond_to_key_request(request_id, private_key_pem)
9792
else:
9893
print("[key-request] ERROR: No private key found matching the provided public key")
@@ -112,7 +107,6 @@ def handle_private_key(self, message: bytes) -> None:
112107

113108
print(f"[private-key] Received key {key_id}")
114109

115-
# Save the private key securely
116110
self.save_private_key(key_id, private_key_pem)
117111

118112
print(f"[private-key] Key {key_id} saved successfully")
@@ -121,10 +115,6 @@ def handle_private_key(self, message: bytes) -> None:
121115
traceback.print_exc()
122116

123117
def get_private_key_matching_public_key(self, public_key_pem: str) -> str | None:
124-
"""
125-
Find and retrieve the private key that corresponds to the provided public key.
126-
Returns the private key in PEM format, or None if no matching key exists.
127-
"""
128118
try:
129119
from settings import ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR
130120
from cryptography.hazmat.primitives import serialization
@@ -176,7 +166,12 @@ def get_private_key_matching_public_key(self, public_key_pem: str) -> str | None
176166
# Compare public keys
177167
if derived_public_key_bytes == target_public_key_bytes:
178168
print(f"[key-request] Found matching private key: {pem_file.name}")
179-
return private_key_bytes.decode('utf-8')
169+
# Return the private key in traditional RSA PRIVATE KEY format
170+
return private_key.private_bytes(
171+
encoding=serialization.Encoding.PEM,
172+
format=serialization.PrivateFormat.TraditionalOpenSSL,
173+
encryption_algorithm=serialization.NoEncryption()
174+
).decode('utf-8')
180175

181176
except Exception as e:
182177
print(f"[key-request] Error reading key file {pem_file.name}: {e}")
@@ -198,27 +193,22 @@ def save_private_key(self, key_id: str, private_key_pem: str) -> None:
198193
try:
199194
from settings import ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR
200195
from cryptography.hazmat.primitives import serialization
201-
from datetime import datetime as dt
202196

203197
key_folder = Path(ZEUZ_NODE_PRIVATE_RSA_KEYS_DIR)
204198
key_folder.mkdir(parents=True, exist_ok=True)
205199

206-
# Validate the private key
207200
private_key = serialization.load_pem_private_key(
208201
private_key_pem.encode('utf-8'),
209202
password=None,
210203
)
211204

212-
# Save with descriptive filename
213-
timestamp = dt.now().strftime("%Y%m%d_%H%M%S")
214-
key_filename = f"received_key_{key_id}_{timestamp}.pem"
205+
key_filename = f"received-{key_id}.pem"
215206
key_path = key_folder / key_filename
216207

217-
# Save the key
218208
with open(key_path, 'wb') as f:
219209
f.write(private_key.private_bytes(
220210
encoding=serialization.Encoding.PEM,
221-
format=serialization.PrivateFormat.PKCS8,
211+
format=serialization.PrivateFormat.TraditionalOpenSSL,
222212
encryption_algorithm=serialization.NoEncryption()
223213
))
224214

@@ -243,7 +233,7 @@ def respond_to_key_request(self, request_id: str, private_key_pem: str) -> None:
243233
response = RequestFormatter.request(
244234
"post",
245235
api_url,
246-
json_data={
236+
json={
247237
"request_id": request_id,
248238
"donor_node_id": node_id,
249239
"private_key": private_key_pem

0 commit comments

Comments
 (0)