This library and repository was previously located at https://github.com/pipermerriam/ethereum-keys. It was transferred to the Ethereum foundation github in November 2017 and renamed to eth-keys. The PyPi package was also renamed from ethereum-keys to eth-keys.
The KeyAPI object is the primary API for interacting with the eth-keys
libary. The object takes a single optional argument in its constructor which
designates what backend will be used for eliptical curve cryptography
operations. The built-in backends are:
eth_keys.backends.NativeECCBackend: A pure python implementation of the ECC operations.
eth_keys.backends.CoinCurveECCBackend: Uses the coincurve library for ECC operations.
By default, eth-keys will try to use the CoinCurveECCBackend,
falling back to the NativeECCBackend if the coincurve library is not
available.
Note: The coincurve library is not automatically installed with eth-keys and must be installed separately.
The backend argument can be given in any of the following forms.
Instance of the backend class
The backend class
String with the dot-separated import path for the backend class.
>>> from eth_keys import KeyAPI
>>> from eth_keys.backends import NativeECCBackend
# These are all the same
>>> keys = KeyAPI(NativeECCBackend)
>>> keys = KeyAPI(NativeECCBackend())
>>> keys = KeyAPI('eth_keys.backends.NativeECCBackend')
# Or for the coincurve base backend
>>> keys = KeyAPI('eth_keys.backends.CoinCurveECCBackend')
The backend can also be configured using the environment variable
ECC_BACKEND_CLASS which should be set to the dot-separated python import path
to the desired backend.
>>> import os
>>> os.environ['ECC_BACKEND_CLASS'] = 'eth_keys.backends.CoinCurveECCBackend'
Returns the PublicKey instances computed from the given private_key
instance.
private_key: must be an instance of PublicKey
Common APIs for PublicKey, PrivateKey and Signature
There is a common API for the following objects.
PublicKey
PrivateKey
Signature
Each of these objects has all of the following APIs.
obj.to_bytes(): Returns the object in it's canonical bytes serialization.
obj.to_hex(): Returns a text string of the hex encoded canonical representation.
KeyAPI.PublicKey(public_key_bytes)
The PublicKey class takes a single argument which must be a bytes string with length 64.
Note that there are two other common formats for public keys: 65 bytes with a leading \x04 byte
and 33 bytes starting with either \x02 or \x03. To use the former with the PublicKey object,
remove the first byte. For the latter, refer to PublicKey.from_compressed_bytes.
Same as PrivateKey.sign except that message_hash should be the Keccak
hash of the message.
KeyAPI.Signature(signature_bytes=None, vrs=None)
The Signature class can be instantiated in one of two ways.
signature_bytes: a bytes string with length 65.
vrs: a 3-tuple composed of the integers v, r, and s.
Note: If using the signature_bytes to instantiate, the byte string should be encoded as r_bytes | s_bytes | v_bytes where | represents concatenation. r_bytes and s_bytes should be 32 bytes in length. v_bytes should be a single byte \x00 or \x01.
Signatures are expected to use 1 or 0 for their v value.
The following methods and properties are available
Signature.v
This property returns the v value from the signature as an integer.
Signature.r
This property returns the r value from the signature as an integer.
Signature.s
This property returns the s value from the signature as an integer.
Signature.vrs
This property returns a 3-tuple of (v, r, s).
Signature.verify_msg(message, public_key) -> bool
This method returns True or False based on whether the signature is a valid
for the given public key.
Same as Signature.recover_public_key_from_msg except that message_hash
should be the Keccak hash of the message.
Exceptions
eth_api.exceptions.ValidationError
This error is raised during instantaition of any of the PublicKey,
PrivateKey or Signature classes if their constructor parameters are
invalid.
eth_api.exceptions.BadSignature
This error is raised from any of the recover or verify methods involving
signatures if the signature is invalid.
Developer Setup
If you would like to hack on eth-keys, please check out the Snake Charmers
Tactical Manual
for information on how we do:
Testing
Pull Requests
Documentation
We use prek to maintain consistent code style. Once
installed, it will run automatically with every commit. You can also run it manually
with make lint. If you need to make a commit that skips the prek checks, you
can do so with git commit --no-verify.
Development Environment Setup
You can set up your dev environment with:
git clone git@github.com:ethereum/eth-keys.git
cd eth-keys
uv sync
uv run prek install
Release setup
To release a new version:
make release bump=$$VERSION_PART_TO_BUMP$$
How to bumpversion
The version format for this repo is {major}.{minor}.{patch} for stable, and
{major}.{minor}.{patch}-{stage}.{devnum} for unstable (stage can be alpha or beta).
To issue the next version in line, specify which part to bump,
like make release bump=minor or make release bump=devnum. This is typically done from the
main branch, except when releasing a beta (in which case the beta is released from main,
and the previous stable branch is released from said branch).
If you are in a beta version, make release bump=stage will switch to a stable.
To issue an unstable version when the current version is stable, specify the
new version explicitly, like make release bump="--new-version 4.0.0-alpha.1 devnum"
Works well with
Use together with eth-keys: extensions, drivers, servers and companion tools.
web3Pythie’s Index: 65web3: A Python library for interacting with Ethereum5.4M downloads/mo
eth-accountPythie’s Index: 52eth-account: Sign Ethereum transactions and messages with local private keys6.5M downloads/mo
eth-utilsPythie’s Index: 36eth-utils: Common utility functions for python code that interacts with Ethereum8.3M downloads/mo
eth-abiPythie’s Index: 46eth_abi: Python utilities for working with Ethereum ABI definitions, especially encoding and decoding7.7M downloads/mo
coincurvePythie’s Index: 24Safest and fastest Python library for secp256k1 elliptic curve operations3M downloads/mo
eth-keyfilePythie’s Index: 49A library for handling the encrypted keyfiles used to store ethereum private keys6.4M downloads/mo