Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of JSONDict for encryption requests field schema; marshmallow Dict field is sufficient. #30

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ name = "pypi"
python_version = "3"

[packages]
nucypher = {git = "https://github.com/nucypher/nucypher.git", ref = "dkg-dev-6"}
nucypher-core = ">=0.9.0" # must be the same as nucypher
nucypher = {git = "https://github.com/nucypher/nucypher.git", ref = "dkg-dev-7"}
nucypher-core = ">=0.10.0" # must be the same as nucypher
flask-cors = "*"

[dev-packages]
nucypher = {git = "https://github.com/nucypher/nucypher.git", editable = true, ref = "dkg-dev-6", extras = ["dev"]} # needed for testerchain, and must be editable
nucypher = {git = "https://github.com/nucypher/nucypher.git", editable = true, ref = "dkg-dev-7", extras = ["dev"]} # needed for testerchain, and must be editable
pytest = "<7" # match with nucypher/nucypher
pytest-cov = "*"
pytest-mock = "*"
Expand Down
86 changes: 43 additions & 43 deletions Pipfile.lock

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

6 changes: 3 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ msgspec==0.15.1 ; python_version >= '3.8'
multidict==5.2.0 ; python_version >= '3.6'
mypy-extensions==0.4.4 ; python_version >= '2.7'
nodeenv==1.8.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6'
nucypher @ git+https://github.com/nucypher/nucypher.git@4862a9a086d8e69f31f3b93f8421e810db5169ef
nucypher-core==0.9.0
nucypher @ git+https://github.com/nucypher/nucypher.git@1032a2b5560ef8e5afa02097510f13812943e219
nucypher-core==0.10.0
numpy==1.24.3 ; python_version >= '3.8'
packaging==23.1 ; python_version >= '3.7'
pandas==1.5.3 ; python_version >= '3.8'
Expand Down Expand Up @@ -143,7 +143,7 @@ rich==12.6.0 ; python_full_version >= '3.6.3' and python_full_version < '4.0.0'
rlp==3.0.0
rpds-py==0.7.1 ; python_version >= '3.8'
semantic-version==2.10.0 ; python_version >= '2.7'
sentry-sdk==1.25.1
sentry-sdk==1.26.0
service-identity==21.1.0
setuptools==67.8.0 ; python_version >= '3.7'
six==1.16.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'
Expand Down
37 changes: 0 additions & 37 deletions porter/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,40 +102,3 @@ def _deserialize(self, value, attr, data, **kwargs):
f"Unexpected object type, {type(result)}; expected {self.expected_type}")

return result


