Skip to content

Commit

Permalink
feat(cheatcodes): add ens namehash cheatcode (foundry-rs#7882)
Browse files Browse the repository at this point in the history
* feat(cheatcodes): add ens namehash cheatcode

* fix test

* fix test

* rename the cheatcode from namehash -> ensNamehash

* update cheatcodes.json
  • Loading branch information
meetmangukiya authored May 7, 2024
1 parent a87faf6 commit 7ce6c9b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,10 @@ interface Vm {
/// Encodes a `string` value to a base64url string.
#[cheatcode(group = Utilities)]
function toBase64URL(string calldata data) external pure returns (string memory);

/// Returns ENS namehash for provided string.
#[cheatcode(group = Utilities)]
function ensNamehash(string calldata name) external pure returns (bytes32);
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use alloy_signer_wallet::{
LocalWallet, MnemonicBuilder,
};
use alloy_sol_types::SolValue;
use foundry_common::ens::namehash;
use foundry_evm_core::constants::DEFAULT_CREATE2_DEPLOYER;
use k256::{
ecdsa::SigningKey,
Expand Down Expand Up @@ -137,6 +138,13 @@ impl Cheatcode for computeCreate2Address_1Call {
}
}

impl Cheatcode for ensNamehashCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { name } = self;
Ok(namehash(name).abi_encode())
}
}

/// Using a given private key, return its public ETH address, its public key affine x and y
/// coordinates, and its private key (see the 'Wallet' struct)
///
Expand Down
1 change: 1 addition & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions testdata/default/cheats/EnsNamehash.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

import "ds-test/test.sol";
import "cheats/Vm.sol";

contract EnsNamehashTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testEnsNamehash() public {
assertEq(vm.ensNamehash(""), 0x0000000000000000000000000000000000000000000000000000000000000000);
assertEq(vm.ensNamehash("eth"), 0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae);
assertEq(vm.ensNamehash("foo.eth"), 0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f);
}
}

0 comments on commit 7ce6c9b

Please sign in to comment.