Skip to content

Commit 4a4a606

Browse files
Test encoding of addresses with leading zeros (#6107)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 45b5fa7 commit 4a4a606

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

test/utils/RLP.t.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,13 @@ contract RLPTest is Test {
132132

133133
assertEq(RLP.encoder().push(b).push(a).push(u).encode(), RLP.encode(list));
134134
}
135+
136+
function testComputeCreateAddress(address deployer, uint256 nonce) external pure {
137+
nonce = bound(nonce, 0, type(uint64).max);
138+
139+
assertEq(
140+
address(uint160(uint256(keccak256(RLP.encoder().push(deployer).push(nonce).encode())))),
141+
vm.computeCreateAddress(deployer, nonce)
142+
);
143+
}
135144
}

test/utils/RLP.test.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { ethers } = require('hardhat');
22
const { expect } = require('chai');
33
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
44

5+
const { MAX_UINT64 } = require('../helpers/constants');
6+
const { product } = require('../helpers/iterate');
57
const { generators } = require('../helpers/random');
68

79
async function fixture() {
@@ -33,11 +35,15 @@ describe('RLP', function () {
3335
});
3436

3537
it('encode/decode addresses', async function () {
36-
const addr = generators.address();
37-
const expected = ethers.encodeRlp(addr);
38-
39-
await expect(this.mock.$encode_address(addr)).to.eventually.equal(expected);
40-
await expect(this.mock.$decodeAddress(expected)).to.eventually.equal(addr);
38+
for (const addr of [
39+
ethers.ZeroAddress, // zero address
40+
'0x0000F90827F1C53a10cb7A02335B175320002935', // address with leading zeros
41+
generators.address(), // random address
42+
]) {
43+
const expected = ethers.encodeRlp(addr);
44+
await expect(this.mock.$encode_address(addr)).to.eventually.equal(expected);
45+
await expect(this.mock.$decodeAddress(expected)).to.eventually.equal(addr);
46+
}
4147
});
4248

4349
it('encode/decode uint256', async function () {
@@ -146,4 +152,21 @@ describe('RLP', function () {
146152
await expect(this.mock.$decodeBytes(input)).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
147153
});
148154
});
155+
156+
it('RLP encoder predict create addresses', async function () {
157+
for (const [from, nonce] of product(
158+
[
159+
ethers.ZeroAddress, // zero address
160+
'0x0000F90827F1C53a10cb7A02335B175320002935', // address with heading zeros
161+
generators.address(), // random address
162+
],
163+
[0n, 1n, 42n, 65535n, MAX_UINT64],
164+
)) {
165+
await expect(
166+
this.mock
167+
.$encode_list([this.mock.$encode_address(from), this.mock.$encode_uint256(nonce)])
168+
.then(encoded => ethers.getAddress(ethers.dataSlice(ethers.keccak256(encoded), 12))), // hash the encoded content, take the last 20 bytes and format as (checksummed) address
169+
).to.eventually.equal(ethers.getCreateAddress({ from, nonce }));
170+
}
171+
});
149172
});

0 commit comments

Comments
 (0)