class JSONDict(BaseField, fields.Dict):
"""Serializes/Deserializes Dictionaries to/from JSON strings."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _serialize(self, value, attr, obj, **kwargs):
try:
value = super()._serialize(value, attr, obj, **kwargs)
except Exception as e:
raise InvalidInputData(
f"Could not convert input for {self.name} to JSON: {e}"
)
try:
value_json = json.dumps(value)
return value_json
except Exception as e:
raise InvalidInputData(
f"Could not convert input for {self.name} to JSON: {e}"
)

def _deserialize(self, value, attr, data, **kwargs):
try:
result = json.loads(value)
except Exception as e:
raise InvalidInputData(
f"Could not convert input for {self.name} to dictionary: {e}"
)

try:
return super()._deserialize(result, attr, data, **kwargs)
except Exception as e:
raise InvalidInputData(
f"Could not convert input for {self.name} to dictionary: {e}"
)
24 changes: 10 additions & 14 deletions porter/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
from marshmallow import INCLUDE, Schema
from marshmallow import fields as marshmallow_fields
from marshmallow import validates_schema
from marshmallow.fields import URL, Dict, String

from porter.cli.types import EIP55_CHECKSUM_ADDRESS
from porter.fields.base import (
JSON,
Base64BytesRepresentation,
Integer,
JSONDict,
PositiveInteger,
StringList,
)
from porter.fields.base import JSON, Integer, PositiveInteger, StringList
from porter.fields.cbd import (
EncryptedThresholdDecryptionRequestField,
EncryptedThresholdDecryptionResponseField,
Expand Down Expand Up @@ -54,7 +46,7 @@ def option_bob_encrypting_key():
class UrsulaInfoSchema(BaseSchema):
"""Schema for the result of sampling of Ursulas."""
checksum_address = UrsulaChecksumAddress()
uri = URL()
uri = marshmallow_fields.URL()
encrypting_key = UmbralKey()

# maintain field declaration ordering
Expand Down Expand Up @@ -132,7 +124,9 @@ class PRERetrievalOutcomeSchema(BaseSchema):
"""Schema for the result of /retrieve_cfrags endpoint."""

cfrags = marshmallow_fields.Dict(keys=UrsulaChecksumAddress(), values=CapsuleFrag())
errors = marshmallow_fields.Dict(keys=UrsulaChecksumAddress(), values=String())
errors = marshmallow_fields.Dict(
keys=UrsulaChecksumAddress(), values=marshmallow_fields.String()
)

# maintain field declaration ordering
class Meta:
Expand Down Expand Up @@ -211,10 +205,12 @@ class PRERetrieveCFrags(BaseSchema):
class CBDDecryptionOutcomeSchema(BaseSchema):
"""Schema for the result of /retrieve_cfrags endpoint."""

encrypted_decryption_responses = Dict(
encrypted_decryption_responses = marshmallow_fields.Dict(
keys=UrsulaChecksumAddress(), values=EncryptedThresholdDecryptionResponseField()
)
errors = Dict(keys=UrsulaChecksumAddress(), values=String())
errors = marshmallow_fields.Dict(
keys=UrsulaChecksumAddress(), values=marshmallow_fields.String()
)

# maintain field declaration ordering
class Meta:
Expand All @@ -233,7 +229,7 @@ class CBDDecrypt(BaseSchema):
required=True
)
)
encrypted_decryption_requests = JSONDict(
encrypted_decryption_requests = marshmallow_fields.Dict(
keys=UrsulaChecksumAddress(),
values=EncryptedThresholdDecryptionRequestField(),
required=True,
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ eth-tester==0.9.0b1 ; python_version < '4' and python_full_version >= '3.6.8'
eth-typing==3.3.0 ; python_version < '4' and python_full_version >= '3.7.2'
eth-utils==2.1.0 ; python_version >= '3.7' and python_version < '4'
flask==2.2.5 ; python_version >= '3.7'
flask-cors==3.0.10
flask-cors==4.0.0
frozenlist==1.3.3 ; python_version >= '3.7'
hendrix==4.0.0
hexbytes==0.3.0 ; python_version >= '3.7' and python_version < '4'
Expand All @@ -56,8 +56,8 @@ msgpack==1.0.5
msgpack-python==0.5.6
multidict==5.2.0 ; python_version >= '3.6'
mypy-extensions==0.4.4 ; python_version >= '2.7'
nucypher @ git+https://github.com/nucypher/nucypher.git@4862a9a086d8e69f31f3b93f8421e810db5169ef
nucypher-core==0.9.0
nucypher @ git+https://github.com/nucypher/nucypher.git@1032a2b5560ef8e5afa02097510f13812943e219
nucypher-core==0.10.0
packaging==23.1 ; python_version >= '3.7'
parsimonious==0.9.0
pendulum==3.0.0a1 ; python_version >= '3.7' and python_version < '4.0'
Expand Down
21 changes: 7 additions & 14 deletions tests/cbd/test_cbd_specifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def test_cbd_decrypt(
porter, dkg_setup, dkg_encrypted_data, get_random_checksum_address
):
ritual_id, public_key, cohort, _, threshold = dkg_setup
ritual_id, public_key, cohort, threshold = dkg_setup
ciphertext, expected_plaintext, conditions = dkg_encrypted_data

cbd_decrypt_schema = CBDDecrypt()
Expand Down Expand Up @@ -61,37 +61,30 @@ def test_cbd_decrypt(

with pytest.raises(InvalidInputData):
request_data = {
"encrypted_decryption_requests": json.dumps(encrypted_decryption_requests)
"encrypted_decryption_requests": encrypted_decryption_requests,
}
cbd_decrypt_schema.load(request_data)

# invalid param names
with pytest.raises(InvalidInputData):
request_data = {
"dkg_threshold": threshold,
"encrypted_decryption_requests": json.dumps(encrypted_decryption_requests),
"encrypted_decryption_requests": encrypted_decryption_requests,
}
cbd_decrypt_schema.load(request_data)

with pytest.raises(InvalidInputData):
request_data = {
"threshold": threshold,
"encrypted_dec_requests": json.dumps(encrypted_decryption_requests),
"encrypted_dec_requests": encrypted_decryption_requests,
}
cbd_decrypt_schema.load(request_data)

# invalid param types
with pytest.raises(InvalidInputData):
request_data = {
"threshold": "threshold? we don't need no stinking threshold",
"encrypted_decryption_requests": json.dumps(encrypted_decryption_requests),
}
cbd_decrypt_schema.load(request_data)

with pytest.raises(InvalidInputData):
request_data = {
"threshold": threshold,
"encrypted_decryption_requests": encrypted_decryption_requests, # not json string
"encrypted_decryption_requests": encrypted_decryption_requests,
}
cbd_decrypt_schema.load(request_data)

Expand All @@ -100,14 +93,14 @@ def test_cbd_decrypt(
request_data = {
"threshold": len(encrypted_decryption_requests)
+ 1, # threshold larger than number of requests
"encrypted_decryption_requests": json.dumps(encrypted_decryption_requests),
"encrypted_decryption_requests": encrypted_decryption_requests,
}
cbd_decrypt_schema.load(request_data)

# simple schema successful load
request_data = {
"threshold": threshold,
"encrypted_decryption_requests": json.dumps(encrypted_decryption_requests),
"encrypted_decryption_requests": encrypted_decryption_requests,
}
cbd_decrypt_schema.load(request_data)

Expand Down
3 changes: 1 addition & 2 deletions tests/cbd/test_porter_cbd_python_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def test_cbd_decryption(porter, dkg_setup, dkg_encrypted_data):
ritual_id, public_key, cohort, params, threshold = dkg_setup
ritual_id, public_key, cohort, threshold = dkg_setup
ciphertext, expected_plaintext, conditions = dkg_encrypted_data

decryption_request = ThresholdDecryptionRequest(
Expand Down Expand Up @@ -73,7 +73,6 @@ def test_cbd_decryption(porter, dkg_setup, dkg_encrypted_data):
ciphertext,
conditions, # aad
combined_shares,
params, # dkg params
)
assert bytes(cleartext) == expected_plaintext

Expand Down
Loading