@@ -2,6 +2,8 @@ const { ethers } = require('hardhat');
22const { expect } = require ( 'chai' ) ;
33const { loadFixture } = require ( '@nomicfoundation/hardhat-network-helpers' ) ;
44
5+ const { MAX_UINT64 } = require ( '../helpers/constants' ) ;
6+ const { product } = require ( '../helpers/iterate' ) ;
57const { generators } = require ( '../helpers/random' ) ;
68
79async 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