Skip to main content

A Bitcoin blockchain parser written in Python

Project description

bitcaviar-plus

A Bitcoin blockchain parser written in Python.

Installation

Clone repository

pip install bitcaviar-plus

Usage

Deserialize first block from file blk00000.dat

from bitcaviar_plus.block import deserialize_block


def parse_genesis_block():
    with open('path/to/file/blk00000.dat', 'rb') as f:
        block = deserialize_block(f)
        print(block)

Deserialize entire blockchain

import os
from bitcaviar_plus.block import deserialize_block
from bitcaviar_plus.errors import InvalidMagicBytes


def parse_entire_blockchain():
    file_counter = -1
    while True:
        file_counter += 1
        file_name = 'path/to/file/blk{}.dat'.format(str(file_counter).zfill(5))
        with open(file_name, 'rb') as f:
            file_size = os.path.getsize(file_name)
            while f.tell() < file_size:
                try:
                    block = deserialize_block(f)
                except InvalidMagicBytes as e:
                    print(e)

Example output

{
  "magic_number":"f9beb4d9",
  "size":"0000011d",
  "id":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
  "transaction_count":"01",
  "header":{
    "version":"00000001",
    "previous_block_id":"0000000000000000000000000000000000000000000000000000000000000000",
    "merkle_root":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
    "time":"495fab29",
    "bits":"1d00ffff",
    "nonce":"7c2bac1d"
  },
  "transactions":[
    {
      "version":"00000001",
      "input_count":"01",
      "output_count":"01",
      "lock_time":"00000000",
      "id":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
      "inputs":[
        {
          "id":"0000000000000000000000000000000000000000000000000000000000000000",
          "vout":"ffffffff",
          "script_sig_size":"4d",
          "script_sig":"04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73",
          "sequence":"ffffffff"
        }
      ],
      "outputs":[
        {
          "value":"000000012a05f200",
          "script_pub_key_size":"43",
          "script_pub_key":"4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac"
        }
      ]
    }
  ]
}

Attribution

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitcaviar-plus-0.0.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

bitcaviar_plus-0.0.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file bitcaviar-plus-0.0.2.tar.gz.

File metadata

  • Download URL: bitcaviar-plus-0.0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for bitcaviar-plus-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4a07d277a90c87098534f9c10949f9357d6d80d2a2ba893705212998ab24680a
MD5 f8236bc704e071acb3885270cf77afbe
BLAKE2b-256 8f6600f2f5d4d4d09dd9ba10ea31ba54aaab60833e29a95dfd91e7b774fac779

See more details on using hashes here.

File details

Details for the file bitcaviar_plus-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: bitcaviar_plus-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for bitcaviar_plus-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c9dc84f73912c614ed7cbeb4d7da93f8f2adea86fbc5d966694cd687e772d586
MD5 5fd3418030f93ede094c66eb9b624807
BLAKE2b-256 e8be4a99901146f63637d78a61e073c02b69ae425773bf951ca0c64caa1acfb2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page