|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-only |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import { ISP1LightClient } from "./interfaces/ISP1LightClient.sol"; |
| 5 | +import { SSZ } from "../Telepathy/libraries/SimpleSerialize.sol"; |
| 6 | +import { Merkle } from "../Spectre/lib/Merkle.sol"; |
| 7 | +import { Receipt } from "../Electron/lib/Receipt.sol"; |
| 8 | +import { BlockHashAdapter } from "../BlockHashAdapter.sol"; |
| 9 | + |
| 10 | +contract SP1HeliosAdapter is BlockHashAdapter { |
| 11 | + string public constant PROVIDER = "sp1-helios"; |
| 12 | + bytes32 internal constant MESSAGE_DISPATCHED_EVENT_SIG = |
| 13 | + 0x218247aabc759e65b5bb92ccc074f9d62cd187259f2a0984c3c9cf91f67ff7cf; // keccak256("MessageDispatched(uint256,(uint256,uint256,uint256,address,address,bytes,address[],address[]))"); |
| 14 | + |
| 15 | + address public immutable SP1_HELIOS_ADDRESS; |
| 16 | + address public immutable SOURCE_YAHO; |
| 17 | + uint256 public immutable SOURCE_CHAIN_ID; |
| 18 | + |
| 19 | + error HeaderNotAvailable(); |
| 20 | + error InvalidBlockNumberProof(); |
| 21 | + error InvalidBlockHashProof(); |
| 22 | + error InvalidReceiptsRoot(); |
| 23 | + error ErrorParseReceipt(); |
| 24 | + error InvalidEventSignature(); |
| 25 | + error InvalidEventSource(); |
| 26 | + |
| 27 | + constructor(address sp1HeliosAddress, uint256 sourceChainId, address sourceYaho) { |
| 28 | + SP1_HELIOS_ADDRESS = sp1HeliosAddress; |
| 29 | + SOURCE_CHAIN_ID = sourceChainId; |
| 30 | + SOURCE_YAHO = sourceYaho; |
| 31 | + } |
| 32 | + |
| 33 | + function storeBlockHeader( |
| 34 | + uint256 slot, |
| 35 | + uint256 blockNumber, |
| 36 | + bytes32[] calldata blockNumberProof, |
| 37 | + bytes32 blockHash, |
| 38 | + bytes32[] calldata blockHashProof |
| 39 | + ) external { |
| 40 | + bytes32 header = _getHeader(slot); |
| 41 | + |
| 42 | + if (!SSZ.verifyBlockNumber(blockNumber, blockNumberProof, header)) { |
| 43 | + revert InvalidBlockNumberProof(); |
| 44 | + } |
| 45 | + |
| 46 | + if (!SSZ.verifyBlockHash(blockHash, blockHashProof, header)) { |
| 47 | + revert InvalidBlockHashProof(); |
| 48 | + } |
| 49 | + |
| 50 | + _storeHash(SOURCE_CHAIN_ID, blockNumber, blockHash); |
| 51 | + } |
| 52 | + |
| 53 | + function verifyAndStoreDispatchedMessage( |
| 54 | + uint64 headerSlot, |
| 55 | + uint64 txSlot, |
| 56 | + bytes32[] memory receiptsRootProof, |
| 57 | + bytes32 receiptsRoot, |
| 58 | + bytes[] memory receiptProof, |
| 59 | + bytes memory txIndexRLPEncoded, |
| 60 | + uint256 logIndex |
| 61 | + ) external { |
| 62 | + bytes32 header = _getHeader(headerSlot); |
| 63 | + |
| 64 | + bool isValidReceiptsRoot = Merkle.verifyReceiptsRoot( |
| 65 | + receiptsRootProof, |
| 66 | + receiptsRoot, |
| 67 | + headerSlot, |
| 68 | + txSlot, |
| 69 | + header |
| 70 | + ); |
| 71 | + if (!isValidReceiptsRoot) revert InvalidReceiptsRoot(); |
| 72 | + |
| 73 | + Receipt.ParsedReceipt memory parsedReceipt = Receipt.parseReceipt( |
| 74 | + receiptsRoot, |
| 75 | + receiptProof, |
| 76 | + txIndexRLPEncoded, |
| 77 | + logIndex |
| 78 | + ); |
| 79 | + if (!parsedReceipt.isValid) revert ErrorParseReceipt(); |
| 80 | + if (bytes32(parsedReceipt.topics[0]) != MESSAGE_DISPATCHED_EVENT_SIG) revert InvalidEventSignature(); |
| 81 | + if (parsedReceipt.eventSource != SOURCE_YAHO) revert InvalidEventSource(); |
| 82 | + |
| 83 | + uint256 messageId = uint256(parsedReceipt.topics[1]); |
| 84 | + bytes32 messageHash = keccak256(parsedReceipt.data); |
| 85 | + |
| 86 | + _storeHash(SOURCE_CHAIN_ID, messageId, messageHash); |
| 87 | + } |
| 88 | + |
| 89 | + function _getHeader(uint256 slot) internal view returns (bytes32) { |
| 90 | + bytes32 header = ISP1LightClient(SP1_HELIOS_ADDRESS).headers(slot); |
| 91 | + if (header == bytes32(0)) { |
| 92 | + revert HeaderNotAvailable(); |
| 93 | + } |
| 94 | + return header; |
| 95 | + } |
| 96 | +} |
0 commit comments