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

Output error message from Invalid Signature request. #639

Merged
merged 2 commits into from
Apr 13, 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
58 changes: 28 additions & 30 deletions ocean_provider/utils/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,36 @@ def verify_signature(signer_address, signature, original_msg, nonce):
:return: True if signature is valid, throws InvalidSignatureError otherwise
"""
verify_nonce(signer_address, nonce)
# old_signature = signature

message = f"{original_msg}{str(nonce)}"
signature_bytes = Web3.toBytes(hexstr=signature)
if signature_bytes[64] == 27:
new_signature = b"".join([signature_bytes[0:64], b"\x00"])
elif signature_bytes[64] == 28:
new_signature = b"".join([signature_bytes[0:64], b"\x01"])
else:
new_signature = signature_bytes

signature = keys.Signature(signature_bytes=new_signature)

# TODO: restore this check
# if old_signature != str(signature):
# msg = (
# f"Invalid signature. Please check the nonce or documentId from the original message."
# f" In case of compute endpoints, check also the job ID."
# f" Got: {old_signature}, expected {signature}\n."
# )
# logger.error(msg)
# raise InvalidSignatureError(msg)

message_hash = Web3.solidityKeccak(
["bytes"],
[Web3.toBytes(text=message)],
)
prefix = "\x19Ethereum Signed Message:\n32"
signable_hash = Web3.solidityKeccak(
["bytes", "bytes"], [Web3.toBytes(text=prefix), Web3.toBytes(message_hash)]
)
vkey = keys.ecdsa_recover(signable_hash, signature)
try:
signature_bytes = Web3.toBytes(hexstr=signature)
if signature_bytes[64] == 27:
new_signature = b"".join([signature_bytes[0:64], b"\x00"])
elif signature_bytes[64] == 28:
new_signature = b"".join([signature_bytes[0:64], b"\x01"])
else:
new_signature = signature_bytes

signature = keys.Signature(signature_bytes=new_signature)

message_hash = Web3.solidityKeccak(
["bytes"],
[Web3.toBytes(text=message)],
)
prefix = "\x19Ethereum Signed Message:\n32"
signable_hash = Web3.solidityKeccak(
["bytes", "bytes"], [Web3.toBytes(text=prefix), Web3.toBytes(message_hash)]
)
vkey = keys.ecdsa_recover(signable_hash, signature)
except Exception as e:
msg = (
f"Invalid signature {signature} for "
f"ethereum address {signer_address}, message {original_msg} "
f"and nonce {nonce}. Got {e}"
)
logger.error(msg)
raise InvalidSignatureError(msg)

if Web3.toChecksumAddress(signer_address) != Web3.toChecksumAddress(
vkey.to_address()
Expand Down
30 changes: 26 additions & 4 deletions ocean_provider/validation/provider_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,28 @@ def __init__(self, request=None):
def validate(self):
for validator in self._validators:
if validator.fails():
raise ValidationException(validator.messages())
messages = self.overwrite_messages(validator)
raise ValidationException(messages)
return True

def overwrite_messages(self, validator):
messages = validator.messages()

if not hasattr(validator._processor, "signature_error_message"):
return messages

for overwritable_key in [
"signature",
"download_signature",
"decrypt_signature",
]:
if overwritable_key in messages:
messages[
overwritable_key
] = validator._processor.signature_error_message

return messages


class CustomValidator(Validator):
"""
Expand Down Expand Up @@ -145,7 +164,8 @@ def validate_signature(self, value, params, **kwargs):
try:
verify_signature(owner, value, original_msg, nonce)
return True
except InvalidSignatureError:
except InvalidSignatureError as e:
self.signature_error_message = str(e)
pass

return False
Expand Down Expand Up @@ -176,7 +196,8 @@ def validate_download_signature(self, value, params, **kwargs):
try:
verify_signature(owner, value, original_msg, nonce)
return True
except InvalidSignatureError:
except InvalidSignatureError as e:
self.signature_error_message = str(e)
pass

return False
Expand Down Expand Up @@ -222,7 +243,8 @@ def validate_decrypt_signature(self, value, params, **kwargs):
verify_signature(decrypter_address, value, original_msg, nonce)
logger.info("Correct signature.")
return True
except InvalidSignatureError:
except InvalidSignatureError as e:
self.signature_error_message = str(e)
pass

return False
Expand Down