Skip to content

Commit

Permalink
feeds: Adds keystore generator script
Browse files Browse the repository at this point in the history
  • Loading branch information
pmerkleplant committed Jul 22, 2024
1 parent 55a758b commit c766c82
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
The script is interactive and requires user input for various parameters like feed name, paths to ETH keystore file and password file, ETH address, and an Ethereum Mainnet RPC endpoint. The script should not be run as root, and a user with sudo permissions is required. If run as root, the script will prompt the user to create a new user.

### Key Steps:
Retrieve and Execute the Installation Script:

1. Install the Foundry Toolchain:

```bash
curl -L https://foundry.paradigm.xyz | bash
```

2. Generate a new Encrypted Keystore Wallet:

```bash
cd /tmp
wget https://raw.githubusercontent.com/chronicleprotocol/scripts/main/feeds/keystore-generator.sh
chmod a+x keystore-generator.sh
# $id : The 1-byte validator identifier, eg `0xFF`
# $keystore_path : The path where to install the keystore to
# $keystore_password : The password to encrypt the keystore with
./keystore-generator $id $keystore_path $keystore_password
```

3. Retrieve and Execute the Installation Script:

```bash
cd /tmp
wget https://raw.githubusercontent.com/chronicleprotocol/scripts/main/feeds/k3s-install/install.sh
wget https://raw.githubusercontent.com/chronicleprotocol/scripts/main/feeds/k3s-install/install.sh
chmod a+x install.sh
./install.sh
```
Expand Down
71 changes: 71 additions & 0 deletions feeds/keystore-generator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

# Tool to generate new encrypted keystore with Ethereum address matching
# a specific first byte identifier.
#
# Dependencies:
# - cast, see https://getfoundry.sh
# - unix utilities
#
# Usage:
#
# $ ./key-generator.sh <0x prefixed byte> <keystore path> <keystore password>
#
# Example:
#
# $ ./key-generator.sh 0xff ./keystores test
set -euo pipefail # Enable strict mode for bash

# Fail if foundry toolchain's cast not installed.
if ! command -v cast &> /dev/null
then
echo "Error: Please install the foundry toolchain's cast tool, see https://getfoundry.sh"
fi

# Fail if invalid number of arguments provided.
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <0x prefixed byte> <keystore path> <keystore password>"
exit 1
fi

# Store arguments into variables.
assigned_id="$1"
path="$2"
password="$3"

# Fail if invalid arguments provided.
if [ -z "$assigned_id" ] || [ -z "$path" ] || [ -z "$password" ]; then
echo "Usage: $0 <0x prefixed byte> <keystore path> <keystore password>"
exit 1
fi

# Note to ensure assigned_id is in lower case.
assigned_id=$(echo "$assigned_id" | tr '[:upper:]' '[:lower:]')

ctr=0
while true; do
# Create new keystore and catch output.
output=$(cast wallet new --unsafe-password "$password" "$path")

# Get path and address of new keystore from output.
keystore=$(echo "$output" | awk '/Created new encrypted keystore file:/ {print $6}')
address=$(echo "$output" | awk '/Address:/ {print $2}')

# Get address' id in lower case.
id=$(echo "${address:0:4}" | tr '[:upper:]' '[:lower:]')

# Check whether first byte matches assigned id.
if [ "$id" == "$assigned_id" ]; then
# Address suitable, print output and exit.
echo "Generated new validator address with id=$id. Needed $ctr tries."
echo "Keystore: $keystore"
echo "Address: $address"

exit 0
else
# Address not suitable, delete keystore and continue search.
rm "$keystore"
fi

ctr=$((ctr + 1))
done

0 comments on commit c766c82

Please sign in to comment.