From 2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef Mon Sep 17 00:00:00 2001 From: karmacoma <85039585+karmacoma-eth@users.noreply.github.com> Date: Wed, 3 Aug 2022 01:31:06 -0700 Subject: [PATCH] feat: makeAddr and makeAddrAndKey (#146) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: makeAddr and makeAddrAndKey Two small quality-of-life functions to easily create labeled addresses. Inspired by https://twitter.com/eth_call/status/1549792921976803328 * ♻️ Remove bytes conversion Co-authored-by: ZeroEkkusu <94782988+ZeroEkkusu@users.noreply.github.com> --- src/Test.sol | 12 ++++++++++++ src/test/StdCheats.t.sol | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Test.sol b/src/Test.sol index da549594a..3d7e9f9bc 100644 --- a/src/Test.sol +++ b/src/Test.sol @@ -86,6 +86,18 @@ abstract contract Test is DSTest, Script { vm.startPrank(who); } + // creates a labeled address and the corresponding private key + function makeAddrAndKey(string memory name) internal returns(address addr, uint256 privateKey) { + privateKey = uint256(keccak256(abi.encodePacked(name))); + addr = vm.addr(privateKey); + vm.label(addr, name); + } + + // creates a labeled address + function makeAddr(string memory name) internal returns(address addr) { + (addr,) = makeAddrAndKey(name); + } + // DEPRECATED: Use `deal` instead function tip(address token, address to, uint256 give) internal { emit log_named_string("WARNING", "Test tip(address,address,uint256): The `tip` stdcheat has been deprecated. Use `deal` instead."); diff --git a/src/test/StdCheats.t.sol b/src/test/StdCheats.t.sol index 2b49e9e79..6d477e987 100644 --- a/src/test/StdCheats.t.sol +++ b/src/test/StdCheats.t.sol @@ -63,6 +63,19 @@ contract StdCheatsTest is Test { vm.stopPrank(); } + function testMakeAddrEquivalence() public { + (address addr, ) = makeAddrAndKey("1337"); + assertEq(makeAddr("1337"), addr); + } + + function testMakeAddrSigning() public { + (address addr, uint256 key) = makeAddrAndKey("1337"); + bytes32 hash = keccak256("some_message"); + + (uint8 v, bytes32 r, bytes32 s) = vm.sign(key, hash); + assertEq(ecrecover(hash, v, r, s), addr); + } + function testDeal() public { deal(address(this), 1 ether); assertEq(address(this).balance, 1 ether); @@ -157,7 +170,7 @@ contract StdCheatsTest is Test { function deployCodeHelper(string memory what) external { deployCode(what); } - + function testDeployCodeFail() public { vm.expectRevert(bytes("Test deployCode(string): Deployment failed.")); this.deployCodeHelper("StdCheats.t.sol:RevertingContract");