bip32


Namebip32 JSON
Version 3.4 PyPI version JSON
download
home_pagehttp://github.com/darosior/python-bip32
SummaryMinimalistic implementation of the BIP32 key derivation scheme
upload_time2022-12-10 17:29:27
maintainer
docs_urlNone
authorAntoine Poinsot
requires_python
licenseMIT
keywords bitcoin bip32 hdwallet
VCS
bugtrack_url
requirements coincurve base58
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # python-bip32

A basic implementation of the [bip-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki).

## Usage

```python
>>> from bip32 import BIP32, HARDENED_INDEX
>>> bip32 = BIP32.from_seed(bytes.fromhex("01"))
# Specify the derivation path as a list ...
>>> bip32.get_xpriv_from_path([1, HARDENED_INDEX, 9998])
'xprv9y4sBgCuub5x2DtbdNBDDCZ3btybk8YZZaTvzV5rmYd3PbU63XLo2QEj6cUt4JAqpF8gJiRKFUW8Vm7thPkccW2DpUvBxASycypEHxmZzts'
# ... Or in usual m/the/path/
>>> bip32.get_xpriv_from_path("m/1/0'/9998")
'xprv9y4sBgCuub5x2DtbdNBDDCZ3btybk8YZZaTvzV5rmYd3PbU63XLo2QEj6cUt4JAqpF8gJiRKFUW8Vm7thPkccW2DpUvBxASycypEHxmZzts'
>>> bip32.get_xpub_from_path([HARDENED_INDEX, 42])
'xpub69uEaVYoN1mZyMon8qwRP41YjYyevp3YxJ68ymBGV7qmXZ9rsbMy9kBZnLNPg3TLjKd2EnMw5BtUFQCGrTVDjQok859LowMV2SEooseLCt1'
# You can also use "h" or "H" to signal for hardened derivation
>>> bip32.get_xpub_from_path("m/0h/42")
'xpub69uEaVYoN1mZyMon8qwRP41YjYyevp3YxJ68ymBGV7qmXZ9rsbMy9kBZnLNPg3TLjKd2EnMw5BtUFQCGrTVDjQok859LowMV2SEooseLCt1'
# You can use pubkey-only derivation
>>> bip32 = BIP32.from_xpub("xpub6AKC3u8URPxDojLnFtNdEPFkNsXxHfgRhySvVfEJy9SVvQAn14XQjAoFY48mpjgutJNfA54GbYYRpR26tFEJHTHhfiiZZ2wdBBzydVp12yU")
>>> bip32.get_xpub_from_path([42, 43])
'xpub6FL7T3s7GuVb4od1gvWuumhg47y6TZtf2DSr6ModQpX4UFGkQXw8oEVhJXcXJ4edmtAWCTrefD64B9RP4sYSkSumTW1wadTS3SYurBGYccT'
>>> bip32.get_xpub_from_path("m/42/43")
'xpub6FL7T3s7GuVb4od1gvWuumhg47y6TZtf2DSr6ModQpX4UFGkQXw8oEVhJXcXJ4edmtAWCTrefD64B9RP4sYSkSumTW1wadTS3SYurBGYccT'
>>> bip32.get_pubkey_from_path("m/1/1/1/1/1/1/1/1/1/1/1")
b'\x02\x0c\xac\n\xa8\x06\x96C\x8e\x9b\xcf\x83]\x0c\rCm\x06\x1c\xe9T\xealo\xa2\xdf\x195\xebZ\x9b\xb8\x9e'
```

## Installation

```
pip install bip32
```

### Dependencies

