Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active July 14, 2024 01:20
Show Gist options
  • Save miguelmota/3793b160992b4ea0b616497b8e5aee2f to your computer and use it in GitHub Desktop.
Save miguelmota/3793b160992b4ea0b616497b8e5aee2f to your computer and use it in GitHub Desktop.
Generate Ethereum Private key, Public key, and Address using Bash and OpenSSL
# Generate the private and public keys
openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout > key
# 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
# (Optional) import the private key to geth
geth account import priv
@bertho-zero
Copy link

Create address with checksum (EIP 55): 0xcf8e7afab9576f3bd85885065aecd479d35f4d70 -> 0xCf8e7aFAB9576F3Bd85885065aeCD479D35F4d70

address=$(cat address)
hash=$(echo -n "$address" | keccak-256sum | tr -d ' -')

checksum_address=""
for i in $(seq 0 39); do
    char="${address:$i:1}"
    hash_char="${hash:$i:1}"
    if [[ "$char" =~ [0-9] ]]; then
        checksum_address+="$char"
    elif (( 0x$hash_char >= 8 )); then
        checksum_address+="${char^^}"
    else
        checksum_address+="$char"
    fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment