Skip to content

BenWestgate/python-codex32

Repository files navigation

python-codex32

Python implementation of BIP-0093 (codex32): checksummed, SSSS-aware BIP32 seed strings.

This repository implements the codex32 string format described by BIP-0093. It provides encoding/decoding, short/long ms32 checksums, CRC padding for convertbits, Shamir-share interpolation helpers and helpers to build codex32 strings from seed bytes.

Features

  • Encode/decode codex32 data via from_string and from_unchecksummed_string.
  • Short checksum (13 chars) and long checksum (15 chars) support.
  • Construct codex32 strings from raw seed bytes via from_seed.
  • CRC-based default padding scheme for from_seed.
  • Parse codex32 strings and access parts via properties.
  • Interpolate/recover shares via interpolate_at.
  • Default identifier is the bech32-encoded BIP32 fingerprint.

Installation

pip install codex32

Quick usage

from codex32.codex32 import Codex32String as ms32

# Create a codex32 string from seed bytes (16..64 bytes).
s = ms32.from_seed(
    data=bytes.fromhex('ffeeddccbbaa99887766554433221100'), # seed bytes, length 16..64
    ident="cash",         # 4-character identifier, fingerprint if omitted
    hrp="ms",             # human readable part, default 'ms'
    k=3,                  # threshold 0 or 2..9 (0 special: share index must be 's')
    share_idx='s',        # single-char share index (or 's' for k=0)
    pad_val=0             # None -> CRC padding, otherwise integer padding value
)
print(s.s)                # codex32 string

# Parse & validate an existing codex32 string
a = ms32.from_string("ms13casha320zyxwvutsrqpnmlkjhgfedca2a8d0zehn8a0t", hrp="ms")
parts = a.parts             # Parts object
data_bytes = parts.data     # encoded seed bytes
print(data_bytes.hex())     # hex encoding of seed bytes

# Create from unchecksummed data-part (will append checksum)
c = ms32.from_unchecksummed_string("ms13cashcacdefghjklmnpqrstuvwxyz023")

# Interpolate shares to obtain target share index:
# shares is a list of Codex32String objects containing compatible shares
shares = [s, a, c]
derived_share_d = ms32.interpolate_at(shares, target='d')
print(derived_share_d.s)

# Parse & validate an existing codex32 string with any hrp
e = ms32("cl10lueasd35kw6r5de5kueedxyesqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanvrktzhlhusz")
print(e.s)

# Relabel a codex32 string object
e.ident = "cln2"
print(str(e))

About

Python Implementation of BIP93

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages