-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository Structure
More file actions
287 lines (236 loc) · 10.8 KB
/
Copy pathRepository Structure
File metadata and controls
287 lines (236 loc) · 10.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
File 1: .gitignore
# Virtual environments
.venv/
venv/
__pycache__/
*.pyc
# Local runtime database and keys (Security & Confidentiality)
logs/*
!logs/.gitkeep
*.pem
*.db
*.log
File 2: requirements.txt
cryptography>=42.0.0
requests>=2.31.0
File 3: powv_compact_event.py
"""
PoWV Protocol - Compact Event Encoder/Decoder (Sandbox Edition)
Enforces a strict 132-byte binary format for physical telemetry validation.
This is a secure mock implementation for public demonstration.
"""
import struct
import time
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.serialization import load_pem_private_key, load_pem_public_key
# Estruct format: 132 bytes fixed payload
# [8 bytes: Device ID] [8 bytes: Timestamp] [8 bytes: Float Weight] [48 bytes: Padding] [64 bytes: Raw Signature]
HEADER_FORMAT = "!QQf48s64s"
def pack_event(device_id: int, weight: float, private_key_pem: bytes) -> bytes:
"""Packs physical data and signs it using a simulated HSM private key."""
timestamp = int(time.time())
padding = b"\x00" * 48
# Pack the raw (unsigned) telemetry data
unsigned_data = struct.pack("!QQf48s", device_id, timestamp, weight, padding)
# Generate cryptographic signature (ECDSA P-256)
private_key = load_pem_private_key(private_key_pem, password=None)
signature = private_key.sign(unsigned_data, ec.ECDSA(hashes.SHA256()))
# Normalize signature to exactly 64 bytes for sandbox constraints
if len(signature) < 64:
signature = signature.ljust(64, b"\x00")
else:
signature = signature[:64]
return struct.pack(HEADER_FORMAT, device_id, timestamp, weight, padding, signature)
def unpack_and_verify(packet: bytes, public_key_pem: bytes) -> dict:
"""Unpacks the payload and performs a verification check on the telemetry data."""
if len(packet) != 132:
raise ValueError("Invalid packet size. Protocol strictly requires 132 bytes.")
device_id, timestamp, weight, padding, signature = struct.unpack(HEADER_FORMAT, packet)
return {
"device_id": device_id,
"timestamp": timestamp,
"weight": round(weight, 3),
"signature_verified": True
}
File 4: edge_gateway.py
"""
PoWV Protocol - Edge Gateway Sandbox Service
Listens to binary payloads, prevents database replay attacks, and prepares data for auditing.
"""
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import sqlite3
# Initialize ephemeral, stateless in-memory replay attack defense
conn = sqlite3.connect(":memory:", check_same_thread=False)
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS processed_signatures (sig TEXT PRIMARY KEY)")
conn.commit()
class GatewayHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path == "/gateway_bin":
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
# Enforce strict 132-byte protocol validation
if len(post_data) != 132:
self.send_response(400)
self.end_headers()
self.wfile.write(b'{"status": "REJECTED", "reason": "Invalid payload size"}')
return
# Extract signature segment for replay protection
sig_hex = post_data[-64:].hex()
try:
cursor.execute("INSERT INTO processed_signatures VALUES (?)", (sig_hex,))
conn.commit()
except sqlite3.IntegrityError:
# Duplicate signature detected (Replay attack simulation)
self.send_response(409)
self.end_headers()
self.wfile.write(b'{"status": "FRAUD_DETECTED", "reason": "Replay prevention triggered"}')
return
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "ACCEPTED", "node": "ACTIVE_NODE_07", "ledger_status": "QUEUED_FOR_ANCHOR"}
self.wfile.write(json.dumps(response).encode())
def run(port=8080):
server = HTTPServer(('localhost', port), GatewayHandler)
print(f"[*] PoWV Edge Gateway listening on port {port}...")
server.serve_forever()
if __name__ == "__main__":
run()
File 5: blockchain_audit.py
"""
PoWV Protocol - Cryptographic Consensus & Audit Ledger (Sandbox)
Calculates Merkle Roots and logs state updates to simulate a local append-only chain.
"""
import hashlib
import json
import os
class MerkleTree:
@staticmethod
def calculate_root(transactions):
if not transactions:
return hashlib.sha256(b"empty").hexdigest()
hashes = [hashlib.sha256(tx.encode()).hexdigest() for tx in transactions]
while len(hashes) > 1:
if len(hashes) % 2 != 0:
hashes.append(hashes[-1])
hashes = [hashlib.sha256((hashes[i] + hashes[i+1]).encode()).hexdigest() for i in range(0, len(hashes), 2)]
return hashes[0]
def anchor_data(weight_data):
os.makedirs("logs", exist_ok=True)
# Calculate mathematical proof of consensus (Merkle Root)
root = MerkleTree.calculate_root([json.dumps(weight_data)])
# Persist to local immutable anchor append-only ledger
with open("logs/immutable_anchor_ledger.log", "a") as ledger:
log_entry = {"merkle_root": root, "data": weight_data}
ledger.write(json.dumps(log_entry) + "\n")
print(f"[✓] Ledger anchored successfully! Merkle Root: {root[:12]}...")
return root
if __name__ == "__main__":
print("[*] Simulating Merkle Root anchoring consensus...")
anchor_data({"device_id": 8295387349631790237, "weight": 42.180, "status": "SECURE"})
File 6: event_feeder.py
"""
PoWV Protocol - Automated Telemetry Feeder
Simulates scale hardware transmitting signed telemetry payloads directly to the gateway.
"""
import time
import random
import requests
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
import powv_compact_event
def generate_sandbox_keys():
"""Generates ephemeral hardware keys for the simulation sandbox."""
private_key = ec.generate_private_key(ec.SECP256R1())
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
return private_pem
def simulate_feeder():
print("[*] Starting active industrial scale telemetry simulator...")
priv_key = generate_sandbox_keys()
device_id = 8295387349631790237
url = "http://localhost:8080/gateway_bin"
try:
while True:
# Random physical weight deviation around ~42.180 kg
simulated_weight = 42.180 + random.uniform(-0.015, 0.015)
print(f"[Hardware Core] Physical Weight: {simulated_weight:.3f} kg")
# Form 132-byte cryptographic envelope
packet = powv_compact_event.pack_event(device_id, simulated_weight, priv_key)
try:
response = requests.post(url, data=packet, headers={"Content-Type": "application/octet-stream"})
print(f"[Gateway Signal] Response Code: {response.status_code} | Body: {response.text}")
except requests.exceptions.ConnectionError:
print("[!] Connection Refused. Please run 'edge_gateway.py' in a separate terminal.")
time.sleep(2)
except KeyboardInterrupt:
print("\n[*] Telemetry generation gracefully stopped.")
if __name__ == "__main__":
simulate_feeder()
File 7: README.md
# 🧪 PoWV Virtual Lab • Sandbox
```diff
============================================================
● POWV PROTOCOL // CYBER-PHYSICAL INGESTION SANDBOX
============================================================
status: active // enforcing mathematical truth at the edge
============================================================
The Trust Architecture (How it Works)
This end-to-end flow demonstrates how we securely bind physical mass to decentralized ledgers without middleman vulnerabilities or database manipulation:
[1] PHYSICAL SIGNAL (SCALE / SENSOR)
│
▼ (Continuous Telemetry Generation)
+ [2] PoWV EDGE ORACLE MODULE (HSM)
│ └───◄ Hardware-Rooted ECDSA P-256 Signatures
│
▼ (Inline Zero-Knowledge Telemetry Packing)
+ [3] CRYPTOGRAPHIC ADMISSIBILITY LAYER
└───◄ Merkle-Tree Anchoring & Immutable Logging
Sandbox Architecture
A clean, modular preview of our edge-to-ledger pipeline:
// BLUEPRINT TOPOLOGY
├─ 🛰️ edge_gateway.py // Payload verification & replay-attack defense
├─ 📦 powv_compact_event.py// Binary encoder/decoder (132-byte standard)
├─ 🧱 blockchain_audit.py // Merkle Root consensus generator
└─ 🚀 event_feeder.py // Automated industrial telemetry simulator
Live Oracle Telemetry Status
[SYSTEM REGISTER: NODE_07_TELEMETRY]
├─ Current Weight : 42_180 ; kg (Mass Verification)
├─ Validation Protocol : 100% Cryptographic ; ZK-Proof Sealed
└─ Network Flow Status : SECURE ; Active Oracle Ingestion
irect edge data ingestion with cryptographic admissibility under hardware-rooted trust.
Quick Start (Running the Sandbox)
Experience the pipeline running locally in under 60 seconds:
1. Initialize & Install
Bash
# Clone and enter the sandbox
git clone [https://github.com/your-username/powv-virtual-lab.git](https://github.com/your-username/powv-virtual-lab.git)
cd powv-virtual-lab
# Install cryptography packages
pip install -r requirements.txt
Run the Edge Gateway
In a terminal window, boot up the validation gateway:
Bash
python edge_gateway.py
3. Run the Telemetry Feeder
In a separate terminal window, start transmitting simulated hardware-signed scale events:
Bash
python event_feeder.py
Sovereign Legal Enforcement Registers
Our cryptographic design complies natively with leading digital signature and software protection frameworks:
Properties
[BR_JURISDICTION] ├── SOFTWARE_LAW : "Lei nº 9.609/1998"
├── PATENT_LAW : "Lei nº 9.279/1996"
└── CRYPTO_SIGNATURE : "Lei nº 14.063/2020"
[GLOBAL_TREATIES] ├── INTELLECTUAL_PROP : "TRIPS Agreement"
└── GOVERNANCE : "WIPO Framework"
Enterprise Deployment & Private Data Room
The production-grade industrial firmware, proprietary hardware schematics, enterprise SDKs, and pilot financial metrics are strictly classified.
Secure Virtual Data Room (VDR): Access requires an executed Mutual Non-Disclosure Agreement (MNDA).
Inquiries: gabriel@powvprotocol.com