patternpythonMinor
Python mnemonic ↔ hex conversion (Bitcoin BIP39) - follow-up
Viewed 0 times
bip39conversionhexpythonmnemonicfollowbitcoin
Problem
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
```
#!/usr/bin/python
import hashlib, re
from binascii import hexlify, unhexlify
# get the 2048 word wordlist
def get_wordlist():
try:
# https://github.com/bitcoin/bips/blob/master/bip-0039/english.txt
BIP0039_ENG_WORDLIST = make_request(
"https://raw.githubusercontent.com/bitcoin/"
"bips/master/bip-0039/english.txt").split('\n')
assert len(BIP0039_ENG_WORDLIST) == 2048
except:
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)
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic
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)
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.```
#!/usr/bin/python
import hashlib, re
from binascii import hexlify, unhexlify
# get the 2048 word wordlist
def get_wordlist():
try:
# https://github.com/bitcoin/bips/blob/master/bip-0039/english.txt
BIP0039_ENG_WORDLIST = make_request(
"https://raw.githubusercontent.com/bitcoin/"
"bips/master/bip-0039/english.txt").split('\n')
assert len(BIP0039_ENG_WORDLIST) == 2048
except:
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)
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic
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)
Solution
Wildcard imports are considered bad practice. From PEP8:
Wildcard imports (
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.
Wildcard imports (
from import * ) should be avoided, as theymake 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.
Context
StackExchange Code Review Q#87639, answer score: 2
Revisions (0)
No revisions yet.