diff --git a/ocean_provider/utils/accounts.py b/ocean_provider/utils/accounts.py index 73ca588c..335e762a 100644 --- a/ocean_provider/utils/accounts.py +++ b/ocean_provider/utils/accounts.py @@ -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() diff --git a/ocean_provider/validation/provider_requests.py b/ocean_provider/validation/provider_requests.py index ba0f32ef..924436f0 100644 --- a/ocean_provider/validation/provider_requests.py +++ b/ocean_provider/validation/provider_requests.py @@ -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): """ @@ -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 @@ -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 @@ -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