btc-lib


Namebtc-lib JSON
Version 1.1.0 PyPI version JSON
download
home_pagehttps://github.com/qwerty-w/btc-lib
SummarySimple Bitcoin Library
upload_time2024-08-19 15:54:20
maintainerNone
docs_urlNone
authorqwerty-w
requires_python>=3.12
licenseMIT License Copyright (c) 2021 qwerty-w Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords bitcoin blockchain library simple btc lib
VCS
bugtrack_url
requirements base58check ecdsa pytest pytest-repeat httpx
Travis-CI No Travis.
coveralls test coverage No coveralls.
            btc-lib: Simple Bitcoin Library.
=======================

This library is a simple python cold wallet management library for the Bitcoin network. It allows you to generate private/public keys, get Bitcoin addresses from them, create transactions, deserialize already created ones, sign them, sign messages, create multi-signatures and multi-addresses using Bitcoin Script. But its most important difference from most similar libraries is support for Segwit (bech32 encoding) addresses and transactions with them. You can get address using PublicKey.get_address, or create it from your own hash.

**Examples:**

``` python

>>> from btclib import address, Input, Output, Transaction, Service, AddressType, NetworkType
>>>
>>> wif = 'cMtnJjkY8hBrNdNN1kPBCMuTM5h4rxes9nrfRfktTn8tW6HW2pC2'
>>> pv = address.PrivateKey.from_wif(wif)
>>> pb = pv.public
>>> pb.compressed, pb.network
(True, <NetworkType.TEST: 'testnet'>)
>>>
>>> pb = pb.change_network()  # create new instance with toggled network
>>> pb.get_address(AddressType.P2PKH).string
'12Nj1W9U7xvzbRFsMErK8hsm7pYGZv9jsT'
>>>
>>> pb.get_address(AddressType.P2SH_P2WPKH).string
'39YgiFhV8U5rWiUQLh5sDeGJvaft81k1sV'
>>>
>>> pb.get_address(AddressType.P2WPKH).string
'bc1qpuf7m9ysjtnxhpfvx80v6lptsk33lm2x3t9s5w'
>>>
>>> pb.get_address(AddressType.P2WSH).string
'bc1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqhehfwy'
>>>
>>>
>>> pb.network = NetworkType.TEST  # change network in same instance
>>> addr = pb.get_address(AddressType.P2WSH)
>>> addr.string
'tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'
>>>
>>> # To see address info:
>>> s = Service(NetworkType.TEST)
>>> ainf = s.get_address(addr)  # request to one of the blockchain APIs
>>> ainf
AddressInfo(received=100000, sent=0, tx_count=1, address=P2WSH.from_string('tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'))
>>> ainf.balance
100000
>>>
>>> # Get unspent outputs (UTXO):
>>> utxo = s.get_unspent(addr)
>>> utxo
[Unspent(748131e63a0c27a407316cdafb7cad20ec0994c856862d63e50e706073bc7f00, 0, 100000, block=2103815, address=P2WSH('tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'))]
>>>
>>> # Convert unspents to inputs
>>> # Input also contains a private key to be able to sign yourself in a transaction
>>> ins = [Input.from_unspent(u, pv, addr) for u in utxo]
>>> ous = [Output.from_address(address.from_string('tb1q7n075vj7tz4jm28zky7dzknuxujzl5vt6pxkz4'), 90000)]
>>>
>>> tx = Transaction(ins, ous)
>>> tx.default_sign()  # will be used: inp.default_sign() for inp in tx.inputs
>>>
>>> # Notice: inp.default_sign (tx) will try to sign itself in the tx transaction (set the desired inp.script / inp.witness value),
>>> # if inp.address was obtained using PublicKey.get_address, it will succeed, but if the address hash was generated by your custom script,
>>> # and the address object itself was obtained using address.<P2PKH/P2SH/P2WPKH/P2WSH>.from_hash, maybe the signature algorithm will differ
>>> # from the algorithm in inp.default_sign, for this use inp.custom_sign(script=Script(...), witness=Script(...)).
>>> # To summarize: if the address was obtained with PublicKey.get_address(), Input.default_sign will be able to sign it otherwise, use
>>> # inp.custom_sign with custom scripts.
>>>
>>> tx.serialize().hex()
'02000000000101007fbc7360700ee5632d8656c89409ec20ad7cfbda6c3107a4270c3ae63181740000000000ffffffff01905f010000000000160014f4dfea325e58ab2da8e2b13cd...'
>>> tx.id.hex()
'fff79b6d9f6a4068d5b8298c522177e9783af70d61653d628314e155a1e0e94e'
>>> b = s.push(tx)
>>> type(b)
<class 'btclib.transaction.BroadcastedTransaction'>
>>> b.get_confirmations(s.head())
0
>>>
>>> # After a while
>>> b = s.get_transaction(tx.id.hex())
>>> b
{'inputs': [{'txid': '748131e63a0c27a407316cdafb7cad20ec0994c856862d63e50e706073bc7f00', 'vout': 0, 'amount': 100000, 'script': '', 'witness': '473044022072909d3facf0377c1eee3b0000798ca0e76146777a026fd39cbe9307c3d73bb2022007d468e6f9985854d223a5f7742c6f312a48b4cdf9b64a46a6abb75939baf1830125512102e7b47c65a13f84fc934367d3d5be65015f62a56ab103d49df6aa25dacf540c0351ae', 'sequence': 4294967295}], 'outputs': [{'pkscript': '0014f4dfea325e58ab2da8e2b13cd15a7c37242fd18b', 'amount': 90000}], 'version': 2, 'locktime': 0}
>>> b.block
2103822
>>> b.get_confirmations(s.head())
718449
```

