@hyperbridge/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,51 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
10
+
11
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
12
+
13
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
14
+
15
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
16
+
17
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
18
+
19
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
20
+
21
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
22
+
23
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
24
+
25
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
26
+
27
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
28
+
29
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
30
+
31
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
32
+
33
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
34
+
35
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
36
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
37
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
38
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
39
+
40
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
41
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
42
+
43
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
44
+
45
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
46
+
47
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
48
+
49
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
50
+
51
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @hyperbridge/core
2
+
3
+ ![Unit Tests](https://github.com/polytope-labs/hyperbridge-sdk/actions/workflows/test-core.yml/badge.svg)
4
+ [![NPM](https://img.shields.io/npm/v/@hyperbridge/core?label=%40hyperbridge%2Fcore)](https://www.npmjs.com/package/@hyperbridge/core)
5
+
6
+ [ISMP](https://docs.hyperbridge.network/protocol/ismp) protocol specification for EVM environments.
7
+
8
+ ### Interfaces
9
+
10
+ - [`IHost`](contracts/interfaces/IHost.sol) - The protocol host interface
11
+ - [`IDispatcher`](contracts/interfaces/IDispatcher.sol) - The protocol dispatcher interface
12
+ - [`IHandler`](contracts/interfaces/IHandler.sol) - The protocol message handler interface
13
+ - [`IConsensus`](contracts/interfaces/IConsensus.sol) - The consensus interface
14
+ - [`IApp`](contracts/interfaces/IApp.sol) - The protocol application interface
15
+
16
+ ### Libraries
17
+
18
+ - [`Message`](contracts/libraries/Message.sol) - The protocol message types and data structures
19
+ - [`StateMachine`](contracts/libraries/StateMachine.sol) - State machine identifier utilities
20
+
21
+ ### Apps
22
+
23
+ - [`HyperApp`](contracts/apps/HyperApp.sol) - Abstract base contract that implements `IApp` for building cross-chain applications with built-in fee estimation and host authorization
24
+ - [`HyperFungibleToken`](contracts/apps/HyperFungibleToken.sol) - Abstract ERC20 token implementation with gateway-restricted minting and burning capabilities for cross-chain bridging
25
+
26
+ ## License
27
+
28
+ This library is licensed under the Apache 2.0 License, Copyright (c) 2025 Polytope Labs.
@@ -0,0 +1,179 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ import {PostRequest, PostResponse, GetRequest} from "../libraries/Message.sol";
17
+ import {DispatchPost, DispatchPostResponse, DispatchGet, IDispatcher} from "../interfaces/IDispatcher.sol";
18
+ import {IApp, IncomingPostRequest, IncomingPostResponse, IncomingGetResponse} from "../interfaces/IApp.sol";
19
+
20
+ /**
21
+ * @dev Uniswap interface for estimating fees in the native token
22
+ */
23
+ interface IUniswapV2Router02 {
24
+ function WETH() external pure returns (address);
25
+
26
+ function getAmountsIn(uint256, address[] calldata) external pure returns (uint256[] memory);
27
+ }
28
+
29
+ /**
30
+ * @title HyperApp
31
+ * @notice Abstract base contract for building cross-chain applications on Hyperbridge
32
+ * @dev Provides a simplified interface for implementing `IApp` with built-in utilities for fee estimation,
33
+ * host authorization, and cross-chain message handling. Extend this contract to build your Hyperbridge application.
34
+ */
35
+ abstract contract HyperApp is IApp {
36
+ /**
37
+ * @dev Call was not expected
38
+ */
39
+ error UnexpectedCall();
40
+
41
+ /**
42
+ * @dev Account is unauthorized
43
+ */
44
+ error UnauthorizedCall();
45
+
46
+ /**
47
+ * @dev restricts caller to the local `Host`
48
+ */
49
+ modifier onlyHost() {
50
+ if (msg.sender != host()) revert UnauthorizedCall();
51
+ _;
52
+ }
53
+
54
+ /**
55
+ * @dev Should return the `Host` address for the current chain.
56
+ * The `Host` is an immutable contract that will never change.
57
+ */
58
+ function host() public view virtual returns (address);
59
+
60
+ /**
61
+ * @dev returns the quoted fee in the feeToken for dispatching a POST request
62
+ */
63
+ function quote(DispatchPost memory request) public view returns (uint256) {
64
+ uint256 len = 32 > request.body.length ? 32 : request.body.length;
65
+ return request.fee + (len * IDispatcher(host()).perByteFee(request.dest));
66
+ }
67
+
68
+ /**
69
+ * @dev returns the quoted fee in the native token for dispatching a POST request
70
+ */
71
+ function quoteNative(DispatchPost memory request) public view returns (uint256) {
72
+ uint256 fee = quote(request);
73
+ address _host = host();
74
+ address _uniswap = IDispatcher(_host).uniswapV2Router();
75
+ address[] memory path = new address[](2);
76
+ path[0] = IUniswapV2Router02(_uniswap).WETH();
77
+ path[1] = IDispatcher(_host).feeToken();
78
+ return IUniswapV2Router02(_uniswap).getAmountsIn(fee, path)[0];
79
+ }
80
+
81
+ /**
82
+ * @dev returns the quoted fee in the feeToken for dispatching a GET request
83
+ */
84
+ function quote(DispatchGet memory request) public view returns (uint256) {
85
+ address _host = host();
86
+ uint256 pbf = IDispatcher(_host).perByteFee(IDispatcher(_host).host());
87
+ uint256 minimumFee = 32 * pbf;
88
+ uint256 totalFee = request.fee + (pbf * request.context.length);
89
+ return minimumFee > totalFee ? minimumFee : totalFee;
90
+ }
91
+
92
+ /**
93
+ * @dev returns the quoted fee in the native token for dispatching a GET request
94
+ */
95
+ function quoteNative(DispatchGet memory request) public view returns (uint256) {
96
+ uint256 fee = quote(request);
97
+ address _host = host();
98
+ address _uniswap = IDispatcher(_host).uniswapV2Router();
99
+ address[] memory path = new address[](2);
100
+ path[0] = IUniswapV2Router02(_uniswap).WETH();
101
+ path[1] = IDispatcher(_host).feeToken();
102
+ return IUniswapV2Router02(_uniswap).getAmountsIn(fee, path)[0];
103
+ }
104
+
105
+ /**
106
+ * @dev returns the quoted fee in the feeToken for dispatching a POST response
107
+ */
108
+ function quote(DispatchPostResponse memory response) public view returns (uint256) {
109
+ uint256 len = 32 > response.response.length ? 32 : response.response.length;
110
+ return response.fee + (len * IDispatcher(host()).perByteFee(response.request.source));
111
+ }
112
+
113
+ /**
114
+ * @dev returns the quoted fee in the native token for dispatching a POST response
115
+ */
116
+ function quoteNative(DispatchPostResponse memory request) public view returns (uint256) {
117
+ uint256 fee = quote(request);
118
+ address _host = host();
119
+ address _uniswap = IDispatcher(_host).uniswapV2Router();
120
+ address[] memory path = new address[](2);
121
+ path[0] = IUniswapV2Router02(_uniswap).WETH();
122
+ path[1] = IDispatcher(_host).feeToken();
123
+ return IUniswapV2Router02(_uniswap).getAmountsIn(fee, path)[0];
124
+ }
125
+
126
+ /**
127
+ * @notice Callback for receiving incoming POST requests
128
+ * @dev Called by the Host when a new POST request is received for this app.
129
+ * Override this method to implement request handling logic. The default implementation reverts.
130
+ */
131
+ function onAccept(IncomingPostRequest calldata) external virtual onlyHost {
132
+ revert UnexpectedCall();
133
+ }
134
+
135
+ /**
136
+ * @notice Callback for handling POST request timeouts
137
+ * @dev Called by the Host when a previously dispatched POST request has timed out.
138
+ * Override this method to implement cleanup or retry logic. The default implementation reverts.
139
+ */
140
+ function onPostRequestTimeout(PostRequest memory) external virtual onlyHost {
141
+ revert UnexpectedCall();
142
+ }
143
+
144
+ /**
145
+ * @notice Callback for receiving POST responses
146
+ * @dev Called by the Host when a response is received for a previously dispatched POST request.
147
+ * Override this method to process response data. The default implementation reverts.
148
+ */
149
+ function onPostResponse(IncomingPostResponse memory) external virtual onlyHost {
150
+ revert UnexpectedCall();
151
+ }
152
+
153
+ /**
154
+ * @notice Callback for handling POST response timeouts
155
+ * @dev Called by the Host when a POST response that was sent has timed out.
156
+ * Override this method to handle response timeout scenarios. The default implementation reverts.
157
+ */
158
+ function onPostResponseTimeout(PostResponse memory) external virtual onlyHost {
159
+ revert UnexpectedCall();
160
+ }
161
+
162
+ /**
163
+ * @notice Callback for receiving GET responses
164
+ * @dev Called by the Host when a response is received for a previously dispatched GET request.
165
+ * Override this method to process the retrieved state data. The default implementation reverts.
166
+ */
167
+ function onGetResponse(IncomingGetResponse memory) external virtual onlyHost {
168
+ revert UnexpectedCall();
169
+ }
170
+
171
+ /**
172
+ * @notice Callback for handling GET request timeouts
173
+ * @dev Called by the Host when a previously dispatched GET request has timed out.
174
+ * Override this method to handle GET timeout scenarios. The default implementation reverts.
175
+ */
176
+ function onGetTimeout(GetRequest memory) external virtual onlyHost {
177
+ revert UnexpectedCall();
178
+ }
179
+ }
@@ -0,0 +1,72 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
17
+
18
+ /**
19
+ * @title HyperFungibleToken
20
+ * @notice An cross-chain fungible token implementation that can only be minted and burned by a designated gateway address.
21
+ * @dev This abstract contract extends OpenZeppelin's ERC20 implementation and adds gateway-restricted minting and burning capabilities.
22
+ * It is designed to be used in cross-chain bridge scenarios where a trusted gateway manages token supply.
23
+ * Derived contracts must implement the gateway() function to return their specific gateway address.
24
+ */
25
+ abstract contract HyperFungibleToken is ERC20 {
26
+ /// @notice Custom error thrown when a non-gateway address attempts to mint or burn
27
+ error OnlyGateway();
28
+
29
+ /**
30
+ * @notice Restricts function access to only the gateway address
31
+ * @dev Reverts with OnlyGateway error if called by any address other than the gateway
32
+ */
33
+ modifier onlyGateway() {
34
+ if (msg.sender != gateway()) revert OnlyGateway();
35
+ _;
36
+ }
37
+
38
+ /**
39
+ * @notice Initializes the token with a name and symbol
40
+ * @param name The name of the token
41
+ * @param symbol The symbol of the token
42
+ */
43
+ constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
44
+
45
+ /**
46
+ * @notice Returns the gateway address
47
+ * @dev Must be implemented by derived contracts to return their specific gateway address
48
+ * The `gateway` should be an immutable contract that will never change.
49
+ * @return The address of the gateway contract
50
+ */
51
+ function gateway() public view virtual returns (address);
52
+
53
+ /**
54
+ * @notice Mints tokens to the specified account
55
+ * @dev Can only be called by the gateway address
56
+ * @param to The address that will receive the minted tokens
57
+ * @param amount The amount of tokens to mint
58
+ */
59
+ function mint(address to, uint256 amount) external onlyGateway {
60
+ _mint(to, amount);
61
+ }
62
+
63
+ /**
64
+ * @notice Burns tokens from the specified account
65
+ * @dev Can only be called by the gateway address. The account must have sufficient balance.
66
+ * @param from The address from which tokens will be burned
67
+ * @param amount The amount of tokens to burn
68
+ */
69
+ function burn(address from, uint256 amount) external onlyGateway {
70
+ _burn(from, amount);
71
+ }
72
+ }
@@ -0,0 +1,98 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ import {PostRequest, PostResponse, GetResponse, GetRequest} from "../libraries/Message.sol";
17
+
18
+ /**
19
+ * @title IncomingPostRequest
20
+ * @notice Encapsulates an incoming POST request with relayer information
21
+ * @dev Used by the Host to deliver POST requests to applications
22
+ */
23
+ struct IncomingPostRequest {
24
+ // The Post request containing source, dest, nonce, from, to, timeout, and body
25
+ PostRequest request;
26
+ // Relayer responsible for delivering the request
27
+ address relayer;
28
+ }
29
+
30
+ /**
31
+ * @title IncomingPostResponse
32
+ * @notice Encapsulates an incoming POST response with relayer information
33
+ * @dev Used by the Host to deliver POST responses to applications
34
+ */
35
+ struct IncomingPostResponse {
36
+ // The Post response containing the original request, response data, and timeout
37
+ PostResponse response;
38
+ // Relayer responsible for delivering the response
39
+ address relayer;
40
+ }
41
+
42
+ /**
43
+ * @title IncomingGetResponse
44
+ * @notice Encapsulates an incoming GET response with relayer information
45
+ * @dev Used by the Host to deliver GET responses containing state data to applications
46
+ */
47
+ struct IncomingGetResponse {
48
+ // The Get response containing the request and retrieved state values
49
+ GetResponse response;
50
+ // Relayer responsible for delivering the response
51
+ address relayer;
52
+ }
53
+
54
+ /**
55
+ * @title IApp
56
+ * @author Polytope Labs (hello@polytope.technology)
57
+ * @notice Interface for cross-chain applications built on Hyperbridge
58
+ * @dev Applications must implement this interface to receive and handle cross-chain messages.
59
+ * The Host calls these methods to deliver messages, responses, and timeout notifications.
60
+ * All methods are permissioned and should only be callable by the Host contract.
61
+ */
62
+ interface IApp {
63
+ /**
64
+ * @dev Called by the `Host` to notify an app of a new request the app may choose to respond immediately, or in a later block
65
+ * @param incoming post request
66
+ */
67
+ function onAccept(IncomingPostRequest memory incoming) external;
68
+
69
+ /**
70
+ * @dev Called by the `Host` to notify an app of a post response to a previously sent out request
71
+ * @param incoming post response
72
+ */
73
+ function onPostResponse(IncomingPostResponse memory incoming) external;
74
+
75
+ /**
76
+ * @dev Called by the `Host` to notify an app of a get response to a previously sent out request
77
+ * @param incoming get response
78
+ */
79
+ function onGetResponse(IncomingGetResponse memory incoming) external;
80
+
81
+ /**
82
+ * @dev Called by the `Host` to notify an app of post requests that were previously sent but have now timed-out
83
+ * @param request post request
84
+ */
85
+ function onPostRequestTimeout(PostRequest memory request) external;
86
+
87
+ /**
88
+ * @dev Called by the `Host` to notify an app of post responses that were previously sent but have now timed-out
89
+ * @param response post response
90
+ */
91
+ function onPostResponseTimeout(PostResponse memory response) external;
92
+
93
+ /**
94
+ * @dev Called by the `Host` to notify an app of get requests that were previously sent but have now timed-out
95
+ * @param request get request
96
+ */
97
+ function onGetTimeout(GetRequest memory request) external;
98
+ }
@@ -0,0 +1,86 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ /**
17
+ * @title StateCommitment
18
+ * @notice Represents a commitment to an intermediate state in the state machine
19
+ * @dev Contains metadata about the state machine including timestamp and merkle roots
20
+ */
21
+ struct StateCommitment {
22
+ /// @notice Unix timestamp of the state machine at the time of this commitment
23
+ /// @dev Used for calculating request timeouts and enforcing time-based logic
24
+ uint256 timestamp;
25
+ /// @notice Merkle root of the overlay trie containing all ISMP requests and responses
26
+ /// @dev Used to verify inclusion proofs for cross-chain messages
27
+ bytes32 overlayRoot;
28
+ /// @notice Merkle root of the state trie at the given block height
29
+ /// @dev Represents the complete state of the state machine at this height
30
+ bytes32 stateRoot;
31
+ }
32
+
33
+ /**
34
+ * @title StateMachineHeight
35
+ * @notice Uniquely identifies a specific height in a state machine
36
+ * @dev Consensus clients may track multiple concurrent state machines, hence the need for an identifier
37
+ */
38
+ struct StateMachineHeight {
39
+ /// @notice Unique identifier for the state machine (e.g., parachain ID, chain ID)
40
+ /// @dev Each blockchain or parachain in the network has a unique identifier
41
+ uint256 stateMachineId;
42
+ /// @notice Block height or number in the state machine
43
+ /// @dev Represents the sequential position in the blockchain
44
+ uint256 height;
45
+ }
46
+
47
+ /**
48
+ * @title IntermediateState
49
+ * @notice Represents an intermediate state in the state transition sequence of a state machine
50
+ * @dev Used to track finalized states that have been verified through consensus
51
+ */
52
+ struct IntermediateState {
53
+ /// @notice Unique identifier for the state machine
54
+ /// @dev Same as StateMachineHeight.stateMachineId
55
+ uint256 stateMachineId;
56
+ /// @notice Block height of this intermediate state
57
+ /// @dev The specific height at which this state was committed
58
+ uint256 height;
59
+ /// @notice The state commitment at this height
60
+ /// @dev Contains the timestamp and merkle roots for this state
61
+ StateCommitment commitment;
62
+ }
63
+
64
+ /**
65
+ * @title IConsensus
66
+ * @author Polytope Labs (hello@polytope.technology)
67
+ * @notice Interface for consensus verification in the Hyperbridge protocol
68
+ * @dev Consensus clients implement this interface to verify state transitions from different chains.
69
+ * The internals are intentionally opaque to the ISMP framework, allowing consensus mechanisms
70
+ * to evolve independently (e.g., GRANDPA for Polkadot, Sync Committee for Ethereum).
71
+ * Different consensus mechanisms can be plugged in as long as they conform to this interface.
72
+ */
73
+ interface IConsensus {
74
+ /**
75
+ * @notice Verifies a consensus proof and returns the updated consensus state
76
+ * @dev This function is called by the Handler to verify incoming consensus updates.
77
+ * The implementation details vary based on the consensus mechanism being verified.
78
+ * @param trustedState The current trusted consensus state (encoded based on consensus type)
79
+ * @param proof The consensus proof to be verified (e.g., validator signatures, merkle proofs)
80
+ * @return The new consensus state after verification (encoded)
81
+ * @return Array of newly finalized intermediate states that can be trusted
82
+ */
83
+ function verifyConsensus(bytes memory trustedState, bytes memory proof)
84
+ external
85
+ returns (bytes memory, IntermediateState[] memory);
86
+ }