pycoin Package

pycoin Package

block Module

exception pycoin.block.BadMerkleRootError[source]

Bases: exceptions.Exception

class pycoin.block.Block(version, previous_block_hash, merkle_root, timestamp, difficulty, nonce)[source]

Bases: object

A Block is an element of the Bitcoin chain.

class Tx(version, txs_in, txs_out, lock_time=0, unspents=None)

Bases: pycoin.coins.Tx.Tx

ALLOW_SEGWIT = True
MAX_MONEY = Decimal('2100000000000000')
MAX_TX_SIZE = 1000000
SolutionChecker

alias of BitcoinSolutionChecker

Solver

alias of BitcoinSolver

class Spendable(coin_value, script, tx_hash, tx_out_index, block_index_available=0, does_seem_spent=False, block_index_spent=0)

Bases: pycoin.coins.bitcoin.TxOut.TxOut

class TxIn(previous_hash, previous_index, script='', sequence=4294967295)

Bases: object

The part of a Tx that specifies where the Bitcoin comes from.

address(ui_context)
classmethod coinbase_tx_in(class_, script)
is_coinbase()
classmethod parse(f)
public_key_sec()

Return the public key as sec, or None in case of failure.

stream(f, blank_solutions=False)
as_bin(as_spendable=False)

Return the txo as binary.

as_dict()
as_text()
classmethod from_bin(blob)
classmethod from_dict(d)
classmethod from_text(text)
classmethod from_tx_out(tx_out, previous_hash, previous_index, block_index_available=0)
classmethod parse(f)
stream(f, as_spendable=False)
tx_in(script='', sequence=4294967295)
class TxIn(previous_hash, previous_index, script='', sequence=4294967295)

Bases: object

The part of a Tx that specifies where the Bitcoin comes from.

address(ui_context)
classmethod coinbase_tx_in(class_, script)
is_coinbase()
classmethod parse(f)
public_key_sec()

Return the public key as sec, or None in case of failure.

stream(f, blank_solutions=False)
class TxOut(coin_value, script)

Bases: object

COIN_VALUE_CAST_F

alias of int

classmethod parse(f)
puzzle_script()
stream(f)
bad_signature_count(*args, **kwargs)
blanked_hash()

Return the hash for this Tx object with solution scripts blanked. This hash is useful for determining if two Txs might be equivalent modulo malleability. (That is, even if tx1 is morphed into tx2 using the malleability weakness, they will still have the same blanked hash.)

Returns:32 byte long binary blob corresponding to the blanked hash
check()

Basic checks that don’t depend on any context. Adapted from Bicoin Code: main.cpp

check_unspents()
classmethod coinbase_tx(public_key_sec, coin_value, coinbase_bytes='', version=1, lock_time=0)

Create the special “first in block” transaction that includes the mining fees.

fee()
has_witness_data()

Return a boolean indicating if the transaction has any segwit data.

hash(hash_type=None)

Return the binary hash for this Tx object.

Parameters:hash_type – (optional) if set, generates a hash specific to a particular type of signature.
Returns:32 byte long binary blob corresponding to the hash
is_coinbase()
missing_unspent(idx)
missing_unspents()
classmethod parse(class_, f, allow_segwit=None)

Parse a Bitcoin transaction Tx.

Parameters:
  • f – a file-like object that contains a binary streamed transaction
  • allow_segwit – (optional) set to True to allow parsing of segwit transactions. The default value is defined by the class variable ALLOW_SEGWIT
parse_unspents(f)
set_witness(tx_idx_in, witness)

Set the witness data for a given TxIn.

Parameters:
  • tx_idx_in – an integer index corresponding to the txs_in puzzle script entry that this is a witness to
  • witness – a list of binary blobs that witness the solution to the given puzzle script
stream(f, blank_solutions=False, include_unspents=False, include_witness_data=True)

Stream a Bitcoin transaction Tx to the file-like object f.

Parameters:
  • f – writable file-like object to stream binary data of transaction
  • blank_solutions – (optional) clear out the solutions scripts, effectively “unsigning” the transaction before writing it. Defaults to False
  • include_unspents – (optional) stread out the Spendable objects after streaming the transaction. This is a pycoin-specific extension. Defaults to False.
  • include_witness_data – (optional) stream segwit transactions including the witness data if the transaction has any witness data. Defaults to True.
