libcrypto.formats¶
Encoding and format-conversion utilities.
Base58 and Base58Check¶
| Function | Description |
|---|---|
base58_encode(data, alphabet=BASE58_ALPHABET) -> str |
Encode bytes to Base58. |
base58_decode(encoded, alphabet=BASE58_ALPHABET) -> bytes |
Decode Base58 to bytes. Raises InvalidFormatError on bad characters. |
base58_check_encode(data, alphabet=BASE58_ALPHABET) -> str |
Base58 with a 4-byte double-SHA256 checksum. |
base58_check_decode(encoded, alphabet=BASE58_ALPHABET) -> bytes |
Verify and strip the checksum. Raises InvalidFormatError. |
The alphabet argument allows alternative dictionaries, such as the Ripple/XRP
alphabet.
WIF (Wallet Import Format)¶
private_key_to_wif(private_key, compressed=True, network="bitcoin") -> str¶
Encode a 32-byte or integer private key to WIF for a supported network.
wif_to_private_key(wif) -> tuple[bytes, bool, str]¶
Decode WIF to (private_key_bytes, is_compressed, network). Raises
InvalidFormatError for a bad checksum, length, version byte, or compression
flag.
from libcrypto import private_key_to_wif, wif_to_private_key
wif = private_key_to_wif(bytes.fromhex("00"*31 + "01"), compressed=True)
key, compressed, network = wif_to_private_key(wif)
Integer / bytes / hex conversions¶
| Function | Description |
|---|---|
bytes_to_int(data, byteorder="big") -> int |
Bytes to integer. |
int_to_bytes(value, length=0, byteorder="big") -> bytes |
Integer to bytes (minimal length when length=0). |
bytes_to_hex(data) -> str |
Bytes to lowercase hex. |
hex_to_bytes(hex_str) -> bytes |
Hex (optionally 0x-prefixed, odd length tolerated) to bytes. |
Exceptions¶
| Exception | Meaning |
|---|---|
InvalidFormatError |
Any format decode/verify failure (subclass of ValueError). |
Performance note¶
base58_encode accumulates digits in a list and reverses once; base58_decode
uses a character→value map. Both avoid quadratic behavior on long inputs while
producing byte-for-byte identical results to the naive implementation.