-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlagrange-zk-ecdsa-generator.py
71 lines (58 loc) · 2.47 KB
/
lagrange-zk-ecdsa-generator.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
import subprocess
import re
import os
from ecdsa import SigningKey, SECP256k1
# remove the existing keystore if it exists
if os.path.exists("lagrange-zk/avs"):
os.remove("lagrange-zk/avs")
#nothing
# Sanity checks
# Check if .env already contains "x coordinate" and LAGRANGE_OPERATOR_KEYSTORE_PW
with open(".env", "r") as file:
if "ECDSA_X" in file.read():
print("Error: .env file already contains x coordinate. Please delete LAGRANGE_ECDSA_SK,ECDSA_X,ECDSA_Y from the .env")
exit(1)
file.seek(0) # Reset file pointer to the beginning
if "LAGRANGE_OPERATOR_KEYSTORE_PW" in file.read():
file.seek(0) # Reset file pointer to the beginning
for line in file:
if line.startswith("LAGRANGE_OPERATOR_KEYSTORE_PW"):
keystore_pw = line.split("=")[1].strip()
break
else:
print("Error: LAGRANGE_OPERATOR_KEYSTORE_PW not found in .env file.")
exit(1)
else:
print("Error: LAGRANGE_OPERATOR_KEYSTORE_PW not found in .env file.")
exit(1)
# Execute the command to retrieve the private key
command = "cast wallet new"
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
# Parse the output to extract the private key
private_key_match = re.search(r"Private key: (0x[0-9a-fA-F]+)", result.stdout)
if private_key_match:
private_key_hex = private_key_match.group(1)
else:
print("Error: Private key not found in the command output.")
exit(1)
# Convert the private key from hexadecimal to bytes
private_key_bytes = bytes.fromhex(private_key_hex[2:]) # Exclude the '0x' prefix and convert to bytes
# Create the signing key object
signing_key = SigningKey.from_string(private_key_bytes, curve=SECP256k1)
# Get the corresponding public key
public_key = signing_key.verifying_key
# Extract the x and y coordinates
x = public_key.pubkey.point.x()
y = public_key.pubkey.point.y()
# Open a file in append mode and write the coordinates to it
with open(".env", "a") as file:
file.write(f"\n")
file.write(f"LAGRANGE_ECDSA_SK={private_key_hex}")
file.write(f"\n")
file.write(f"ECDSA_X={x}")
file.write(f"\n")
file.write(f"ECDSA_Y={y}")
print("Please back up this ECDSA SK:")
print("LAGRANGE_ECDSA_SK", private_key_hex)
command = f"cast wallet import -k lagrange-zk --private-key={private_key_hex} avs --unsafe-password={keystore_pw}"
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)