bitcoin-utils


Namebitcoin-utils JSON
Version 0.6.6 PyPI version JSON
download
home_pagehttps://github.com/karask/python-bitcoin-utils
SummaryBitcoin utility functions
upload_time2024-03-13 12:49:42
maintainer
docs_urlNone
authorKonstantinos Karasavvas
requires_python
licenseMIT
keywords bitcoin library utilities tools
VCS
bugtrack_url
requirements base58check ecdsa sympy python-bitcoinrpc hdwallet
Travis-CI No Travis.
coveralls test coverage No coveralls.
            python-bitcoin-utils
====================

This is a bitcoin library that provides tools/utilities to interact with the Bitcoin network. One of the primary goals of the library is to explain the low-level details of Bitcoin. The code is easy to read and properly documented explaining in detail all the thorny aspects of the implementation. It is a low-level library which assumes some high-level understanding of how Bitcoin works. In the future this might change.

This is an early version of the library (v0.6.6) and currently, it supports private/public keys, all type of addresses and creation of any transaction, incl. segwit and taproot, with all SIGHASH types. All script op codes are included. Extra functionality will be added continuously and the documentation will be improved as the work progresses.

The API documentation can be build with Sphinx but is also available as a PDF for convenience. One can currently use the library for experimenting and learning the inner workings of Bitcoin. It is not meant for production yet and parts of the API might be updated with new versions.

Complementary to this library is a CC BY-SA 4.0 licensed `Bitcoin programming book <https://github.com/karask/bitcoin-textbook>`_.


Notes
-----
* For schnorr, bech32[m], ripemd160 the python Bitcoin Core reference implementations are used.
* For making calls to a Bitcoin node a simple node proxy object exists, which wraps the python-bitcoinrpc library.
* For Hierarchical Deterministic keys we wrap the python hdwallet library. For now we wrap only some very basic functionality to acquire a PrivateKey object that is used throughtout the library.


Installation
------------
Python version 3.10 and above is required. Then just install with:

$ pip install bitcoin-utils


Examples
--------

Keys and Addresses
^^^^^^^^^^^^^^^^^^

Legacy Keys and Addresses
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_addresses.py - creates a private key which we use to derive a public key and in turn an address. We also use the private key to sign a message and then verify it using the public key. 

Segwit Addresses
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_segwit_addresses.py - creates P2WPKH, P2SH-P2WPKH, P2WSH and P2SH-P2WSH addresses.

