candidateblock_bitcoin_library.mnemonic module

class candidateblock_bitcoin_library.mnemonic.Mnemonic

Bases: object

Mnemonic Class

A mnemonic sentence (“mnemonic code”, “seed phrase”, “seed words”) is a way of representing a large randomly-generated number as a sequence of words, making it easier for humans to store BIP-39 defines the method

classmethod decode(bip39_mnemonic: str) bytes

For each word in the bip39 mnemonic sentence, convert to its 11-Bit numberic value and logic shift the bits in to the correct integer place. Verify the checksum is valid.

Parameters:

bip39_mnemonic (str) – A string storing Mnemonic 12-24 words

Returns:

entropy (orginal entropy, a cryptographically secure random number)

Return type:

bytes

classmethod encode(entropy: bytes, words: int) str

Mnemonic words are generated automatically by the wallet using the standardized process defined in BIP-39.

Input a random sequence (entropy) of 128 to 256 bits. Create a checksum of the random sequence by taking the first (entropy-length/32) bits of its SHA256 hash. Add the checksum to the end of the random sequence. Split the result into 11-bit length segments. Map each 11-bit value to a word from the predefined dictionary of 2048 words. The mnemonic code is the sequence of words.

Parameters:
  • entropy (bytes) – entropy (a cryptographically secure random number)

  • words (int) – Number of words only valid values 12, 15, 18, 21 or 24

Returns:

A string storing Mnemonic 12-24 words

Return type:

str

classmethod generate_mnemonic(words: int) str

Mnemonic words are generated automatically by the wallet using the standardized process defined in BIP-39.

Create a random sequence (entropy) of 128 to 256 bits. Generate the mnemonic sentence (a sequence of 12-24 words).

Parameters:

words (int) – Number of words only valid values 12, 15, 18, 21 or 24

Returns:

A string storing Mnemonic 12-24 words

Return type:

str

classmethod mnemonic_to_seed(mnemonic_sentence: str, passphrase: str = '') bytes

Mnemonic To Seed

To create a binary seed from the mnemonic, we use the PBKDF2 function with a mnemonic sentence (in UTF-8 NFKD) used as the password and the string “mnemonic” + passphrase (again in UTF-8 NFKD) used as the salt. The iteration count is set to 2048 and HMAC-SHA512 is used as the pseudo-random function. The length of the derived key is 512 bits (64 bytes).

Parameters:

mnemonic_sentence (str) – A string storing Mnemonic 12-24 words

Returns:

seed 512 bits (64 bytes).

Return type:

bytes