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 PrivateKey, Address, Transaction, Output, get_inputs
>>>
>>> my_wif = 'cMtnJjkY8hBrNdNN1kPBCMuTM5h4rxes9nrfRfktTn8tW6HW2pC2'
>>> my_pv = PrivateKey(my_wif)
>>> my_pub = my_pv.pub
>>>
>>> my_pub.get_address('P2PKH').string
'12Nj1W9U7xvzbRFsMErK8hsm7pYGZv9jsT'
>>>
>>> my_pub.get_address('P2SH-P2WPKH').string
'39YgiFhV8U5rWiUQLh5sDeGJvaft81k1sV'
>>>
>>> my_pub.get_address('P2WPKH').string
'bc1qpuf7m9ysjtnxhpfvx80v6lptsk33lm2x3t9s5w'
>>>
>>> my_pub.get_address('P2WSH').string
'bc1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqhehfwy'
>>>
>>>
>>> addr = my_pub.get_address('P2WSH', 'testnet')
>>> addr.string
'tb1qxmh2drh6xsqyr5m4c8f72fwmqskmgk0rdqtggn6leswzf6m4kxvqq3px5t'
>>>
>>> # To see address info:
>>> addr.get_info() # request to one of the blockchain APIs
AddressINF(received=100000, sent=0, tx_count=1, balance=100000)
>>>
>>> # To get unspents (UTXO):
>>> addr.get_unspents()
[Unspent(tx_id='748131e63a0c27a407316cdafb7cad20ec0994c856862d63e50e706073bc7f00', out_index=0, amount=100000)]
>>>
>>> # To get inputs (itself converts unspent to input)
>>> inps = get_inputs([my_pv, addr]) # Input contains a private key to be able to sign yourself in a transaction
>>>
>>> out = Output(Address('tb1q7n075vj7tz4jm28zky7dzknuxujzl5vt6pxkz4'), 90000)
>>>
>>> tx = Transaction(inps, [out])
>>> tx.default_sign_inputs() # 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 algorithm,
>>> # 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(<some_type>, <some_network>), Input.default_sign will be able to sign it.
>>> # else, use inp.custom_sign with custom scripts.
>>>
>>> tx.serialize()
'02000000000101007fbc7360700ee5632d8656c89409ec20ad7cfbda6c3107a4270c3ae63181740000000000ffffffff01905f010000000000160014f4dfea325e58ab2da8e2b13cd...'
>>> tx.get_id()
'fff79b6d9f6a4068d5b8298c522177e9783af70d61653d628314e155a1e0e94e'
>>> tx.push()
True
```
This transaction - https://www.blockchain.com/ru/btc-testnet/tx/fff79b6d9f6a4068d5b8298c522177e9783af70d61653d628314e155a1e0e94e.
Installation
------------
btc-lib is distributed on `PyPI` and is available on Linux/macOS
and Windows and supports Python 3.10+.
```bash
$ python3 -m pip install btc-lib
```