Skip to main content

Python implementation of the BIP32 key derivation scheme

Project description

python-bip32

A basic implementation of the bip-0032.

Usage

>>> from hdwallets 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 hdwallets

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.

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_master_xpriv(path)

Equivalent to get_xpriv_from_path([]).

get_master_xpub(path)

Equivalent to get_xpub_from_path([]).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hdwallets-0.0.8.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

hdwallets-0.0.8-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file hdwallets-0.0.8.tar.gz.

File metadata

  • Download URL: hdwallets-0.0.8.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.8 CPython/3.8.0 Linux/5.4.0-31-generic

File hashes

Hashes for hdwallets-0.0.8.tar.gz
Algorithm Hash digest
SHA256 71e9c7617a1de14d4cf3e989fb0475d4211bf2f41dab5e6600fc38d343163d08
MD5 50c792a4febb2bfc3ed66d8d34ef0cb9
BLAKE2b-256 bbba2b59d77c040485224f9cf9aae7167220c6a2791983b9b4db8cd499df29a1

See more details on using hashes here.

File details

Details for the file hdwallets-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: hdwallets-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.8 CPython/3.8.0 Linux/5.4.0-31-generic

File hashes

Hashes for hdwallets-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2d39d55822bf7e9cab1aace9ded94079a0f200447faa8709f411a53f2e3af7ac
MD5 9527b56940b526be10a5d74e63c70e18
BLAKE2b-256 2940a64d1fc14d2d20735d02010b6c49021dc4eb5ccac2cce74cf9c91dacc37a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page