stream_unspents(f)
total_in()
total_out()
classmethod tx_from_hex(hex_string)
tx_outs_as_spendable(block_index_available=0)
unspents_from_db(tx_db, ignore_missing=False)
validate_unspents(tx_db)

Spendable objects returned from blockchain.info or similar services contain coin_value information that must be trusted on faith. Mistaken coin_value data can result in coins being wasted to fees.

This function solves this problem by iterating over the incoming transactions, fetching them from the tx_db in full, and verifying that the coin_values are as expected.

Returns the fee for this transaction. If any of the spendables set by tx.set_unspents do not match the authenticated transactions, a ValidationFailureError is raised.

w_hash()

Return the segwit-specific binary hash for this Tx object.

Returns:32 byte long binary blob corresponding to the hash
w_id()

Return the segwit-specific binary hash for this Tx object as a hex string. Note that this is a reversed version of Tx.w_hash.

Returns:64 character long hex string corresponding to the hash
as_bin()[source]

Return the block (or header) as binary.

as_blockheader()[source]
as_hex()[source]

Return the block (or header) as hex.

check_merkle_hash()[source]

Raise a BadMerkleRootError if the Merkle hash of the transactions does not match the Merkle hash included in the block.

classmethod from_bin(class_, bytes)[source]
hash()[source]

Calculate the hash for the block header. Note that this has the bytes in the opposite order from how the header is usually displayed (so the long string of 00 bytes is at the end, not the beginning).

id()[source]

Returns the hash of the block displayed with the bytes in the order they are usually displayed in.

classmethod parse(class_, f, include_transactions=True, include_offsets=None, check_merkle_hash=True)[source]

Parse the Block from the file-like object

classmethod parse_as_header(class_, f)[source]

Parse the Block header from the file-like object

previous_block_id()[source]

Returns the hash of the previous block, with the bytes in the order they are usually displayed in.

set_nonce(nonce)[source]
set_txs(txs, check_merkle_hash=True)[source]
stream(f)[source]

Stream the block header in the standard way to the file-like object f. The Block subclass also includes the transactions.

stream_header(f)[source]

Stream the block header in the standard way to the file-like object f.

pycoin.block.difficulty_max_mask_for_bits(bits)[source]

bloomfilter Module

class pycoin.bloomfilter.BloomFilter(size_in_bytes, hash_function_count, tweak)[source]

Bases: object

MASK_ARRAY = [1, 2, 4, 8, 16, 32, 64, 128]
add_hash160(the_hash160)[source]
add_item(item_bytes)[source]
add_spendable(spendable)[source]
check_bit(v)[source]
filter_load_params()[source]
set_bit(v)[source]
pycoin.bloomfilter.filter_size_required(element_count, false_positive_probability)[source]
pycoin.bloomfilter.hash_function_count_required(filter_size, element_count)[source]
pycoin.bloomfilter.murmur3(data, seed=0)[source]

encoding Module

intbytes Module

Provide the following functions, all cribbed from six http://pythonhosted.org/six/

iterbytes(buf):
return an iterator of ints corresponding to the bytes of buf
indexbytes(buf, i):
return the int for the ith byte of buf
int2byte(an_int):
convert a small integer (< 256) into bytes (with length 1)
byte2int(bs):
turn bs[0] into an int (0-255)
pycoin.intbytes.byte2int(bs)[source]
pycoin.intbytes.indexbytes(buf, i)[source]

merkle Module

Implement Merkle hashing. See http://en.wikipedia.org/wiki/Merkle_tree

The MIT License (MIT)

Copyright (c) 2013 by Richard Kiss

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pycoin.merkle.merkle(hashes, hash_f=<function double_sha256>)[source]

Take a list of hashes, and return the root merkle hash.

pycoin.merkle.merkle_pair(hashes, hash_f)[source]

Take a list of hashes, and return the parent row in the tree of merkle hashes.

pycoin.merkle.test_merkle()[source]

ui Module

version Module