staking-deposit-cli/eth2deposit/key_handling/key_derivation/mnemonic.py

65 lines
2.0 KiB
Python
Raw Normal View History

import os
2020-02-18 16:47:27 +00:00
from unicodedata import normalize
2020-02-18 16:44:32 +00:00
from secrets import randbits
2020-02-18 16:03:52 +00:00
from typing import (
Optional,
Sequence,
2020-05-26 09:32:20 +00:00
Tuple,
2020-02-18 16:03:52 +00:00
)
from eth2deposit.utils.crypto import (
2020-02-18 16:47:27 +00:00
SHA256,
PBKDF2,
)
2020-02-18 16:44:32 +00:00
2020-02-18 16:03:52 +00:00
def _get_word_list(language: str, path: str) -> Sequence[str]:
return open(os.path.join(path, '%s.txt' % language), encoding='utf-8').readlines()
2020-02-18 16:03:52 +00:00
def _get_word(*, word_list: Sequence[str], index: int) -> str:
2020-02-18 17:18:01 +00:00
assert index < 2048
return word_list[index][:-1]
2020-05-26 09:32:20 +00:00
def get_seed(*, mnemonic: str, password: str) -> bytes:
2020-02-18 17:23:57 +00:00
"""
2020-05-26 09:32:20 +00:00
Derive the seed for the pre-image root of the tree.
Ref: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#from-mnemonic-to-seed
2020-02-18 17:23:57 +00:00
"""
2020-02-18 16:47:27 +00:00
mnemonic = normalize('NFKD', mnemonic)
salt = normalize('NFKD', 'mnemonic' + password).encode('utf-8')
return PBKDF2(password=mnemonic, salt=salt, dklen=64, c=2048, prf='sha512')
2020-05-26 09:32:20 +00:00
def get_languages(path: str) -> Tuple[str, ...]:
2020-02-18 16:03:52 +00:00
"""
Walk the `path` and list all the languages with word-lists available.
"""
(_, _, filenames) = next(os.walk(path))
2020-05-26 09:32:20 +00:00
languages = tuple([name[:-4] for name in filenames])
return languages
2020-02-18 16:03:52 +00:00
def get_mnemonic(*, language: str, words_path: str, entropy: Optional[bytes]=None) -> str:
2020-02-18 16:03:52 +00:00
"""
2020-05-26 09:32:20 +00:00
Return a mnemonic string in a given `language` based on `entropy`.
2020-02-18 16:03:52 +00:00
"""
if entropy is None:
entropy = randbits(256).to_bytes(32, 'big')
entropy_length = len(entropy) * 8
assert entropy_length in range(128, 257, 32)
checksum_length = (entropy_length // 32)
checksum = int.from_bytes(SHA256(entropy), 'big') >> 256 - checksum_length
entropy_bits = int.from_bytes(entropy, 'big') << checksum_length
entropy_bits += checksum
entropy_length += checksum_length
mnemonic = []
word_list = _get_word_list(language, words_path)
2020-02-18 16:03:52 +00:00
for i in range(entropy_length // 11 - 1, -1, -1):
index = (entropy_bits >> i * 11) & 2**11 - 1
2020-02-18 17:18:01 +00:00
word = _get_word(word_list=word_list, index=index)
mnemonic.append(word)
2020-02-18 16:03:52 +00:00
return ' '.join(mnemonic)