[][src]Module bitcoin_spv::btcspv

btcspv provides basic Bitcoin transaction and header parsing, as well as utility functions like merkle verification and difficulty adjustment calculation.

Functions

calculate_difficulty

Calculate difficulty from the difficulty 1 target and current target. Difficulty 1 is 0x1d00ffff on mainnet and testnet.

determine_input_length

Determines the length of an input from its scriptsig: 36 for outpoint, 1 for scriptsig length, 4 for sequence.

determine_output_length

Determines the length of an output. 5 types: WPKH, WSH, PKH, SH, and OP_RETURN.

determine_var_int_data_length

Determines the length of a VarInt in bytes. A VarInt of > 1 byte is prefixed with a flag indicating its length.

extract_difficulty

Extracts the expected difficulty from a block header. Does NOT verify the work.

extract_hash

Extracts the hash from the output script. Determines type by the length prefix and validates format.

extract_input_at_index

Extracts the nth input from the vin (0-indexed).

extract_input_tx_id

Extracts the outpoint index from an input, 32 byte tx id.

extract_input_tx_id_le

Extracts the outpoint tx id from an input, 32 byte tx id.

extract_merkle_root_be

Extracts the transaction merkle root from a block header. Use verify_hash256_merkle to verify proofs with this root.

extract_merkle_root_le

Extracts the transaction merkle root from a block header.

extract_op_return_data

Extracts the data from an op return output. Errors if no data or not an op return.

extract_outpoint

Extracts the outpoint from the input in a tx, 32 byte tx id with 4 byte index.

extract_output_at_index

Extracts the output at a given index in the TxIns vector.

extract_output_script_len

Extracts the output script length. Indexes the length prefix on the pk_script.

extract_prev_block_hash_be

Extracts the previous block's hash from a block header. Block headers do NOT include block number :(

extract_prev_block_hash_le

Extracts the previous block's hash from a block header. Block headers do NOT include block number :(

extract_script_sig

Extracts the VarInt-prepended scriptSig from the input in a tx. Will return vec![0] if passed a witness input.

extract_script_sig_len

Determines the length of a scriptSig in an input. Will return 0 if passed a witness input.

extract_sequence_le_legacy

Extracts the LE sequence bytes from an input. Sequence is used for relative time locks.

extract_sequence_le_witness

Extracts the LE sequence bytes from an input. Sequence is used for relative time locks.

extract_sequence_legacy

Extracts the sequence from the input. Sequence is a 4-byte little-endian number.

extract_sequence_witness

Extracts the sequence from the input in a tx. Sequence is a 4-byte little-endian number.

extract_target

Extracts the target from a block header.

extract_timestamp

Extracts the BE timestamp from a block header. Time is not 100% reliable.

extract_timestamp_le

Extracts the LE timestamp from a block header. Time is not 100% reliable.

extract_tx_index

Extracts the LE tx input index from the input in a tx, 4 byte tx index.

extract_tx_index_le

Extracts the LE tx input index from the input in a tx, 4 byte tx index.

extract_value

Extracts the value from the output in a tx. Value is an 8-byte little-endian number.

extract_value_le

Extracts the value bytes from the output in a tx. Value is an 8-byte little-endian number.

hash160

Implements bitcoin's hash160 (rmd160(sha2())). Returns the digest.

hash256

Implements bitcoin's hash256 (double sha2). Returns the digest.

hash256_merkle_step

Concatenates and hashes two inputs for merkle proving.

is_legacy_input

Determines whether an input is legacy. True for LEGACY, False for WITNESS, False if no scriptSig.

retarget_algorithm

Performs the bitcoin difficulty retarget. Implements the Bitcoin algorithm precisely.

validate_vin

Checks that the vin passed up is properly formatted; Consider a vin with a valid vout in its scriptsig.

validate_vout

Checks that the vout passed up is properly formatted; Consider a vin with a valid vout in its scriptsig.

verify_hash256_merkle

Verifies a Bitcoin-style merkle tree. Leaves are 0-indexed.