-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
314 lines (262 loc) · 9.01 KB
/
Copy pathcli.py
File metadata and controls
314 lines (262 loc) · 9.01 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
"""
PGDN Publisher CLI
A simplified CLI for publishing DePIN scan results to blockchain ledgers and reports.
Returns only JSON output.
"""
import json
import sys
import argparse
import os
from typing import Dict, Any
def load_env():
"""Load environment variables from .env file if it exists."""
try:
with open('.env', 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key] = value
except FileNotFoundError:
pass # .env file is optional
try:
from pgdn_publisher import (
publish_to_ledger,
publish_report,
LedgerPublisher,
ReportPublisher,
PublisherConfig
)
except ImportError:
print(json.dumps({
"success": False,
"error": "pgdn_publisher library not found. Please install the library first."
}))
sys.exit(1)
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="PGDN Publisher - Blockchain ledger and report publishing CLI",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Publish scan to zkSync ledger
pgdn-publisher ledger --network zksync --scan-data '{"host_uid": "validator_123", "trust_score": 85, "summary_hash": "0x..."}'
# Publish scan to SUI ledger
pgdn-publisher ledger --network sui --scan-data '{"host_uid": "validator_123", "trust_score": 85, "summary_hash": "abc123"}'
# Publish scan report
pgdn-publisher report --scan-data '{"scan_id": 123, "trust_score": 85}'
# Check SUI ledger status
pgdn-publisher status --network sui
# Retrieve report from Walrus
pgdn-publisher retrieve --walrus-hash "abc123def456"
"""
)
# Global configuration options
parser.add_argument(
'--config-file',
help='Path to configuration file (JSON format)'
)
parser.add_argument(
'--network',
choices=['zksync', 'sui'],
help='Blockchain network to use (overrides PGDN_NETWORK env var)'
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Ledger publishing command
ledger_parser = subparsers.add_parser('ledger', help='Publish scan to blockchain ledger')
ledger_parser.add_argument(
'--scan-data',
required=True,
help='Scan data as JSON string (must include summary_hash)'
)
ledger_parser.add_argument(
'--no-wait',
action='store_true',
help='Do not wait for transaction confirmation'
)
# Report publishing command
report_parser = subparsers.add_parser('report', help='Publish scan report')
report_parser.add_argument(
'--scan-data',
required=True,
help='Scan data as JSON string'
)
report_parser.add_argument(
'--destinations',
nargs='+',
choices=['walrus', 'local_file'],
default=['walrus', 'local_file'],
help='Publishing destinations (default: walrus local_file)'
)
# Status command
subparsers.add_parser('status', help='Check ledger connection status')
# Retrieve command
retrieve_parser = subparsers.add_parser('retrieve', help='Retrieve report from Walrus')
retrieve_parser.add_argument(
'--walrus-hash',
required=True,
help='Walrus hash of the report to retrieve'
)
return parser.parse_args()
def load_scan_data(scan_data_str: str) -> Dict[str, Any]:
"""Load and validate scan data from JSON string."""
try:
return json.loads(scan_data_str)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in scan data: {e}")
def handle_ledger_command(args, config: PublisherConfig) -> Dict[str, Any]:
"""Handle ledger publishing command."""
try:
scan_data = load_scan_data(args.scan_data)
# Validate that summary_hash is present
if 'summary_hash' not in scan_data:
return {
"success": False,
"command": "ledger",
"error": "summary_hash is required in scan data"
}
# Set wait for confirmation based on flag
wait_for_confirmation = not args.no_wait
# Create publisher and publish
publisher = LedgerPublisher(config)
result = publisher.publish(scan_data, wait_for_confirmation=wait_for_confirmation)
return {
"success": True,
"command": "ledger",
"result": result
}
except Exception as e:
return {
"success": False,
"command": "ledger",
"error": str(e)
}
def handle_report_command(args, config: PublisherConfig) -> Dict[str, Any]:
"""Handle report publishing command."""
try:
scan_data = load_scan_data(args.scan_data)
# Publish report
results = publish_report(
scan_data,
destinations=args.destinations,
config=config
)
# Convert PublishResult objects to dicts for JSON serialization
serializable_results = {}
for dest, result in results.items():
serializable_results[dest] = {
"success": result.success,
"destination": result.destination,
"identifier": result.identifier,
"error": result.error
}
# Determine overall success
overall_success = any(r.success for r in results.values())
return {
"success": overall_success,
"command": "report",
"results": serializable_results,
"destinations": args.destinations
}
except Exception as e:
return {
"success": False,
"command": "report",
"error": str(e)
}
def handle_status_command(config: PublisherConfig) -> Dict[str, Any]:
"""Handle status command."""
try:
publisher = LedgerPublisher(config)
status = publisher.get_status()
return {
"success": True,
"command": "status",
"status": status
}
except Exception as e:
return {
"success": False,
"command": "status",
"error": str(e)
}
def handle_retrieve_command(args, config: PublisherConfig) -> Dict[str, Any]:
"""Handle retrieve command."""
try:
publisher = ReportPublisher(config)
report = publisher.retrieve_from_walrus(args.walrus_hash)
if report:
return {
"success": True,
"command": "retrieve",
"walrus_hash": args.walrus_hash,
"report": report
}
else:
return {
"success": False,
"command": "retrieve",
"error": "Report not found or could not be retrieved"
}
except Exception as e:
return {
"success": False,
"command": "retrieve",
"error": str(e)
}
def main():
"""Main CLI entry point."""
try:
# Load .env file first
load_env()
args = parse_arguments()
if not args.command:
print(json.dumps({
"success": False,
"error": "No command specified. Use --help for usage information."
}))
sys.exit(1)
# Load configuration
try:
config = PublisherConfig.from_env(network=args.network)
except Exception as e:
print(json.dumps({
"success": False,
"error": f"Configuration error: {e}"
}))
sys.exit(1)
# Route to command handlers
if args.command == 'ledger':
result = handle_ledger_command(args, config)
elif args.command == 'report':
result = handle_report_command(args, config)
elif args.command == 'status':
result = handle_status_command(config)
elif args.command == 'retrieve':
result = handle_retrieve_command(args, config)
else:
result = {
"success": False,
"error": f"Unknown command: {args.command}"
}
# Output result as JSON
print(json.dumps(result, indent=2))
# Exit with appropriate code
if not result.get('success', False):
sys.exit(1)
except KeyboardInterrupt:
print(json.dumps({
"success": False,
"error": "Operation cancelled by user"
}))
sys.exit(1)
except Exception as e:
print(json.dumps({
"success": False,
"error": f"Unexpected error: {str(e)}"
}))
sys.exit(1)
if __name__ == "__main__":
main()