Skip to content

Instantly share code, notes, and snippets.

@0x4007
Last active August 9, 2023 18:17
Show Gist options
  • Save 0x4007/54fdc710064ba84e5b2bc9b5d18a9dd5 to your computer and use it in GitHub Desktop.
Save 0x4007/54fdc710064ba84e5b2bc9b5d18a9dd5 to your computer and use it in GitHub Desktop.
For dApp development, its common for scripts to use `PRIVATE_KEY` and `PUBLIC_KEY` so for security reasons I automatically generate a temporary (but valid) keypair. I load this in with my `.bashrc`. Type in `keygen` to automatically generate a new keypair and assign to their respective variable names.
#!/bin/bash env
keygen() {
PREFIX="$HOME/.evm/"
KEY="$(echo "$PREFIX")key"
PUB="$(echo "$PREFIX")pub"
PRIV="$(echo "$PREFIX")priv"
ADDRESS="$(echo "$PREFIX")address"
# Generate the private and public keys
openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout >"$KEY" 2>/dev/null
# Extract the public key and remove the EC prefix 0x04
cat "$KEY" | grep pub -A 5 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^04//' >"$PUB"
# Extract the private key and remove the leading zero byte
cat "$KEY" | grep priv -A 3 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^00//' >"$PRIV"
# Generate the hash and take the address part
cat "$PUB" | keccak-256sum -x -l | tr -d ' -' | tail -c 41 >"$ADDRESS"
export EVM_PRIVATE_KEY=$(cat "$PRIV")
export EVM_ADDRESS="0x$(cat "$ADDRESS")"
echo "EVM_ADDRESS = $EVM_ADDRESS"
echo "EVM_PRIVATE_KEY = $EVM_PRIVATE_KEY"
}
export -f keygen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment