Skip to content

Commit

Permalink
8th oct. linting and fuzzing added in the workflo. mypy, flake8, blac…
Browse files Browse the repository at this point in the history
…k and isort added to implemet python project practice
  • Loading branch information
Aviksaikat committed Oct 8, 2023
1 parent e8e73be commit 467e805
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 22 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# fds.py

[Fair Data Society](https://github.com/fairDataSociety/fds.js) Python SDK

## Dependencies

```bash
Python >= 3.8
Python >= 3.9
pip
poetry
eth-ape
```

## Setup

1. Install [poetry](https://python-poetry.org/docs/)
2. Then run the following command

```bash
poetry install
```

- Currently under development
34 changes: 33 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ mdformat = ">=0.7.17"
mdformat-gfm = ">=0.3.5"
mdformat-frontmatter = ">=0.4.1"
mdformat-pyproject = ">=0.0.1"
pyproject-flake8 = "^6.1.0"
flake8-pyproject = "^1.2.3"


[build-system]
Expand Down Expand Up @@ -61,3 +63,12 @@ use_parentheses = true

[tool.mdformat]
number = true

[[tool.mypy.overrides]]
module = "eth_keys.*"
ignore_missing_imports = true

# * not working so defined in setup.cfg
# [tool.flake8]
# line-length = 100
# exclude = ["__pycache__", ".venv/*"]
6 changes: 5 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ formats = bdist_wheel

[flake8]
# Some sane defaults for the code style checker flake8
max_line_length = 88
max_line_length = 100
extend_ignore = E203, W503
# ^ Black-compatible
# E203 and W503 have edge cases handled by black
Expand All @@ -121,9 +121,13 @@ exclude =
dist
.eggs
docs/conf.py
.venv/*

[pyscaffold]
# PyScaffold's parameters when the project was created.
# This will be used when updating. Do not change!
version = 4.5
package = fds_py

[mypy]
ignore_missing_imports = true
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
"isort>=5.10.1,<6", # Import sorting linter
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style
# headers in issue templates
"mdformat-pyproject>=0.0.1", # Allows configuring in pyproject.toml
# "pyproject-flake8>=6.1.0", # need to connect pyproject.toml config to flake8
],
},
)
2 changes: 0 additions & 2 deletions src/fds/fds_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
handles crypto
"""
import codecs
import hashlib
from binascii import hexlify, unhexlify
from os import urandom

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from eth_keys import keys
Expand Down
7 changes: 1 addition & 6 deletions src/fds/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ape
import ape # type: ignore
import pytest

from fds.fds_crypto import Crypto
Expand Down Expand Up @@ -116,11 +116,6 @@ def geth_second_account(test_accounts):
return test_accounts[7]


@pytest.fixture(scope="session")
def networks():
return ape.networks


@pytest.fixture
def ethereum(networks):
return networks.ethereum
Expand Down
12 changes: 5 additions & 7 deletions src/fds/tests/unit_tests/test_fds_crypto.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from eth_utils import keccak

from fds.fds_crypto import Crypto
from eth_utils import keccak # type: ignore


def test_public_key_derivation(crypto_instance):
address1 = "0xecedad96c545979b7e57be4cabeb679ef85c25e2"
public_key1 = "0x37f91e6ec022b55c08eca29f89e47f6f03ad1af35d3a7b2cacd1514c0c9e31c0358a181b3d50552440a9b1c7ea2942b94b178179a16798bcd7fc77b1cccff309"
public_key1 = "0x37f91e6ec022b55c08eca29f89e47f6f03ad1af35d3a7b2cacd1514c0c9e31c0358a181b3d50552440a9b1c7ea2942b94b178179a16798bcd7fc77b1cccff309" # noqa: E501
private_key1 = "0x7cbb15a540c3954792bf3729f9b26c0242e745890332bcf2ffeaece345f9d141"

address2 = "0x74ff5f6a11c3d9782191dc3f3042708e396cbf3c"
public_key2 = "0xa621164c25da8bb0d87652c0c24d946dc4793f45609fd6006e23e6255646bb32d60af2800b1492aa8d8927c6904f2acab727637c072dbce786a5cd36f18cff86"
public_key2 = "0xa621164c25da8bb0d87652c0c24d946dc4793f45609fd6006e23e6255646bb32d60af2800b1492aa8d8927c6904f2acab727637c072dbce786a5cd36f18cff86" # noqa: E501
private_key2 = "0x95e8f771761c8cd8a711ca57434ad3769e9fc6fe451561820781efc8ed999a85"

# * as the last 40 characters or 20 bytes will be the address
Expand All @@ -33,7 +31,7 @@ def test_calculate_shared_secret(crypto_instance):
# crypto = Crypto()

private_key1 = "0x7cbb15a540c3954792bf3729f9b26c0242e745890332bcf2ffeaece345f9d141"
recipient_public_key1 = "0x37f91e6ec022b55c08eca29f89e47f6f03ad1af35d3a7b2cacd1514c0c9e31c0358a181b3d50552440a9b1c7ea2942b94b178179a16798bcd7fc77b1cccff309"
recipient_public_key1 = "0x37f91e6ec022b55c08eca29f89e47f6f03ad1af35d3a7b2cacd1514c0c9e31c0358a181b3d50552440a9b1c7ea2942b94b178179a16798bcd7fc77b1cccff309" # noqa: E501

shared_secret = crypto_instance.calculate_shared_secret(private_key1, recipient_public_key1)

Expand All @@ -56,4 +54,4 @@ def test_encrypt_and_decrypt_string(crypto_instance):


if __name__ == "__main__":
test_calculate_shared_secret()
test_calculate_shared_secret() # type: ignore
7 changes: 4 additions & 3 deletions src/fds/tests/unit_tests/test_fds_wallet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from fds.fds_wallet import Wallet
# from fds.fds_wallet import Wallet


def test_generate_wallet(wallet_instance):
Expand Down Expand Up @@ -37,7 +37,8 @@ def test_fail_from_json_invalid(test_wallet):
# Provide an incorrect password to test decryption failure
invalid_password = "wrong_password"
try:
wallet_from_json = test_wallet.from_json(wallet_json, invalid_password)
# * we don't nened the wallet object for this test
_ = test_wallet.from_json(wallet_json, invalid_password)
except ValueError as e:
assert str(e) == "MAC mismatch", f"Unexpected error message: {e}"
return
Expand All @@ -47,7 +48,7 @@ def test_fail_from_json_invalid(test_wallet):


def test_encrypt(test_wallet):
private_key = "0x7cbb15a540c3954792bf3729f9b26c0242e745890332bcf2ffeaece345f9d141" # Replace with a valid private key
private_key = "0x7cbb15a540c3954792bf3729f9b26c0242e745890332bcf2ffeaece345f9d141" # noqa: E501
password = "test_password"

wallet_json = test_wallet.encrypt(private_key, password)
Expand Down

0 comments on commit 467e805

Please sign in to comment.