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: object

ALLOW_SEGWIT = True
MAX_MONEY = Decimal('2100000000000000')
MAX_TX_SIZE = 1000000
SIGHASH_ALL = 1
SIGHASH_ANYONECANPAY = 128
SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
class Spendable(coin_value, script, tx_hash, tx_out_index, block_index_available=0, does_seem_spent=False, block_index_spent=0)

Bases: pycoin.tx.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(address_prefix='\x00')
bitcoin_address(address_prefix='\x00')
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)
verify(tx_out_script, signature_for_hash_type_f, lock_time, expected_hash_type=None, traceback_f=None, flags=None, tx_version=None)

Return True or False depending upon whether this TxIn verifies.

tx_out_script: the script of the TxOut that corresponds to this input signature_hash: the hash of the partial transaction

Block.Tx.Spendable.as_bin(as_spendable=False)

Return the txo as binary.

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

Bases: object

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

address(address_prefix='\x00')
bitcoin_address(address_prefix='\x00')
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)
verify(tx_out_script, signature_for_hash_type_f, lock_time, expected_hash_type=None, traceback_f=None, flags=None, tx_version=None)

Return True or False depending upon whether this TxIn verifies.

tx_out_script: the script of the TxOut that corresponds to this input signature_hash: the hash of the partial transaction

class Block.Tx.TxOut(coin_value, script)

Bases: object

COIN_VALUE_CAST_F

alias of int

address(netcode=None)
bitcoin_address(netcode=None)
hash160()
classmethod parse(f)
stream(f)
Block.Tx.as_bin(include_unspents=False, include_witness_data=True)

Return the transaction as binary.

Block.Tx.as_hex(include_unspents=False, include_witness_data=True)

Return the transaction as hex.

Block.Tx.bad_signature_count(flags=None)
Block.Tx.blanked_hash()

Return the hash for this Tx object with solution scripts blanked. 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.)

Block.Tx.check()

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

Block.Tx.check_unspents()
classmethod Block.Tx.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.

Block.Tx.fee()
classmethod Block.Tx.from_bin(blob)

Return the Tx for the given binary blob.

classmethod Block.Tx.from_hex(hex_string)

Return the Tx for the given hex string.

Block.Tx.has_witness_data()
Block.Tx.hash(hash_type=None)

Return the hash for this Tx object.

Block.Tx.hash_outputs(hash_type, tx_in_idx)
Block.Tx.hash_prevouts(hash_type)
Block.Tx.hash_sequence(hash_type)
Block.Tx.id()

Return the human-readable hash for this Tx object.

Block.Tx.is_coinbase()
Block.Tx.is_signature_ok(tx_in_idx, flags=None, traceback_f=None)
Block.Tx.missing_unspent(idx)
Block.Tx.missing_unspents()
classmethod Block.Tx.parse(class_, f, allow_segwit=None)

Parse a Bitcoin transaction Tx from the file-like object f.

Block.Tx.parse_unspents(f)
Block.Tx.segwit_signature_preimage(script, tx_in_idx, hash_type)
Block.Tx.set_unspents(unspents)
Block.Tx.set_witness(tx_idx_in, witness)
Block.Tx.sign(hash160_lookup, hash_type=None, **kwargs)

Sign a standard transaction. hash160_lookup:

A dictionary (or another object with .get) where keys are hash160 and values are tuples (secret exponent, public_pair, is_compressed) or None (in which case the script will obviously not be signed).
Block.Tx.sign_tx_in(hash160_lookup, tx_in_idx, tx_out_script, hash_type=None, **kwargs)
Block.Tx.signature_for_hash_type_segwit(script, tx_in_idx, hash_type)
Block.Tx.signature_hash(tx_out_script, unsigned_txs_out_idx, hash_type)

Return the canonical hash for a transaction. We need to remove references to the signature, since it’s a signature of the hash before the signature is applied.

tx_out_script: the script the coins for unsigned_txs_out_idx are coming from unsigned_txs_out_idx: where to put the tx_out_script hash_type: one of SIGHASH_NONE, SIGHASH_SINGLE, SIGHASH_ALL, optionally bitwise or’ed with SIGHASH_ANYONECANPAY

Block.Tx.solve(hash160_lookup, tx_in_idx, tx_out_script, hash_type=None, **kwargs)

Sign a standard transaction. hash160_lookup:

An object with a get method that accepts a hash160 and returns the corresponding (secret exponent, public_pair, is_compressed) tuple or None if it’s unknown (in which case the script will obviously not be signed). A standard dictionary will do nicely here.
tx_in_idx:
the index of the tx_in we are currently signing
tx_out:
the tx_out referenced by the given tx_in
Block.Tx.stream(f, blank_solutions=False, include_unspents=False, include_witness_data=True)

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

Block.Tx.stream_unspents(f)
Block.Tx.total_in()
Block.Tx.total_out()
classmethod Block.Tx.tx_from_hex(hex_string)
Block.Tx.tx_outs_as_spendable(block_index_available=0)
Block.Tx.unspents_from_db(tx_db, ignore_missing=False)
Block.Tx.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.

Block.Tx.verify_tx_in(tx_in_idx, tx_out_script, expected_hash_type=None)
Block.Tx.w_hash()
Block.Tx.w_id()
Block.as_bin()[source]

Return the block (or header) as binary.

Block.as_blockheader()[source]
Block.as_hex()[source]

Return the block (or header) as hex.

Block.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 Block.from_bin(class_, bytes)[source]
Block.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).

Block.id()[source]

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

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

Parse the Block from the file-like object

classmethod Block.parse_as_header(class_, f)[source]

Parse the Block header from the file-like object

Block.previous_block_id()[source]

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

Block.set_nonce(nonce)[source]
Block.set_txs(txs)[source]
Block.stream(f)[source]

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

Block.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

