Ethereum Keyfile Handler

0.9.1 · active · verified Thu Apr 09

eth-keyfile is a Python library for securely handling the encrypted keyfiles used to store Ethereum private keys. It provides functionality to create, load, and decrypt these keyfiles, which conform to the Web3 secret storage standards. The library is currently at version 0.9.1 and maintains a stable release cadence, with ongoing support for modern Python versions (>=3.8, <4). It was previously known as `ethereum-keyfile` and was renamed and moved to the Ethereum foundation GitHub in November 2017.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a new Ethereum private key, create an encrypted keyfile JSON object using that key and a password, and then decrypt the private key back from the keyfile. It also shows an example of creating a Keyfile V4, highlighting the `version` parameter. The private key and password are handled as bytestrings.

import os
import json
from eth_keyfile import create_keyfile_json, decode_keyfile_json

# 1. Generate a new private key (32 bytes)
private_key = os.urandom(32)
password = b"my_secure_password"

# 2. Create a keyfile JSON object (default is V3)
keyfile_json = create_keyfile_json(private_key, password)

# You would typically save this to a file:
# with open('my_keystore.json', 'w') as f:
#    json.dump(keyfile_json, f)

print(f"Generated Keyfile (first 50 chars): {json.dumps(keyfile_json)[:50]}...")

# 3. Decode the private key from the keyfile JSON
decoded_private_key = decode_keyfile_json(keyfile_json, password)

assert private_key == decoded_private_key
print(f"Successfully decoded private key: {decoded_private_key.hex()}")

# Example with Keyfile V4 (requires different private key range)
# Note: private_key for v4 must be less than MAX_V4_PRIVATE_KEY
# For simplicity, using a valid v3 key here, but in a real scenario, 
# ensure it's valid for bls12-381 curve if using v4.
try:
    keyfile_json_v4 = create_keyfile_json(
        private_key, 
        password, 
        version=4, 
        description="My V4 Key", 
        path="m/123/456"
    )
    print(f"\nGenerated V4 Keyfile (first 50 chars): {json.dumps(keyfile_json_v4)[:50]}...")
    decoded_private_key_v4 = decode_keyfile_json(keyfile_json_v4, password)
    assert private_key == decoded_private_key_v4
    print(f"Successfully decoded V4 private key: {decoded_private_key_v4.hex()}")
except Exception as e:
    print(f"\nCould not create V4 keyfile with this private key (expected for some randomly generated keys): {e}")

view raw JSON →