Last active
April 23, 2023 19:36
-
-
Save Aviksaikat/fd5dfaef4c69e23116148b4b7c0377b6 to your computer and use it in GitHub Desktop.
Generate Ethereum private key, public key, and address using Python code
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
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