Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 64fdfc8

Browse files
authoredJul 9, 2024
Uploaded Helper Scripts
These three files comprise the two-step installation process, with init.sh installing all necessary deps and the py script deals with key gen and insertion. Signed-off-by: Christian Kessler <[email protected]>
1 parent 6af392e commit 64fdfc8

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
 

‎bootnodes.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"/ip4/52.8.152.227/tcp/30333/p2p/12D3KooWErWiPk2XDWU6oai4fhjsQxBduyNeMxbbeApVk8ew6FpZ",
2+
"/ip4/50.18.213.115/tcp/30334/p2p/12D3KooWPKWmKxEq2diEo5pMK1RoeN3FPTDVJbNEZNmpm5EqfMAS",
3+
"/ip4/52.53.68.125/tcp/30335/p2p/12D3KooWBzS3ubHkRKDz2SLcst6XP8hrbCe9e9C2N6B7jRPT19uL",
4+
"/ip4/18.144.114.190/tcp/30336/p2p/12D3KooWNA89CAx2j6HWtx4n1jgr82sbg8xV95ALPEB2mruLjnhD"
5+

‎build-keygen.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import subprocess
2+
import sys
3+
import shutil
4+
5+
def check_command(command):
6+
"""Check if a command is available on the system."""
7+
if shutil.which(command) is None:
8+
print(f"Error: {command} is not installed or not found in PATH.")
9+
sys.exit(1)
10+
11+
def run_command(command, input_text=None, shell=False):
12+
"""Run a system command and handle errors."""
13+
try:
14+
result = subprocess.run(command, input=input_text, text=True, capture_output=True, check=True, shell=shell)
15+
print(f"Successfully ran: {' '.join(command) if not shell else command}")
16+
return result.stdout.strip()
17+
except subprocess.CalledProcessError as e:
18+
print(f"Error running command: {' '.join(command) if not shell else command}: {e}")
19+
sys.exit(1)
20+
21+
def build_project():
22+
"""Build the project in release mode."""
23+
print("Building the project in release mode...")
24+
run_command(["cargo", "build", "--release"])
25+
26+
def generate_keys(password, output_file):
27+
"""Generate keys and save them to a file with schema labels."""
28+
print("Generating keys...")
29+
schemes = ["sr25519", "sr25519", "sr25519", "ed25519"]
30+
with open(output_file, "w") as f:
31+
for scheme in schemes:
32+
key_output = run_command(["./target/release/argochain", "key", "generate", "--scheme", scheme, "--password-interactive"], input_text=password + '\n')
33+
secret_seed_line = next((line for line in key_output.split('\n') if line.startswith("secret seed")), None)
34+
if secret_seed_line:
35+
f.write(f"{scheme}: {secret_seed_line}\n")
36+
37+
def insert_keys(password, keys_file):
38+
"""Insert keys from the file, ensuring unique keys."""
39+
print("Inserting keys...")
40+
key_types = [
41+
("sr25519", "babe"),
42+
("ed25519", "gran"),
43+
("sr25519", "imon"),
44+
("sr25519", "audi")
45+
]
46+
47+
with open(keys_file, "r") as f:
48+
keys = f.read().splitlines()
49+
50+
used_keys = set()
51+
for scheme, key_type in key_types:
52+
for line in keys:
53+
if line.startswith(f"{scheme}:"):
54+
key = next((part for part in line.split() if part.startswith("0x")), None)
55+
if key and key not in used_keys:
56+
used_keys.add(key)
57+
print(f"Inserting key with scheme {scheme} and key type {key_type}")
58+
run_command(["./target/release/argochain", "key", "insert", "--scheme", scheme, "--password-interactive", "--key-type", key_type], input_text=password + '\n')
59+
break
60+
61+
def main():
62+
# Check if necessary commands are available
63+
check_command("cargo")
64+
check_command("python3")
65+
66+
password = "5683"
67+
output_file = "keys.txt"
68+
69+
# Build the project
70+
build_project()
71+
72+
# Generate keys and insert them, do this twice
73+
for _ in range(2):
74+
generate_keys(password, output_file)
75+
insert_keys(password, output_file)
76+
77+
if __name__ == "__main__":
78+
main()

‎init.sh

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/bin/bash
2+
3+
# Function to print messages
4+
print_message() {
5+
MESSAGE=$1
6+
echo "${MESSAGE}"
7+
}
8+
9+
# Function to check the OS
10+
check_os() {
11+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
12+
OS="linux"
13+
elif [[ "$OSTYPE" == "darwin"* ]]; then
14+
OS="macos"
15+
else
16+
print_message "Unsupported OS: $OSTYPE"
17+
exit 1
18+
fi
19+
print_message "Detected OS: $OS"
20+
}
21+
22+
# Function to install a package if it's not already installed
23+
install_if_not_installed() {
24+
PACKAGE=$1
25+
if [[ "$OS" == "linux" ]]; then
26+
if ! dpkg -l | grep -q "$PACKAGE"; then
27+
print_message "Installing $PACKAGE..."
28+
sudo apt-get install -y "$PACKAGE" > /dev/null 2>&1
29+
else
30+
print_message "$PACKAGE is already installed."
31+
fi
32+
elif [[ "$OS" == "macos" ]]; then
33+
if ! brew list -1 | grep -q "$PACKAGE"; then
34+
print_message "Installing $PACKAGE..."
35+
brew install "$PACKAGE" > /dev/null 2>&1
36+
else
37+
print_message "$PACKAGE is already installed."
38+
fi
39+
fi
40+
}
41+
42+
# Function to install dependencies based on the OS
43+
install_dependencies() {
44+
if [[ "$OS" == "linux" ]]; then
45+
print_message "Installing protobuf-compiler..."
46+
install_if_not_installed "protobuf-compiler"
47+
48+
print_message "Installing build dependencies for Substrate..."
49+
dependencies=("build-essential" "clang" "libclang-dev" "curl" "libssl-dev" "llvm" "libudev-dev" "pkg-config" "zlib1g-dev" "git")
50+
for package in "${dependencies[@]}"; do
51+
install_if_not_installed "$package"
52+
done
53+
elif [[ "$OS" == "macos" ]]; then
54+
print_message "Installing protobuf..."
55+
brew install protobuf > /dev/null 2>&1
56+
57+
print_message "Installing build dependencies for Substrate..."
58+
dependencies=("clang" "cmake" "pkg-config" "openssl" "llvm" "protobuf" "git")
59+
for package in "${dependencies[@]}"; do
60+
install_if_not_installed "$package"
61+
done
62+
fi
63+
}
64+
65+
# Function to source the environment
66+
source_environment() {
67+
if [[ "$OS" == "linux" ]]; then
68+
if [ -f "$HOME/.cargo/env" ]; then
69+
source "$HOME/.cargo/env"
70+
print_message "Sourced cargo environment for Linux."
71+
else
72+
print_message "Cargo environment file not found for Linux."
73+
fi
74+
elif [[ "$OS" == "macos" ]]; then
75+
if [ -f "/usr/local/bin/cargo/env" ]; then
76+
source "/usr/local/bin/cargo/env"
77+
print_message "Sourced cargo environment for macOS."
78+
else
79+
print_message "Cargo environment file not found for macOS."
80+
fi
81+
fi
82+
}
83+
84+
# Function to install Python3
85+
install_python3() {
86+
print_message "Installing Python3 and pip..."
87+
install_if_not_installed "python3"
88+
install_if_not_installed "python3-pip"
89+
print_message "Installing tqdm for Python..."
90+
pip3 install tqdm > /dev/null 2>&1
91+
}
92+
93+
# Function to insert bootnodes into minervaRaw.json
94+
insert_bootnodes() {
95+
python3 <<EOF
96+
import re
97+
import time
98+
from tqdm import tqdm
99+
100+
def insert_bootnodes(original_file, bootnodes_file):
101+
try:
102+
with open(original_file, 'r') as file:
103+
original_content = file.read()
104+
105+
with open(bootnodes_file, 'r') as file:
106+
bootnodes_content = file.read().strip()
107+
108+
# Progress bar for processing the content
109+
for _ in tqdm(range(10), desc="🌟 Processing content", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}"):
110+
time.sleep(0.1) # Simulate work being done
111+
112+
# Find the bootNodes section, clear its contents, and insert the new bootnodes content
113+
pattern = re.compile(r'("bootNodes"\\s*:\\s*\\[)[^\\]]*?(\\])', re.DOTALL)
114+
new_content = pattern.sub(r'\\1\n' + bootnodes_content + r'\\2', original_content)
115+
116+
# Progress bar for writing the new content
117+
for _ in tqdm(range(10), desc="🌟 Writing new content", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}"):
118+
time.sleep(0.1) # Simulate work being done
119+
120+
# Write the modified content back to the original file
121+
with open(original_file, 'w') as file:
122+
file.write(new_content)
123+
124+
print(f"Successfully inserted bootnodes into {original_file}")
125+
126+
except Exception as e:
127+
print(f"An error occurred: {e}")
128+
129+
def main():
130+
original_file = 'minervaRaw.json' # Path to the original JSON file
131+
bootnodes_file = 'bootnodes.txt' # Path to the bootnodes file
132+
133+
# Progress bar for reading files
134+
for _ in tqdm(range(10), desc="📄 Reading files", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}"):
135+
time.sleep(0.1) # Simulate work being done
136+
137+
insert_bootnodes(original_file, bootnodes_file)
138+
139+
if __name__ == "__main__":
140+
main()
141+
EOF
142+
}
143+
144+
# Main script execution
145+
check_os
146+
install_dependencies
147+
source_environment
148+
install_python3
149+
insert_bootnodes
150+
151+
print_message "Installation and setup complete. Please restart your terminal."

0 commit comments

Comments
 (0)
Please sign in to comment.