Base58check
This library provides functions for Base58check encoding and decoding of binary data, commonly used in cryptocurrencies like Bitcoin. It is currently at version 1.0.2 and maintains a stable, low-cadence release cycle.
Common errors
-
ValueError: Invalid base58check string
cause Attempting to decode a string that is not a valid Base58Check format (e.g., incorrect characters, wrong length, or a failed checksum verification).fixEnsure the input string was originally encoded using Base58Check and is not corrupted. Implement `try...except ValueError` for robust error handling. -
AttributeError: module 'base58check' has no attribute 'encode'
cause Trying to use `base58check.encode()` or `base58check.decode()` directly instead of the library's specific function names `base58check.b58encode()` and `base58check.b58decode()`.fixCorrect the function call to `base58check.b58encode()` for encoding and `base58check.b58decode()` for decoding.
Warnings
- gotcha The library implements Base58Check encoding/decoding, which includes a checksum. It does not provide plain Base58 encoding without the checksum. Ensure your application requires the checksum for security and integrity.
- gotcha When decoding, an invalid Base58Check string (e.g., incorrect characters, wrong length, or a failed checksum) will raise a `ValueError`. It's essential to handle these exceptions gracefully.
- gotcha The `b58encode` function accepts either a string or bytes. While it converts strings to bytes internally using UTF-8, it is generally best practice to explicitly provide `bytes` for binary data encoding to avoid potential encoding issues.
Install
-
pip install base58check
Imports
- b58encode
from base58check import encode
import base58check encoded_data = base58check.b58encode(b'my_data')
- b58decode
from base58check import decode
import base58check decoded_data = base58check.b58decode('encodedstring')
Quickstart
import base58check
# Encode binary data
original_data = b"Hello, Base58Check!"
encoded_string = base58check.b58encode(original_data)
print(f"Encoded: {encoded_string}")
# Decode a Base58Check string
decoded_data = base58check.b58decode(encoded_string)
print(f"Decoded: {decoded_data}")
# Example with a Bitcoin-like address (simulated for demonstration)
simulated_address_bytes = b'\x00' + b'\x01\x09\x66\x77\x60\x06\x95\x3D\x55\x67\x43\x9E\x5E\x39\x8E\x8B\x0D\x9C\x1C\x00\x10' # Example payload
simulated_address_encoded = base58check.b58encode(simulated_address_bytes)
print(f"Simulated Address: {simulated_address_encoded}")