-
Notifications
You must be signed in to change notification settings - Fork 18
chore: Make contract check EIP-7702 aware #1074
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
Open
pxrl
wants to merge
2
commits into
master
Choose a base branch
from
pxrl/7702Util
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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,23 +1,47 @@ | ||
import { isAddress } from "viem"; | ||
import { providers, utils } from "ethers"; | ||
import bs58 from "bs58"; | ||
import { Address as V2Address } from "@solana/kit"; | ||
import { BigNumber, chainIsEvm } from "./"; | ||
|
||
/** | ||
* Verify whether an address' bytecode resembles an EIP-7702 delegation. | ||
* @param code Bytecode for a given address. | ||
* @returns True if the bytecode resembles an EIP-7702 delegation, otherwise false. | ||
*/ | ||
export function is7702Delegate(code: string): boolean { | ||
// Sample 7702 delegation bytecode: 0xef010063c0c19a282a1b52b07dd5a65b58948a07dae32b | ||
return code.length === 48 && code.startsWith("0xef0100") && isAddress(`0x${code.slice(8)}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nb. an EOA revokes delegation by setting the delegated address to address(0x0). From what I've understood, this removes the code at the EOA and resets the code hash to keccak256(""). I haven't verified this with any test yet, though. |
||
} | ||
|
||
/** | ||
* Checks if a contract is deployed at the given address | ||
* @param address The ETH address to check | ||
* @param provider A valid Ethers.js provider | ||
* @param ignore7702 A boolean to indicate whether EIP-7702 delegations should be considered as contract code. | ||
* @returns A boolean indicating if a contract is deployed at the given address or not (true = contract, false = no contract) | ||
*/ | ||
export async function isContractDeployedToAddress(address: string, provider: providers.Provider): Promise<boolean> { | ||
export async function isContractDeployedToAddress( | ||
address: string, | ||
provider: providers.Provider, | ||
ignore7702 = false | ||
): Promise<boolean> { | ||
// A base case for if the address is null or malformed | ||
if (!address || !utils.isAddress(address)) { | ||
if (!address || !isAddress(address)) { | ||
return false; | ||
} | ||
// Retrieve the code at the address | ||
|
||
const code = await provider.getCode(address); | ||
// If the code is not empty, then there is a contract at this address | ||
return code !== "0x"; | ||
if (code === "0x") { | ||
return false; | ||
} | ||
|
||
// Ignore EIP-7702 delegations if ignore7702 was set. | ||
if (ignore7702) { | ||
return !is7702Delegate(code); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export function compareAddresses(addressA: string, addressB: string): 1 | -1 | 0 { | ||
|
@@ -56,7 +80,7 @@ export function toAddress(hexString: string): string { | |
} | ||
|
||
export function isValidEvmAddress(address: string): boolean { | ||
if (utils.isAddress(address)) { | ||
if (isAddress(address)) { | ||
return true; | ||
} | ||
// We may throw an error here if hexZeroPadFails. This will happen if the address to pad is greater than 20 bytes long, indicating | ||
|
@@ -65,7 +89,7 @@ export function isValidEvmAddress(address: string): boolean { | |
// For both cases, this indicates that the address cannot be casted as a bytes20 EVM address, so we should return false. | ||
try { | ||
const evmAddress = utils.hexZeroPad(utils.hexStripZeros(address), 20); | ||
return utils.isAddress(utils.getAddress(evmAddress)); | ||
return isAddress(utils.getAddress(evmAddress)); | ||
} catch (_e) { | ||
return false; | ||
} | ||
|
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.
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.
Why not reuse
utils.isAddress
?