|
| 1 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { encodeAbiParameters, encodeFunctionData, Hex, hexToBytes, keccak256, namehash, numberToHex, stringToBytes, toHex } from 'viem'; |
| 4 | +import L2ResolverAbi from '../out/L2Resolver.sol/L2Resolver.json'; |
| 5 | + |
| 6 | +type ChainItem = { |
| 7 | + chainId: number; |
| 8 | + subDomain: string; |
| 9 | + shortName: string; |
| 10 | +}; |
| 11 | + |
| 12 | +type BatchOutput = { |
| 13 | + chainIds: number[]; |
| 14 | + forwardCalldata: Hex; |
| 15 | + reverseCalldata: Hex; |
| 16 | +}; |
| 17 | + |
| 18 | +const SOURCE_URL: string = "https://chainid.network/chains_mini.json"; |
| 19 | +const BATCH_SIZE: number = 10; |
| 20 | +const L2_SUFFIX: string = '.l2.eth'; |
| 21 | +const CHAIN_IDENTIFIER_EIP7930_KEY: string = 'chain.id.eip7930'; |
| 22 | +const REVERSE_LOOKUP_NODE: Hex = keccak256(stringToBytes('reverse.chain.id.eip7930')) as Hex; |
| 23 | + |
| 24 | +/** |
| 25 | + * Processes a batch of chain items and returns the chainIds, forwardCalldata, and reverseCalldata for setRecords. |
| 26 | + * @param batch - The batch of chain items to process. |
| 27 | + * @returns The chainIds, forwardCalldata, and reverseCalldata. |
| 28 | + */ |
| 29 | +const processBatch = async ( |
| 30 | + batch: ChainItem[] |
| 31 | +): Promise<[number[], Hex, Hex]> => { |
| 32 | + const forwardNodes: Hex[] = []; |
| 33 | + const forwardKeys: string[] = []; |
| 34 | + const forwardValues: Hex[] = []; |
| 35 | + |
| 36 | + const reverseNodes: Hex[] = []; |
| 37 | + const reverseKeys: string[] = []; |
| 38 | + const reverseValues: Hex[] = []; |
| 39 | + |
| 40 | + const chainIds: number[] = []; |
| 41 | + |
| 42 | + for (const item of batch) { |
| 43 | + const subDomain = item.subDomain || item.shortName; |
| 44 | + if (subDomain === '' || subDomain === null) { |
| 45 | + throw new Error('Subdomain cannot be empty'); |
| 46 | + } |
| 47 | + const domain = `${subDomain}${L2_SUFFIX}`.toLowerCase(); |
| 48 | + |
| 49 | + chainIds.push(item.chainId); |
| 50 | + |
| 51 | + const nodeNamehash = namehash(domain); |
| 52 | + const chainIdBytes = encodeEIP7930ChainIdBytes(item.chainId); |
| 53 | + |
| 54 | + forwardNodes.push(nodeNamehash); |
| 55 | + forwardKeys.push(CHAIN_IDENTIFIER_EIP7930_KEY); |
| 56 | + forwardValues.push(chainIdBytes); |
| 57 | + |
| 58 | + const identifierHash = keccak256(chainIdBytes); |
| 59 | + const reverseKey = `${CHAIN_IDENTIFIER_EIP7930_KEY}${identifierHash}`; |
| 60 | + const encodedDomain = encodeAbiParameters([{ type: 'string' }], [domain]); |
| 61 | + |
| 62 | + reverseNodes.push(REVERSE_LOOKUP_NODE); |
| 63 | + reverseKeys.push(reverseKey); |
| 64 | + reverseValues.push(encodedDomain); |
| 65 | + } |
| 66 | + |
| 67 | + const forwardCalldata = encodeFunctionData({ |
| 68 | + abi: L2ResolverAbi.abi, |
| 69 | + functionName: 'setRecords', |
| 70 | + args: [forwardNodes, forwardKeys, forwardValues], |
| 71 | + }); |
| 72 | + |
| 73 | + const reverseCalldata = encodeFunctionData({ |
| 74 | + abi: L2ResolverAbi.abi, |
| 75 | + functionName: 'setRecords', |
| 76 | + args: [reverseNodes, reverseKeys, reverseValues], |
| 77 | + }); |
| 78 | + |
| 79 | + return [chainIds, forwardCalldata, reverseCalldata]; |
| 80 | +}; |
| 81 | + |
| 82 | +const fetchChains = async (): Promise<ChainItem[]> => { |
| 83 | + const response = await fetch(SOURCE_URL); |
| 84 | + if (!response.ok) { |
| 85 | + throw new Error(`Failed to fetch chains: ${response.status} ${response.statusText}`); |
| 86 | + } |
| 87 | + return (await response.json()) as ChainItem[]; |
| 88 | +}; |
| 89 | + |
| 90 | +const filteredChainItem = (item: ChainItem): ChainItem => { |
| 91 | + return { |
| 92 | + chainId: item.chainId, |
| 93 | + subDomain: item.subDomain, |
| 94 | + shortName: item.shortName, |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +const chunkArray = <ChainItem>(items: ChainItem[], chunkSize: number): ChainItem[][] => { |
| 99 | + const chunks: ChainItem[][] = []; |
| 100 | + for (let i = 0; i < items.length; i += chunkSize) { |
| 101 | + chunks.push(items.slice(i, i + chunkSize)); |
| 102 | + } |
| 103 | + return chunks; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * Encodes the chainId to the bytes format required by EIP-7930. |
| 108 | + * @param chainId - The chainId to encode. |
| 109 | + * @returns The encoded bytes. |
| 110 | + */ |
| 111 | +const encodeEIP7930ChainIdBytes = (chainId: number): Hex => { |
| 112 | + const version = Uint8Array.of(0x00, 0x01); |
| 113 | + const chainType = Uint8Array.of(0x00, 0x00); |
| 114 | + const chainRef = hexToBytes(numberToHex(chainId)); |
| 115 | + const chainRefLength = Uint8Array.of(chainRef.length); |
| 116 | + const addressLength = Uint8Array.of(0x00); |
| 117 | + |
| 118 | + const bytes = new Uint8Array(version.length + chainType.length + chainRefLength.length + chainRef.length + addressLength.length); |
| 119 | + bytes.set(version, 0); |
| 120 | + bytes.set(chainType, 2); |
| 121 | + bytes.set(chainRefLength, 4); |
| 122 | + bytes.set(chainRef, 5); |
| 123 | + bytes.set(addressLength, 5 + chainRef.length); |
| 124 | + |
| 125 | + return toHex(bytes); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Main function to fetch chains, filter them, and process them in batches. |
| 130 | + * Writes the outputs to batches.json. |
| 131 | + */ |
| 132 | +const main = async (): Promise<void> => { |
| 133 | + const chains: ChainItem[] = await fetchChains(); |
| 134 | + const records: ChainItem[] = chains.map(filteredChainItem); |
| 135 | + const batches: ChainItem[][] = chunkArray(records, BATCH_SIZE); |
| 136 | + |
| 137 | + const outputs: BatchOutput[] = []; |
| 138 | + for (const batch of batches) { |
| 139 | + const [chainIds, forwardCalldata, reverseCalldata] = await processBatch(batch); |
| 140 | + outputs.push({ chainIds, forwardCalldata, reverseCalldata }); |
| 141 | + } |
| 142 | + |
| 143 | + await mkdir('out', { recursive: true }); |
| 144 | + await writeFile(join('out', 'batches.json'), JSON.stringify(outputs, null, 2), 'utf8'); |
| 145 | +}; |
| 146 | + |
| 147 | +main().catch((error) => { |
| 148 | + console.error(error); |
| 149 | + process.exitCode = 1; |
| 150 | +}); |
| 151 | + |
| 152 | +export { encodeEIP7930ChainIdBytes, processBatch }; |
| 153 | + |
0 commit comments