Skip to main content

Module bip39

Module bip39 

Source
Expand description

BIP-39: bytes as words from the English list, as a hardware wallet shows its recovery phrase and takes it back.

The bytes are the wallet’s entropy, 16 to 32 bytes in steps of 4. The first byte of their SHA-256 digest supplies a checksum of one bit per 32 bits of entropy, appended to them; the result is read eleven bits at a time, most significant first, each an index into the 2048 words of the English list. 32 bytes are 24 words, 16 bytes are 12.

The words, not the entropy, are what a wallet derives its keys from, so the same entropy in another language’s list is a different wallet. This module knows the English list only, which is the one hardware wallets use.

The decoder takes either case and the first four letters of a word, which the list keeps unique and which metal backup plates carry. A word it does not find, a count that is not a phrase and a checksum that does not match are refused, each by the word’s position and never by the word, since the words are a secret.

Structs§

Decoded
A phrase read back: the entropy, wiped when dropped.

Enums§

Bip39Error
Why a phrase or a word does not read. None of these names a word.

Constants§

MAX_WORDS
The most words a phrase has.
MIN_WORDS
The fewest words a phrase has.
PHRASE_LENGTHS
The word counts a phrase may have, as a person reads them.
WORD_MAX
The longest word in the list, in letters.

Functions§

bytes_for
Bytes of entropy a phrase of words words carries: four per three words.
decode
Read a phrase back into its entropy. Words are separated by whitespace.
encode
Write entropy as a phrase, words separated by single spaces.
is_entropy_len
Whether bytes bytes of entropy make a phrase: 16 to 32 in steps of 4.
is_phrase_len
Whether a phrase may have words words: 12 to 24 in steps of 3.
phrase_lengths
The word counts a phrase may have, shortest first.
read_word
One word as typed, for a frontend that takes a phrase a word at a time: the word it is, in full and lower case. word is its position from 1, for the error to name.
words_for
Words in a phrase for bytes bytes of entropy, rounded up for a length that is not one: three words per four bytes.