• Stars
    star
    1,813
  • Rank 25,382 (Top 0.6 %)
  • Language
    Python
  • License
    Other
  • Created over 11 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python3 library providing an easy interface to the Bitcoin data structures and protocol.

python-bitcoinlib

This Python3 library provides an easy interface to the bitcoin data structures and protocol. The approach is low-level and "ground up", with a focus on providing tools to manipulate the internals of how Bitcoin works.

"The Swiss Army Knife of the Bitcoin protocol." - Wladimir J. van der Laan

Requirements

sudo apt-get install libssl-dev

The RPC interface, bitcoin.rpc, should work with Bitcoin Core v24.0 or later. Older versions may work but there do exist some incompatibilities.

Structure

Everything consensus critical is found in the modules under bitcoin.core. This rule is followed pretty strictly, for instance chain parameters are split into consensus critical and non-consensus-critical.

bitcoin.core            - Basic core definitions, datastructures, and
                          (context-independent) validation
bitcoin.core.key        - ECC pubkeys
bitcoin.core.script     - Scripts and opcodes
bitcoin.core.scripteval - Script evaluation/verification
bitcoin.core.serialize  - Serialization

In the future the bitcoin.core may use the Satoshi sourcecode directly as a library. Non-consensus critical modules include the following:

bitcoin          - Chain selection
bitcoin.base58   - Base58 encoding
bitcoin.bloom    - Bloom filters (incomplete)
bitcoin.net      - Network communication (in flux)
bitcoin.messages - Network messages (in flux)
bitcoin.rpc      - Bitcoin Core RPC interface support
bitcoin.wallet   - Wallet-related code, currently Bitcoin address and
                   private key support

Effort has been made to follow the Satoshi source relatively closely, for instance Python code and classes that duplicate the functionality of corresponding Satoshi C++ code uses the same naming conventions: CTransaction, CBlockHeader, nValue etc. Otherwise Python naming conventions are followed.

Mutable vs. Immutable objects

Like the Bitcoin Core codebase CTransaction is immutable and CMutableTransaction is mutable; unlike the Bitcoin Core codebase this distinction also applies to COutPoint, CTxIn, CTxOut, and CBlock.

Endianness Gotchas

Rather confusingly Bitcoin Core shows transaction and block hashes as little-endian hex rather than the big-endian the rest of the world uses for SHA256. python-bitcoinlib provides the convenience functions x() and lx() in bitcoin.core to convert from big-endian and little-endian hex to raw bytes to accommodate this. In addition see b2x() and b2lx() for conversion from bytes to big/little-endian hex.

Module import style

While not always good style, it's often convenient for quick scripts if import * can be used. To support that all the modules have __all__ defined appropriately.

Example Code

See examples/ directory. For instance this example creates a transaction spending a pay-to-script-hash transaction output:

$ PYTHONPATH=. examples/spend-p2sh-txout.py
<hex-encoded transaction>

Selecting the chain to use

Do the following:

import bitcoin
bitcoin.SelectParams(NAME)

Where NAME is one of 'testnet', 'mainnet', 'signet', or 'regtest'. The chain currently selected is a global variable that changes behavior everywhere, just like in the Satoshi codebase.

Unit tests

Under bitcoin/tests using test data from Bitcoin Core. To run them:

python3 -m unittest discover

Alternately, if Tox (see https://tox.readthedocs.org/) is available on your system, you can run unit tests for multiple Python versions:

./runtests.sh

HTML coverage reports can then be found in the htmlcov/ subdirectory.

Documentation

Sphinx documentation is in the "doc" subdirectory. Run "make help" from there to see how to build. You will need the Python "sphinx" package installed.

Currently this is just API documentation generated from the code and docstrings. Higher level written docs would be useful, perhaps starting with much of this README. Pages are written in reStructuredText and linked from index.rst.

More Repositories

1

replace-by-fee-tools

Tools to test out replace-by-fee
Python
146
star
2

dust-b-gone

Clean your Bitcoin-QT wallet of unwanted dust
Python
53
star
3

checklocktimeverify-demos

CHECKLOCKTIMEVERIFY (BIP65) Demos
Python
43
star
4

emin-gun-sirer-lying-about-things

Emin Gรผn Sirerโ€ Lying About Things
HTML
24
star
5

ripple-consensus-analysis-paper

TeX
17
star
6

tree-chains-paper

TeX
16
star
7

python-merbinnertree

(Summed) Merklized Binary Radix Tree library
Python
16
star
8

uniquebits

Python
16
star
9

proofmarshal

Rust
11
star
10

blockpop

Proof-of-publication with the blockchain
Python
11
star
11

rust-obstack

A fast segmented stack allocator for Rust, supporting multiple objects of any type.
Rust
11
star
12

trustbits

Protocol Buffer
10
star
13

stealth-addresses-ref-implementation

Stealth Addresses Reference Implementation
Python
9
star
14

bloom-io-attack

Python
9
star
15

decentralized-consensus-systems

Paper on decentralized consensus systems
TeX
6
star
16

dotfiles-public

My personal dotfiles (public part)
Vim Script
5
star
17

namecoin

C++
4
star
18

gracie

Import of http://anonscm.debian.org/loggerhead/collab-maint/gracie/ from Bazaar
Python
3
star
19

unholynames

Block headers over DNS
Python
3
star
20

entropy-oscillator.firm

Entropy Oscillator - Firmware
3
star
21

meter-clock.firm

Meter Clock - firmware
3
star
22

meter-clock.schem

Meter Clock - Schematics
Shell
3
star
23

tuke

A EDA toolkit for programmically creating hardware with Python.
Python
3
star
24

congestion

Python
3
star
25

dropcheck

Rust
3
star
26

csv-demos

Python
3
star
27

entropy-oscillator.elec

Entropy Oscillator - Electronics
Python
3
star
28

alternate-pace.firm

Alternate Pace - Firmware
C
2
star
29

metesky

Bill of materials tools.
Python
2
star
30

litecoin

2
star
31

64-bit-counter.firm

64-bit-counter - Firmware
C
2
star
32

meter-clock.hard

Meter Clock - PCB layout and hardware design
2
star
33

meter-clock

Meter Clock
2
star
34

entropy-oscillator

An entropy oscillator.
2
star
35

hacker-barbie-visits-the-agency

2
star
36

stegbak

Steganographic backups
C
2
star
37

ondrop

Rust
2
star
38

64-bit-counter.elec

64-bit-counter - Electronics
KiCad Layout
2
star
39

all-your-base

Rust
2
star
40

avalon-ref

Avalon Reference Documentation, Bill of Materials, Chip Communication and etc.
2
star
41

alternate-pace.elec

Alternate Pace - Electronics
Shell
2
star
42

owned

Traits for taking ownership.
Rust
2
star
43

alternate-pace

A clock with an alternate pace.
2
star
44

blockheadertwit

Bitcoin blockheaders over twitter
Python
2
star
45

cypherpunk-desert-bus-2016

2
star
46

64-bit-counter

A 64-bit non-volatile counter fed by a 64mhz source.
2
star
47

tx-flood-attack

Python
1
star
48

petertodd.github.io

JavaScript
1
star
49

coercion

In-place type conversion.
Rust
1
star
50

python-proofmarshal

Python
1
star
51

coinkite-real-time-invoice

Real Time Invoice System with Feedback for Bitcoin
Python
1
star