Crypto

A module that contains cryptographic functions.

BIP32 implemented on curve ED25519 Paper: https://github.com/LedgerHQ/orakolo/blob/master/papers/Ed25519_BIP%20Final.pdf This is a modified version of https://github.com/johnoliverdriscoll/py-edhd/blob/master/src/edhd/__init__.py

class pycardano.crypto.bip32.BIP32ED25519PrivateKey(private_key: bytes, chain_code: bytes)

Bases: object

sign(message: bytes) bytes
class pycardano.crypto.bip32.BIP32ED25519PublicKey(public_key: bytes, chain_code: bytes)

Bases: object

classmethod from_private_key(private_key: BIP32ED25519PrivateKey) BIP32ED25519PublicKey
verify(signature, message)
class pycardano.crypto.bip32.HDWallet(root_xprivate_key: bytes, root_public_key: bytes, root_chain_code: bytes, xprivate_key: bytes, public_key: bytes, chain_code: bytes, path: str = 'm', seed: Optional[bytes] = None, mnemonic: Optional[str] = None, passphrase: Optional[str] = None, entropy: Optional[str] = None)

Bases: object

Hierarchical Deterministic Wallet for Cardano

classmethod from_seed(seed: str, entropy: Optional[str] = None, passphrase: Optional[str] = None, mnemonic: Optional[str] = None) HDWallet

Create an HDWallet instance from master key.

Parameters:
  • seed – Master key of 96 bytes from seed hex string.

  • entropy – Entropy hex string, default to None.

  • passphrase – Mnemonic passphrase or password, default to None.

  • mnemonic – Mnemonic words, default to None.

Returns:

HDWallet – Hierarchical Deterministic Wallet instance.

classmethod from_mnemonic(mnemonic: str, passphrase: str = '') HDWallet

Create master key and HDWallet from Mnemonic words.

Parameters:
  • mnemonic – Mnemonic words.

  • passphrase – Mnemonic passphrase or password, default to None.

Returns:

HDWallet – Hierarchical Deterministic Wallet instance.

classmethod from_entropy(entropy: str, passphrase: str = '') HDWallet

Create master key and HDWallet from Mnemonic words.

Parameters:
  • entropy – Entropy hex string.

  • passphrase – Mnemonic passphrase or password, default to None.

Returns:

HDWallet – Hierarchical Deterministic Wallet instance.

derive_from_path(path: str, private: bool = True) HDWallet

Derive keys from a path following CIP-1852 specifications.

Parameters:
  • path – Derivation path for the key generation.

  • private – whether to derive private child keys or public child keys.

Returns:

HDWallet instance with keys derived

Examples

>>> mnemonic_words = "test walk nut penalty hip pave soap entry language right filter choice"
>>> hdwallet = HDWallet.from_mnemonic(mnemonic_words)
>>> child_hdwallet = hdwallet.derive_from_path("m/1852'/1815'/0'/0/0")
>>> child_hdwallet.public_key.hex()
'73fea80d424276ad0978d4fe5310e8bc2d485f5f6bb3bf87612989f112ad5a7d'
derive(index: int, private: bool = True, hardened: bool = False) HDWallet

Returns a new HDWallet derived from given index.

Parameters:
  • index – Derivation index.

  • private – whether to derive private child keys or public child keys.

  • hardened – whether to derive hardened address. Default to False.

Returns:

HDWallet instance with keys derived

Examples

>>> mnemonic_words = "test walk nut penalty hip pave soap entry language right filter choice"
>>> hdwallet = HDWallet.from_mnemonic(mnemonic_words)
>>> hdwallet = hdwallet.derive(1852, hardened=True)
>>> hdwallet = hdwallet.derive(1815, hardened=True)
>>> hdwallet = hdwallet.derive(0, hardened=True)
>>> hdwallet = hdwallet.derive(0)
>>> hdwallet = hdwallet.derive(0)
>>> hdwallet.public_key.hex()
'73fea80d424276ad0978d4fe5310e8bc2d485f5f6bb3bf87612989f112ad5a7d'
property root_xprivate_key
property root_public_key
property root_chain_code
property xprivate_key
property public_key
property chain_code
static generate_mnemonic(language: str = 'english', strength: int = 256) str

Generate mnemonic words.

Parameters:
  • language (str) – language for the mnemonic words.

  • strength (int) – length of the mnemoic words. Valid values are 128/160/192/224/256.

Returns:

mnemonic words.

Return type:

mnemonic (str)

static is_mnemonic(mnemonic: str, language: Optional[str] = None) bool

Check if mnemonic words are valid.

Parameters:
  • mnemonic (str) – Mnemonic words in string format.

  • language (Optional[str]) – Mnemonic language, default to None.

Returns:

bool. Whether the input mnemonic words is valid.

static is_entropy(entropy: str) bool

Check entropy hex string.

Parameters:

entropy – entropy converted from mnemonic words.

Returns:

bool. Whether entropy is valid or not.