Skip to content

Commit

Permalink
Output error message from Invalid Signature request.
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c committed Apr 13, 2023
1 parent 8b6d4bb commit 2e42052
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
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
23 changes: 19 additions & 4 deletions ocean_provider/validation/provider_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ def __init__(self, request=None):
def validate(self):
for validator in self._validators:
if validator.fails():
raise ValidationException(validator.messages())
messages = validator.messages()
for overwritable_key in [
"signature",
"download_signature",
"decrypt_signature",
]:
if overwritable_key in messages and hasattr(
validator._processor, "signature_error_message"
):
messages[
overwritable_key
] = validator._processor.signature_error_message
raise ValidationException(messages)
return True


Expand Down Expand Up @@ -145,7 +157,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 +189,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 +236,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

0 comments on commit 2e42052

Please sign in to comment.