|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Script} from "forge-std/Script.sol"; |
| 5 | +import {stdJson} from "forge-std/StdJson.sol"; |
| 6 | +import {console} from "forge-std/console.sol"; |
| 7 | +import {Merkle} from "murky/src/Merkle.sol"; |
| 8 | +import {ScriptHelper} from "murky/script/common/ScriptHelper.sol"; |
| 9 | + |
| 10 | +contract MakeMerkle is Script, ScriptHelper { |
| 11 | + using stdJson for string; |
| 12 | + |
| 13 | + Merkle private m = new Merkle(); |
| 14 | + |
| 15 | + string private inputPath = "/script/target/input.json"; |
| 16 | + string private outputPath = "/script/target/output.json"; |
| 17 | + |
| 18 | + string private elements = vm.readFile(string.concat(vm.projectRoot(), inputPath)); |
| 19 | + string[] private types = elements.readStringArray(".types"); |
| 20 | + uint256 private count = elements.readUint(".count"); |
| 21 | + bytes32[] private leafs = new bytes32[](count); |
| 22 | + |
| 23 | + string[] private inputs = new string[](count); |
| 24 | + string[] private outputs = new string[](count); |
| 25 | + |
| 26 | + string private output; |
| 27 | + |
| 28 | + function getValuesByIndex(uint256 i, uint256 j) internal pure returns (string memory) { |
| 29 | + return string.concat(".values.", vm.toString(i), ".", vm.toString(j)); |
| 30 | + } |
| 31 | + |
| 32 | + function generateJsonEntries(string memory _inputs, string memory _proof, string memory _root, string memory _leaf) |
| 33 | + internal |
| 34 | + pure |
| 35 | + returns (string memory) |
| 36 | + { |
| 37 | + string memory result = string.concat( |
| 38 | + "{", |
| 39 | + "\"inputs\":", |
| 40 | + _inputs, |
| 41 | + ",", |
| 42 | + "\"proof\":", |
| 43 | + _proof, |
| 44 | + ",", |
| 45 | + "\"root\":\"", |
| 46 | + _root, |
| 47 | + "\",", |
| 48 | + "\"leaf\":\"", |
| 49 | + _leaf, |
| 50 | + "\"", |
| 51 | + "}" |
| 52 | + ); |
| 53 | + |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + function run() public { |
| 58 | + console.log("Generating Merkle Proof for %s", inputPath); |
| 59 | + |
| 60 | + for (uint256 i = 0; i < count; ++i) { |
| 61 | + string[] memory input = new string[](types.length); |
| 62 | + bytes32[] memory data = new bytes32[](types.length); |
| 63 | + |
| 64 | + for (uint256 j = 0; j < types.length; ++j) { |
| 65 | + if (compareStrings(types[j], "address")) { |
| 66 | + address value = elements.readAddress(getValuesByIndex(i, j)); |
| 67 | + data[j] = bytes32(uint256(uint160(value))); |
| 68 | + input[j] = vm.toString(value); |
| 69 | + } else if (compareStrings(types[j], "uint")) { |
| 70 | + uint256 value = vm.parseUint(elements.readString(getValuesByIndex(i, j))); |
| 71 | + data[j] = bytes32(value); |
| 72 | + input[j] = vm.toString(value); |
| 73 | + } |
| 74 | + } |
| 75 | + leafs[i] = keccak256(bytes.concat(keccak256(ltrim64(abi.encode(data))))); |
| 76 | + inputs[i] = stringArrayToString(input); |
| 77 | + } |
| 78 | + |
| 79 | + for (uint256 i = 0; i < count; ++i) { |
| 80 | + string memory proof = bytes32ArrayToString(m.getProof(leafs, i)); |
| 81 | + string memory root = vm.toString(m.getRoot(leafs)); |
| 82 | + string memory leaf = vm.toString(leafs[i]); |
| 83 | + string memory input = inputs[i]; |
| 84 | + outputs[i] = generateJsonEntries(input, proof, root, leaf); |
| 85 | + } |
| 86 | + output = stringArrayToArrayString(outputs); |
| 87 | + vm.writeFile(string.concat(vm.projectRoot(), outputPath), output); |
| 88 | + |
| 89 | + console.log("DONE: The output is found at %s", outputPath); |
| 90 | + } |
| 91 | +} |
0 commit comments