-
Notifications
You must be signed in to change notification settings - Fork 320
Fix 1271 contract signatures #2539
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
safe_transaction_service/safe_messages/migrations/0006_remove_contract_signatures.py
This file contains hidden or 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,28 @@ | ||
| # Generated by Django 5.0.13 on 2025-06-20 12:09 | ||
| # Contract signatures will not be valid as v1.4.1 contracts change how they are validated | ||
| # Contract signatures must be deleted from database to prevent issues | ||
|
|
||
| from django.db import migrations | ||
|
|
||
| from safe_eth.safe.safe_signature import SafeSignatureType | ||
|
|
||
|
|
||
| def delete_safe_message_contract_confirmations(apps, schema_editor): | ||
| SafeMessageConfirmation = apps.get_model("safe_messages", "SafeMessageConfirmation") | ||
| SafeMessageConfirmation.objects.filter( | ||
| signature_type=SafeSignatureType.CONTRACT_SIGNATURE.value | ||
| ).delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("safe_messages", "0005_safemessage_origin"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| delete_safe_message_contract_confirmations, | ||
| reverse_code=migrations.RunPython.noop, | ||
| ), | ||
| ] |
This file contains hidden or 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 hidden or 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
58 changes: 58 additions & 0 deletions
58
safe_transaction_service/safe_messages/tests/test_migrations.py
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,66 @@ | ||
| from unittest import mock | ||
| from unittest.mock import PropertyMock | ||
|
|
||
| from django.test import TestCase | ||
|
|
||
| from django_test_migrations.migrator import Migrator | ||
| from safe_eth.safe import Safe | ||
| from safe_eth.safe.safe_signature import SafeSignatureType | ||
|
|
||
| from safe_transaction_service.safe_messages.tests.factories import ( | ||
| SafeMessageConfirmationFactory, | ||
| ) | ||
|
|
||
|
|
||
| class TestMigrations(TestCase): | ||
| def setUp(self) -> None: | ||
| self.migrator = Migrator(database="default") | ||
|
|
||
| @mock.patch.object( | ||
| Safe, "domain_separator", new_callable=mock.PropertyMock, return_value=b"23" | ||
| ) | ||
| def test_migration_forward_0006_remove_contract_signatures( | ||
| self, domain_separator_mock: PropertyMock | ||
| ): | ||
| old_state = self.migrator.apply_initial_migration( | ||
| ("safe_messages", "0005_safemessage_origin") | ||
| ) | ||
| SafeMessageConfirmationFactory( | ||
| signature_type=SafeSignatureType.CONTRACT_SIGNATURE.value | ||
| ) | ||
| SafeMessageConfirmationFactory(signature_type=SafeSignatureType.EOA.value) | ||
| SafeMessageConfirmationFactory( | ||
| signature_type=SafeSignatureType.APPROVED_HASH.value | ||
| ) | ||
| SafeMessageConfirmationFactory( | ||
| signature_type=SafeSignatureType.CONTRACT_SIGNATURE.value | ||
| ) | ||
|
|
||
| SafeMessageOld = old_state.apps.get_model("safe_messages", "SafeMessage") | ||
| SafeMessageConfirmationOld = old_state.apps.get_model( | ||
| "safe_messages", "SafeMessageConfirmation" | ||
| ) | ||
| self.assertEqual(SafeMessageOld.objects.count(), 4) | ||
| self.assertEqual(SafeMessageConfirmationOld.objects.count(), 4) | ||
| self.assertEqual( | ||
| SafeMessageConfirmationOld.objects.filter( | ||
| signature_type=SafeSignatureType.CONTRACT_SIGNATURE.value | ||
| ).count(), | ||
| 2, | ||
| ) | ||
|
|
||
| new_state = self.migrator.apply_tested_migration( | ||
| ("safe_messages", "0006_remove_contract_signatures"), | ||
| ) | ||
| SafeMessageNew = old_state.apps.get_model("safe_messages", "SafeMessage") | ||
| SafeMessageConfirmationNew = new_state.apps.get_model( | ||
| "safe_messages", "SafeMessageConfirmation" | ||
| ) | ||
| self.assertEqual(SafeMessageNew.objects.count(), 4) | ||
| self.assertEqual(SafeMessageConfirmationNew.objects.count(), 2) | ||
| self.assertEqual( | ||
| SafeMessageConfirmationNew.objects.filter( | ||
| signature_type=SafeSignatureType.CONTRACT_SIGNATURE.value | ||
| ).count(), | ||
| 0, | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,22 +1,36 @@ | ||
| from typing import Any | ||
|
|
||
| from eth_account.messages import defunct_hash_message | ||
| from eth_account.messages import encode_defunct | ||
| from eth_typing import ChecksumAddress, Hash32 | ||
| from safe_eth.eth import get_auto_ethereum_client | ||
| from safe_eth.eth.eip712 import eip712_encode_hash | ||
| from safe_eth.eth.eip712 import eip712_encode | ||
| from safe_eth.safe import Safe | ||
|
|
||
|
|
||
| def get_hash_for_message(message: str | dict[str, Any]) -> Hash32: | ||
| def encode_eip191_message(message: str) -> bytes: | ||
| signable_message = encode_defunct(text=message) | ||
| return ( | ||
| defunct_hash_message(text=message) | ||
| b"\x19" | ||
| + signable_message.version | ||
| + signable_message.header | ||
| + signable_message.body | ||
| ) | ||
|
|
||
|
|
||
| def encode_eip712_message(message: dict[str, Any]) -> bytes: | ||
| return b"".join(eip712_encode(message)) | ||
|
|
||
|
|
||
| def get_message_encoded(message: str | dict[str, Any]) -> bytes: | ||
Uxio0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return ( | ||
| encode_eip191_message(message) | ||
| if isinstance(message, str) | ||
| else eip712_encode_hash(message) | ||
| else encode_eip712_message(message) | ||
| ) | ||
|
|
||
|
|
||
| def get_safe_message_hash_for_message( | ||
| safe_address: ChecksumAddress, message_hash: Hash32 | ||
| ) -> Hash32: | ||
| def get_safe_message_hash_and_preimage_for_message( | ||
| safe_address: ChecksumAddress, message: bytes | ||
| ) -> tuple[Hash32, bytes]: | ||
| safe = Safe(safe_address, get_auto_ethereum_client()) | ||
| return safe.get_message_hash(message_hash) | ||
| return safe.get_message_hash_and_preimage(message) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.