Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/utils/AddressUtils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import { isAddress } from "viem";
Copy link
Member

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?

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)}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down