This uses [`coincurve`](https://github.com/ofek/coincurve) as a wrapper for [`libsecp256k1`](https://github.com/bitcoin-core/secp256k1) for EC operations.

### Running the test suite

```
# From the root of the repository
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt && pip install pytest
PYTHONPATH=$PYTHONPATH:$PWD/bip32 pytest -vvv
```

## Interface

All public keys below are compressed.

All `path` below are a list of integers representing the index of the key at each depth.

### BIP32

#### from_seed(seed)

__*classmethod*__

Instanciate from a raw seed (as `bytes`). See [bip-0032's master key
generation](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation).

#### from_xpriv(xpriv)

__*classmethod*__

Instanciate with an encoded serialized extended private key (as `str`) as master.

#### from_xpub(xpub)

__*classmethod*__

Instanciate with an encoded serialized extended public key (as `str`) as master.

You'll only be able to derive unhardened public keys.

#### get_extended_privkey_from_path(path)

Returns `(chaincode (bytes), privkey (bytes))` of the private key pointed by the path.

#### get_privkey_from_path(path)

Returns `privkey (bytes)`, the private key pointed by the path.

#### get_extended_pubkey_from_path(path)

Returns `(chaincode (bytes), pubkey (bytes))` of the public key pointed by the path.

Note that you don't need to have provided the master private key if the path doesn't
include an index `>= HARDENED_INDEX`.

#### get_pubkey_from_path(path)

Returns `pubkey (bytes)`, the public key pointed by the path.

Note that you don't need to have provided the master private key if the path doesn't
include an index `>= HARDENED_INDEX`.

#### get_xpriv_from_path(path)

Returns `xpriv (str)` the serialized and encoded extended private key pointed by the given
path.

#### get_xpub_from_path(path)

Returns `xpub (str)` the serialized and encoded extended public key pointed by the given
path.

Note that you don't need to have provided the master private key if the path doesn't
include an index `>= HARDENED_INDEX`.

#### get_xpriv(path)

Equivalent to `get_xpriv_from_path([])`.

#### get_xpriv_bytes(path)

Equivalent to `get_xpriv([])`, but not serialized in base58

#### get_xpub(path)

Equivalent to `get_xpub_from_path([])`.

#### get_xpub_bytes(path)

Equivalent to `get_xpub([])`, but not serialized in base58



            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/darosior/python-bip32",
    "name": "bip32",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "bitcoin,bip32,hdwallet",
    "author": "Antoine Poinsot",
    "author_email": "darosior@protonmail.com",
    "download_url": "https://files.pythonhosted.org/packages/67/06/df2b5d324dfd2f71172edd54db539879362de2be5d9fd897d86b4a2577de/bip32-3.4.tar.gz",
    "platform": null,
    "description": "# python-bip32\n\nA basic implementation of the [bip-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki).\n\n## Usage\n\n```python\n>>> from bip32 import BIP32, HARDENED_INDEX\n>>> bip32 = BIP32.from_seed(bytes.fromhex(\"01\"))\n# Specify the derivation path as a list ...\n>>> bip32.get_xpriv_from_path([1, HARDENED_INDEX, 9998])\n'xprv9y4sBgCuub5x2DtbdNBDDCZ3btybk8YZZaTvzV5rmYd3PbU63XLo2QEj6cUt4JAqpF8gJiRKFUW8Vm7thPkccW2DpUvBxASycypEHxmZzts'\n# ... Or in usual m/the/path/\n>>> bip32.get_xpriv_from_path(\"m/1/0'/9998\")\n'xprv9y4sBgCuub5x2DtbdNBDDCZ3btybk8YZZaTvzV5rmYd3PbU63XLo2QEj6cUt4JAqpF8gJiRKFUW8Vm7thPkccW2DpUvBxASycypEHxmZzts'\n>>> bip32.get_xpub_from_path([HARDENED_INDEX, 42])\n'xpub69uEaVYoN1mZyMon8qwRP41YjYyevp3YxJ68ymBGV7qmXZ9rsbMy9kBZnLNPg3TLjKd2EnMw5BtUFQCGrTVDjQok859LowMV2SEooseLCt1'\n# You can also use \"h\" or \"H\" to signal for hardened derivation\n>>> bip32.get_xpub_from_path(\"m/0h/42\")\n'xpub69uEaVYoN1mZyMon8qwRP41YjYyevp3YxJ68ymBGV7qmXZ9rsbMy9kBZnLNPg3TLjKd2EnMw5BtUFQCGrTVDjQok859LowMV2SEooseLCt1'\n# You can use pubkey-only derivation\n>>> bip32 = BIP32.from_xpub(\"xpub6AKC3u8URPxDojLnFtNdEPFkNsXxHfgRhySvVfEJy9SVvQAn14XQjAoFY48mpjgutJNfA54GbYYRpR26tFEJHTHhfiiZZ2wdBBzydVp12yU\")\n>>> bip32.get_xpub_from_path([42, 43])\n'xpub6FL7T3s7GuVb4od1gvWuumhg47y6TZtf2DSr6ModQpX4UFGkQXw8oEVhJXcXJ4edmtAWCTrefD64B9RP4sYSkSumTW1wadTS3SYurBGYccT'\n>>> bip32.get_xpub_from_path(\"m/42/43\")\n'xpub6FL7T3s7GuVb4od1gvWuumhg47y6TZtf2DSr6ModQpX4UFGkQXw8oEVhJXcXJ4edmtAWCTrefD64B9RP4sYSkSumTW1wadTS3SYurBGYccT'\n>>> bip32.get_pubkey_from_path(\"m/1/1/1/1/1/1/1/1/1/1/1\")\nb'\\x02\\x0c\\xac\\n\\xa8\\x06\\x96C\\x8e\\x9b\\xcf\\x83]\\x0c\\rCm\\x06\\x1c\\xe9T\\xealo\\xa2\\xdf\\x195\\xebZ\\x9b\\xb8\\x9e'\n```\n\n## Installation\n\n```\npip install bip32\n```\n\n### Dependencies\n\nThis uses [`coincurve`](https://github.com/ofek/coincurve) as a wrapper for [`libsecp256k1`](https://github.com/bitcoin-core/secp256k1) for EC operations.\n\n### Running the test suite\n\n```\n# From the root of the repository\npython3 -m venv venv\n. venv/bin/activate\npip install -r requirements.txt && pip install pytest\nPYTHONPATH=$PYTHONPATH:$PWD/bip32 pytest -vvv\n```\n\n## Interface\n\nAll public keys below are compressed.\n\nAll `path` below are a list of integers representing the index of the key at each depth.\n\n### BIP32\n\n#### from_seed(seed)\n\n__*classmethod*__\n\nInstanciate from a raw seed (as `bytes`). See [bip-0032's master key\ngeneration](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation).\n\n#### from_xpriv(xpriv)\n\n__*classmethod*__\n\nInstanciate with an encoded serialized extended private key (as `str`) as master.\n\n#### from_xpub(xpub)\n\n__*classmethod*__\n\nInstanciate with an encoded serialized extended public key (as `str`) as master.\n\nYou'll only be able to derive unhardened public keys.\n\n#### get_extended_privkey_from_path(path)\n\nReturns `(chaincode (bytes), privkey (bytes))` of the private key pointed by the path.\n\n#### get_privkey_from_path(path)\n\nReturns `privkey (bytes)`, the private key pointed by the path.\n\n#### get_extended_pubkey_from_path(path)\n\nReturns `(chaincode (bytes), pubkey (bytes))` of the public key pointed by the path.\n\nNote that you don't need to have provided the master private key if the path doesn't\ninclude an index `>= HARDENED_INDEX`.\n\n#### get_pubkey_from_path(path)\n\nReturns `pubkey (bytes)`, the public key pointed by the path.\n\nNote that you don't need to have provided the master private key if the path doesn't\ninclude an index `>= HARDENED_INDEX`.\n\n#### get_xpriv_from_path(path)\n\nReturns `xpriv (str)` the serialized and encoded extended private key pointed by the given\npath.\n\n#### get_xpub_from_path(path)\n\nReturns `xpub (str)` the serialized and encoded extended public key pointed by the given\npath.\n\nNote that you don't need to have provided the master private key if the path doesn't\ninclude an index `>= HARDENED_INDEX`.\n\n#### get_xpriv(path)\n\nEquivalent to `get_xpriv_from_path([])`.\n\n#### get_xpriv_bytes(path)\n\nEquivalent to `get_xpriv([])`, but not serialized in base58\n\n#### get_xpub(path)\n\nEquivalent to `get_xpub_from_path([])`.\n\n#### get_xpub_bytes(path)\n\nEquivalent to `get_xpub([])`, but not serialized in base58\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Minimalistic implementation of the BIP32 key derivation scheme",
    "version": "3.4",
    "split_keywords": [
        "bitcoin",
        "bip32",
        "hdwallet"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "2c9eb8b8d962a3c071577b11dab73539",
                "sha256": "c18099c25cabc087d081142d040eacb6fdf09f2c61fde3247e1f61313d1c0d26"
            },
            "downloads": -1,
            "filename": "bip32-3.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2c9eb8b8d962a3c071577b11dab73539",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 10406,
            "upload_time": "2022-12-10T17:29:25",
            "upload_time_iso_8601": "2022-12-10T17:29:25.486558Z",
            "url": "https://files.pythonhosted.org/packages/ef/c4/fe8eec10c41648b503c509073d5e2b0028627787377b2f479a14f26557eb/bip32-3.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "b2c0d0f23cfdeb5d0f54dd127b0cdb82",
                "sha256": "09213225d99ede936c39a4f5a520549d8ecc49ebe3fa15608a17ea4aa9f61a10"
            },
            "downloads": -1,
            "filename": "bip32-3.4.tar.gz",
            "has_sig": false,
            "md5_digest": "b2c0d0f23cfdeb5d0f54dd127b0cdb82",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10411,
            "upload_time": "2022-12-10T17:29:27",
            "upload_time_iso_8601": "2022-12-10T17:29:27.637123Z",
            "url": "https://files.pythonhosted.org/packages/67/06/df2b5d324dfd2f71172edd54db539879362de2be5d9fd897d86b4a2577de/bip32-3.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-10 17:29:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "darosior",
    "github_project": "python-bip32",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "coincurve",
            "specs": [
                [
                    ">=",
                    "15.0"
                ],
                [
                    "<",
                    "19"
                ]
            ]
        },
        {
            "name": "base58",
            "specs": [
                [
                    "~=",
                    "2.0"
                ]
            ]
        }
    ],
    "lcname": "bip32"
}
        
Elapsed time: 0.01756s