Skip to content

Commit

Permalink
Handle exception for get_signing_address to get previous behaviour
Browse files Browse the repository at this point in the history
- Before deprecating `pyethereum` NULL_ADDRESS was returned instead of raising an exception
  • Loading branch information
Uxio0 committed Feb 9, 2022
1 parent 44e2c65 commit 92b2d67
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 9 additions & 3 deletions gnosis/safe/signatures.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import List, Tuple, Union

from eth_keys import keys
from eth_keys.exceptions import BadSignature
from hexbytes import HexBytes

from gnosis.eth.constants import NULL_ADDRESS


def signature_split(
signatures: Union[bytes, str], pos: int = 0
Expand Down Expand Up @@ -53,7 +56,10 @@ def signatures_to_bytes(signatures: List[Tuple[int, int, int]]) -> bytes:
def get_signing_address(signed_hash: Union[bytes, str], v: int, r: int, s: int) -> str:
"""
:return: checksummed ethereum address, for example `0x568c93675A8dEb121700A6FAdDdfE7DFAb66Ae4A`
:rtype: str
:rtype: str or `NULL_ADDRESS` if signature is not valid
"""
public_key = keys.ecdsa_recover(signed_hash, keys.Signature(vrs=(v - 27, r, s)))
return public_key.to_checksum_address()
try:
public_key = keys.ecdsa_recover(signed_hash, keys.Signature(vrs=(v - 27, r, s)))
return public_key.to_checksum_address()
except BadSignature:
return NULL_ADDRESS
26 changes: 26 additions & 0 deletions gnosis/safe/tests/test_signatures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.test import TestCase

from eth_account import Account
from web3 import Web3

from gnosis.eth.constants import NULL_ADDRESS

from ..signatures import get_signing_address


class TestSafeSignature(TestCase):
def test_get_signing_address(self):
account = Account.create()
# Random hash
random_hash = Web3.keccak(text="tanxugueiras")
signature = account.signHash(random_hash)
self.assertEqual(
get_signing_address(random_hash, signature.v, signature.r, signature.s),
account.address,
)

# Invalid signature will return the `NULL_ADDRESS`
self.assertEqual(
get_signing_address(random_hash, signature.v - 8, signature.r, signature.s),
NULL_ADDRESS,
)

0 comments on commit 92b2d67

Please sign in to comment.