Repository navigation
Expand file tree
/
Copy pathnode.py
More file actions
130 lines (115 loc) · 5.11 KB
/
Copy pathnode.py
File metadata and controls
130 lines (115 loc) · 5.11 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
import http.server
import socketserver
import json
import math
import time
import hashlib
import hmac
import os
PORT = 8000
NODE_SECRET_RAW = "solar-depin-secret-node-key-2026"
NODE_PRIVATE_KEY = hashlib.sha256(NODE_SECRET_RAW.encode()).hexdigest()
is_demo_mode = False
demo_sim_hour = 6.0
def get_sun_telemetry():
global demo_sim_hour, is_demo_mode
t = time.localtime()
if is_demo_mode:
demo_sim_hour = (demo_sim_hour + 0.5) % 24.0
hour = demo_sim_hour
time_str = f"{int(hour):02d}:{int((hour%1)*60):02d} (Demo)"
else:
hour = t.tm_hour + (t.tm_min / 60.0)
time_str = f"{t.tm_hour:02d}:{t.tm_min:02d}"
if 6.0 <= hour <= 20.0:
sun_angle = math.sin((hour - 6.0) / 14.0 * math.pi)
base_irradiance = sun_angle * 950.0
cloud_cover = round(10.0 + 5.0 * math.sin(time.time() / 10.0), 1)
irradiance = round(base_irradiance * (1.0 - cloud_cover / 100.0), 2)
current_power_kw = round((irradiance / 1000.0) * 30.0 * 0.8, 2)
else:
irradiance, cloud_cover, current_power_kw = 0.0, 5.0, 0.0
timestamp = int(time.time())
payload_to_sign = f"node_id:solar-node-01|ts:{timestamp}|power:{current_power_kw}|irradiance:{irradiance}"
signature = hmac.new(NODE_PRIVATE_KEY.encode(), payload_to_sign.encode(), hashlib.sha256).hexdigest()
public_node_address = "0x" + hashlib.sha256(NODE_PRIVATE_KEY.encode()).hexdigest()[:40]
return {
"status": "online",
"node_address": public_node_address,
"timestamp": timestamp,
"metrics": {
"time_local": time_str,
"solar_irradiance_wm2": irradiance,
"cloud_cover_pct": cloud_cover,
"current_power_kw": current_power_kw,
"max_capacity_kw": 30.0
},
"proof_of_yield": {
"payload": payload_to_sign,
"signature": signature,
"hash_algorithm": "HMAC-SHA256"
}
}
class SolarNodeHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
global is_demo_mode
if self.path == "/api/data":
payload = json.dumps(get_sun_telemetry(), indent=2).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(payload)
elif self.path == "/api/telegraph":
# Спеціальний фреймворк-ендпоінт для Telegraph Protocol
raw_data = get_sun_telemetry()
telegraph_response = {
"telegraph_version": "1.0",
"miner_address": raw_data["node_address"],
"data_type": "solar_energy_telemetry",
"ground_truth": {
"power_kw": raw_data["metrics"]["current_power_kw"],
"irradiance_wm2": raw_data["metrics"]["solar_irradiance_wm2"]
},
"verifiable_proof": {
"scheme": "HMAC-SHA256",
"payload": raw_data["proof_of_yield"]["payload"],
"signature": raw_data["proof_of_yield"]["signature"]
}
}
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(json.dumps(telegraph_response, indent=2).encode("utf-8"))
elif self.path == "/api/toggle-mode":
is_demo_mode = not is_demo_mode
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"demo_mode": is_demo_mode}).encode("utf-8"))
elif self.path in ["/", "/index.html"]:
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.end_headers()
html_path = "/root/solar-node/index.html"
if os.path.exists(html_path):
with open(html_path, "rb") as f:
self.wfile.write(f.read())
def do_POST(self):
if self.path == "/api/verify":
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
req = json.loads(post_data.decode('utf-8'))
secret_input = req.get('secret', '')
payload = req.get('payload', '')
sig = req.get('signature', '')
derived_key = hashlib.sha256(secret_input.encode()).hexdigest()
calc_sig = hmac.new(derived_key.encode(), payload.encode(), hashlib.sha256).hexdigest()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"valid": (calc_sig == sig)}).encode("utf-8"))
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), SolarNodeHandler) as httpd:
httpd.serve_forever()