Bip32PrivateKey

class cometa.cryptography.bip32_private_key.Bip32PrivateKey(ptr)[source]

Bases: object

Represents a BIP32 hierarchical deterministic (HD) private key.

BIP32 private keys enable the derivation of child keys following a hierarchical tree structure, allowing a single seed to generate a practically unlimited number of keys.

This is the foundation for HD wallets in Cardano, following CIP-1852 for key derivation paths.

Warning

Private keys should be handled with extreme care. Never expose them in logs, error messages, or user interfaces.

Example

>>> priv_key = Bip32PrivateKey.from_bip39_entropy(b"password", entropy)
>>> child = priv_key.derive([harden(1852), harden(1815), harden(0)])
__init__(ptr)[source]
Return type:

None

__enter__()[source]
Return type:

Bip32PrivateKey

__exit__(exc_type, exc_val, exc_tb)[source]
Return type:

None

__repr__()[source]

Return repr(self).

Return type:

str

classmethod from_bytes(data)[source]

Creates a BIP32 private key from raw bytes.

Parameters:

data (bytes | bytearray) – The private key as raw bytes (96 bytes).

Returns:

A new Bip32PrivateKey instance.

Raises:

CardanoError – If creation fails.

Return type:

Bip32PrivateKey

classmethod from_hex(hex_string)[source]

Creates a BIP32 private key from a hexadecimal string.

Parameters:

hex_string (str) – The private key as a hexadecimal string (192 characters).

Returns:

A new Bip32PrivateKey instance.

Raises:

CardanoError – If creation fails or hex is invalid.

Return type:

Bip32PrivateKey

classmethod from_bip39_entropy(password, entropy)[source]

Generates a BIP32 private key from BIP39 entropy.

This creates an HD root key from mnemonic entropy, typically derived from a 12, 15, 18, 21, or 24-word seed phrase.

Parameters:
  • password (bytes | bytearray | str) – Optional passphrase for additional security. Can be empty string or bytes for no passphrase.

  • entropy (bytes | bytearray) – The entropy derived from a BIP39 mnemonic.

Returns:

A new Bip32PrivateKey instance as the root key.

Raises:

CardanoError – If key generation fails.

Return type:

Bip32PrivateKey

Example

>>> from cometa import mnemonic_to_entropy
>>> entropy = mnemonic_to_entropy("abandon " * 11 + "about")
>>> root_key = Bip32PrivateKey.from_bip39_entropy(b"", entropy)
derive(indices)[source]

Derives a child private key using the specified derivation path.

Both hardened (>= 2^31) and non-hardened indices are supported. Use the harden() function to create hardened indices.

Parameters:

indices (List[int]) – List of derivation indices.

Returns:

The derived child Bip32PrivateKey.

Raises:

CardanoError – If derivation fails.

Return type:

Bip32PrivateKey

Example

>>> # CIP-1852 account derivation path
>>> account = root_key.derive([harden(1852), harden(1815), harden(0)])
get_public_key()[source]

Derives the corresponding public key from this private key.

Returns:

The Bip32 public key derived from this private key.

Raises:

CardanoError – If derivation fails.

Return type:

Bip32PublicKey

to_ed25519_key()[source]

Converts this BIP32 private key to an Ed25519 private key.

Returns:

The Ed25519 private key extracted from this BIP32 key.

Raises:

CardanoError – If conversion fails.

Return type:

Ed25519PrivateKey

to_bytes()[source]

Returns the private key as raw bytes.

Warning

Handle the returned bytes with care as they contain sensitive key material.

Returns:

The private key bytes (96 bytes).

Return type:

bytes

to_hex()[source]

Returns the private key as a hexadecimal string.

Warning

Handle the returned string with care as it contains sensitive key material.

Returns:

The private key as a hex string.

Return type:

str

__eq__(other)[source]

Checks equality with another Bip32PrivateKey.

Parameters:

other (object)

Return type:

bool

__hash__()[source]

Returns a Python hash for use in sets and dicts.

Return type:

int

__str__()[source]

Returns a safe string representation (does not expose key material).

Return type:

str