From c89603e9c425f50aa83519d82a1abf3475e56d4e Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Tue, 18 Feb 2020 17:44:32 +0100 Subject: [PATCH] Adds Makefile + Lint --- .gitignore | 9 +++++++++ Makefile | 17 +++++++++++++++++ src/__init__.py | 0 src/key_derivation/__init__.py | 0 src/key_derivation/mnemonic.py | 5 ++++- src/key_derivation/path.py | 2 +- src/tests/test_key_derivation/__init__.py | 0 src/utils/__init__.py | 0 src/utils/crypto.py | 1 + src/utils/serialization.py | 17 ++++------------- 10 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/__init__.py create mode 100644 src/key_derivation/__init__.py create mode 100644 src/tests/test_key_derivation/__init__.py create mode 100644 src/utils/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c6c39f --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Eth2 specs: +eth2.0-specs/ + +# Python testing & linting: +venv/ +.pytest_cache +.hypothesis +.mypy_cache +__pycache__ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..44c0bf9 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +clean: + rm -rf eth2.0-specs/ + rm -rf venv/ + find . -name __pycache__ -exec rm -rf {} \; + find . -name .mypy_cache -exec rm -rf {} \; + find . -name .pytest_cache -exec rm -rf {} \; + +install_test: + python3 -m venv venv; . venv/bin/activate; pip3 install -r requirements.txt + +test: + . venv/bin/activate; python -m pytest ./src + +lint: + . venv/bin/activate; \ + flake8 --ignore=E252,W504,W503 --max-line-length=120 ./src \ + && mypy --follow-imports=skip --ignore-missing-imports src diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/key_derivation/__init__.py b/src/key_derivation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/key_derivation/mnemonic.py b/src/key_derivation/mnemonic.py index 9ad1079..02a3087 100644 --- a/src/key_derivation/mnemonic.py +++ b/src/key_derivation/mnemonic.py @@ -1,14 +1,17 @@ from os import walk +from secrets import randbits from typing import ( List, Optional, ) +from ..utils.crypto import SHA256 + word_lists_path = './src/key_derivation/word_lists/' def _get_word_list(language: str): - return open('%s%s.txt' %(word_lists_path, language)).readlines() + return open('%s%s.txt' % (word_lists_path, language)).readlines() def get_languages(path: str=word_lists_path) -> List[str]: diff --git a/src/key_derivation/path.py b/src/key_derivation/path.py index 2e9dbbd..507fa3a 100644 --- a/src/key_derivation/path.py +++ b/src/key_derivation/path.py @@ -26,4 +26,4 @@ def mnemonic_and_path_to_key(*, mnemonic: str, password: str, path: str) -> int: sk = derive_master_SK(seed) for node in path_to_nodes(path): sk = derive_child_SK(parent_SK=sk, index=node) - return sk \ No newline at end of file + return sk diff --git a/src/tests/test_key_derivation/__init__.py b/src/tests/test_key_derivation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/crypto.py b/src/utils/crypto.py index 1ef7769..b7040d4 100644 --- a/src/utils/crypto.py +++ b/src/utils/crypto.py @@ -11,6 +11,7 @@ from Crypto.Cipher import ( AES as _AES ) + def SHA256(x): return _sha256.new(x).digest() diff --git a/src/utils/serialization.py b/src/utils/serialization.py index 52a8dfa..097a7b0 100644 --- a/src/utils/serialization.py +++ b/src/utils/serialization.py @@ -3,15 +3,6 @@ from ssz import ( Serializable, ) -from utils.typing import ( - BLSPubkey, - BLSPrivkey, - BLSSignature, - Domain, - DomainType, - Version, - Root, -) from utils.constants import ( DOMAIN_DEPOSIT, GENESIS_FORK_VERSION, @@ -25,14 +16,14 @@ class SigningRoot(Serializable): ] -def compute_domain(domain_type: DomainType=DomainType(DOMAIN_DEPOSIT), fork_version: Version=GENESIS_FORK_VERSION) -> Domain: +def compute_domain(domain_type: bytes=DOMAIN_DEPOSIT, fork_version: bytes=GENESIS_FORK_VERSION) -> bytes: """ Return the domain for the ``domain_type`` and ``fork_version``. """ - return Domain(domain_type + fork_version) + return domain_type + fork_version -def compute_signing_root(ssz_object, domain: Domain) -> Root: +def compute_signing_root(ssz_object: Serializable, domain: bytes) -> bytes: """ Return the signing root of an object by calculating the root of the object-domain tree. """ @@ -40,4 +31,4 @@ def compute_signing_root(ssz_object, domain: Domain) -> Root: object_root=ssz_object.get_hash_tree_root(), domain=domain, ) - return domain_wrapped_object.get_hash_tree_root() \ No newline at end of file + return domain_wrapped_object.get_hash_tree_root()