-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Added EVMVerifier class that is used on all EVM chains. Also add…
…ed that chains to the SignatureVerifier as EVM chains.
- Loading branch information
Showing
5 changed files
with
105 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import functools | ||
import logging | ||
|
||
from eth_account import Account | ||
from eth_account.messages import encode_defunct | ||
|
||
from aleph.chains.common import get_verification_buffer | ||
from aleph.schemas.pending_messages import BasePendingMessage | ||
from aleph.utils import run_in_executor | ||
|
||
from .abc import Verifier | ||
|
||
LOGGER = logging.getLogger("chains.evm") | ||
CHAIN_NAME = "ETH" | ||
|
||
|
||
class EVMVerifier(Verifier): | ||
async def verify_signature(self, message: BasePendingMessage) -> bool: | ||
"""Verifies a signature of a message, return True if verified, false if not""" | ||
|
||
verification = get_verification_buffer(message) | ||
|
||
message_hash = await run_in_executor( | ||
None, functools.partial(encode_defunct, text=verification.decode("utf-8")) | ||
) | ||
|
||
verified = False | ||
try: | ||
# we assume the signature is a valid string | ||
address = await run_in_executor( | ||
None, | ||
functools.partial( | ||
Account.recover_message, message_hash, signature=message.signature | ||
), | ||
) | ||
if address == message.sender: | ||
verified = True | ||
else: | ||
LOGGER.warning( | ||
"Received bad signature from %s for %s" % (address, message.sender) | ||
) | ||
return False | ||
|
||
except Exception: | ||
LOGGER.exception("Error processing signature for %s" % message.sender) | ||
verified = False | ||
|
||
return verified |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
|
||
from aleph.chains.evm import EVMVerifier | ||
from aleph.schemas.pending_messages import BasePendingMessage, parse_message | ||
|
||
|
||
@pytest.fixture | ||
def evm_message() -> BasePendingMessage: | ||
return parse_message( | ||
{ | ||
"item_hash": "3d1909797f30fa7868d9cc1138128687d1b501de1ab8bbde40caa4e5a03e02bc", | ||
"type": "POST", | ||
"chain": "SOL", | ||
"sender": "0xA07B1214bAe0D5ccAA25449C3149c0aC83658874", | ||
"signature": "0x0ee01508e5dbb978bbb7482f524aca31d96b6d21086ac1f352324c29c88345bb6868d6e785ad588bbf07c2650c1fbbe74a272c82a84cce3f97c6c748594ed4471c", | ||
"item_type": "inline", | ||
"item_content": '{"type":"polygon","address":"0xA07B1214bAe0D5ccAA25449C3149c0aC83658874","content":{"body":"This message was posted from the typescript-SDK test suite"},"time":1689163528.372}', | ||
"time": 1689163528.372, | ||
"channel": "TEST", | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_verify_evm_signature_real(evm_message: BasePendingMessage): | ||
verifier = EVMVerifier() | ||
result = await verifier.verify_signature(evm_message) | ||
assert result is True | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_verify_bad_evm_signature(evm_message: BasePendingMessage): | ||
verifier = EVMVerifier() | ||
evm_message.signature = "baba" | ||
result = await verifier.verify_signature(evm_message) | ||
assert result is False |