Developer Interface

Functions

bithex.compile_hex(hex_string)

Compile a hex string into Script.

Parameters:

hex_string – A string or unicode string that is the hex representation of a bitcoin script.

Returns:

A bitcoin script formatted as a string.

Return type:

str

Raises:
  • InvalidHexError – Raised if the input hex_string doesn’t compile to valid Script.
  • TypeError – Raised if the input hex_string isn’t a string.

Examples

>>> compile_hex('aa106fe28c0ab6f1b372c1a6a246ae63f74f87')
'OP_HASH256 6fe28c0ab6f1b372c1a6a246ae63f74f OP_EQUAL'
>>> compile_hex('76a90a134408afa258a50ed7a188ac')
'OP_DUP OP_HASH160 134408afa258a50ed7a1 OP_EQUALVERIFY OP_CHECKSIG'
>>> compile_hex('0504b0bd6342ac')
'04b0bd6342 OP_CHECKSIG'
>>> compile_hex('aa206fe28c0ab6f1b372c1a6a246ae63f74f931')
InvalidHexError
bithex.classify_hex(hex_string)

Classify the transaction type of a hex string.

Parameters:

hex_string – A string or unicode string that is the hex representation of a bitcoin script.

Returns:

The transaction type of the hex string per https://bitcoin.org/en/developer-guide#standard-transactions.

Return type:

str

Raises:
  • TypeError – Raised if the input hex_string isn’t a string.
  • InvalidHexError – Raised if the input hex_string doesn’t compile to valid Script.

Examples

>>> classify_hex('76a914a134408afa258a50ed7a1d9817f26b63cc9002cc88ac')
'P2PKH'
>>> classify_hex('aa106fe28c0ab6f1b372c1a6a246ae63f74f87')
'nonstandard transaction'
bithex.classify_script(script)

Classify the transaction type of a script.

Parameters:script – A bitcoin script.
Returns:The transaction type of the hex string per https://bitcoin.org/en/developer-guide#standard-transactions. The following are the values that can be returned: ‘P2PKH’,’P2SH’, ‘Multisig’, ‘Pubkey’, ‘Null Data’, ‘nonstandard transaction’.
Return type:str
Raises:TypeError – Raised if the input script isn’t a string.

Examples

>>> classify_script('OP_DUP OP_HASH160 a13 OP_EQUALVERIFY OP_CHECKSIG')
'P2PKH'
>>> classify_script('OP_HASH160 54c557e07dde5bb6cb791c7a540e OP_EQUAL')
'P2SH'
>>> classify_script('OP_HASH256 6fe28c0ab6f1b372c1a6a246ae63 OP_EQUAL')
'nonstandard transaction'

Exceptions

exception bithex.InvalidHexError(value)

Raised by compile_hex, classify_hex if the input doesn’t compile to valid Script.