Skip to content

Commit 8258f18

Browse files
authored
Merge pull request #809 from humanprotocol/upload-encrypted-file
Upload encrypted file
2 parents 96ca12a + 503d071 commit 8258f18

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

packages/sdk/python/human-protocol-sdk/Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ setuptools-pipfile = "*"
1212
[packages]
1313
cryptography = "*"
1414
minio = "*"
15-
validators = "*"
15+
validators = "==0.20.0"
1616
web3 = "==6.8.*"
1717
aiohttp = "<4.0.0" # broken freeze in one of dependencies
1818
pgpy = "*"

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848
reputation_oracle_fee: Decimal,
4949
manifest_url: str,
5050
hash: str,
51+
skip_manifest_url_validation: bool = False,
5152
):
5253
"""
5354
Initializes a Escrow instance.
@@ -59,6 +60,7 @@ def __init__(
5960
reputation_oracle_fee (Decimal): Fee percentage of the Reputation Oracle
6061
manifest_url (str): Manifest file url
6162
hash (str): Manifest file hash
63+
skip_manifest_url_validation (bool): Identify wether validate manifest_url
6264
"""
6365
if not Web3.is_address(recording_oracle_address):
6466
raise EscrowClientError(
@@ -74,7 +76,7 @@ def __init__(
7476
raise EscrowClientError("Fee must be between 0 and 100")
7577
if recording_oracle_fee + reputation_oracle_fee > 100:
7678
raise EscrowClientError("Total fee must be less than 100")
77-
if not URL(manifest_url):
79+
if not URL(manifest_url) and not skip_manifest_url_validation:
7880
raise EscrowClientError(f"Invalid manifest URL: {manifest_url}")
7981
if not hash:
8082
raise EscrowClientError("Invalid empty manifest hash")
@@ -187,7 +189,7 @@ def create_escrow(self, token_address: str, trusted_handlers: List[str]) -> str:
187189
)
188190
return next(
189191
(
190-
self.factory_contract.events.Launched().processLog(log)
192+
self.factory_contract.events.Launched().process_log(log)
191193
for log in transaction_receipt["logs"]
192194
if log["address"] == self.network["factory_address"]
193195
),

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/storage.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -175,31 +175,36 @@ def download_files(self, files: List[str], bucket: str) -> List[bytes]:
175175
raise StorageClientError(str(e))
176176
return result_files
177177

178-
def upload_files(self, files: List[object], bucket: str) -> List[str]:
178+
def upload_files(self, files: List[dict], bucket: str) -> List[dict]:
179179
"""
180180
Uploads a list of files to the specified S3-compatible bucket.
181181
182182
Args:
183-
files (list[object]): A list of files to upload.
183+
files (list[dict]): A list of files to upload.
184184
bucket (str): The name of the S3-compatible bucket to upload to.
185185
186186
Returns:
187-
list: A list of keys assigned to the uploaded files.
187+
list: List of dict with key, url, hash fields
188188
189189
Raises:
190190
StorageClientError: If an error occurs while uploading the files.
191191
"""
192192
result_files = []
193193
for file in files:
194-
try:
195-
artifact = json.dumps(file, sort_keys=True)
196-
except Exception as e:
197-
LOG.error("Can't extract the json from the object")
198-
raise e
194+
if "file" in file and "key" in file and "hash" in file:
195+
data = file["file"]
196+
hash = file["hash"]
197+
key = file["key"]
198+
else:
199+
try:
200+
artifact = json.dumps(file, sort_keys=True)
201+
except Exception as e:
202+
LOG.error("Can't extract the json from the object")
203+
raise e
204+
data = artifact.encode("utf-8")
205+
hash = hashlib.sha1(data).hexdigest()
206+
key = f"s3{hash}.json"
199207

200-
data = artifact.encode("utf-8")
201-
hash = hashlib.sha1(data).hexdigest()
202-
key = f"s3{hash}.json"
203208
url = (
204209
f"{'https' if self.secure else 'http'}://{self.endpoint}/{bucket}/{key}"
205210
)

packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/test_escrow.py

+29
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ def test_escrow_config_valid_params(self):
9090
self.assertEqual(escrow_config.manifest_url, manifest_url)
9191
self.assertEqual(escrow_config.hash, hash)
9292

93+
def test_escrow_config_valid_params(self):
94+
recording_oracle_address = "0x1234567890123456789012345678901234567890"
95+
reputation_oracle_address = "0x1234567890123456789012345678901234567890"
96+
recording_oracle_fee = 10
97+
reputation_oracle_fee = 10
98+
manifest_url = "http://test:6000"
99+
hash = "test"
100+
101+
escrow_config = EscrowConfig(
102+
recording_oracle_address,
103+
reputation_oracle_address,
104+
recording_oracle_fee,
105+
reputation_oracle_fee,
106+
manifest_url,
107+
hash,
108+
True,
109+
)
110+
111+
self.assertEqual(
112+
escrow_config.recording_oracle_address, recording_oracle_address
113+
)
114+
self.assertEqual(
115+
escrow_config.reputation_oracle_address, reputation_oracle_address
116+
)
117+
self.assertEqual(escrow_config.recording_oracle_fee, recording_oracle_fee)
118+
self.assertEqual(escrow_config.reputation_oracle_fee, reputation_oracle_fee)
119+
self.assertEqual(escrow_config.manifest_url, manifest_url)
120+
self.assertEqual(escrow_config.hash, hash)
121+
93122
def test_escrow_config_invalid_address(self):
94123
invalid_address = "invalid_address"
95124
valid_address = "0x1234567890123456789012345678901234567890"

packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/test_storage.py

+29
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ def test_upload_files(self):
176176
)
177177
self.assertEqual(result[0]["hash"], hash)
178178

179+
def test_upload_encrypted_files(self):
180+
encrypted_file = "encrypted file content"
181+
hash = hashlib.sha1(json.dumps(encrypted_file).encode("utf-8")).hexdigest()
182+
encrypted_file_key = f"s3{hash}"
183+
file = {
184+
"file": encrypted_file.encode("utf-8"),
185+
"hash": hash,
186+
"key": encrypted_file_key,
187+
}
188+
189+
self.client.client.stat_object = MagicMock(
190+
side_effect=S3Error(
191+
code="NoSuchKey",
192+
message="Object does not exist",
193+
resource="",
194+
request_id="",
195+
host_id="",
196+
response="",
197+
)
198+
)
199+
self.client.client.put_object = MagicMock()
200+
result = self.client.upload_files(files=[file], bucket=self.bucket)
201+
self.assertEqual(result[0]["key"], encrypted_file_key)
202+
self.assertEqual(
203+
result[0]["url"],
204+
f"https://s3.us-west-2.amazonaws.com/my-bucket/{encrypted_file_key}",
205+
)
206+
self.assertEqual(result[0]["hash"], hash)
207+
179208
def test_upload_files_exist(self):
180209
file3 = "file3 content"
181210
hash = hashlib.sha1(json.dumps("file3 content").encode("utf-8")).hexdigest()

0 commit comments

Comments
 (0)