Skip to content

Instantly share code, notes, and snippets.

@Aviksaikat
Last active April 23, 2023 19:36
Show Gist options
  • Save Aviksaikat/fd5dfaef4c69e23116148b4b7c0377b6 to your computer and use it in GitHub Desktop.
Save Aviksaikat/fd5dfaef4c69e23116148b4b7c0377b6 to your computer and use it in GitHub Desktop.
Generate Ethereum private key, public key, and address using Python code
import sha3
from ecdsa import SigningKey, SECP256k1
# Create a keccak_256 hash object
keccak = sha3.keccak_256()
# Generate a new private key using the SECP256k1 curve
private = SigningKey.generate(curve=SECP256k1)
# Get the corresponding public key in bytes format
public = private.get_verifying_key().to_string()
# Update the keccak hash object with the public key bytes
keccak.update(public)
# Extract the last 20 bytes (40 characters) from the keccak digest as the address
address = "0x{}".format(keccak.hexdigest()[24:])
# Print the private key, public key, and address
print(f"Private key: 0x{private.to_string().hex()}")
print(f"Public key: 0x{public.hex()}")
print(f"Address: {address}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment