Skip to content

Commit

Permalink
refactor: use new AccountAPI.sign_raw_msghash method
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Jun 5, 2024
1 parent eddd1f8 commit 7d8cafd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
17 changes: 4 additions & 13 deletions ape_safe/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,18 @@ def get_delegates(self) -> Dict[AddressType, List[AddressType]]:
return delegates

def add_delegate(self, delegate: AddressType, label: str, delegator: AccountAPI):
# TODO: Replace this by adding raw hash signing into supported account plugins
# See: https://github.com/ApeWorX/ape/issues/1962
if not isinstance(delegator, KeyfileAccount):
raise ActionNotPerformedError("Need access to private key for this method.")

logger.warning("Need to unlock account to add a delegate.")
delegator.unlock() # NOTE: Ensures we have the key handy

msg_hash = self.create_delegate_message(delegate)

# NOTE: This is required as Safe API uses an antiquated .signHash method
sig = EthAccount.signHash(
msg_hash,
delegator._KeyfileAccount__cached_key, # type: ignore[attr-defined]
)
if not (sig := delegator.sign_raw_msghash(msg_hash)):
raise ActionNotPerformedError("Did not sign delegation")

payload = {
"safe": self.address,
"delegate": delegate,
"delegator": delegator.address,
"label": label,
"signature": sig.signature.hex(),
"signature": sig.encode_rsv().hex(),
}
self._post("delegates", json=payload)

Expand Down
6 changes: 3 additions & 3 deletions ape_safe/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests
import urllib3
from ape.api import AccountAPI
from ape.types import AddressType, MessageSignature
from ape.types import AddressType, HexBytes, MessageSignature
from eth_utils import keccak
from requests import Response
from requests.adapters import HTTPAdapter
Expand Down Expand Up @@ -113,11 +113,11 @@ def get_transactions(

yield txn

def create_delegate_message(self, delegate: AddressType) -> bytes:
def create_delegate_message(self, delegate: AddressType) -> HexBytes:
# NOTE: referencing https://github.com/safe-global/safe-eth-py/blob/
# a0a5771622f143ee6301cfc381c5ed50832ff482/gnosis/safe/api/transaction_service_api.py#L34
totp = int(time.time()) // 3600
return keccak(text=(delegate + str(totp)))
return HexBytes(keccak(text=(delegate + str(totp))))

@abstractmethod
def get_delegates(self) -> dict[AddressType, list[AddressType]]: ...
Expand Down

0 comments on commit 7d8cafd

Please sign in to comment.