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

Fix: ETH verify_signature fails on checksummed addresses #29

Merged
merged 1 commit into from
May 22, 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
4 changes: 2 additions & 2 deletions src/aleph/sdk/chains/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def verify_signature(
Verifies a signature.
Args:
signature: The signature to verify. Can be a hex encoded string or bytes.
public_key: The sender's public key to use for verification. Can be a checksummed, hex encoded string or bytes.
public_key: The sender's public key to use for verification. Can be a checksum, hex encoded string or bytes.
message: The message to verify. Can be an utf-8 string or bytes.
Raises:
BadSignatureError: If the signature is invalid.
Expand All @@ -75,7 +75,7 @@ def verify_signature(
message_hash = encode_defunct(text=message)
try:
address = Account.recover_message(message_hash, signature=signature)
if address != public_key:
if address.casefold() != public_key.casefold():
raise BadSignatureError
except (EthBadSignatureError, BadSignatureError) as e:
raise BadSignatureError from e
40 changes: 40 additions & 0 deletions tests/unit/test_chain_ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ async def test_verify_signature(ethereum_account):
message["signature"], message["sender"], get_verification_buffer(message)
)

# cover all branching options
verify_signature(
message["signature"][2:],
message["sender"],
get_verification_buffer(message),
)
verify_signature(
bytes(message["signature"], "utf-8"),
bytes.fromhex(message["sender"][2:]),
get_verification_buffer(message).decode("utf-8"),
)
verify_signature(
bytes(message["signature"], "utf-8")[2:],
message["sender"],
get_verification_buffer(message),
)


@pytest.mark.asyncio
async def test_verify_signature_with_forged_signature(ethereum_account):
Expand Down Expand Up @@ -98,3 +115,26 @@ async def test_decrypt_secp256k1(ethereum_account):
decrypted = await account.decrypt(encrypted)
assert type(decrypted) == bytes
assert content == decrypted


@pytest.mark.asyncio
async def test_verify_signature_wrong_public_key(ethereum_account):
account = ethereum_account

message = asdict(
Message(
"ETH",
account.get_address(),
"POST",
"SomeHash",
)
)

await account.sign_message(message)
assert message["signature"]

wrong_public_key: str = "0x" + "0" * 130
with pytest.raises(BadSignatureError):
verify_signature(
message["signature"], wrong_public_key, get_verification_buffer(message)
)
11 changes: 11 additions & 0 deletions tests/unit/test_chain_solana.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ async def test_SOLAccount(solana_account):
assert verif == verification_buffer
assert message["sender"] == signature["publicKey"]

pubkey = solana_account.get_public_key()
assert type(pubkey) == str
assert len(pubkey) == 64


@pytest.mark.asyncio
async def test_decrypt_curve25516(solana_account):
Expand Down Expand Up @@ -90,6 +94,13 @@ async def test_verify_signature(solana_account):

verify_signature(raw_signature, message["sender"], get_verification_buffer(message))

# as bytes
verify_signature(
base58.b58decode(raw_signature),
base58.b58decode(message["sender"]),
get_verification_buffer(message).decode("utf-8"),
)


@pytest.mark.asyncio
async def test_verify_signature_with_forged_signature(solana_account):
Expand Down