-
Notifications
You must be signed in to change notification settings - Fork 278
feat(protocol-kit): Add mastercopy bytecode matching for L2 deployments #1271
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
feat(protocol-kit): Add mastercopy bytecode matching for L2 deployments #1271
Conversation
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds mastercopy bytecode matching support to the protocol-kit, enabling it to work with Safe contracts whose mastercopy addresses are not listed in the official safe-deployments
package. The feature provides a fallback mechanism that detects Safe versions by comparing bytecode hashes against known official versions.
Key Changes
- Adds a new mastercopy detection utility that reads mastercopy addresses from Safe proxy storage and matches bytecode against known Safe versions
- Enhances the contract manager to use mastercopy matching as a fallback when
VERSION()
calls fail - Provides comprehensive documentation and test coverage for the new functionality
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
packages/protocol-kit/src/utils/mastercopyMatcher.ts |
Core mastercopy detection and bytecode matching utilities |
packages/protocol-kit/src/managers/contractManager.ts |
Integration of mastercopy matching fallback in contract initialization |
packages/protocol-kit/tests/unit/mastercopy-matcher.test.ts |
Unit tests for mastercopy matching functionality |
docs/CUSTOM_MASTERCOPY_GUIDE.md |
User-facing documentation and usage examples |
MASTERCOPY_MATCHING.md |
Technical implementation documentation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
CLA Assistant Lite bot: I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request |
Pull Request Test Coverage Report for Build 18156806908Details
💛 - Coveralls |
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
@copilot please limit this feature to 1.1.1 L2 and 1.3.0 L2 mastercopies. |
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
docs/CUSTOM_MASTERCOPY_GUIDE.md
Outdated
2. **Fallback mechanism**: If the VERSION() call fails: | ||
- Read the mastercopy address from storage slot 0 of the Safe proxy | ||
- Fetch the bytecode of the mastercopy contract | ||
- Compare the bytecode hash against supported Safe L2 versions (1.1.1 L2 and 1.3.0 L2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIU, the feature should be limited to v1.3.0 and v1.4.1 (and not v1.1.1).
@copilot please limit this feature to 1.3.0 L2 and 1.4.1 L2 (instead of 1.1.1 L2). |
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
hey @katspaugh updated the target branch to |
// if contract is not deployed we use the default version | ||
safeVersion = DEFAULT_SAFE_VERSION | ||
// If contract is not deployed or VERSION() call fails, try mastercopy matching (L2 only) | ||
const mastercopyMatch = await detectSafeVersionFromMastercopy( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getSafeContractVersion only checks the contract version. The catch clause will not be triggered because the unofficial contract will have the VERSION anyway right?
@katspaugh the protocol-kit has a mechanism to work with custom contracts https://docs.safe.global/reference-sdk-protocol-kit/initialization/init#contractnetworks-optional In fact, the hardhat tests in the SDK are using this configuration option https://github.com/safe-global/safe-core-sdk/blob/201c50ef97ff5c48661cbe71a013ad7dc2866ada/packages/testing-kit/src/utils/setupTests.ts#L1-L81 This should be enough to workaround the issue regarding the SDK right? |
@yagopv you're right! This is a good solution and this PR is not needed then. I'll close it. Cheers! |
Resolves https://linear.app/safe-global/issue/REVE-391/opbnb-safes-are-not-usable-with-wallet-due-to-the-unofficial
Overview
This PR enables the protocol-kit to work with Safe contracts whose L2 mastercopy (singleton) is not listed in the official
safe-deployments
package, as long as the mastercopy bytecode exactly matches a known Safe L2 version. This allows using the SDK on custom networks, testnets, or with independently deployed Safe L2 contracts.Supported versions: Only 1.3.0 L2 and 1.4.1 L2 mastercopies are supported for bytecode matching.
Problem
Previously, when initializing a Safe instance, if the mastercopy address wasn't found in the
safe-deployments
package for the current network, the SDK would fail to initialize properly. This prevented users from working with Safes deployed on custom networks or using custom-deployed (but code-identical) L2 mastercopies.Solution
The implementation adds a graceful fallback mechanism that attempts to detect the Safe L2 version by comparing bytecode:
VERSION()
on the Safe contract (existing behavior)VERSION()
fails:codeHash
values from safe-deployments for efficient matchingKey Features
Changes
New Files
packages/protocol-kit/src/utils/mastercopyMatcher.ts
- Core utility functions for L2 mastercopy detection and bytecode matchingpackages/protocol-kit/tests/unit/mastercopy-matcher.test.ts
- Comprehensive unit testsdocs/CUSTOM_MASTERCOPY_GUIDE.md
- User guide with examplesModified Files
packages/protocol-kit/src/managers/contractManager.ts
- Enhanced version detection logic with L2 mastercopy matching fallbackUsage Example
No code changes required! The feature works transparently:
Testing
Security Considerations
Documentation
Complete documentation has been added:
docs/CUSTOM_MASTERCOPY_GUIDE.md
Closes #[issue-number]
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.