Last active
March 12, 2024 11:45
-
-
Save miguelmota/3793b160992b4ea0b616497b8e5aee2f to your computer and use it in GitHub Desktop.
Generate Ethereum Private key, Public key, and Address using Bash and OpenSSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Ubuntu:
cat pub | keccak-256sum -x -l | tr -d ' -' | tail -c 41 > address
keccak-256sum: command not found
@miguelmota Thanks for putting this together. I implemented this for people to use in 1 line if they have NixOS or the Nix package manager installed:
nix run \
github:realfolk/nix?dir=lib/packages/generate-ethereum-account \
PATH_TO_STORE_PUBLIC_KEY \
PATH_TO_STORE_PRIVATE_KEY
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lau-bin you cannot really substitute one for another. Because of the slight differences in how each digest algorithm works, this results in a different hash.