-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.py
125 lines (103 loc) · 4.01 KB
/
sign.py
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
import json
import logging
from datetime import datetime, timezone
import requests
from bittensor import wallet as btcli_wallet
from substrateinterface import Keypair
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger()
# Define color codes
CYAN = "\033[96m"
GREEN = "\033[92m"
RED = "\033[91m"
RESET = "\033[0m"
# Prompt the user for wallet details
wallet_name = input(f"{CYAN}Enter wallet name (default: Coldkey): {RESET}") or "Coldkey"
wallet_path = input(f"{CYAN}Enter wallet path (default: ~/.bittensor/wallets/): {RESET}") or "~/.bittensor/wallets/"
logger.info(f"\nUsing wallet name: {wallet_name}")
logger.info(f"Using wallet path: {wallet_path}")
# Load your wallet
my_wallet = btcli_wallet(name=wallet_name, path=wallet_path)
# Prompt for the message details to create the JSON
print(f"{CYAN}Enter the details for the vote message:{RESET}")
subnets: list[dict[str, int | float]] = []
# Example:
# subnets = [
# {"subnet": 1, "percent": 50.0},
# {"subnet": 2, "percent": 50.0},
# ]
while True:
subnet_id = input(f"{CYAN}Enter subnet ID (or press Enter to finish): {RESET}")
if not subnet_id:
break
try:
subnet_id = int(subnet_id)
except ValueError:
print(f"{RED}Invalid subnet ID. Please enter an integer.{RESET}")
continue
percent = input(f"{CYAN}Enter percentage for subnet {subnet_id}: {RESET}")
try:
percent = float(percent)
except ValueError:
print(f"{RED}Invalid percentage. Please enter a number.{RESET}")
continue
subnets.append({"subnet": subnet_id, "percent": percent})
if not subnets:
print(f"{RED}No subnets entered. Exiting.{RESET}")
exit(1)
vote_message: str = json.dumps(
{
"timestamp": datetime.now(timezone.utc).isoformat(),
"weights": subnets,
},
indent=None,
)
print(f"{GREEN}Generated vote message:{RESET}")
print(vote_message)
# Ensure valid JSON
try:
json_object = json.loads(vote_message)
logger.info("Vote message JSON is valid.")
except json.JSONDecodeError as e:
logger.error(f"\nInvalid JSON input: {e}")
exit(1)
# Load your private key (coldkey)
try:
coldkey = my_wallet.get_coldkey()
logger.info("Coldkey loaded successfully.")
except Exception as e:
logger.error(f"Error loading coldkey: {e}")
exit(1)
# Sign the message
signature = coldkey.sign(vote_message.encode("utf-8")).hex()
logger.info("Message signed successfully.")
print(f"{GREEN}Signature: {signature}{RESET}")
# Extract the public key from the coldkey
public_key = coldkey.public_key
# Create a new keypair from the public key
keypair_from_public_key = Keypair(ss58_address=coldkey.ss58_address)
# Verify the signature
is_valid = keypair_from_public_key.verify(vote_message.encode("utf-8"), bytes.fromhex(signature))
logger.info(f"Signature verification result: {is_valid}")
print(f"{GREEN if is_valid else RED}Signature valid: {is_valid}{RESET}")
# Prepare the data to send to the server
vote_data = {"vote_message": vote_message, "signature": signature, "hotkey_ss58": coldkey.ss58_address}
# Debug print to ensure correct payload
print(f"{GREEN}Vote data to send:{RESET}")
print(json.dumps(vote_data, indent=4))
# Send the data to the server endpoint
url = "https://api.minersunion.ai/votes/process_vote/"
headers = {'Content-Type': 'application/json'}
try:
response = requests.post(url, headers=headers, json=vote_data)
response.raise_for_status() # Raise an exception for HTTP errors
print(f"{GREEN}Vote data has been successfully sent to the server.{RESET}")
print(f"Server response: {response.json()}")
except requests.exceptions.RequestException as e:
logger.error(f"Error sending data to server: {e}")
print(f"{RED}Failed to send vote data to the server.{RESET}")
# Save the data to a file (optional, if you still want to keep the local file)
with open("vote_data.json", "w") as vote_file:
json.dump(vote_data, vote_file)
print(f"{GREEN}Vote data has been saved to vote_data.json{RESET}")