Skip to content
Open
Changes from 1 commit
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
34 changes: 29 additions & 5 deletions test/utils/RLP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { product } = require('../helpers/iterate');
const { generators } = require('../helpers/random');

async function fixture() {
Expand Down Expand Up @@ -33,11 +34,15 @@ describe('RLP', function () {
});

it('encode/decode addresses', async function () {
const addr = generators.address();
const expected = ethers.encodeRlp(addr);

await expect(this.mock.$encode_address(addr)).to.eventually.equal(expected);
await expect(this.mock.$decodeAddress(expected)).to.eventually.equal(addr);
for (const addr of [
ethers.ZeroAddress, // zero address
'0x0000F90827F1C53a10cb7A02335B175320002935', // address with heading zeros
generators.address(), // random address
]) {
const expected = ethers.encodeRlp(addr);
await expect(this.mock.$encode_address(addr)).to.eventually.equal(expected);
await expect(this.mock.$decodeAddress(expected)).to.eventually.equal(addr);
}
});

it('encode/decode uint256', async function () {
Expand Down Expand Up @@ -146,4 +151,23 @@ describe('RLP', function () {
await expect(this.mock.$decodeBytes(input)).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
});
});

it('RLP encoder predict create addresses', async function () {
for (const [from, nonce] of product(
[
ethers.ZeroAddress, // zero address
'0x0000F90827F1C53a10cb7A02335B175320002935', // address with heading zeros
generators.address(), // random address
],
[0n, 1n, 42n, 65535n, ethers.MaxUint256],
)) {
await expect(
this.mock
.$encode_list([this.mock.$encode_address(from), this.mock.$encode_uint256(nonce)])
.then(encoded => ethers.keccak256(encoded)) // hash the encoded content
.then(hash => ethers.dataSlice(hash, 12)) // take the last 20 bytes
.then(ethers.getAddress), // format as (checksummed) address
).to.eventually.equal(ethers.getCreateAddress({ from, nonce }));
}
});
});