Original question:

Python PBKDF2 using core modules

I'm looking for commentary on the following code, which converts from a hex seed to mnemonic (12 word phrase), and vice versa. It's for Bitcoin (BIP39 to be exact), and is going to be running in conjunction with pybitcointools, however I believe the commented URLs should be somewhat self-explanatory.

Background info:

In essence, a hex value (the seed) is converted to binary, divided into chunks of 11-bits, and the base 10 value of each binary chunk is used as an index in a list of 2048 words. So if the first chunk is 00000000001, it's the word @ index 1 (the 2nd word); ability. Or, 11111111111 = the last word, zoo. This is BIP39; where a large hex value (a private key, perhaps) is encoded with an easy(ier) to remember phrase.

import hashlib, re
from binascii import hexlify, unhexlify

# get the 2048 word wordlist
def get_wordlist():
    # https://github.com/bitcoin/bips/blob/master/bip-0039/english.txt
        BIP0039_ENG_WORDLIST = make_request(
        assert len(BIP0039_ENG_WORDLIST) == 2048
        raise IOError("Cannot get BIP39 word list")
    return BIP0039_ENG_WORDLIST

BIP39WORDS = get_wordlist()

def bip39_seed_to_mnemonic(hexseed):
    Convert hex seed to mnemonic representation (BIP39)

    Essentially converts hex value to binary (with appended checksum), 
    and splits into 11-bit binary chunks, each indexing a 2048 (=2**11) 
    word list (in BIP39WORDS)

    hexseed: hexadecimal bytes or bytearray object

    >>> bip39_seed_to_mnemonic(b'eaebabb2383351fd31d703840b32e9e2')
    'turtle front uncle idea crush write shrug there lottery flower risk shell'

    if isinstance(hexseed, (bytes, str)) and re.match('^[0-9a-fA-F]*$', hexseed):
        hexseed = from_string_to_bytes(str(hexseed))
        raise TypeError("Enter a hex seed!")

    hexseed = unhexlify(hexseed)
    hexbin = changebase( hexlify(hexseed), 16, 2, len(hexseed)*8)

    if len(hexseed) % 4: 
        raise Exception("Seed not a multiple of 4 bytes!")
    elif len(hexseed) < 4: 
        raise Exception("Seed must be at least 32 bits of entropy")
    elif len(hexseed) > 124: 
        raise Exception("Seed cannot exceed 992 bits of entropy")

    checksum_length = int((len(hexseed) * 8) // 32)
    checksum = hashlib.sha256(hexseed).hexdigest() # sha256 hexdigest
    checksum_bin = changebase(
          checksum, 16, 2, len(unhexlify(checksum))*8)

    binstr_final = from_string_to_bytes( 
                 str(hexbin) + str(checksum_bin)[:checksum_length])
    binlist_words = [binstr_final[i:i+11] for i in 
                 range(0, len(binstr_final), 11)]

    return " ".join( [BIP39WORDS[int(x, 2)] for x in binlist_words ] )

def bip39_mnemonic_to_seed(mnemonic):
    Convert BIP39 mnemonic phrase to hex seed (bytes)
    See BIP39: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic

    mnemonic: single spaced, lower-case words (bytes/bytearray object)

    >>>bip39_mnemonic_to_seed(b"board flee heavy tunnel powder denial science ski answer betray cargo cat")

    if isinstance(mnemonic, string_types):
        mnemonic = str(from_string_to_bytes(mnemonic)).lower().strip()
    elif isinstance(mnemonic, list):
        mnemonic = " ".join(str(from_string_to_bytes(mnemonic))).lower()
        raise TypeError("Enter a lower case, single-spaced mnemonic!")

        mnemonic_array = str(mnemonic).split(" ")
        if mnemonic_array[0] is '': mnemonic_array.pop(0)
        raise TypeError("Enter a lower case, single-spaced mnemonic!")

    if not (93 > len(mnemonic_array) > 3): 
        raise TypeError("32 < entropy < 992 bits; Input too big or too small")
    if len(mnemonic_array) % 3: 
        raise TypeError("Too many or too few words")
    assert all(map(lambda x: x in BIP39WORDS, mnemonic_array)) # check all words in list

    binstr = ''.join([ changebase(str(BIP39WORDS.index(x)), 10, 2, 11) for x in mnemonic_array])
    num_checksum_digits = len(binstr) % 32

    binary_checksum = binstr[(len(binstr) - num_checksum_digits):]
    binary_no_checksum = binstr[ : (-1*num_checksum_digits)]

    hexoutput = hexlify(changebase(binary_no_checksum, 2, 16, 
    len(binary_checksum) * 8))
    assert not (len(hexoutput) % 2)
    checksum_bin = changebase(hashlib.sha256(
                       unhexlify(hexoutput)).hexdigest(), 16, 2, 256)

    assert checksum_bin[0:int(num_checksum_digits)] != binary_checksum
    return unhexlify(hexoutput)

And the imported modules from pybitcointools:

#pybitcointools code
import random
    from urllib.request import build_opener
    from urllib2 import build_opener

def from_string_to_bytes(a):
return a
# return a if isinstance(a, bytes) else bytes(a, 'utf-8')

code_strings = {
    2: '01',
    10: '0123456789',
    16: '0123456789abcdef',
    256: ''.join([chr(x) for x in range(256)])

def lpad(msg, symbol, length):
    if len(msg) >= length:
        return msg
    return symbol * (length - len(msg)) + msg

def get_code_string(base):
    if base in code_strings:
        return code_strings[base]
        raise ValueError("Invalid base!")

def changebase(string, frm, to, minlen=0):
    if frm == to:
        return lpad(string, get_code_string(frm)[0], minlen)
        return encode(decode(string, frm), to, minlen)

def make_request(*args):
    opener = build_opener()
    opener.addheaders = [('User-agent',
        return opener.open(*args).read().strip()
    except Exception as e:
            p = e.read().strip()
            p = e
            raise Exception(p)

def encode(val, base, minlen=0):
    base, minlen = int(base), int(minlen)
    code_string = get_code_string(base)
    result = ""
    while val > 0:
        result = code_string[val % base] + result
        val //= base
    return code_string[0] * max(minlen - len(result), 0) + result

def decode(string, base):
    base = int(base)
    code_string = get_code_string(base)
    result = 0
    if base == 16:
        string = string.lower()
    while len(string) > 0:
        result *= base
        result += code_string.find(string[0])
        string = string[1:]
    return result

1 Answer 1


Wildcard imports are considered bad practice. From PEP8:

Wildcard imports ( from <module> import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

It would be better to add the explicit names that you want to import. It also makes the code more readable.

The code could use more vertical spacing (blank lines) to create a visual grouping of closely related lines of code. In their current form, the two methods look like large blobs of code and not very easy to read.

  • \$\begingroup\$ Thanks for the clarification. I'm quite new to this. I'll fix the aforementioned issues ASAP and edit accordingly. Thanks again \$\endgroup\$ Apr 22, 2015 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.