Skip to content

Commit

Permalink
EscrowConfig can skip manifest URL validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alidzm committed Aug 22, 2023
1 parent 9714620 commit 503d071
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/python/human-protocol-sdk/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ setuptools-pipfile = "*"
[packages]
cryptography = "*"
minio = "*"
validators = "*"
validators = "==0.20.0"
web3 = "==6.8.*"
aiohttp = "<4.0.0" # broken freeze in one of dependencies
pgpy = "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(
reputation_oracle_fee: Decimal,
manifest_url: str,
hash: str,
skip_manifest_url_validation: bool = False,
):
"""
Initializes a Escrow instance.
Expand All @@ -59,6 +60,7 @@ def __init__(
reputation_oracle_fee (Decimal): Fee percentage of the Reputation Oracle
manifest_url (str): Manifest file url
hash (str): Manifest file hash
skip_manifest_url_validation (bool): Identify wether validate manifest_url
"""
if not Web3.is_address(recording_oracle_address):
raise EscrowClientError(
Expand All @@ -74,7 +76,7 @@ def __init__(
raise EscrowClientError("Fee must be between 0 and 100")
if recording_oracle_fee + reputation_oracle_fee > 100:
raise EscrowClientError("Total fee must be less than 100")
if not URL(manifest_url):
if not URL(manifest_url) and not skip_manifest_url_validation:
raise EscrowClientError(f"Invalid manifest URL: {manifest_url}")
if not hash:
raise EscrowClientError("Invalid empty manifest hash")
Expand Down Expand Up @@ -187,7 +189,7 @@ def create_escrow(self, token_address: str, trusted_handlers: List[str]) -> str:
)
return next(
(
self.factory_contract.events.Launched().processLog(log)
self.factory_contract.events.Launched().process_log(log)
for log in transaction_receipt["logs"]
if log["address"] == self.network["factory_address"]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ def test_escrow_config_valid_params(self):
self.assertEqual(escrow_config.manifest_url, manifest_url)
self.assertEqual(escrow_config.hash, hash)

def test_escrow_config_valid_params(self):
recording_oracle_address = "0x1234567890123456789012345678901234567890"
reputation_oracle_address = "0x1234567890123456789012345678901234567890"
recording_oracle_fee = 10
reputation_oracle_fee = 10
manifest_url = "http://test:6000"
hash = "test"

escrow_config = EscrowConfig(
recording_oracle_address,
reputation_oracle_address,
recording_oracle_fee,
reputation_oracle_fee,
manifest_url,
hash,
True,
)

self.assertEqual(
escrow_config.recording_oracle_address, recording_oracle_address
)
self.assertEqual(
escrow_config.reputation_oracle_address, reputation_oracle_address
)
self.assertEqual(escrow_config.recording_oracle_fee, recording_oracle_fee)
self.assertEqual(escrow_config.reputation_oracle_fee, reputation_oracle_fee)
self.assertEqual(escrow_config.manifest_url, manifest_url)
self.assertEqual(escrow_config.hash, hash)

def test_escrow_config_invalid_address(self):
invalid_address = "invalid_address"
valid_address = "0x1234567890123456789012345678901234567890"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,35 @@ def test_upload_files(self):
)
self.assertEqual(result[0]["hash"], hash)

def test_upload_encrypted_files(self):
encrypted_file = "encrypted file content"
hash = hashlib.sha1(json.dumps(encrypted_file).encode("utf-8")).hexdigest()
encrypted_file_key = f"s3{hash}"
file = {
"file": encrypted_file.encode("utf-8"),
"hash": hash,
"key": encrypted_file_key,
}

self.client.client.stat_object = MagicMock(
side_effect=S3Error(
code="NoSuchKey",
message="Object does not exist",
resource="",
request_id="",
host_id="",
response="",
)
)
self.client.client.put_object = MagicMock()
result = self.client.upload_files(files=[file], bucket=self.bucket)
self.assertEqual(result[0]["key"], encrypted_file_key)
self.assertEqual(
result[0]["url"],
f"https://s3.us-west-2.amazonaws.com/my-bucket/{encrypted_file_key}",
)
self.assertEqual(result[0]["hash"], hash)

def test_upload_files_exist(self):
file3 = "file3 content"
hash = hashlib.sha1(json.dumps("file3 content").encode("utf-8")).hexdigest()
Expand Down

0 comments on commit 503d071

Please sign in to comment.