The 21 Blockchain Library (two1.blockchain)

Posted by Nigel Drego

The 21 Blockchain Library (two1.blockchain)

The blockchain module within the 21 Bitcoin Library (two1.blockchain) allows you to receive data about transactions and blocks from a blockchain data provider, as well as submit new transactions to the provider for relay to the network and inclusion in new blocks.

The package is organized around an abstract base class. Users of 21 will want to use the TwentyOneProvider class to instantiate a connection to the default provider.

We will illustrate the use of the two1.blockchain module by showing how to get various blockchain parameters from a provider instance.

Initializing a provider

Start by importing the class and initializing a provider:

from two1.blockchain.twentyone_provider import TwentyOneProvider
provider = TwentyOneProvider()

Get block height, balances, and transactions

Get the current block height, provided by the provider:

>>> provider.get_block_height()
386574

Get all of the transactions for a particular address:

>>> provider.get_transactions(['1P8tESXaim9Y58SCd7hyppWcMMB1YSN2r8'])
defaultdict(<class 'list'>, {'1P8tESXaim9Y58SCd7hyppWcMMB1YSN2r8': [{'transaction': <two1.bitcoin.txn.Transaction object at 0x7f1248701f28>, 'metadata': {'block_hash': <two1.bitcoin.hash.Hash object at 0x7f1248701f60>, 'confirmations': 6682, 'block': 379892, 'network_time': 1445436432}}]})

To further process a single transaction, get it by its Transaction Identifier (txid):

>>> provider.get_transactions_by_id(['eda100d83ab41b097a707d54f36ea410ef0bd90c7b2ee5392aebca5faf1c0593'])
{'eda100d83ab41b097a707d54f36ea410ef0bd90c7b2ee5392aebca5faf1c0593': {'transaction': <two1.bitcoin.txn.Transaction object at 0x7f1248709198>, 'metadata': {'block_hash': <two1.bitcoin.hash.Hash object at 0x7f1248709160>, 'confirmations': 6683, 'block': 379892, 'network_time': 1445436432}}}

Broadcast a transaction

Finally, when you need to send a transaction directly (not through the wallet interface), you can do that too by providing the serialized transaction in hexadecimal. This will return the txid also in hex.

>>> provider.broadcast_transaction('010000000193051caf5fcaeb2a39e52e7b0cd90bef10a46ef3547d707a091bb43ad800a1ed010000006b483045022100e91f0060f892047ae5782c307eaf26f3df5d2384f7ea01366b77ea9e39e0337002203c6d6ed097cb6eada8ac3869a08eae7fa31b97017cace121b07c602bc1c03a0d0121026a55ca7ad4ff198c17af11cc06208cf3337d6a3256172748c208327cb8158b69ffffffff0123f20700000000001976a9146c6bddd91f2e0ad0f4d35a8e79a2a544019ab22388ac00000000')
'028bb132e633f13734c33b72bdb21f339e17211a8d5ebff4373721336d897425'

two1.blockchain: module contents

The two1.blockchain module is organized into the following submodules:

two1.blockchain.twentyone_provider

This submodule provides a concrete TwentyOneProvider class that provides information about a blockchain by contacting a server.

This submodule provides a concrete TwentyOneProvider class that provides information about a blockchain by contacting a server.

class two1.blockchain.twentyone_provider.TwentyOneProvider(twentyone_host_name='https://blockchain.21.co', testnet=False, connection_pool_size=0)

Bases: two1.blockchain.base_provider.BaseProvider

Transaction data provider using the TwentyOne API

DEFAULT_HOST = 'https://blockchain.21.co'
broadcast_transaction(transaction)

Broadcasts a transaction to the Bitcoin network

Parameters:transaction (bytes or str) – serialized, signed transaction
Returns:The transaction ID
Return type:str
get_balance(address_list)

Deprecated Method

get_block_height()

Returns the latest block height

Returns:Block height
Return type:int
get_transactions(address_list, limit=100, min_block=None)

Provides transactions associated with each address in address_list.

Parameters:
  • address_list (list) – List of Base58Check encoded Bitcoin addresses.
  • limit (int) – Maximum number of transactions to return.
  • min_block (int) – Block height from which to start getting transactions. If None, will get transactions from the entire blockchain.
Returns:

A dict keyed by address with each value being a list of Transaction objects.

Return type:

dict

get_transactions_by_id(ids)

Gets transactions by their IDs.

Parameters:ids (list) – List of TXIDs to retrieve.
Returns:A dict keyed by TXID of Transaction objects.
Return type:dict
get_utxos(address_list)

Deprecated Method

testnet

Returns whether or not the data provider is on testnet.

static txn_from_json(txn_json)

Returns a new Transaction from a JSON-serialized transaction

Parameters:txn_json

JSON with the following format:

{ “hash”: “0bf0de38c26195919179f...”, “block_hash”: “000000000000000...”, “block_height”: 303404, “block_time”: “2014-05-30T23:54:55Z”, “chain_received_at”: “2015-08-13T10:52:21.718Z”, “confirmations”: 69389, “lock_time”: 0, “inputs”: [
{
“transaction_hash”: “0bf0de38c2619...”, “output_hash”: “b84a66c46e24fe71f9...”, “output_index”: 0, “value”: 300000, “addresses”: [
“3L7dKYQGNoZub928CJ8NC2WfrM8U8GGBjr”

], “script_signature”: “03046022100de7b67b9...”, “script_signature_hex”: “00493046022100de7b...”, “sequence”: 4294967295

}

], “outputs”: [

{
“transaction_hash”: “0bf0de38c261959...”, “output_index”: 0, “value”: 290000, “addresses”: [
“1K4nPxBMy6sv7jssTvDLJWk1ADHBZEoUVb”

], “script”: “OP_DUP OP_HASH160 c629680b8d...”, “script_hex”: “76a914c629680b8d13...”, “script_type”: “pubkeyhash”, “required_signatures”: 1, “spent”: false, “spending_transaction”: null

}

], “fees”: 10000, “amount”: 290000 }, Transaction.DEFAULT_TRANSACTION_VERSION

Returns:a deserialized transaction derived from the provided json.
Return type:two1.bitcoin.Transaction