Skip to content

Commit

Permalink
refactor: Remove Mnemonic.to_entropy method (unused)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Feb 3, 2020
1 parent de3676d commit fe2b9e5
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 63 deletions.
52 changes: 0 additions & 52 deletions eth_account/hdaccount/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import binascii
import bisect
import hashlib
import itertools
import os
import unicodedata

Expand Down Expand Up @@ -96,57 +95,6 @@ def generate(self, strength=128):
)
return self.to_mnemonic(os.urandom(strength // 8))

# Adapted from <http://tinyurl.com/oxmn476>
def to_entropy(self, words):
if not isinstance(words, list):
words = words.split(" ")
if len(words) not in [12, 15, 18, 21, 24]:
raise ValueError(
"Number of words must be one of the following: [12, 15, 18, 21, 24]"
)
# Look up all the words in the list and construct the
# concatenation of the original entropy and the checksum.
concatLenBits = len(words) * 11
concatBits = [False] * concatLenBits
wordindex = 0
if self.detect_language(" ".join(words)) == "english":
use_binary_search = True
else:
use_binary_search = False
for word in words:
# Find the words index in the wordlist
ndx = (
binary_search(self.wordlist, word)
if use_binary_search
else self.wordlist.index(word)
)
if ndx < 0:
raise LookupError(f'Unable to find "{word}" in word list.')
# Set the next 11 bits to the value of the index.
for ii in range(11):
concatBits[(wordindex * 11) + ii] = (ndx & (1 << (10 - ii))) != 0
wordindex += 1
checksumLengthBits = concatLenBits // 33
entropyLengthBits = concatLenBits - checksumLengthBits
# Extract original entropy as bytes.
entropy = bytearray(entropyLengthBits // 8)
for ii in range(len(entropy)):
for jj in range(8):
if concatBits[(ii * 8) + jj]:
entropy[ii] |= 1 << (7 - jj)
# Take the digest of the entropy.
hashBytes = hashlib.sha256(entropy).digest()
hashBits = list(
itertools.chain.from_iterable(
([c & (1 << (7 - i)) != 0 for i in range(8)] for c in hashBytes)
)
)
# Check all the checksum bits.
for i in range(checksumLengthBits):
if concatBits[entropyLengthBits + i] != hashBits[i]:
raise ValueError("Failed checksum.")
return entropy

def to_mnemonic(self, data):
if len(data) not in [16, 20, 24, 28, 32]:
raise ValueError(
Expand Down
11 changes: 0 additions & 11 deletions tests/core/test_mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import pytest
import random

from eth_account.hdaccount.mnemonic import (
ConfigurationError,
Expand All @@ -43,16 +42,6 @@ def test_detection():
Mnemonic.detect_language("xxxxxxx")


def test_to_entropy():
data = [
bytearray((random.getrandbits(8) for _ in range(32))) for _ in range(1024)
]
data.append(b"Lorem ipsum dolor sit amet amet.")
m = Mnemonic("english")
for d in data:
assert m.to_entropy(m.to_mnemonic(d).split()) == d


def test_expand_word():
m = Mnemonic("english")
assert "" == m.expand_word("")
Expand Down

0 comments on commit fe2b9e5

Please sign in to comment.