retro-bip-utils


Nameretro-bip-utils JSON
Version 1.0.5 PyPI version JSON
download
home_pagehttps://github.com/laalaguer/retro_bip_utils
SummaryImplementation of BIP39, BIP32, BIP44, BIP49 and BIP84 for wallet seeds, keys and addresses generation. Supported coins: Bitcoin, Litecoin, Dogecoin, Ethereum.
upload_time2023-07-25 08:49:22
maintainerEmanuele Bellocchia, A Byte Ahead
docs_urlNone
authorEmanuele Bellocchia, A Byte Ahead
requires_python>=3.6
licenseMIT
keywords bitcoin litecoin dogecoin dash ethereum ripple wallet hd-wallet bip39 bip32 bip44 bip49 bip84 python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # BIP utility library
[![PyPI version](https://badge.fury.io/py/retro-bip-utils.svg)](https://badge.fury.io/py/retro-bip-utils)
[![Build Status](https://travis-ci.com/laalaguer/retro_bip_utils.svg?branch=master)](https://travis-ci.com/laalaguer/retro_bip_utils)
[![codecov](https://codecov.io/gh/laalaguer/retro_bip_utils/branch/master/graph/badge.svg)](https://codecov.io/gh/laalaguer/retro_bip_utils)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/laalaguer/retro_bip_utils/master/LICENSE)

## About the Fork

This fork aims at at retro version v1.0.5 of original `ebellocchia/bip_utils`. The reason is quite simple: the simplicity of the code dependency.

This fork removes one of the dependency: `pysha3` (discontinued > Python 3.6) and uses `eth-hash` instead (underhood, it uses `pysha3` < Python 3.6 and `safe-pysha3` > Python 3.6).

## Introduction

This package contains an implementation of some BIP (Bitcoin Improvement Proposal) specifications, allowing to:
- Generate a mnemonic string from a random entropy
- Generate a secure seed from the mnemonic string
- Use the seed to generate the master key of the wallet and derive the children keys, including address encoding

The implemented BIP specifications are the following:
- [BIP-0039](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) for mnemonic and seed generation
- [BIP-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) for master key generation (from the secure seed) and children keys derivation
- [BIP-0044](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki), [BIP-0049](https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki) and [BIP-0084](https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki) for the hierarchy of deterministic wallets, based on BIP-0032 specification

In addition to this, the package allows to:
- Parse BIP-0032 derivation paths
- Generate P2PKH addresses (included in BIP-0044)
- Generate P2SH addresses (included in BIP-0049)
- Generate P2WPKH addresses (included in BIP-0084)
- Generate Ethereum addresses
- Generate Ripple addresses
- Encode/Decode [WIF](https://en.bitcoin.it/wiki/Wallet_import_format)
- Encode/Decode [base58](https://en.bitcoin.it/wiki/Base58Check_encoding#Background)
- Encode/Decode [bech32](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)

The currently supported coins are:
- Bitcoin (and related test net)
- Litecoin (and related test net)
- Dogecoin (and related test net)
- Dash (and related test net)
- Ethereum
- Ripple

## Install the package

The package requires Python 3, it is not compatible with Python 2.
To install it:
- Using *setuptools*:

        python setup.py install

- Using *pip*:

        pip install retro_bip_utils

To run the tests:

- Without code coverage

        python setup.py test

- With code coverage and report:

        pip install coverage
        coverage run -m unittest discover
        coverage report

## BIP-0039 library

### Mnemonic generation

A mnemonic string can be generated by specifying the words number (in this case a random entropy will be used) or directly the entropy bytes.\
Supported words number:
- 12: *Bip39WordsNum.WORDS_NUM_12*
- 15: *Bip39WordsNum.WORDS_NUM_15*
- 18: *Bip39WordsNum.WORDS_NUM_18*
- 21: *Bip39WordsNum.WORDS_NUM_21*
- 24: *Bip39WordsNum.WORDS_NUM_24*

Supported entropy bits:
- 128: *Bip39EntropyBitLen.BIT_LEN_128*
- 160: *Bip39EntropyBitLen.BIT_LEN_160*
- 192: *Bip39EntropyBitLen.BIT_LEN_192*
- 224: *Bip39EntropyBitLen.BIT_LEN_224*
- 256: *Bip39EntropyBitLen.BIT_LEN_256*

**NOTE:** only the English words list is currently supported.

**Code example**

    import binascii
    from retro_bip_utils import EntropyGenerator, Bip39MnemonicGenerator, Bip39WordsNum

    # Generate a random mnemonic string of 15 words
    mnemonic = Bip39MnemonicGenerator.FromWordsNumber(Bip39WordsNum.WORDS_NUM_15)

    # Generate the mnemonic string from entropy bytes:
    entropy_bytes_hex = b"00000000000000000000000000000000"
    mnemonic = Bip39MnemonicGenerator.FromEntropy(binascii.unhexlify(entropy_bytes_hex))

    # Generate mnemonic from random 192-bit entropy
    entropy_bytes = EntropyGenerator(Bip39EntropyBitLen.BIT_LEN_192).Generate()
    mnemonic = Bip39MnemonicGenerator.FromEntropy(entropy_bytes)

### Mnemonic validation

A mnemonic string can be validated by verifying its checksum.
It is also possible to get back the entropy bytes from a mnemonic.

**Code example**

     from retro_bip_utils import Bip39MnemonicValidator

     # Get back the original entropy from a mnemonic string
     entropy_bytes = Bip39MnemonicValidator(mnemonic).GetEntropy()
     # Validate a mnemonic string by verifying its checksum
     is_valid = Bip39MnemonicValidator(mnemonic).Validate()

### Seed generation

A secure 64-byte seed is generated from a mnemonic and can be protected by a passphrase.\
This seed can be used to contruct a Bip class.

**Code example**

    from retro_bip_utils import Bip39SeedGenerator

    # If not specified, the passphrase will be empty
    passphrase = "my_passphrase"
    seed_bytes = Bip39SeedGenerator(mnemonic).Generate(passphrase)

## BIP-0032 library

The BIP-0032 library is wrapped inside the BIP-0044, BIP-0049 and BIP-0084 libraries, so there is no need to use it alone unless you need to derive some non-standard paths.

### Construction from a seed

The class can be constructed from a seed. The seed can be specified manually or generated by *Bip39SeedGenerator*.\
The constructed class is the master path, so printing the private key will result in printing the master key.

**Code example**

    import binascii
    from retro_bip_utils import Bip32

    # Seed bytes
    seed_bytes = binascii.unhexlify(b"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4")
    # Construct from seed. In case it's a test net, pass True as second parameter. Derivation path returned: m
    bip32_ctx = Bip32.FromSeed(seed_bytes)
    # Print master key in extended format
    print(bip32_ctx.PrivateKey().ToExtended())

In addition to a seed, it's also possible to specify a derivation path.

**Code example**

    # Derivation path returned: m/0'/1'/2
    bip32_ctx = Bip32.FromSeedAndPath(seed_bytes, "m/0'/1'/2")
    # Print private key for derivation path m/0'/1'/2 in extended format
    print(bip32_ctx.PrivateKey().ToExtended())

### Construction from an extended key

Alternatively, the class can be constructed directly from an extended key.\
The object returned will be at the same depth of the specified key.

**Code example**

    from retro_bip_utils import Bip32

    # Private extended key from derivation path m/0'/1 (depth 2)
    key_str = "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs"
    # Construct from key (return object has depth 2)
    bip32_ctx = Bip32.FromExtendedKey(key_str)
    # Print keys
    print(bip32_ctx.PrivateKey().ToExtended())
    print(bip32_ctx.PublicKey().ToExtended())

    # Public extended key from derivation path m/0'/1 (depth 2)
    key_str = "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ"
    # Construct from key (return object has depth 2)
    bip32_ctx = Bip32.FromExtendedKey(key_str)
    # Print key
    print(bip32_ctx.PublicKey().ToExtended())
    # Getting private key from a public-only object triggers a Bip32KeyError exception

### Keys derivation

Each time a key is derived, a new instance of the Bip32 class is returned. This allows to chain the methods call or save a specific key pair for future derivation.\
The *Bip32Utils.HardenIndex* method can be used to make an index hardened.

**Code example**

    import binascii
    from retro_bip_utils import Bip32, Bip32Utils

    # Seed bytes
    seed_bytes = binascii.unhexlify(b"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4")
    # Path: m
    bip32_ctx = Bip32.FromSeed(seed_bytes)
    # Derivation path: m/0'/1'/2/3
    bip32_ctx = bip32_ctx.ChildKey(Bip32Utils.HardenIndex(0)) \
                         .ChildKey(Bip32Utils.HardenIndex(1)) \
                         .ChildKey(2)                         \
                         .ChildKey(3)
    # Print keys in extended format
    print(bip32_ctx.PrivateKey().ToExtended())
    print(bip32_ctx.PublicKey().ToExtended())
    # Print keys in hex format
    print(bip32_ctx.PrivateKey().Raw().ToHex())
    print(bip32_ctx.PublicKey().RawCompressed().ToHex())
    print(bip32_ctx.PublicKey().RawUncompressed().ToHex())
    # Print private key in WIF format
    print(bip32_ctx.PrivateKey().ToWif())
    # Print public key converted to address
    print(bip32_ctx.PublicKey().ToAddress())

    # Alternative: use DerivePath method
    bip32_ctx = Bip32.FromSeed(seed_bytes)
    bip32_ctx = bip32_ctx.DerivePath("0'/1'/2/3")

    # DerivePath derives from the current depth, so it can be split
    bip32_ctx = Bip32.FromSeed(seed_bytes)
    bip32_ctx = bip32_ctx.DerivePath("0'/1'")   # Derivation path: m/0'/1'
    bip32_ctx = bip32_ctx.DerivePath("2/3")     # Derivation path: m/0'/1'/2/3

### Parse path

The Bip32 module allows also to parse derivation paths by returning the list of indexes in the path.\
In case of error, an empty list is returned.

**Code example**

    from retro_bip_utils import Bip32PathParser

    # Print: ["m", 2147483648, 2147483649, 2]
    print(Bip32PathParser.Parse("m/0'/1'/2"))
    # Same but skipping the master. Print: [2147483648, 2147483649, 2]
    print(Bip32PathParser.Parse("0'/1'/2", True))
    # 'p' can be used as an alternative character instead of '
    print(Bip32PathParser.Parse("m/0p/1p/2"))
    # Error path: empty list returned. Print: []
    print(Bip32PathParser.Parse("m/0'/abc/2"))

## Bip-0044, BIP-0049, BIP-0084 libraries

These libraries derives all from the same base class, so they are used exactly in the same way.\
Therefore, the following code examples can be used with the Bip44, Bip49 or Bip84 class.

### Construction from a seed

A Bip class can be constructed from a seed, like Bip32. The seed can be specified manually or generated by *Bip39SeedGenerator*.

**Code example**

    import binascii
    from retro_bip_utils import Bip44, Bip44Coins

    # Seed bytes
    seed_bytes = binascii.unhexlify(b"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4")
    # Derivation path returned: m
    # In case it's a test net, pass True as second parameter
    bip44_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.BITCOIN)

### Construction from an extended key

Alternatively, a Bip class can be constructed directly from an extended key.\
The Bip object returned will be at the same depth of the specified key.

**Code example**

    from retro_bip_utils import Bip44, Bip44Coins

    # Private extended key
    key_str = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
    # Construct from extended key
    bip44_ctx = Bip44.FromExtendedKey(key_str, Bip44Coins.BITCOIN)

### Keys derivation

Like Bip32, each time a key is derived a new instance of the Bip class is returned.\
The keys must be derived with the levels specified by BIP-0044:

    m / purpose' / coin_type' / account' / change / address_index

using the correspondent methods. If keys are derived in the wrong level, a *RuntimeError* will be raised.\
The private and public extended keys can be printed at any level.

Currently supported coins enumerative:
- Bitcoin (and related test net) : *Bip44Coins.BITCOIN, Bip44Coins.BITCOIN_TESTNET*
- Litecoin (and related test net) : *Bip44Coins.LITECOIN, Bip44Coins.LITECOIN_TESTNET*
- Dogecoin (and related test net) : *Bip44Coins.DOGECOIN, Bip44Coins.DOGECOIN_TESTNET*
- Dash (and related test net) : *Bip44Coins.DASH, Bip44Coins.DASH_TESTNET*
- Ethereum : *Bip44Coins.ETHEREUM*
- Ripple : *Bip44Coins.RIPPLE*

The library can be easily extended with other coins anyway.

**Code example**

    import binascii
    from retro_bip_utils import Bip44, Bip44Coins, Bip44Changes

    # Seed bytes
    seed_bytes = binascii.unhexlify(b"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4")
    # Create from seed
    bip44_mst = Bip44.FromSeed(seed_bytes, Bip44Coins.BITCOIN)

    # Print master key in extended format
    print(bip44_mst.PrivateKey().ToExtended())
    # Print master key in hex format
    print(bip44_mst.PrivateKey().Raw().ToHex())

    # Print public key in extended format (default: Bip44PubKeyTypes.EXT_KEY)
    print(bip44_mst.PublicKey())
    # Print public key in raw uncompressed format
    print(bip44_mst.PublicKey().RawUncompressed().ToHex())
    # Print public key in raw compressed format
    print(bip44_mst.PublicKey().RawCompressed().ToHex())

    # Print the master key in WIF
    print(bip44_mst.IsMasterLevel())
    print(bip44_mst.PrivateKey().ToWif())

    # Derive account 0 for Bitcoin: m/44'/0'/0'
    bip44_acc = bip44_mst.Purpose() \
                         .Coin()    \
                         .Account(0)
    # Print keys in extended format
    print(bip44_acc.IsAccountLevel())
    print(bip44_acc.PrivateKey().ToExtended())
    print(bip44_acc.PublicKey().ToExtended())

    # Derive the external chain: m/44'/0'/0'/0
    bip44_change = bip44_acc.Change(Bip44Changes.CHAIN_EXT)
    # Print again keys in extended format
    print(bip44_change.IsChangeLevel())
    print(bip44_change.PrivateKey().ToExtended())
    print(bip44_change.PublicKey().ToExtended())

    # Derive the first 20 addresses of the external chain: m/44'/0'/0'/0/i
    for i in range(20):
        bip44_addr = bip44_change.AddressIndex(i)
        # Print extended keys and address
        print(bip44_addr.PrivateKey().ToExtended())
        print(bip44_addr.PublicKey().ToExtended())
        print(bip44_addr.PublicKey().ToAddress())

In the example above, Bip44 can be substituted with Bip49 or Bip84 without changing the code.

## Ethereum/Ripple addresses

These libraries are used internally by the other libraries, but they are available also for external use.

**Code example**

    from retro_bip_utils import EthAddr, XrpAddr

    # Ethereum needs the uncompressed public key
    addr = EthAddr.ToAddress(pub_key_bytes)
    # Ripple needs the compressed public key
    addr = XrpAddr.ToAddress(pub_key_bytes)

## P2PKH/P2SH/P2WPKH addresses

These libraries are used internally by the other libraries, but they are available also for external use.

**Code example**

    from retro_bip_utils import P2PKH, P2SH, P2WPKH

    # P2PKH addresses (the default uses Bitcoin network address version, you can pass a different one as second parameter)
    addr = P2PKH.ToAddress(pub_key_bytes)
    # P2SH addresses (the default uses Bitcoin network address version, you can pass a different one as second parameter)
    addr = P2SH.ToAddress(pub_key_bytes)
    # P2WPKH addresses (the default uses Bitcoin network address version, you can pass a different one as second parameter)
    addr = P2WPKH.ToAddress(pub_key_bytes)

## WIF

This library is used internally by the other libraries, but it's available also for external use.

**Code example**

    import binascii
    from retro_bip_utils import WifDecoder, WifEncoder

    key_bytes = binascii.unhexlify(b'1837c1be8e2995ec11cda2b066151be2cfb48adf9e47b151d46adab3a21cdf67')

    # Encode
    enc = WifEncoder.Encode(key_bytes)
    # Decode
    dec = WifDecoder.Decode(enc)

## Base58

This library is used internally by the other libraries, but it's available also for external use.\
It supports both normal encode/decode and check_encode/check_decode with Bitcoin and Ripple alphabets (if not specified, the Bitcoin one will be used by default).

**Code example**

    import binascii
    from retro_bip_utils import Base58Decoder, Base58Encoder, Base58Alphabets

    data_bytes = binascii.unhexlify(b"636363")

    # Normal encode
    enc     = Base58Encoder.Encode(data_bytes)
    # Check encode
    chk_enc = Base58Encoder.CheckEncode(data_bytes)

    # Normal decode
    dec     = Base58Decoder.Decode(enc)
    # Check decode, RuntimeError is raised if checksum verification fails
    chk_dec = Base58Decoder.CheckDecode(chk_enc)

    # Same as before with Ripple alphabet
    enc     = Base58Encoder.Encode(data_bytes, Base58Alphabets.RIPPLE)
    chk_enc = Base58Encoder.CheckEncode(data_bytes, Base58Alphabets.RIPPLE)
    dec     = Base58Decoder.Decode(enc, Base58Alphabets.RIPPLE)
    chk_dec = Base58Decoder.CheckDecode(chk_enc, Base58Alphabets.RIPPLE)


## Bech32

This library is used internally by the other libraries, but it's available also for external use.

**Code example**

    import binascii
    from retro_bip_utils import Bech32Decoder, Bech32Encoder

    data_bytes = binascii.unhexlify(b'9c90f934ea51fa0f6504177043e0908da6929983')

    # Encode
    enc = Bech32Encoder.EncodeAddr("bc", 0, data_bytes)
    # Decode
    dec = Bech32Decoder.DecodeAddr("bc", enc)

## Complete code example

Example from mnemonic generation to wallet addresses.

    from retro_bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip44, Bip44Coins, Bip44Changes

    # Generate random mnemonic
    mnemonic = Bip39MnemonicGenerator.FromWordsNumber(12)
    print("Mnemonic string: %s" % mnemonic)
    # Generate seed from mnemonic
    seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

    # Generate BIP44 master keys
    bip_obj_mst = Bip44.FromSeed(seed_bytes, Bip44Coins.BITCOIN)
    # Print master key
    print("Master key (bytes): %s" % bip_obj_mst.PrivateKey().Raw().ToHex())
    print("Master key (extended): %s" % bip_obj_mst.PrivateKey().ToExtended())
    print("Master key (WIF): %s" % bip_obj_mst.PrivateKey().ToWif())

    # Generate BIP44 account keys: m/44'/0'/0'
    bip_obj_acc = bip_obj_mst.Purpose().Coin().Account(0)
    # Generate BIP44 chain keys: m/44'/0'/0'/0
    bip_obj_chain = bip_obj_acc.Change(Bip44Changes.CHAIN_EXT)

    # Generate the address pool (first 20 addresses): m/44'/0'/0'/0/i
    for i in range(20):
        bip_obj_addr = bip_obj_chain.AddressIndex(i)
        print("%d. Address public key (extended): %s" % (i, bip_obj_addr.PublicKey().ToExtended()))
        print("%d. Address private key (extended): %s" % (i, bip_obj_addr.PrivateKey().ToExtended()))
        print("%d. Address: %s" % (i, bip_obj_addr.PublicKey().ToAddress()))

# License

This software is available under the MIT license.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/laalaguer/retro_bip_utils",
    "name": "retro-bip-utils",
    "maintainer": "Emanuele Bellocchia, A Byte Ahead",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "laalaguer@gmail.com",
    "keywords": "bitcoin,litecoin,dogecoin,dash,ethereum,ripple,wallet,hd-wallet,bip39,bip32,bip44,bip49,bip84,python",
    "author": "Emanuele Bellocchia, A Byte Ahead",
    "author_email": "laalaguer@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/40/3e/88f308b85422f8d6fc828e024800b82f50cbfbb3c080ea8ab77134fb9d57/retro_bip_utils-1.0.5.tar.gz",
    "platform": "any",
    "description": "# BIP utility library\n[![PyPI version](https://badge.fury.io/py/retro-bip-utils.svg)](https://badge.fury.io/py/retro-bip-utils)\n[![Build Status](https://travis-ci.com/laalaguer/retro_bip_utils.svg?branch=master)](https://travis-ci.com/laalaguer/retro_bip_utils)\n[![codecov](https://codecov.io/gh/laalaguer/retro_bip_utils/branch/master/graph/badge.svg)](https://codecov.io/gh/laalaguer/retro_bip_utils)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/laalaguer/retro_bip_utils/master/LICENSE)\n\n## About the Fork\n\nThis fork aims at at retro version v1.0.5 of original `ebellocchia/bip_utils`. The reason is quite simple: the simplicity of the code dependency.\n\nThis fork removes one of the dependency: `pysha3` (discontinued > Python 3.6) and uses `eth-hash` instead (underhood, it uses `pysha3` < Python 3.6 and `safe-pysha3` > Python 3.6).\n\n## Introduction\n\nThis package contains an implementation of some BIP (Bitcoin Improvement Proposal) specifications, allowing to:\n- Generate a mnemonic string from a random entropy\n- Generate a secure seed from the mnemonic string\n- Use the seed to generate the master key of the wallet and derive the children keys, including address encoding\n\nThe implemented BIP specifications are the following:\n- [BIP-0039](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) for mnemonic and seed generation\n- [BIP-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) for master key generation (from the secure seed) and children keys derivation\n- [BIP-0044](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki), [BIP-0049](https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki) and [BIP-0084](https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki) for the hierarchy of deterministic wallets, based on BIP-0032 specification\n\nIn addition to this, the package allows to:\n- Parse BIP-0032 derivation paths\n- Generate P2PKH addresses (included in BIP-0044)\n- Generate P2SH addresses (included in BIP-0049)\n- Generate P2WPKH addresses (included in BIP-0084)\n- Generate Ethereum addresses\n- Generate Ripple addresses\n- Encode/Decode [WIF](https://en.bitcoin.it/wiki/Wallet_import_format)\n- Encode/Decode [base58](https://en.bitcoin.it/wiki/Base58Check_encoding#Background)\n- Encode/Decode [bech32](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)\n\nThe currently supported coins are:\n- Bitcoin (and related test net)\n- Litecoin (and related test net)\n- Dogecoin (and related test net)\n- Dash (and related test net)\n- Ethereum\n- Ripple\n\n## Install the package\n\nThe package requires Python 3, it is not compatible with Python 2.\nTo install it:\n- Using *setuptools*:\n\n        python setup.py install\n\n- Using *pip*:\n\n        pip install retro_bip_utils\n\nTo run the tests:\n\n- Without code coverage\n\n        python setup.py test\n\n- With code coverage and report:\n\n        pip install coverage\n        coverage run -m unittest discover\n        coverage report\n\n## BIP-0039 library\n\n### Mnemonic generation\n\nA mnemonic string can be generated by specifying the words number (in this case a random entropy will be used) or directly the entropy bytes.\\\nSupported words number:\n- 12: *Bip39WordsNum.WORDS_NUM_12*\n- 15: *Bip39WordsNum.WORDS_NUM_15*\n- 18: *Bip39WordsNum.WORDS_NUM_18*\n- 21: *Bip39WordsNum.WORDS_NUM_21*\n- 24: *Bip39WordsNum.WORDS_NUM_24*\n\nSupported entropy bits:\n- 128: *Bip39EntropyBitLen.BIT_LEN_128*\n- 160: *Bip39EntropyBitLen.BIT_LEN_160*\n- 192: *Bip39EntropyBitLen.BIT_LEN_192*\n- 224: *Bip39EntropyBitLen.BIT_LEN_224*\n- 256: *Bip39EntropyBitLen.BIT_LEN_256*\n\n**NOTE:** only the English words list is currently supported.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import EntropyGenerator, Bip39MnemonicGenerator, Bip39WordsNum\n\n    # Generate a random mnemonic string of 15 words\n    mnemonic = Bip39MnemonicGenerator.FromWordsNumber(Bip39WordsNum.WORDS_NUM_15)\n\n    # Generate the mnemonic string from entropy bytes:\n    entropy_bytes_hex = b\"00000000000000000000000000000000\"\n    mnemonic = Bip39MnemonicGenerator.FromEntropy(binascii.unhexlify(entropy_bytes_hex))\n\n    # Generate mnemonic from random 192-bit entropy\n    entropy_bytes = EntropyGenerator(Bip39EntropyBitLen.BIT_LEN_192).Generate()\n    mnemonic = Bip39MnemonicGenerator.FromEntropy(entropy_bytes)\n\n### Mnemonic validation\n\nA mnemonic string can be validated by verifying its checksum.\nIt is also possible to get back the entropy bytes from a mnemonic.\n\n**Code example**\n\n     from retro_bip_utils import Bip39MnemonicValidator\n\n     # Get back the original entropy from a mnemonic string\n     entropy_bytes = Bip39MnemonicValidator(mnemonic).GetEntropy()\n     # Validate a mnemonic string by verifying its checksum\n     is_valid = Bip39MnemonicValidator(mnemonic).Validate()\n\n### Seed generation\n\nA secure 64-byte seed is generated from a mnemonic and can be protected by a passphrase.\\\nThis seed can be used to contruct a Bip class.\n\n**Code example**\n\n    from retro_bip_utils import Bip39SeedGenerator\n\n    # If not specified, the passphrase will be empty\n    passphrase = \"my_passphrase\"\n    seed_bytes = Bip39SeedGenerator(mnemonic).Generate(passphrase)\n\n## BIP-0032 library\n\nThe BIP-0032 library is wrapped inside the BIP-0044, BIP-0049 and BIP-0084 libraries, so there is no need to use it alone unless you need to derive some non-standard paths.\n\n### Construction from a seed\n\nThe class can be constructed from a seed. The seed can be specified manually or generated by *Bip39SeedGenerator*.\\\nThe constructed class is the master path, so printing the private key will result in printing the master key.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import Bip32\n\n    # Seed bytes\n    seed_bytes = binascii.unhexlify(b\"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4\")\n    # Construct from seed. In case it's a test net, pass True as second parameter. Derivation path returned: m\n    bip32_ctx = Bip32.FromSeed(seed_bytes)\n    # Print master key in extended format\n    print(bip32_ctx.PrivateKey().ToExtended())\n\nIn addition to a seed, it's also possible to specify a derivation path.\n\n**Code example**\n\n    # Derivation path returned: m/0'/1'/2\n    bip32_ctx = Bip32.FromSeedAndPath(seed_bytes, \"m/0'/1'/2\")\n    # Print private key for derivation path m/0'/1'/2 in extended format\n    print(bip32_ctx.PrivateKey().ToExtended())\n\n### Construction from an extended key\n\nAlternatively, the class can be constructed directly from an extended key.\\\nThe object returned will be at the same depth of the specified key.\n\n**Code example**\n\n    from retro_bip_utils import Bip32\n\n    # Private extended key from derivation path m/0'/1 (depth 2)\n    key_str = \"xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs\"\n    # Construct from key (return object has depth 2)\n    bip32_ctx = Bip32.FromExtendedKey(key_str)\n    # Print keys\n    print(bip32_ctx.PrivateKey().ToExtended())\n    print(bip32_ctx.PublicKey().ToExtended())\n\n    # Public extended key from derivation path m/0'/1 (depth 2)\n    key_str = \"xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ\"\n    # Construct from key (return object has depth 2)\n    bip32_ctx = Bip32.FromExtendedKey(key_str)\n    # Print key\n    print(bip32_ctx.PublicKey().ToExtended())\n    # Getting private key from a public-only object triggers a Bip32KeyError exception\n\n### Keys derivation\n\nEach time a key is derived, a new instance of the Bip32 class is returned. This allows to chain the methods call or save a specific key pair for future derivation.\\\nThe *Bip32Utils.HardenIndex* method can be used to make an index hardened.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import Bip32, Bip32Utils\n\n    # Seed bytes\n    seed_bytes = binascii.unhexlify(b\"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4\")\n    # Path: m\n    bip32_ctx = Bip32.FromSeed(seed_bytes)\n    # Derivation path: m/0'/1'/2/3\n    bip32_ctx = bip32_ctx.ChildKey(Bip32Utils.HardenIndex(0)) \\\n                         .ChildKey(Bip32Utils.HardenIndex(1)) \\\n                         .ChildKey(2)                         \\\n                         .ChildKey(3)\n    # Print keys in extended format\n    print(bip32_ctx.PrivateKey().ToExtended())\n    print(bip32_ctx.PublicKey().ToExtended())\n    # Print keys in hex format\n    print(bip32_ctx.PrivateKey().Raw().ToHex())\n    print(bip32_ctx.PublicKey().RawCompressed().ToHex())\n    print(bip32_ctx.PublicKey().RawUncompressed().ToHex())\n    # Print private key in WIF format\n    print(bip32_ctx.PrivateKey().ToWif())\n    # Print public key converted to address\n    print(bip32_ctx.PublicKey().ToAddress())\n\n    # Alternative: use DerivePath method\n    bip32_ctx = Bip32.FromSeed(seed_bytes)\n    bip32_ctx = bip32_ctx.DerivePath(\"0'/1'/2/3\")\n\n    # DerivePath derives from the current depth, so it can be split\n    bip32_ctx = Bip32.FromSeed(seed_bytes)\n    bip32_ctx = bip32_ctx.DerivePath(\"0'/1'\")   # Derivation path: m/0'/1'\n    bip32_ctx = bip32_ctx.DerivePath(\"2/3\")     # Derivation path: m/0'/1'/2/3\n\n### Parse path\n\nThe Bip32 module allows also to parse derivation paths by returning the list of indexes in the path.\\\nIn case of error, an empty list is returned.\n\n**Code example**\n\n    from retro_bip_utils import Bip32PathParser\n\n    # Print: [\"m\", 2147483648, 2147483649, 2]\n    print(Bip32PathParser.Parse(\"m/0'/1'/2\"))\n    # Same but skipping the master. Print: [2147483648, 2147483649, 2]\n    print(Bip32PathParser.Parse(\"0'/1'/2\", True))\n    # 'p' can be used as an alternative character instead of '\n    print(Bip32PathParser.Parse(\"m/0p/1p/2\"))\n    # Error path: empty list returned. Print: []\n    print(Bip32PathParser.Parse(\"m/0'/abc/2\"))\n\n## Bip-0044, BIP-0049, BIP-0084 libraries\n\nThese libraries derives all from the same base class, so they are used exactly in the same way.\\\nTherefore, the following code examples can be used with the Bip44, Bip49 or Bip84 class.\n\n### Construction from a seed\n\nA Bip class can be constructed from a seed, like Bip32. The seed can be specified manually or generated by *Bip39SeedGenerator*.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import Bip44, Bip44Coins\n\n    # Seed bytes\n    seed_bytes = binascii.unhexlify(b\"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4\")\n    # Derivation path returned: m\n    # In case it's a test net, pass True as second parameter\n    bip44_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.BITCOIN)\n\n### Construction from an extended key\n\nAlternatively, a Bip class can be constructed directly from an extended key.\\\nThe Bip object returned will be at the same depth of the specified key.\n\n**Code example**\n\n    from retro_bip_utils import Bip44, Bip44Coins\n\n    # Private extended key\n    key_str = \"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi\"\n    # Construct from extended key\n    bip44_ctx = Bip44.FromExtendedKey(key_str, Bip44Coins.BITCOIN)\n\n### Keys derivation\n\nLike Bip32, each time a key is derived a new instance of the Bip class is returned.\\\nThe keys must be derived with the levels specified by BIP-0044:\n\n    m / purpose' / coin_type' / account' / change / address_index\n\nusing the correspondent methods. If keys are derived in the wrong level, a *RuntimeError* will be raised.\\\nThe private and public extended keys can be printed at any level.\n\nCurrently supported coins enumerative:\n- Bitcoin (and related test net) : *Bip44Coins.BITCOIN, Bip44Coins.BITCOIN_TESTNET*\n- Litecoin (and related test net) : *Bip44Coins.LITECOIN, Bip44Coins.LITECOIN_TESTNET*\n- Dogecoin (and related test net) : *Bip44Coins.DOGECOIN, Bip44Coins.DOGECOIN_TESTNET*\n- Dash (and related test net) : *Bip44Coins.DASH, Bip44Coins.DASH_TESTNET*\n- Ethereum : *Bip44Coins.ETHEREUM*\n- Ripple : *Bip44Coins.RIPPLE*\n\nThe library can be easily extended with other coins anyway.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import Bip44, Bip44Coins, Bip44Changes\n\n    # Seed bytes\n    seed_bytes = binascii.unhexlify(b\"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4\")\n    # Create from seed\n    bip44_mst = Bip44.FromSeed(seed_bytes, Bip44Coins.BITCOIN)\n\n    # Print master key in extended format\n    print(bip44_mst.PrivateKey().ToExtended())\n    # Print master key in hex format\n    print(bip44_mst.PrivateKey().Raw().ToHex())\n\n    # Print public key in extended format (default: Bip44PubKeyTypes.EXT_KEY)\n    print(bip44_mst.PublicKey())\n    # Print public key in raw uncompressed format\n    print(bip44_mst.PublicKey().RawUncompressed().ToHex())\n    # Print public key in raw compressed format\n    print(bip44_mst.PublicKey().RawCompressed().ToHex())\n\n    # Print the master key in WIF\n    print(bip44_mst.IsMasterLevel())\n    print(bip44_mst.PrivateKey().ToWif())\n\n    # Derive account 0 for Bitcoin: m/44'/0'/0'\n    bip44_acc = bip44_mst.Purpose() \\\n                         .Coin()    \\\n                         .Account(0)\n    # Print keys in extended format\n    print(bip44_acc.IsAccountLevel())\n    print(bip44_acc.PrivateKey().ToExtended())\n    print(bip44_acc.PublicKey().ToExtended())\n\n    # Derive the external chain: m/44'/0'/0'/0\n    bip44_change = bip44_acc.Change(Bip44Changes.CHAIN_EXT)\n    # Print again keys in extended format\n    print(bip44_change.IsChangeLevel())\n    print(bip44_change.PrivateKey().ToExtended())\n    print(bip44_change.PublicKey().ToExtended())\n\n    # Derive the first 20 addresses of the external chain: m/44'/0'/0'/0/i\n    for i in range(20):\n        bip44_addr = bip44_change.AddressIndex(i)\n        # Print extended keys and address\n        print(bip44_addr.PrivateKey().ToExtended())\n        print(bip44_addr.PublicKey().ToExtended())\n        print(bip44_addr.PublicKey().ToAddress())\n\nIn the example above, Bip44 can be substituted with Bip49 or Bip84 without changing the code.\n\n## Ethereum/Ripple addresses\n\nThese libraries are used internally by the other libraries, but they are available also for external use.\n\n**Code example**\n\n    from retro_bip_utils import EthAddr, XrpAddr\n\n    # Ethereum needs the uncompressed public key\n    addr = EthAddr.ToAddress(pub_key_bytes)\n    # Ripple needs the compressed public key\n    addr = XrpAddr.ToAddress(pub_key_bytes)\n\n## P2PKH/P2SH/P2WPKH addresses\n\nThese libraries are used internally by the other libraries, but they are available also for external use.\n\n**Code example**\n\n    from retro_bip_utils import P2PKH, P2SH, P2WPKH\n\n    # P2PKH addresses (the default uses Bitcoin network address version, you can pass a different one as second parameter)\n    addr = P2PKH.ToAddress(pub_key_bytes)\n    # P2SH addresses (the default uses Bitcoin network address version, you can pass a different one as second parameter)\n    addr = P2SH.ToAddress(pub_key_bytes)\n    # P2WPKH addresses (the default uses Bitcoin network address version, you can pass a different one as second parameter)\n    addr = P2WPKH.ToAddress(pub_key_bytes)\n\n## WIF\n\nThis library is used internally by the other libraries, but it's available also for external use.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import WifDecoder, WifEncoder\n\n    key_bytes = binascii.unhexlify(b'1837c1be8e2995ec11cda2b066151be2cfb48adf9e47b151d46adab3a21cdf67')\n\n    # Encode\n    enc = WifEncoder.Encode(key_bytes)\n    # Decode\n    dec = WifDecoder.Decode(enc)\n\n## Base58\n\nThis library is used internally by the other libraries, but it's available also for external use.\\\nIt supports both normal encode/decode and check_encode/check_decode with Bitcoin and Ripple alphabets (if not specified, the Bitcoin one will be used by default).\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import Base58Decoder, Base58Encoder, Base58Alphabets\n\n    data_bytes = binascii.unhexlify(b\"636363\")\n\n    # Normal encode\n    enc     = Base58Encoder.Encode(data_bytes)\n    # Check encode\n    chk_enc = Base58Encoder.CheckEncode(data_bytes)\n\n    # Normal decode\n    dec     = Base58Decoder.Decode(enc)\n    # Check decode, RuntimeError is raised if checksum verification fails\n    chk_dec = Base58Decoder.CheckDecode(chk_enc)\n\n    # Same as before with Ripple alphabet\n    enc     = Base58Encoder.Encode(data_bytes, Base58Alphabets.RIPPLE)\n    chk_enc = Base58Encoder.CheckEncode(data_bytes, Base58Alphabets.RIPPLE)\n    dec     = Base58Decoder.Decode(enc, Base58Alphabets.RIPPLE)\n    chk_dec = Base58Decoder.CheckDecode(chk_enc, Base58Alphabets.RIPPLE)\n\n\n## Bech32\n\nThis library is used internally by the other libraries, but it's available also for external use.\n\n**Code example**\n\n    import binascii\n    from retro_bip_utils import Bech32Decoder, Bech32Encoder\n\n    data_bytes = binascii.unhexlify(b'9c90f934ea51fa0f6504177043e0908da6929983')\n\n    # Encode\n    enc = Bech32Encoder.EncodeAddr(\"bc\", 0, data_bytes)\n    # Decode\n    dec = Bech32Decoder.DecodeAddr(\"bc\", enc)\n\n## Complete code example\n\nExample from mnemonic generation to wallet addresses.\n\n    from retro_bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip44, Bip44Coins, Bip44Changes\n\n    # Generate random mnemonic\n    mnemonic = Bip39MnemonicGenerator.FromWordsNumber(12)\n    print(\"Mnemonic string: %s\" % mnemonic)\n    # Generate seed from mnemonic\n    seed_bytes = Bip39SeedGenerator(mnemonic).Generate()\n\n    # Generate BIP44 master keys\n    bip_obj_mst = Bip44.FromSeed(seed_bytes, Bip44Coins.BITCOIN)\n    # Print master key\n    print(\"Master key (bytes): %s\" % bip_obj_mst.PrivateKey().Raw().ToHex())\n    print(\"Master key (extended): %s\" % bip_obj_mst.PrivateKey().ToExtended())\n    print(\"Master key (WIF): %s\" % bip_obj_mst.PrivateKey().ToWif())\n\n    # Generate BIP44 account keys: m/44'/0'/0'\n    bip_obj_acc = bip_obj_mst.Purpose().Coin().Account(0)\n    # Generate BIP44 chain keys: m/44'/0'/0'/0\n    bip_obj_chain = bip_obj_acc.Change(Bip44Changes.CHAIN_EXT)\n\n    # Generate the address pool (first 20 addresses): m/44'/0'/0'/0/i\n    for i in range(20):\n        bip_obj_addr = bip_obj_chain.AddressIndex(i)\n        print(\"%d. Address public key (extended): %s\" % (i, bip_obj_addr.PublicKey().ToExtended()))\n        print(\"%d. Address private key (extended): %s\" % (i, bip_obj_addr.PrivateKey().ToExtended()))\n        print(\"%d. Address: %s\" % (i, bip_obj_addr.PublicKey().ToAddress()))\n\n# License\n\nThis software is available under the MIT license.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Implementation of BIP39, BIP32, BIP44, BIP49 and BIP84 for wallet seeds, keys and addresses generation. Supported coins: Bitcoin, Litecoin, Dogecoin, Ethereum.",
    "version": "1.0.5",
    "project_urls": {
        "Download": "https://github.com/laalaguer/retro_bip_utils/archive/v1.0.5.tar.gz",
        "Homepage": "https://github.com/laalaguer/retro_bip_utils"
    },
    "split_keywords": [
        "bitcoin",
        "litecoin",
        "dogecoin",
        "dash",
        "ethereum",
        "ripple",
        "wallet",
        "hd-wallet",
        "bip39",
        "bip32",
        "bip44",
        "bip49",
        "bip84",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "01706c0726001beec7ef5070d4809c9f8bb87808146bf18a4fd16cd390864b69",
                "md5": "e031bd2a96d9707b28dd690ec07f8d0c",
                "sha256": "3209ba5e99edd3aaf74e07ff13f94b59bd39ecb3d963af1dde9428ff6a29f4eb"
            },
            "downloads": -1,
            "filename": "retro_bip_utils-1.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e031bd2a96d9707b28dd690ec07f8d0c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 68480,
            "upload_time": "2023-07-25T08:49:21",
            "upload_time_iso_8601": "2023-07-25T08:49:21.198363Z",
            "url": "https://files.pythonhosted.org/packages/01/70/6c0726001beec7ef5070d4809c9f8bb87808146bf18a4fd16cd390864b69/retro_bip_utils-1.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "403e88f308b85422f8d6fc828e024800b82f50cbfbb3c080ea8ab77134fb9d57",
                "md5": "91736806afc73fde1b59f2d0e939a0de",
                "sha256": "b7db3526575482c7e136e18b779b6158d78bec229720b0bf8910198e2878257c"
            },
            "downloads": -1,
            "filename": "retro_bip_utils-1.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "91736806afc73fde1b59f2d0e939a0de",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 42982,
            "upload_time": "2023-07-25T08:49:22",
            "upload_time_iso_8601": "2023-07-25T08:49:22.956575Z",
            "url": "https://files.pythonhosted.org/packages/40/3e/88f308b85422f8d6fc828e024800b82f50cbfbb3c080ea8ab77134fb9d57/retro_bip_utils-1.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-25 08:49:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "laalaguer",
    "github_project": "retro_bip_utils",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "tox": true,
    "lcname": "retro-bip-utils"
}
        
Elapsed time: 0.12949s