This transaction - https://live.blockcypher.com/btc-testnet/tx/fff79b6d9f6a4068d5b8298c522177e9783af70d61653d628314e155a1e0e94e/.

Installation
------------

btc-lib is distributed on `PyPI` and is available on Windows/Linux/macOS
and Windows and supports Python 3.12+.

```bash
$ python3 -m pip install btc-lib
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/qwerty-w/btc-lib",
    "name": "btc-lib",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "bitcoin, blockchain, library, simple, btc, lib",
    "author": "qwerty-w",
    "author_email": "qwerty-w <itsqwz@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/6f/a4/05ee71eb4db266d52bc40bd5062272f70c3ed3a61f1b9333842af082fb6e/btc_lib-1.1.0.tar.gz",
    "platform": null,
    "description": "btc-lib: Simple Bitcoin Library.\r\n=======================\r\n\r\nThis library is a simple python cold wallet management library for the Bitcoin network. It allows you to generate private/public keys, get Bitcoin addresses from them, create transactions, deserialize already created ones, sign them, sign messages, create multi-signatures and multi-addresses using Bitcoin Script. But its most important difference from most similar libraries is support for Segwit (bech32 encoding) addresses and transactions with them. You can get address using PublicKey.get_address, or create it from your own hash.\r\n\r\n**Examples:**\r\n\r\n``` python\r\n\r\n>>> from btclib import address, Input, Output, Transaction, Service, AddressType, NetworkType\r\n>>>\r\n>>> wif = 'cMtnJjkY8hBrNdNN1kPBCMuTM5h4rxes9nrfRfktTn8tW6HW2pC2'\r\n>>> pv = address.PrivateKey.from_wif(wif)\r\n>>> pb = pv.public\r\n>>> pb.compressed, pb.network\r\n(True, <NetworkType.TEST: 'testnet'>)\r\n>>>\r\n>>> pb = pb.change_network()  # create new instance with toggled network\r\n>>> pb.get_address(AddressType.P2PKH).string\r\n'12Nj1W9U7xvzbRFsMErK8hsm7pYGZv9jsT'\r\n>>>\r\n>>> pb.get_address(AddressType.P2SH_P2WPKH).string\r\n'39YgiFhV8U5rWiUQLh5sDeGJvaft81k1sV'\r\n>>>\r\n>>> pb.get_address(AddressType.P2WPKH).string\r\n'bc1qpuf7m9ysjtnxhpfvx80v6lptsk33lm2x3t9s5w'\r\n>>>\r\n>>> pb.get_address(AddressType.P2WSH).string\r\n'bc1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqhehfwy'\r\n>>>\r\n>>>\r\n>>> pb.network = NetworkType.TEST  # change network in same instance\r\n>>> addr = pb.get_address(AddressType.P2WSH)\r\n>>> addr.string\r\n'tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'\r\n>>>\r\n>>> # To see address info:\r\n>>> s = Service(NetworkType.TEST)\r\n>>> ainf = s.get_address(addr)  # request to one of the blockchain APIs\r\n>>> ainf\r\nAddressInfo(received=100000, sent=0, tx_count=1, address=P2WSH.from_string('tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'))\r\n>>> ainf.balance\r\n100000\r\n>>>\r\n>>> # Get unspent outputs (UTXO):\r\n>>> utxo = s.get_unspent(addr)\r\n>>> utxo\r\n[Unspent(748131e63a0c27a407316cdafb7cad20ec0994c856862d63e50e706073bc7f00, 0, 100000, block=2103815, address=P2WSH('tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'))]\r\n>>>\r\n>>> # Convert unspents to inputs\r\n>>> # Input also contains a private key to be able to sign yourself in a transaction\r\n>>> ins = [Input.from_unspent(u, pv, addr) for u in utxo]\r\n>>> ous = [Output.from_address(address.from_string('tb1q7n075vj7tz4jm28zky7dzknuxujzl5vt6pxkz4'), 90000)]\r\n>>>\r\n>>> tx = Transaction(ins, ous)\r\n>>> tx.default_sign()  # will be used: inp.default_sign() for inp in tx.inputs\r\n>>>\r\n>>> # Notice: inp.default_sign (tx) will try to sign itself in the tx transaction (set the desired inp.script / inp.witness value),\r\n>>> # if inp.address was obtained using PublicKey.get_address, it will succeed, but if the address hash was generated by your custom script,\r\n>>> # and the address object itself was obtained using address.<P2PKH/P2SH/P2WPKH/P2WSH>.from_hash, maybe the signature algorithm will differ\r\n>>> # from the algorithm in inp.default_sign, for this use inp.custom_sign(script=Script(...), witness=Script(...)).\r\n>>> # To summarize: if the address was obtained with PublicKey.get_address(), Input.default_sign will be able to sign it otherwise, use\r\n>>> # inp.custom_sign with custom scripts.\r\n>>>\r\n>>> tx.serialize().hex()\r\n'02000000000101007fbc7360700ee5632d8656c89409ec20ad7cfbda6c3107a4270c3ae63181740000000000ffffffff01905f010000000000160014f4dfea325e58ab2da8e2b13cd...'\r\n>>> tx.id.hex()\r\n'fff79b6d9f6a4068d5b8298c522177e9783af70d61653d628314e155a1e0e94e'\r\n>>> b = s.push(tx)\r\n>>> type(b)\r\n<class 'btclib.transaction.BroadcastedTransaction'>\r\n>>> b.get_confirmations(s.head())\r\n0\r\n>>>\r\n>>> # After a while\r\n>>> b = s.get_transaction(tx.id.hex())\r\n>>> b\r\n{'inputs': [{'txid': '748131e63a0c27a407316cdafb7cad20ec0994c856862d63e50e706073bc7f00', 'vout': 0, 'amount': 100000, 'script': '', 'witness': '473044022072909d3facf0377c1eee3b0000798ca0e76146777a026fd39cbe9307c3d73bb2022007d468e6f9985854d223a5f7742c6f312a48b4cdf9b64a46a6abb75939baf1830125512102e7b47c65a13f84fc934367d3d5be65015f62a56ab103d49df6aa25dacf540c0351ae', 'sequence': 4294967295}], 'outputs': [{'pkscript': '0014f4dfea325e58ab2da8e2b13cd15a7c37242fd18b', 'amount': 90000}], 'version': 2, 'locktime': 0}\r\n>>> b.block\r\n2103822\r\n>>> b.get_confirmations(s.head())\r\n718449\r\n```\r\n\r\nThis transaction - https://live.blockcypher.com/btc-testnet/tx/fff79b6d9f6a4068d5b8298c522177e9783af70d61653d628314e155a1e0e94e/.\r\n\r\nInstallation\r\n------------\r\n\r\nbtc-lib is distributed on `PyPI` and is available on Windows/Linux/macOS\r\nand Windows and supports Python 3.12+.\r\n\r\n```bash\r\n$ python3 -m pip install btc-lib\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2021 qwerty-w  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Simple Bitcoin Library",
    "version": "1.1.0",
    "project_urls": {
        "Homepage": "https://github.com/qwerty-w/btc-lib",
        "Repository": "https://github.com/qwerty-w/btc-lib.git"
    },
    "split_keywords": [
        "bitcoin",
        " blockchain",
        " library",
        " simple",
        " btc",
        " lib"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1d2a8bd5e1c7981151bedf1b4e9859fd4b03c470d76a09ff742190678b77b655",
                "md5": "09bd34e8e26be63b5a547f33b630946d",
                "sha256": "a72de62b0c417430c659ffa282a8094532cdbfe5271fdecb2459862137c8ee65"
            },
            "downloads": -1,
            "filename": "btc_lib-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "09bd34e8e26be63b5a547f33b630946d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 31672,
            "upload_time": "2024-08-19T15:54:18",
            "upload_time_iso_8601": "2024-08-19T15:54:18.735446Z",
            "url": "https://files.pythonhosted.org/packages/1d/2a/8bd5e1c7981151bedf1b4e9859fd4b03c470d76a09ff742190678b77b655/btc_lib-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6fa405ee71eb4db266d52bc40bd5062272f70c3ed3a61f1b9333842af082fb6e",
                "md5": "b1f41a8dd9d5a1693b33cfd31443332b",
                "sha256": "e3a1e9209e82d9d79ac0abcf2639a5a8f4f5d9baecee7db3bf7f11afb0b3dd48"
            },
            "downloads": -1,
            "filename": "btc_lib-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b1f41a8dd9d5a1693b33cfd31443332b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 37191,
            "upload_time": "2024-08-19T15:54:20",
            "upload_time_iso_8601": "2024-08-19T15:54:20.359128Z",
            "url": "https://files.pythonhosted.org/packages/6f/a4/05ee71eb4db266d52bc40bd5062272f70c3ed3a61f1b9333842af082fb6e/btc_lib-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-19 15:54:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "qwerty-w",
    "github_project": "btc-lib",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "base58check",
            "specs": [
                [
                    "~=",
                    "1.0.2"
                ]
            ]
        },
        {
            "name": "ecdsa",
            "specs": [
                [
                    "~=",
                    "0.19.0"
                ]
            ]
        },
        {
            "name": "pytest",
            "specs": [
                [
                    "~=",
                    "8.2.2"
                ]
            ]
        },
        {
            "name": "pytest-repeat",
            "specs": [
                [
                    "~=",
                    "0.9.3"
                ]
            ]
        },
        {
            "name": "httpx",
            "specs": [
                [
                    "~=",
                    "0.27.0"
                ]
            ]
        }
    ],
    "lcname": "btc-lib"
}
        
Elapsed time: 0.30518s