Skip to content
/ py-api Public

Bitcoin full node as a Python library

Notifications You must be signed in to change notification settings

k-nuth/py-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python API

Bitcoin Cash full node as a Python library

PyPi Version License py-standard-style Telegram

Knuth Python API is a high performance implementation of the Bitcoin Cash protocol focused on users requiring extra performance and flexibility. It is a Bitcoin Cash node you can use as a library.

Getting started with Python

  1. Create a new Python console project:
$ mkdir HelloKnuth
$ cd HelloKnuth
  1. Add a reference to our Python API package:
$ pip install kth
  1. Create a new file called index.py and write some code:
import kth
import signal
import asyncio

running_ = False

def shutdown(sig, frame):
    global running_
    print('Graceful shutdown ...')
    running_ = False

async def main():
    global running_
    signal.signal(signal.SIGINT, shutdown)
    config = kth.config.getDefault(kth.config.Network.mainnet)

    with kth.node.Node(config, True) as node:
        await node.launch(kth.primitives.StartModules.all)
        print("Knuth node has been launched.")
        running_ = True

        (_, height) = await node.chain.getLastHeight()
        print(f"Current height in local copy: {height}")

        if await comeBackAfterTheBCHHardFork(node):
            print("Bitcoin Cash has been created!")

        # node.close()
        print("Good bye!")

async def comeBackAfterTheBCHHardFork(node):
    hfHeight = 478559
    while running_:
        (_, height) = await node.chain.getLastHeight()
        if height >= hfHeight:
            return True
        await asyncio.sleep(10)

    return False

asyncio.run(main())
  1. Enjoy Knuth node as a Python library:
$ python index.py

Issues

Each of our modules has its own Github repository, but in case you want to create an issue, please do so in our main repository.