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(data)[source]

Calculates checksum for given data key

Parameters

data (bytes, hexstring) – key string

Return str

Checksum of key in bits

static detect_language(words)[source]

Detect language of given phrase

>>> Mnemonic().detect_language('chunk gun celery million wood kite tackle twenty story episode raccoon dutch')
'english'
Parameters

words (str) – List of space separated 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 as multiply of 32, 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 separated words

Return str

Sanitized list of words

to_entropy(words, includes_checksum=True)[source]

Convert Mnemonic words back to key data entropy

>>> Mnemonic().to_entropy('chunk gun celery million wood kite tackle twenty story episode raccoon dutch').hex()
'28acfc94465fd2f6774759d6897ec122'
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

Entropy seed

to_mnemonic(data, add_checksum=True, check_on_curve=True)[source]

Convert key data entropy to Mnemonic sentence

>>> Mnemonic().to_mnemonic('28acfc94465fd2f6774759d6897ec122')
'chunk gun celery million wood kite tackle twenty story episode raccoon dutch'
Parameters
  • data (bytes, hexstring) – Key data entropy

  • add_checksum (bool) – Included a checksum? Default is True

  • check_on_curve (bool) – Check if data integer value is on secp256k1 curve. Should be enabled when not testing and working with crypto

Return str

Mnemonic passphrase consisting of a space seperated list of words

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

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

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

>>> Mnemonic().to_seed('chunk gun celery million wood kite tackle twenty story episode raccoon dutch').hex()
'6969ed4666db67fc74fae7869e2acf3c766b5ef95f5e31eb2fcebd93d76069c6de971225f700042b0b513f0ad6c8562277fc4b5ee1344b720f1686dc2dccc220'
Parameters
  • words (str) – Mnemonic passphrase as string with space separated words

  • password (str) – A password to protect key, leave empty to disable

  • validate (bool) – Validate checksum for given word phrase, default is True

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