Hierarchical Deterministic Keys
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/hd_keys.py - creates an extended private key, from an xpriv/tpriv and path, which we use to derive a public key and in turn all different address (legacy, segwit v0 and taproot (segwit v1).

Legacy Transactions (P2PKH, P2SH)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Transaction with P2PKH input and outputs
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/p2pkh_transaction.py - creates a simple transaction with one input and two outputs.

Create a P2PKH Transaction with different SIGHASHes
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/multi_input_sighash_transaction.py - creates a 2-input 2-output transaction with different signature types.

Create a P2SH Address
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2sh_transaction.py - creates a P2SH address that corresponds to a P2PK redeem script and sends some funds to it.

Create (spent) a P2SH Transaction
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_transaction.py - creates a transaction that spends a P2SH output.

Non-standard Transactions
^^^^^^^^^^^^^^^^^^^^^^^^^

Create a non-standard tx
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/create_non_std_tx.py - sends funds to an address with a non-standard tx (script: OP_ADD OP_5 OP_EQUAL)

Spend a non-standard tx
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_non_std_tx.py - spends funds from script OP_ADD OP_5 OP_EQUAL 

Segwit Transactions
^^^^^^^^^^^^^^^^^^^

Transaction to pay to a P2WPKH
  http://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2wpkh_transaction.py - send coins from two P2PKH UTXOs to a native segwit address (P2WPKH)

Spend from a P2SH(P2WPKH) nested segwit address
   http://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_p2wpkh_address.py - spend a P2WPKH that is nested into a P2SH for old client compatibility

Transaction to pay to a P2SH(P2WSH(P2PK))
  http://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2sh_p2wsh_p2pk_address.py - send coins from a P2PKH UTXO to a P2SH(P2WSH(P2PK))

Spend from a P2SH(P2WPKH) nested segwit address
   http://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_from_p2sh_p2wsh_p2pk_address.py - spend a P2WSH with a P2PK as witness script that is nested into a P2SH for old client compatibility


Timelock Transactions
^^^^^^^^^^^^^^^^^^^^^

Create a P2SH address with a relative timelock
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/create_p2sh_csv_p2pkh_address.py - creates a P2SH address that locks funds (sent to it) with a private key (P2PKH) and a relative locktime of 200 blocks in the future.

Spend from a timelocked address
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_csv_p2pkh.py - spends from a P2SH(CSV+P2PKH) address as created from above.

Taproot (segwit v1) Transactions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Spend from a taproot address
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_default_path.py - single input, single output default key path spending.

Spend a multi input that contains both taproot and legacy UTXOs
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_multi_input_p2tr_and_p2pkh.py - three inputs (two taproot and one legacy), single legacy output.

Send to taproot address that contains a single script path spend
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2tr_with_single_script.py - single input, single output (key path and single script path).

Spend taproot from key path (has single alternative script path spend)
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_single_script_by_key_path.py - single input, single output, spend key path.

Spend taproot from script path (has single alternative script path spend)
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_single_script_by_script_path.py - single input, single output, spend script path.

Send to taproot address that contains two scripts path spends
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2tr_with_two_scripts.py - single input, single output (key path and two script paths - A and B).

Spend taproot from script path (has two alternative script path spend - A and B)
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_two_scripts_by_script_path.py - single input, single output, spend script path A.

Send to taproot address that contains three scripts path spends
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2tr_with_three_scripts.py - single input, single output (key path and three script paths - A, B and C).

Spend taproot from script path (has three alternative script path spends - A, B and C)
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_three_scripts_by_script_path.py - single input, single output, spend script path B.

Other
^^^^^

Use NodeProxy to make calls to a Bitcoin node
  https://github.com/karask/python-bitcoin-utils/blob/master/examples/node_proxy.py - make Bitcoin command-line interface calls programmatically (NodeProxy wraps jsonrpc-requests library)


Please explore the codebase or the API documentation (BitcoinUtilities.pdf) for supported functionality and other options.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/karask/python-bitcoin-utils",
    "name": "bitcoin-utils",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "bitcoin library utilities tools",
    "author": "Konstantinos Karasavvas",
    "author_email": "kkarasavvas@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ab/a0/791c8015cea15be61f46c49a5abf2bf2fd52f6e3158509fc4db7b87765be/bitcoin-utils-0.6.6.tar.gz",
    "platform": null,
    "description": "python-bitcoin-utils\n====================\n\nThis is a bitcoin library that provides tools/utilities to interact with the Bitcoin network. One of the primary goals of the library is to explain the low-level details of Bitcoin. The code is easy to read and properly documented explaining in detail all the thorny aspects of the implementation. It is a low-level library which assumes some high-level understanding of how Bitcoin works. In the future this might change.\n\nThis is an early version of the library (v0.6.6) and currently, it supports private/public keys, all type of addresses and creation of any transaction, incl. segwit and taproot, with all SIGHASH types. All script op codes are included. Extra functionality will be added continuously and the documentation will be improved as the work progresses.\n\nThe API documentation can be build with Sphinx but is also available as a PDF for convenience. One can currently use the library for experimenting and learning the inner workings of Bitcoin. It is not meant for production yet and parts of the API might be updated with new versions.\n\nComplementary to this library is a CC BY-SA 4.0 licensed `Bitcoin programming book <https://github.com/karask/bitcoin-textbook>`_.\n\n\nNotes\n-----\n* For schnorr, bech32[m], ripemd160 the python Bitcoin Core reference implementations are used.\n* For making calls to a Bitcoin node a simple node proxy object exists, which wraps the python-bitcoinrpc library.\n* For Hierarchical Deterministic keys we wrap the python hdwallet library. For now we wrap only some very basic functionality to acquire a PrivateKey object that is used throughtout the library.\n\n\nInstallation\n------------\nPython version 3.10 and above is required. Then just install with:\n\n$ pip install bitcoin-utils\n\n\nExamples\n--------\n\nKeys and Addresses\n^^^^^^^^^^^^^^^^^^\n\nLegacy Keys and Addresses\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_addresses.py - creates a private key which we use to derive a public key and in turn an address. We also use the private key to sign a message and then verify it using the public key. \n\nSegwit Addresses\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_segwit_addresses.py - creates P2WPKH, P2SH-P2WPKH, P2WSH and P2SH-P2WSH addresses.\n\nHierarchical Deterministic Keys\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/hd_keys.py - creates an extended private key, from an xpriv/tpriv and path, which we use to derive a public key and in turn all different address (legacy, segwit v0 and taproot (segwit v1).\n\nLegacy Transactions (P2PKH, P2SH)\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nTransaction with P2PKH input and outputs\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/p2pkh_transaction.py - creates a simple transaction with one input and two outputs.\n\nCreate a P2PKH Transaction with different SIGHASHes\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/multi_input_sighash_transaction.py - creates a 2-input 2-output transaction with different signature types.\n\nCreate a P2SH Address\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2sh_transaction.py - creates a P2SH address that corresponds to a P2PK redeem script and sends some funds to it.\n\nCreate (spent) a P2SH Transaction\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_transaction.py - creates a transaction that spends a P2SH output.\n\nNon-standard Transactions\n^^^^^^^^^^^^^^^^^^^^^^^^^\n\nCreate a non-standard tx\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/create_non_std_tx.py - sends funds to an address with a non-standard tx (script: OP_ADD OP_5 OP_EQUAL)\n\nSpend a non-standard tx\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_non_std_tx.py - spends funds from script OP_ADD OP_5 OP_EQUAL \n\nSegwit Transactions\n^^^^^^^^^^^^^^^^^^^\n\nTransaction to pay to a P2WPKH\n  http://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2wpkh_transaction.py - send coins from two P2PKH UTXOs to a native segwit address (P2WPKH)\n\nSpend from a P2SH(P2WPKH) nested segwit address\n   http://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_p2wpkh_address.py - spend a P2WPKH that is nested into a P2SH for old client compatibility\n\nTransaction to pay to a P2SH(P2WSH(P2PK))\n  http://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2sh_p2wsh_p2pk_address.py - send coins from a P2PKH UTXO to a P2SH(P2WSH(P2PK))\n\nSpend from a P2SH(P2WPKH) nested segwit address\n   http://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_from_p2sh_p2wsh_p2pk_address.py - spend a P2WSH with a P2PK as witness script that is nested into a P2SH for old client compatibility\n\n\nTimelock Transactions\n^^^^^^^^^^^^^^^^^^^^^\n\nCreate a P2SH address with a relative timelock\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/create_p2sh_csv_p2pkh_address.py - creates a P2SH address that locks funds (sent to it) with a private key (P2PKH) and a relative locktime of 200 blocks in the future.\n\nSpend from a timelocked address\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_csv_p2pkh.py - spends from a P2SH(CSV+P2PKH) address as created from above.\n\nTaproot (segwit v1) Transactions\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nSpend from a taproot address\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_default_path.py - single input, single output default key path spending.\n\nSpend a multi input that contains both taproot and legacy UTXOs\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_multi_input_p2tr_and_p2pkh.py - three inputs (two taproot and one legacy), single legacy output.\n\nSend to taproot address that contains a single script path spend\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2tr_with_single_script.py - single input, single output (key path and single script path).\n\nSpend taproot from key path (has single alternative script path spend)\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_single_script_by_key_path.py - single input, single output, spend key path.\n\nSpend taproot from script path (has single alternative script path spend)\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_single_script_by_script_path.py - single input, single output, spend script path.\n\nSend to taproot address that contains two scripts path spends\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2tr_with_two_scripts.py - single input, single output (key path and two script paths - A and B).\n\nSpend taproot from script path (has two alternative script path spend - A and B)\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_two_scripts_by_script_path.py - single input, single output, spend script path A.\n\nSend to taproot address that contains three scripts path spends\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2tr_with_three_scripts.py - single input, single output (key path and three script paths - A, B and C).\n\nSpend taproot from script path (has three alternative script path spends - A, B and C)\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2tr_three_scripts_by_script_path.py - single input, single output, spend script path B.\n\nOther\n^^^^^\n\nUse NodeProxy to make calls to a Bitcoin node\n  https://github.com/karask/python-bitcoin-utils/blob/master/examples/node_proxy.py - make Bitcoin command-line interface calls programmatically (NodeProxy wraps jsonrpc-requests library)\n\n\nPlease explore the codebase or the API documentation (BitcoinUtilities.pdf) for supported functionality and other options.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Bitcoin utility functions",
    "version": "0.6.6",
    "project_urls": {
        "Homepage": "https://github.com/karask/python-bitcoin-utils"
    },
    "split_keywords": [
        "bitcoin",
        "library",
        "utilities",
        "tools"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "28087b6e673aa6e8cfa96a59d98754e14714d3e9c6169b3fe536caf211f38c2d",
                "md5": "494cf73c0eec8dad61a0f4b0f35bfe31",
                "sha256": "015ba8b05f74eb8eeab29c7f9983d89f2afebd2f684297a6f28c66597cb69879"
            },
            "downloads": -1,
            "filename": "bitcoin_utils-0.6.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "494cf73c0eec8dad61a0f4b0f35bfe31",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 75835,
            "upload_time": "2024-03-13T12:50:00",
            "upload_time_iso_8601": "2024-03-13T12:50:00.153386Z",
            "url": "https://files.pythonhosted.org/packages/28/08/7b6e673aa6e8cfa96a59d98754e14714d3e9c6169b3fe536caf211f38c2d/bitcoin_utils-0.6.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "aba0791c8015cea15be61f46c49a5abf2bf2fd52f6e3158509fc4db7b87765be",
                "md5": "1691534aa8548bd1683730ba20950b11",
                "sha256": "2a82cd48ebc49b6e8a485646f365f12cfd9630e9c3d17bca9bc03d25b51ed8c9"
            },
            "downloads": -1,
            "filename": "bitcoin-utils-0.6.6.tar.gz",
            "has_sig": false,
            "md5_digest": "1691534aa8548bd1683730ba20950b11",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 39146,
            "upload_time": "2024-03-13T12:49:42",
            "upload_time_iso_8601": "2024-03-13T12:49:42.044862Z",
            "url": "https://files.pythonhosted.org/packages/ab/a0/791c8015cea15be61f46c49a5abf2bf2fd52f6e3158509fc4db7b87765be/bitcoin-utils-0.6.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-13 12:49:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "karask",
    "github_project": "python-bitcoin-utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "base58check",
            "specs": [
                [
                    ">=",
                    "1.0.2"
                ],
                [
                    "<",
                    "2.0"
                ]
            ]
        },
        {
            "name": "ecdsa",
            "specs": [
                [
                    "==",
                    "0.17.0"
                ]
            ]
        },
        {
            "name": "sympy",
            "specs": [
                [
                    ">=",
                    "1.2"
                ],
                [
                    "<",
                    "2.0"
                ]
            ]
        },
        {
            "name": "python-bitcoinrpc",
            "specs": [
                [
                    "<",
                    "2.0"
                ],
                [
                    ">=",
                    "1.0"
                ]
            ]
        },
        {
            "name": "hdwallet",
            "specs": [
                [
                    "==",
                    "2.2.1"
                ]
            ]
        }
    ],
    "lcname": "bitcoin-utils"
}
        
Elapsed time: 0.20611s