Skip to content
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

fix: handle empty external address #70

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
48 changes: 16 additions & 32 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {getLcLoggerConsole} from "@lodestar/light-client/utils";

import Web3 from "web3";
import {toBuffer, keccak256} from "ethereumjs-util";
import {DefaultStateManager} from "@ethereumjs/statemanager";
import {numberToHex} from "web3-utils";

import {defaultAbiCoder} from "@ethersproject/abi";

import Footer from "./components/Footer";
Expand All @@ -32,8 +31,7 @@ import {
ERC20Contract,
} from "./Networks";
import {ParsedAccount, DisplayAccount} from "./AccountHelper";

const stateManager = new DefaultStateManager();
import {isNullStorage, isExternalAddress, verifyProof} from "./utils";

/**
* A checkpoint is a 32bits hex string starting with `0x`
Expand Down Expand Up @@ -351,9 +349,6 @@ export default function App(): JSX.Element {
);
}

const externalAddressStorageHash = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421";
const externalAddressCodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";

async function fetchAndVerifyAddressBalances({
web3,
executionPayload,
Expand All @@ -366,44 +361,33 @@ async function fetchAndVerifyAddressBalances({
erc20Contracts: Record<string, ERC20Contract>;
}): Promise<ParsedAccount> {
if (!web3) throw Error(`No valid connection to EL`);
const params: [string, string[], number] = [address, [], executionPayload.blockNumber];
const stateRoot = toHexString(executionPayload.stateRoot);
const proof = await web3.eth.getProof(...params);

const {balance, nonce} = proof;

// Verify the proof, web3 converts nonce and balance into number strings, however
// ethereumjs verify proof requires them in the original hex format
proof.nonce = numberToHex(proof.nonce);
proof.balance = numberToHex(proof.balance);

const proof = await web3.eth.getProof(address, [], executionPayload.blockNumber);
const proofStateRoot = toHexString(keccak256(toBuffer(proof.accountProof[0])));
let verified =
const accountVerified =
stateRoot === proofStateRoot &&
(proof.codeHash !== externalAddressCodeHash || proof.storageHash === externalAddressStorageHash) &&
(await stateManager.verifyProof(proof));
(!isExternalAddress(proof) || isNullStorage(proof)) &&
(await verifyProof(web3, {...proof, address}));

let tokens = [];
let tokensVerified = true;

for (const contractName of Object.keys(erc20Contracts)) {
const {contractAddress, balanceMappingIndex} = erc20Contracts[contractName];
const balanceSlot = web3.utils.keccak256(
defaultAbiCoder.encode(["address", "uint"], [address, balanceMappingIndex])
);
const contractProof = await web3.eth.getProof(contractAddress, [balanceSlot], executionPayload.blockNumber);
if (contractProof.codeHash === externalAddressCodeHash) {
if (isExternalAddress(contractProof)) {
throw Error(`No contract deployed at ${contractAddress} for ${contractName}`);
}
const contractProofStateRoot = toHexString(keccak256(toBuffer(contractProof.accountProof[0])));
// Verify the proof, web3 converts nonce and balance into number strings, however
// ethereumjs verify proof requires them in the original hex format
contractProof.nonce = numberToHex(contractProof.nonce);
contractProof.balance = numberToHex(contractProof.balance);

verified =
verified &&
tokensVerified =
tokensVerified &&
stateRoot === contractProofStateRoot &&
BigInt(contractProof.storageProof[0]?.key) === BigInt(balanceSlot) &&
(await stateManager.verifyProof(contractProof));
(await verifyProof(web3, {...contractProof, address: contractAddress}));
tokens.push({
name: contractName,
contractAddress,
Expand All @@ -412,10 +396,10 @@ async function fetchAndVerifyAddressBalances({
}

return {
balance: web3.utils.fromWei(balance, "ether"),
nonce,
verified,
balance: web3.utils.fromWei(proof.balance, "ether"),
nonce: proof.nonce,
verified: accountVerified && tokensVerified,
tokens,
type: proof.codeHash === externalAddressCodeHash ? "external" : "contract",
type: isExternalAddress(proof) ? "external" : "contract",
};
}
54 changes: 54 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {DefaultStateManager, Proof} from "@ethereumjs/statemanager";

import {KECCAK256_NULL_S, KECCAK256_RLP_S} from "ethereumjs-util";
import Web3 from "web3";

const stateManager = new DefaultStateManager();
const NULL_HASH = `0x${KECCAK256_NULL_S}`;
const NULL_RLP_HASH = `0x${KECCAK256_RLP_S}`;

/**
* If `codeHash` is `null`, then the address is an external address.
*/
export function isExternalAddress(proof: Proof): boolean {
return isNullCodeHash(proof);
}

export function isNullCodeHash({codeHash}: Proof): boolean {
return codeHash === NULL_HASH;
}

export function isNullStorage({storageHash}: Proof): boolean {
return storageHash === NULL_RLP_HASH;
}

/**
* @param hash a 32bits hash
* @returns true is provided `hash` is the default hash
*/
function isDefaultHash(hash: string): boolean {
return hash === "0x0000000000000000000000000000000000000000000000000000000000000000";
}

/**
* Verify a `Proof` using a `DefaultStateManager`
*/
export async function verifyProof(web3: Web3, proof: Proof): Promise<boolean> {
// Verify the proof, web3 converts nonce and balance into number strings, however
// ethereumjs verify proof requires them in the original hex format
proof.nonce = Web3.utils.numberToHex(proof.nonce);
proof.balance = web3.utils.numberToHex(proof.balance);
// Handle null hashes, as returned by Web3.
// ethereumjs/statemanager expects hashes of proper default value
if (isDefaultHash(proof.storageHash)) {
proof.storageHash = NULL_RLP_HASH;
}
if (isDefaultHash(proof.codeHash)) {
proof.codeHash = NULL_HASH;
}
try {
return await stateManager.verifyProof(proof);
} catch (e) {
return false;
}
}
Loading