From 10becf384a469e832634291205edb55771f67181 Mon Sep 17 00:00:00 2001 From: banteg <4562643+banteg@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:25:11 +0400 Subject: [PATCH 1/3] feat: sign eip-712 message instead of hash see also https://docs.safe.global/advanced/smart-account-signatures --- ape_safe/accounts.py | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/ape_safe/accounts.py b/ape_safe/accounts.py index 795271f..d5a88a5 100644 --- a/ape_safe/accounts.py +++ b/ape_safe/accounts.py @@ -179,16 +179,14 @@ def _get_path(self, alias: str) -> Path: def get_signatures( - safe_tx_hash: str, + safe_tx: SafeTx, signers: Iterable[AccountAPI], ) -> Dict[AddressType, MessageSignature]: signatures: Dict[AddressType, MessageSignature] = {} for signer in signers: - message = encode_defunct(hexstr=safe_tx_hash) - signature = signer.sign_message(message) + signature = signer.sign_message(safe_tx) if signature: - signature_adjusted = adjust_v_in_signature(signature) - signatures[signer.address] = signature_adjusted + signatures[signer.address] = signature return signatures @@ -677,7 +675,7 @@ def skip_signer(signer: AccountAPI): # NOTE: It is okay to have less signatures, but it never should fetch more than needed signers = [x for x in available_signers if x.address not in sigs_by_signer] if signers: - new_signatures = get_signatures(safe_tx_hash, signers) + new_signatures = get_signatures(safe_tx, signers) sigs_by_signer = {**sigs_by_signer, **new_signatures} if ( @@ -744,7 +742,7 @@ def add_signatures( ][:amount_needed] safe_tx_hash = _get_safe_tx_id(safe_tx, confirmations) - signatures = get_signatures(safe_tx_hash, signers) + signatures = get_signatures(safe_tx, signers) if signatures: self.client.post_signatures(safe_tx_hash, signatures) @@ -762,20 +760,3 @@ def _get_safe_tx_id(safe_tx: SafeTx, confirmations: list[SafeTxConfirmation]) -> return value raise ApeSafeError("Failed to get transaction hash.") - - -def adjust_v_in_signature(signature: MessageSignature) -> MessageSignature: - MIN_VALID_V_VALUE_FOR_SAFE_ECDSA = 27 - v = signature.v - - if v < MIN_VALID_V_VALUE_FOR_SAFE_ECDSA: - v += MIN_VALID_V_VALUE_FOR_SAFE_ECDSA - - # Add 4 because we signed with the prefix. - v += 4 - - return MessageSignature( - v=v, - r=signature.r, - s=signature.s, - ) From 89883ab2f17540ef0f6f71a3bd5ae98aefbe9ce5 Mon Sep 17 00:00:00 2001 From: banteg <4562643+banteg@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:20:30 +0400 Subject: [PATCH 2/3] lint: import --- ape_safe/accounts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ape_safe/accounts.py b/ape_safe/accounts.py index d5a88a5..dd77e38 100644 --- a/ape_safe/accounts.py +++ b/ape_safe/accounts.py @@ -16,7 +16,6 @@ from ape.utils import ZERO_ADDRESS, cached_property from ape_ethereum.transactions import StaticFeeTransaction, TransactionType from eip712.common import create_safe_tx_def -from eth_account.messages import encode_defunct from eth_utils import keccak, to_bytes, to_int from ethpm_types import ContractType From 50ccbc3bc708762b83a5b6f41d535aea127a1ae0 Mon Sep 17 00:00:00 2001 From: banteg <4562643+banteg@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:30:26 +0400 Subject: [PATCH 3/3] fix: cli signing --- ape_safe/_cli/pending.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ape_safe/_cli/pending.py b/ape_safe/_cli/pending.py index 62e96ce..75229da 100644 --- a/ape_safe/_cli/pending.py +++ b/ape_safe/_cli/pending.py @@ -129,7 +129,7 @@ def propose(cli_ctx, ecosystem, safe, data, gas_price, value, receiver, nonce, s ) safe_tx = safe.create_safe_tx(txn) safe_tx_hash = get_safe_tx_hash(safe_tx) - signatures = get_signatures(safe_tx_hash, safe.local_signers) + signatures = get_signatures(safe_tx, safe.local_signers) num_confirmations = 0 submitter = sender if isinstance(sender, AccountAPI) else None