Validators Module

Validation functions for Hivemind Bitcoin address formats.

This module provides functions for validating both legacy and Bech32 Bitcoin addresses. It supports validation for both mainnet and testnet addresses.

hivemind.validators.bech32_decode(bech: str) Tuple[str | None, List[int] | None][source]

Validate a Bech32 string and determine its HRP and data components.

Parameters:

bech (str) – The Bech32 string to decode

Returns:

A tuple containing the human-readable part (HRP) and data part as integers

Return type:

Tuple[str | None, List[int] | None]

The function performs various validations including:

  • Character set validation

  • Case consistency check

  • Length constraints

  • Checksum verification

hivemind.validators.bech32_hrp_expand(hrp: str) List[int][source]

Expand the HRP into values for checksum computation.

Parameters:

hrp (str) – The human-readable part of the address

Returns:

The expanded values used in checksum computation

Return type:

list[int]

This function splits each character into high bits (>>5) and low bits (&31) with a zero byte separator between them.

hivemind.validators.bech32_polymod(values: List[int]) int[source]

Compute the Bech32 checksum.

Parameters:

values (list[int]) – List of integers representing the data to checksum

Returns:

The computed checksum value

Return type:

int

This is an internal function that implements the Bech32 checksum algorithm using the specified generator polynomial.

hivemind.validators.bech32_verify_checksum(hrp: str, data: List[int]) bool[source]

Verify a checksum given HRP and converted data characters.

Parameters:
  • hrp (str) – The human-readable part of the address

  • data (list[int]) – The data part as a list of integers

Returns:

True if the checksum is valid, False otherwise

Return type:

bool

hivemind.validators.valid_address(address: str, testnet: bool = False) bool[source]

Validate a Bitcoin address (both legacy and Bech32 formats).

Parameters:
  • address (str) – The Bitcoin address to validate

  • testnet (bool) – Whether to validate as testnet address

Returns:

True if the address is valid, False otherwise

Return type:

bool

This function checks both legacy and Bech32 address formats. For legacy addresses, it uses regex patterns. For Bech32 addresses, it delegates to valid_bech32_address().

hivemind.validators.valid_bech32_address(address: str, testnet: bool = False) bool[source]

Validate a Bech32 Bitcoin address.

Parameters:
  • address (str) – The Bech32 address to validate

  • testnet (bool) – Whether to validate as testnet address

Returns:

True if the address is valid, False otherwise

Return type:

bool

This function performs both structural validation through regex patterns and Bech32 specific validation through bech32_decode(). It supports both lowercase and uppercase address formats.