add_address(address, address_prefix='\x00')[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

Various utilities useful for converting one Bitcoin format to another, including some the human-transcribable format hashed_base58.

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.

exception pycoin.encoding.EncodingError[source]

Bases: exceptions.Exception

pycoin.encoding.a2b_base58(s)[source]

Convert base58 to binary using BASE58_ALPHABET.

pycoin.encoding.a2b_hashed_base58(s)[source]

If the passed string is hashed_base58, return the binary data. Otherwise raises an EncodingError.

pycoin.encoding.b2a_base58(s)[source]

Convert binary to base58 using BASE58_ALPHABET. Like Bitcoin addresses.

pycoin.encoding.b2a_hashed_base58(data)[source]

A “hashed_base58” structure is a base58 integer (which looks like a string) with four bytes of hash data at the end. Bitcoin does this in several places, including Bitcoin addresses.

This function turns data (of type “bytes”) into its hashed_base58 equivalent.

pycoin.encoding.bitcoin_address_to_hash160_sec(bitcoin_address, address_prefix='\x00')[source]

Convert a Bitcoin address back to the hash160_sec format of the public key. Since we only know the hash of the public key, we can’t get the full public key back.

pycoin.encoding.bitcoin_address_to_hash160_sec_with_prefix(bitcoin_address)[source]

Convert a Bitcoin address back to the hash160_sec format and also return the prefix.

pycoin.encoding.double_sha256(data)[source]

A standard compound hash.

pycoin.encoding.from_bytes_32(v)[source]
pycoin.encoding.from_long(v, prefix, base, charset)[source]

The inverse of to_long. Convert an integer to an arbitrary base.

v: the integer value to convert prefix: the number of prefixed 0s to include base: the new base charset: an array indicating what printable character to use for each value.

pycoin.encoding.hash160(data)[source]

A standard compound hash.

pycoin.encoding.hash160_sec_to_bitcoin_address(hash160_sec, address_prefix='\x00')[source]

Convert the hash160 of a sec version of a public_pair to a Bitcoin address.

pycoin.encoding.is_hashed_base58_valid(base58)[source]

Return True if and only if base58 is valid hashed_base58.

pycoin.encoding.is_sec_compressed(sec)[source]

Return a boolean indicating if the sec represents a compressed public key.

pycoin.encoding.is_valid_bitcoin_address(bitcoin_address, allowable_prefixes='\x00')[source]

Return True if and only if bitcoin_address is valid.

pycoin.encoding.is_valid_wif(wif, allowable_wif_prefixes=['\x80'])[source]

Return a boolean indicating if the WIF is valid.

pycoin.encoding.public_pair_to_bitcoin_address(public_pair, compressed=True, address_prefix='\x00')[source]

Convert a public_pair (corresponding to a public key) to a Bitcoin address.

pycoin.encoding.public_pair_to_hash160_sec(public_pair, compressed=True)[source]

Convert a public_pair (corresponding to a public key) to hash160_sec format. This is a hash of the sec representation of a public key, and is used to generate the corresponding Bitcoin address.

pycoin.encoding.public_pair_to_sec(public_pair, compressed=True)[source]

Convert a public pair (a pair of bignums corresponding to a public key) to the gross internal sec binary format used by OpenSSL.

pycoin.encoding.ripemd160(data)[source]
pycoin.encoding.sec_to_public_pair(sec, strict=True)[source]

Convert a public key in sec binary format to a public pair.

pycoin.encoding.secret_exponent_to_wif(secret_exp, compressed=True, wif_prefix='\x80')[source]

Convert a secret exponent (correspdong to a private key) to WIF format.

pycoin.encoding.to_bytes_32(v)[source]
pycoin.encoding.to_long(base, lookup_f, s)[source]

Convert an array to a (possibly bignum) integer, along with a prefix value of how many prefixed zeros there are.

base:
the source base
lookup_f:
a function to convert an element of s to a value between 0 and base-1.
s:
the value to convert
pycoin.encoding.wif_to_secret_exponent(wif, allowable_wif_prefixes=['\x80'])[source]

Convert a WIF string to the corresponding secret exponent.

pycoin.encoding.wif_to_tuple_of_prefix_secret_exponent_compressed(wif)[source]

Return a tuple of (prefix, secret_exponent, is_compressed).

pycoin.encoding.wif_to_tuple_of_secret_exponent_compressed(wif, allowable_wif_prefixes=['\x80'])[source]

Convert a WIF string to the corresponding secret exponent. Private key manipulation. Returns a tuple: the secret exponent, as a bignum integer, and a boolean indicating if the WIF corresponded to a compressed key or not.

Not that it matters, since we can use the secret exponent to generate both the compressed and uncompressed Bitcoin address.

intbytes Module

Provide the following functions:

bytes_to_ints(bytes):
yield an iterator of ints. Designed to deal with how Python 2 treats bytes[0] as a string while Python 3 treats bytes[0] as an int.
bytes_from_int(an_int):
convert a small integer (< 256) into bytes (of length 1)
byte_to_int(one_byte):
turn one byte into an int
bytes_from_ints(list_of_small_ints):
return a bytes object from a list of small (< 256) integers
to_bytes(v, length, byteorder):
convert integer v into a bytes object
from_bytes(bytes, byteorder, *, signed=False):
convert the bytes object into an integer

The last two functions are designed to mimic the methods of the same name that exist on int in Python 3 only. For Python 3, it uses those implementations.

pycoin.intbytes.bytes_from_ints(l)
pycoin.intbytes.bytes_to_ints(x)
pycoin.intbytes.from_bytes(bytes, byteorder='big', signed=False)[source]
pycoin.intbytes.int_from_bytes(s)[source]
pycoin.intbytes.int_to_bytes(v)[source]
pycoin.intbytes.to_bytes(v, length, byteorder='big')[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

pycoin.ui.address_for_pay_to_script(script, netcode=None)[source]
pycoin.ui.address_for_pay_to_script_wit(script, netcode=None)[source]
pycoin.ui.script_obj_from_address(address, netcodes=None)[source]
pycoin.ui.standard_tx_out_script(address, netcodes=None)[source]

version Module

Subpackages