# eth-keyfile
[![Join the conversation on Discord](https://img.shields.io/discord/809793915578089484?color=blue&label=chat&logo=discord&logoColor=white)](https://discord.gg/GHryRvPB84) [![Build Status](https://circleci.com/gh/ethereum/eth-keyfile.svg?style=shield)](https://circleci.com/gh/ethereum/eth-keyfile)
[![PyPI version](https://badge.fury.io/py/eth-keyfile.svg)](https://badge.fury.io/py/eth-keyfile)
[![Python versions](https://img.shields.io/pypi/pyversions/eth-keyfile.svg)](https://pypi.python.org/pypi/eth-keyfile)
A library for handling the encrypted keyfiles used to store ethereum private keys
> This library and repository was previously located at https://github.com/pipermerriam/ethereum-keyfile. It was transferred to the Ethereum foundation github in November 2017 and renamed to `eth-keyfile`. The PyPi package was also renamed from `ethereum-keyfile` to `eth-keyfile`.
Read more in the documentation below. [View the change log](https://github.com/ethereum/eth-keyfile/blob/master/CHANGELOG.rst).
## Quickstart
```sh
python -m pip install eth-keyfile
```
## Documentation
### `eth_keyfile.load_keyfile(path_or_file_obj) --> keyfile_json`
Takes either a filesystem path represented as a string or a file object and
returns the parsed keyfile json as a python dictionary.
```python
>>> from eth_keyfile import load_keyfile
>>> load_keyfile('path/to-my-keystore/keystore.json')
{
"crypto" : {
"cipher" : "aes-128-ctr",
"cipherparams" : {
"iv" : "6087dab2f9fdbbfaddc31a909735c1e6"
},
"ciphertext" : "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46",
"kdf" : "pbkdf2",
"kdfparams" : {
"c" : 262144,
"dklen" : 32,
"prf" : "hmac-sha256",
"salt" : "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"
},
"mac" : "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"
},
"id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6",
"version" : 3
}
```
### `eth_keyfile.create_keyfile_json(private_key, password, kdf="pbkdf2", work_factor=None, salt_size=16) --> keyfile_json`
Takes the following parameters:
- `private_key`: A bytestring of length 32
- `password`: A bytestring which will be the password that can be used to decrypt the resulting keyfile.
- `kdf`: The key derivation function. Allowed values are `pbkdf2` and `scrypt`. By default, `pbkdf2` will be used.
- `work_factor`: The work factor which will be used for the given key derivation function. By default `1000000` will be used for `pbkdf2` and `262144` for `scrypt`.
- `salt_size`: Salt size in bytes.
Returns the keyfile json as a python dictionary.
```python
>>> private_key = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
>>> create_keyfile_json(private_key, b'foo')
{
"address" : "1a642f0e3c3af545e7acbd38b07251b3990914f1",
"crypto" : {
"cipher" : "aes-128-ctr",
"cipherparams" : {
"iv" : "6087dab2f9fdbbfaddc31a909735c1e6"
},
"ciphertext" : "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46",
"kdf" : "pbkdf2",
"kdfparams" : {
"c" : 262144,
"dklen" : 32,
"prf" : "hmac-sha256",
"salt" : "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"
},
"mac" : "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"
},
"id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6",
"version" : 3
}
```
### `eth_keyfile.decode_keyfile_json(keyfile_json, password) --> private_key`
Takes the keyfile json as a python dictionary and the password for the keyfile,
returning the decoded private key.
```python
>>> keyfile_json = {
... "crypto" : {
... "cipher" : "aes-128-ctr",
... "cipherparams" : {
... "iv" : "6087dab2f9fdbbfaddc31a909735c1e6"
... },
... "ciphertext" : "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46",
... "kdf" : "pbkdf2",
... "kdfparams" : {
... "c" : 262144,
... "dklen" : 32,
... "prf" : "hmac-sha256",
... "salt" : "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"
... },
... "mac" : "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"
... },
... "id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6",
... "version" : 3
... }
>>> decode_keyfile_json(keyfile_json, b'foo')
b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
```
### `eth_keyfile.extract_key_from_keyfile(path_or_file_obj, password) --> private_key`
Takes a filesystem path represented by a string or a file object and the
password for the keyfile. Returns the private key as a bytestring.
```python
>>> extract_key_from_keyfile('path/to-my-keystore/keyfile.json', b'foo')
b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
```
## Developer Setup
If you would like to hack on eth-keyfile, please check out the [Snake Charmers
Tactical Manual](https://github.com/ethereum/snake-charmers-tactical-manual)
for information on how we do:
- Testing
- Pull Requests
- Documentation
We use [pre-commit](https://pre-commit.com/) 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 `pre-commit` checks, you
can do so with `git commit --no-verify`.
### Development Environment Setup
You can set up your dev environment with:
```sh
git clone git@github.com:ethereum/eth-keyfile.git
cd eth-keyfile
virtualenv -p python3 venv
. venv/bin/activate
python -m pip install -e ".[dev]"
pre-commit install
```
### Release setup
To release a new version:
```sh
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"`
Raw data
{
"_id": null,
"home_page": "https://github.com/ethereum/eth-keyfile",
"name": "eth-keyfile",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8, <4",
"maintainer_email": "",
"keywords": "ethereum",
"author": "The Ethereum Foundation",
"author_email": "snakecharmers@ethereum.org",
"download_url": "https://files.pythonhosted.org/packages/99/39/c65fd9fd00071a93639c07e76a81f943be68f0e016a4a900262f6a33a628/eth-keyfile-0.7.0.tar.gz",
"platform": null,
"description": "# eth-keyfile\n\n[![Join the conversation on Discord](https://img.shields.io/discord/809793915578089484?color=blue&label=chat&logo=discord&logoColor=white)](https://discord.gg/GHryRvPB84) [![Build Status](https://circleci.com/gh/ethereum/eth-keyfile.svg?style=shield)](https://circleci.com/gh/ethereum/eth-keyfile)\n[![PyPI version](https://badge.fury.io/py/eth-keyfile.svg)](https://badge.fury.io/py/eth-keyfile)\n[![Python versions](https://img.shields.io/pypi/pyversions/eth-keyfile.svg)](https://pypi.python.org/pypi/eth-keyfile)\n\nA library for handling the encrypted keyfiles used to store ethereum private keys\n\n> This library and repository was previously located at https://github.com/pipermerriam/ethereum-keyfile. It was transferred to the Ethereum foundation github in November 2017 and renamed to `eth-keyfile`. The PyPi package was also renamed from `ethereum-keyfile` to `eth-keyfile`.\n\nRead more in the documentation below. [View the change log](https://github.com/ethereum/eth-keyfile/blob/master/CHANGELOG.rst).\n\n## Quickstart\n\n```sh\npython -m pip install eth-keyfile\n```\n\n## Documentation\n\n### `eth_keyfile.load_keyfile(path_or_file_obj) --> keyfile_json`\n\nTakes either a filesystem path represented as a string or a file object and\nreturns the parsed keyfile json as a python dictionary.\n\n```python\n>>> from eth_keyfile import load_keyfile\n>>> load_keyfile('path/to-my-keystore/keystore.json')\n{\n \"crypto\" : {\n \"cipher\" : \"aes-128-ctr\",\n \"cipherparams\" : {\n \"iv\" : \"6087dab2f9fdbbfaddc31a909735c1e6\"\n },\n \"ciphertext\" : \"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46\",\n \"kdf\" : \"pbkdf2\",\n \"kdfparams\" : {\n \"c\" : 262144,\n \"dklen\" : 32,\n \"prf\" : \"hmac-sha256\",\n \"salt\" : \"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd\"\n },\n \"mac\" : \"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2\"\n },\n \"id\" : \"3198bc9c-6672-5ab3-d995-4942343ae5b6\",\n \"version\" : 3\n}\n```\n\n### `eth_keyfile.create_keyfile_json(private_key, password, kdf=\"pbkdf2\", work_factor=None, salt_size=16) --> keyfile_json`\n\nTakes the following parameters:\n\n- `private_key`: A bytestring of length 32\n- `password`: A bytestring which will be the password that can be used to decrypt the resulting keyfile.\n- `kdf`: The key derivation function. Allowed values are `pbkdf2` and `scrypt`. By default, `pbkdf2` will be used.\n- `work_factor`: The work factor which will be used for the given key derivation function. By default `1000000` will be used for `pbkdf2` and `262144` for `scrypt`.\n- `salt_size`: Salt size in bytes.\n\nReturns the keyfile json as a python dictionary.\n\n```python\n>>> private_key = b'\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01'\n>>> create_keyfile_json(private_key, b'foo')\n{\n \"address\" : \"1a642f0e3c3af545e7acbd38b07251b3990914f1\",\n \"crypto\" : {\n \"cipher\" : \"aes-128-ctr\",\n \"cipherparams\" : {\n \"iv\" : \"6087dab2f9fdbbfaddc31a909735c1e6\"\n },\n \"ciphertext\" : \"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46\",\n \"kdf\" : \"pbkdf2\",\n \"kdfparams\" : {\n \"c\" : 262144,\n \"dklen\" : 32,\n \"prf\" : \"hmac-sha256\",\n \"salt\" : \"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd\"\n },\n \"mac\" : \"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2\"\n },\n \"id\" : \"3198bc9c-6672-5ab3-d995-4942343ae5b6\",\n \"version\" : 3\n}\n```\n\n### `eth_keyfile.decode_keyfile_json(keyfile_json, password) --> private_key`\n\nTakes the keyfile json as a python dictionary and the password for the keyfile,\nreturning the decoded private key.\n\n```python\n>>> keyfile_json = {\n... \"crypto\" : {\n... \"cipher\" : \"aes-128-ctr\",\n... \"cipherparams\" : {\n... \"iv\" : \"6087dab2f9fdbbfaddc31a909735c1e6\"\n... },\n... \"ciphertext\" : \"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46\",\n... \"kdf\" : \"pbkdf2\",\n... \"kdfparams\" : {\n... \"c\" : 262144,\n... \"dklen\" : 32,\n... \"prf\" : \"hmac-sha256\",\n... \"salt\" : \"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd\"\n... },\n... \"mac\" : \"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2\"\n... },\n... \"id\" : \"3198bc9c-6672-5ab3-d995-4942343ae5b6\",\n... \"version\" : 3\n... }\n>>> decode_keyfile_json(keyfile_json, b'foo')\nb'\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01'\n```\n\n### `eth_keyfile.extract_key_from_keyfile(path_or_file_obj, password) --> private_key`\n\nTakes a filesystem path represented by a string or a file object and the\npassword for the keyfile. Returns the private key as a bytestring.\n\n```python\n>>> extract_key_from_keyfile('path/to-my-keystore/keyfile.json', b'foo')\nb'\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01'\n```\n\n## Developer Setup\n\nIf you would like to hack on eth-keyfile, please check out the [Snake Charmers\nTactical Manual](https://github.com/ethereum/snake-charmers-tactical-manual)\nfor information on how we do:\n\n- Testing\n- Pull Requests\n- Documentation\n\nWe use [pre-commit](https://pre-commit.com/) to maintain consistent code style. Once\ninstalled, it will run automatically with every commit. You can also run it manually\nwith `make lint`. If you need to make a commit that skips the `pre-commit` checks, you\ncan do so with `git commit --no-verify`.\n\n### Development Environment Setup\n\nYou can set up your dev environment with:\n\n```sh\ngit clone git@github.com:ethereum/eth-keyfile.git\ncd eth-keyfile\nvirtualenv -p python3 venv\n. venv/bin/activate\npython -m pip install -e \".[dev]\"\npre-commit install\n```\n\n### Release setup\n\nTo release a new version:\n\n```sh\nmake release bump=$$VERSION_PART_TO_BUMP$$\n```\n\n#### How to bumpversion\n\nThe version format for this repo is `{major}.{minor}.{patch}` for stable, and\n`{major}.{minor}.{patch}-{stage}.{devnum}` for unstable (`stage` can be alpha or beta).\n\nTo issue the next version in line, specify which part to bump,\nlike `make release bump=minor` or `make release bump=devnum`. This is typically done from the\nmain branch, except when releasing a beta (in which case the beta is released from main,\nand the previous stable branch is released from said branch).\n\nIf you are in a beta version, `make release bump=stage` will switch to a stable.\n\nTo issue an unstable version when the current version is stable, specify the\nnew version explicitly, like `make release bump=\"--new-version 4.0.0-alpha.1 devnum\"`\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "eth-keyfile: A library for handling the encrypted keyfiles used to store ethereum private keys",
"version": "0.7.0",
"project_urls": {
"Homepage": "https://github.com/ethereum/eth-keyfile"
},
"split_keywords": [
"ethereum"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "494e17b30ce75a94d077e3840af4126c2d61e5a1fbbc03c2a3b58741aa46f7db",
"md5": "0a72344544c30fd58df32206bb3f0333",
"sha256": "6a89b231a2fe250c3a8f924f2695bb9cce33ddd0d6f7ebbcdacd183d7f83d537"
},
"downloads": -1,
"filename": "eth_keyfile-0.7.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0a72344544c30fd58df32206bb3f0333",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8, <4",
"size": 7382,
"upload_time": "2023-12-06T19:37:43",
"upload_time_iso_8601": "2023-12-06T19:37:43.028665Z",
"url": "https://files.pythonhosted.org/packages/49/4e/17b30ce75a94d077e3840af4126c2d61e5a1fbbc03c2a3b58741aa46f7db/eth_keyfile-0.7.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9939c65fd9fd00071a93639c07e76a81f943be68f0e016a4a900262f6a33a628",
"md5": "76c4efa5d98774ef1190b6488e27c55c",
"sha256": "6bdb8110c3a50439deb68a04c93c9d5ddd5402353bfae1bf4cfca1d6dff14fcf"
},
"downloads": -1,
"filename": "eth-keyfile-0.7.0.tar.gz",
"has_sig": false,
"md5_digest": "76c4efa5d98774ef1190b6488e27c55c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8, <4",
"size": 11316,
"upload_time": "2023-12-06T19:37:44",
"upload_time_iso_8601": "2023-12-06T19:37:44.840876Z",
"url": "https://files.pythonhosted.org/packages/99/39/c65fd9fd00071a93639c07e76a81f943be68f0e016a4a900262f6a33a628/eth-keyfile-0.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-06 19:37:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ethereum",
"github_project": "eth-keyfile",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"tox": true,
"lcname": "eth-keyfile"
}