bitcoinlib.mnemonic module

class bitcoinlib.mnemonic.Mnemonic(language='english')[source]

Bases: object

Class to convert, generate and parse Mnemonic sentences

Implementation of BIP0039 for Mnemonics passphrases

Took some parts from Pavol Rusnak Trezors implementation, see https://github.com/trezor/python-mnemonic

Init Mnemonic class and read wordlist of specified language

Parameters:language (str) – use specific wordlist, i.e. chinese, dutch (in development), english, french, italian, japanese or spanish. Leave empty for default ‘english’
static checksum()[source]

Calculates checksum for given data key

Parameters:data (bytes, hexstring) – key string
Return str:Checksum of key in bits
static detect_language()[source]

Detect language of given phrase

Parameters:words (str) – List of space seperated words
Return str:Language
generate(strength=128, add_checksum=True)[source]

Generate a random Mnemonic key

Uses cryptographically secure os.urandom() function to generate data. Then creates a Mnemonic sentence with the ‘to_mnemonic’ method.

Parameters:
  • strength (int) – Key strength in number of bits, default is 128 bits. It advised to specify 128 bits or more, i.e.: 128, 256, 512 or 1024
  • add_checksum (bool) – Included a checksum? Default is True
Return str:

Mnemonic passphrase consisting of a space seperated list of words

sanitize_mnemonic(words)[source]

Check and convert list of words to utf-8 encoding.

Raises an error if unrecognised word is found

Parameters:words (str) – List of space seperated words
Return str:Sanitized list of words
to_entropy(words, includes_checksum=True)[source]

Convert Mnemonic words back to key data entrophy

Parameters:
  • words (str) – Mnemonic words as string of list of words
  • includes_checksum (bool) – Boolean to specify if checksum is used. Default is True
Return bytes:

Entrophy seed

to_mnemonic(data, add_checksum=True)[source]

Convert key data entropy to Mnemonic sentence

Parameters:
  • data (bytes, hexstring) – Key data entropy
  • add_checksum (bool) – Included a checksum? Default is True
Return str:

Mnemonic passphrase consisting of a space seperated list of words

to_seed(words, password='')[source]

Use Mnemonic words and password to create a PBKDF2 seed (Password-Based Key Derivation Function 2)

First use ‘sanitize_mnemonic’ to determine language and validate and check words

Parameters:
  • words (str) – Mnemonic passphrase as string with space seperated words
  • password (str) – A password to protect key, leave empty to disable
Return bytes:

PBKDF2 seed

word(index)[source]

Get word from wordlist

Parameters:index (int) – word index ID
Return str:A word from the dictionary
wordlist()[source]

Get full selected wordlist. A wordlist is selected when initializing Mnemonic class

Return list:Full list with 2048 words