// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; import {MultisigChallenge} from "../src/MultisigChallenge.sol"; import {Secp256k1} from "../src/crypto/Secp256k1.sol"; /// @notice Minimal Foundry cheatcode surface used by the deploy script. interface ScriptVm { function readFile(string calldata path) external view returns (string memory); function parseJsonUint(string calldata json, string calldata key) external pure returns (uint256); function parseJsonUintArray(string calldata json, string calldata key) external pure returns (uint256[] memory); function startBroadcast() external; function stopBroadcast() external; } /// @title DeployChallenge /// @notice Deploys the challenge and publishes the heartbeat record read from /// `data/instance.flat.json`. The record is uploaded a few heartbeats at a time, /// so that no transaction is too large for a block. /// @dev Run with: forge script script/DeployChallenge.s.sol --rpc-url --broadcast --slow contract DeployChallenge { ScriptVm private constant vm = ScriptVm(address(uint160(uint256(keccak256("hevm cheat code"))))); uint256 private constant FIELDS = 9; // flattened fields per heartbeat uint256 private constant BATCH = 16; // heartbeats per transaction, about 3.3M gas function run() external returns (MultisigChallenge deployed) { string memory json = vm.readFile("data/instance.flat.json"); uint256 threshold = vm.parseJsonUint(json, ".threshold"); uint256[] memory flatKeys = vm.parseJsonUintArray(json, ".pubkeys"); uint256 n = flatKeys.length / 2; Secp256k1.Point[] memory signers = new Secp256k1.Point[](n); for (uint256 i = 0; i < n; ++i) { signers[i] = Secp256k1.Point({x: flatKeys[2 * i], y: flatKeys[2 * i + 1]}); } vm.startBroadcast(); deployed = new MultisigChallenge(signers, threshold); for (uint256 slot = 0; slot < n; ++slot) { uint256[] memory fields = vm.parseJsonUintArray(json, string.concat(".heartbeats.", _toString(slot))); uint256 total = fields.length / FIELDS; for (uint256 from = 0; from < total; from += BATCH) { uint256 to = from + BATCH < total ? from + BATCH : total; deployed.publishHeartbeats(slot, _heartbeats(fields, from, to)); } } deployed.sealRecord(); vm.stopBroadcast(); } /// @dev Heartbeats `from` (included) to `to` (excluded) of a signer's flattened record. function _heartbeats(uint256[] memory f, uint256 from, uint256 to) private pure returns (MultisigChallenge.Heartbeat[] memory beats) { beats = new MultisigChallenge.Heartbeat[](to - from); for (uint256 i = from; i < to; ++i) { uint256 o = i * FIELDS; beats[i - from] = MultisigChallenge.Heartbeat({ epoch: f[o], messageHash: bytes32(f[o + 1]), v: uint8(f[o + 2]), r: f[o + 3], s: f[o + 4], basePoint: Secp256k1.Point({x: f[o + 5], y: f[o + 6]}), livenessPoint: Secp256k1.Point({x: f[o + 7], y: f[o + 8]}) }); } } function _toString(uint256 value) private pure returns (string memory) { if (value == 0) return "0"; uint256 temp = value; uint256 digits; while (temp != 0) { digits++; temp /= 10; } bytes memory buffer = new bytes(digits); while (value != 0) { digits -= 1; buffer[digits] = bytes1(uint8(48 + value % 10)); value /= 10; } return string(buffer); } }