Multi-Cryptocurrency Python API.
Bitprim Python-API is a library written in Python which exposes an API that allows you to programmatically access all of the Bitprim node features:
- Full blockchain
Bitprim Python-API supports the following cryptocurrencies:
- 64-bit machine.
- Python >= 3.4.x (64-bits) or Python >= 2.7.x (64-bits). Only CPython (reference implementation) is supported. PyPy, Jython, IronPython, ... are not supported for the moment.
- pip package manager.
The Bitprim Python-API installation is very easy using the pip package manager.
$ pip install bitprim
This installs the Bitprim Python-API customized for Bitcoin Cash cryptocurrency. If you want to use another currency, you have to specify it in the following way:
# For Bitcoin Cash (default) $ pip install --install-option="--currency=BCH" bitprim # For Bitcoin Legacy $ pip install --install-option="--currency=BTC" bitprim # For Litecoin $ pip install --install-option="--currency=LTC" bitprim
Building from source Requirements
Bitprim Python-API is a thin library built on top of the Bitprim C-API, therefore it is sometimes necessary to have a C compiler to use the library.
We have prebuilt binaries for macOS and Windows, so for both macOS and Windows it is not necessary for you to have a C compiler installed. But, due to limitations of the pip package manager, pre-built binaries for Linux can not be provided, therefore, in Linux it is necessary to compile from source code, and therefore have a C language compiler.
How to use it
"Hello, Blockchain!" example:
# hello_blockchain.py import bitprim import sys def main(): with bitprim.Executor("my_config_file", sys.stdout, sys.stderr) as ex: ex.init_chain() ex.run_wait() def last_height_handler(error, height): print(height) ex.chain.fetch_last_height(last_height_handler) if __name__ == '__main__': main()
Needed to use the Bitprim Python-API features.
with bitprim.Executor("my_config_file", sys.stdout, sys.stderr) as ex:
Construct a Bitprim Executor object, which is necessary to run the node, interact with the blockchain, with the P2P peers and other components of the API.
"my_config_file" is the path to the configuration file; in the bitprim-config repository you can find some example files.
If you pass an empty string (
""), default configuration will be used.
stderr are file objects corresponding to the Python interpreter’s standard output and error streams. These are used to tell the Bitprim node where to print the logs.
You can use any file object, for example, you can make the Bitprim node redirect the logs to a file.
If you pass
None, there will be no logging information.
Initialize the filesystem database where the Blockchain will be stored.
You need to have enough disk space to store the full Blockchain.
This is equivalent to executing:
bn -i -c my_config_file.
Run the node.
In this step, the connections and handshake with the peers will be established, and the initial process of downloading blocks will start. Once this stage has finished, the node will begin to receive transactions and blocks through the P2P network.
This is equivalent to executing:
bn -c my_config_file.
def last_height_handler(error, height): print(height) ex.chain.fetch_last_height(last_height_handler)
Ask the Blockchain what is the height of the last downloaded block and print it in the standard output.
In order to get the height a callback (or handler) has to be passed as a parameter of the
... and enjoy the Bitprim programmable APIs:
Bitprim is a high performance node, so we have some options and pre-built packages tuned for several platforms. Specifically, you can choose your computer microarchitecture to download a pre-built executable compiled to take advantage of the instructions available in your processor. For example:
# For Haswell microarchitecture and Bitcoin Cash currency $ pip install --install-option="--currency=BCH" --install-option="--microarch=haswell" bitprim
So, you can manually choose the appropiate microarchitecture, some examples are: x86_64, haswell, ivybridge, sandybridge, bulldozer, ...
By default, if you do not specify any, the building system will select a base microarchitecture corresponding to your Instruction Set Architecture (ISA). For example, for Intel 80x86, the x86_64 microarchitecture will be selected.
Automatic Microarchitecture selection
Our build system has the ability to automatically detect the microarchitecture of your processor. To do this, first, you have to install our pip package called cpuid. Our build system detects if this package is installed and in such case, makes use of it to detect the best possible executable for your processor.
$ pip install cpuid $ pip install bitprim