-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dndll
committed
Feb 14, 2024
1 parent
5342629
commit bfd57bd
Showing
22 changed files
with
419 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,12 @@ | |
[submodule "vendor/succinctx"] | ||
path = vendor/succinctx | ||
url = [email protected]:succinctlabs/succinctx.git | ||
[submodule "circuits/plonky2x/verifier/lib/openzeppelin-contracts-upgradeable"] | ||
path = circuits/plonky2x/verifier/lib/openzeppelin-contracts-upgradeable | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable | ||
[submodule "circuits/plonky2x/verifier/lib/openzeppelin-contracts"] | ||
path = circuits/plonky2x/verifier/lib/openzeppelin-contracts | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | ||
[submodule "circuits/plonky2x/verifier/lib/foundry-devops"] | ||
path = circuits/plonky2x/verifier/lib/foundry-devops | ||
url = https://github.com/chainaccelorg/foundry-devops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[profile.default] | ||
fs_permissions = [ { access = "read", path = "./broadcast" } ] | ||
libs = [ "lib" ] | ||
out = "out" | ||
remappings = [ "@openzeppelin/contracts=lib/openzeppelin-contracts/contracts", "@openzeppelin/contracts-upgradeable=lib/openzeppelin-contracts-upgradeable/contracts" ] | ||
src = "src" | ||
# See more config options https://github.com/foundry-rs/foundry/tree/master/config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.19; | ||
|
||
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
import {NearX} from "../src/NearX.sol"; | ||
import {Script} from "forge-std/Script.sol"; | ||
|
||
contract Deploy is Script { | ||
function setUp() public {} | ||
|
||
function run() external returns (address) { | ||
address proxy = deployNearX(); | ||
return proxy; | ||
} | ||
|
||
function deployNearX() public returns (address) { | ||
vm.startBroadcast(); | ||
|
||
NearX lightClient = new NearX(); | ||
|
||
ERC1967Proxy proxy = new ERC1967Proxy(address(lightClient), ""); | ||
|
||
lightClient.initialize(); | ||
|
||
vm.stopBroadcast(); | ||
return address(proxy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.19; | ||
|
||
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
import {NearX} from "../src/NearX.sol"; | ||
import {Script} from "forge-std/Script.sol"; | ||
import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol"; | ||
|
||
contract Initialise is Script { | ||
function setUp() public {} | ||
|
||
function run() external { | ||
address proxyAddress = DevOpsTools.get_most_recent_deployment( | ||
"ERC1967Proxy", | ||
block.chainid | ||
); | ||
vm.startBroadcast(); | ||
NearX lightClient = NearX(payable(proxyAddress)); | ||
|
||
address initialGateway = 0x6e4f1e9eA315EBFd69d18C2DB974EEf6105FB803; | ||
lightClient.updateGateway(initialGateway); | ||
|
||
bytes32 syncFunctionId = vm.envBytes32("SYNC_FUNCTION_ID"); | ||
lightClient.updateSyncId(syncFunctionId); | ||
|
||
bytes32 verifyFunctionId = vm.envBytes32("VERIFY_FUNCTION_ID"); | ||
lightClient.updateVerifyId(verifyFunctionId); | ||
|
||
bytes32 header = vm.envBytes32("NEAR_CHECKPOINT_HEADER_HASH"); | ||
lightClient.setCheckpointHeader(header); | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {Script} from "forge-std/Script.sol"; | ||
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol"; | ||
import {NearX} from "../src/NearX.sol"; | ||
|
||
contract Upgrade is Script { | ||
function run() external returns (address) { | ||
address mostRecentlyDeployedProxy = DevOpsTools | ||
.get_most_recent_deployment("ERC1967Proxy", block.chainid); | ||
vm.startBroadcast(); | ||
|
||
NearX newAddress = new NearX(); | ||
|
||
vm.stopBroadcast(); | ||
address proxy = upgrade(mostRecentlyDeployedProxy, address(newAddress)); | ||
return proxy; | ||
} | ||
|
||
function upgrade(address proxyAddress, address newAddress) | ||
public | ||
returns (address) | ||
{ | ||
vm.startBroadcast(); | ||
|
||
NearX proxy = NearX(payable(proxyAddress)); | ||
proxy.upgradeToAndCall(address(newAddress), ""); | ||
|
||
vm.stopBroadcast(); | ||
return address(proxy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.13; | ||
|
||
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | ||
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | ||
import {ISuccinctGateway} from "./interfaces/ISuccinctGateway.sol"; | ||
import {INearX, TransactionOrReceiptId, encodePackedIds, decodePackedIds} from "./interfaces/INearX.sol"; | ||
|
||
/// @notice The NearX contract is a light client for Near. | ||
contract NearX is INearX, Initializable, OwnableUpgradeable, UUPSUpgradeable { | ||
uint32 public constant DEFAULT_GAS_LIMIT = 1000000; | ||
|
||
/// @notice The address of the gateway contract. | ||
address public gateway; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize() public initializer { | ||
__Ownable_init(msg.sender); //sets owner to msg.sender | ||
__UUPSUpgradeable_init(); | ||
} | ||
|
||
function _authorizeUpgrade(address newImplementation) | ||
internal | ||
override | ||
onlyOwner | ||
{} | ||
|
||
/// @notice Sync function id. | ||
bytes32 public syncFunctionId; | ||
|
||
/// @notice Verify function id. | ||
bytes32 public verifyFunctionId; | ||
|
||
/// @notice The latest header that has been committed. | ||
bytes32 public latestHeader; | ||
|
||
function updateGateway(address _gateway) external onlyOwner { | ||
gateway = _gateway; | ||
} | ||
|
||
function updateSyncId(bytes32 _functionId) external onlyOwner { | ||
syncFunctionId = _functionId; | ||
} | ||
|
||
function updateVerifyId(bytes32 _functionId) external onlyOwner { | ||
verifyFunctionId = _functionId; | ||
} | ||
|
||
/// Note: Only for testnet. The genesis header should be set when initializing the contract. | ||
function setCheckpointHeader(bytes32 _header) external onlyOwner { | ||
latestHeader = _header; | ||
} | ||
|
||
function ensureInitialized() internal view { | ||
if (gateway == address(0)) { | ||
revert GatewayNotInitialised(); | ||
} | ||
if (syncFunctionId == bytes32(0) || verifyFunctionId == bytes32(0)) { | ||
revert FunctionIdsNotInitialised(); | ||
} | ||
if (latestHeader == bytes32(0)) { | ||
revert HeaderNotInitialised(); | ||
} | ||
} | ||
|
||
/// @notice Inputs of a sync request. | ||
function requestSync() external payable { | ||
ensureInitialized(); | ||
bytes memory context; | ||
|
||
ISuccinctGateway(gateway).requestCallback{value: msg.value}( | ||
syncFunctionId, | ||
abi.encodePacked(latestHeader), | ||
context, | ||
NearX.handleSync.selector, | ||
DEFAULT_GAS_LIMIT | ||
); | ||
|
||
emit SyncRequested(latestHeader); | ||
} | ||
|
||
function handleSync(bytes memory _output, bytes memory _context) external { | ||
if (msg.sender != gateway || !ISuccinctGateway(gateway).isCallback()) { | ||
revert NotFromSuccinctGateway(msg.sender); | ||
} | ||
|
||
bytes32 targetHeader = abi.decode(_output, (bytes32)); | ||
|
||
latestHeader = targetHeader; | ||
|
||
emit HeadUpdate(targetHeader); | ||
} | ||
|
||
function requestVerify(TransactionOrReceiptId[] memory ids) | ||
external | ||
payable | ||
{ | ||
ensureInitialized(); | ||
bytes memory context; | ||
bytes memory input = abi.encodePacked( | ||
latestHeader, | ||
encodePackedIds(ids) | ||
); | ||
|
||
ISuccinctGateway(gateway).requestCallback{value: msg.value}( | ||
verifyFunctionId, | ||
input, | ||
context, | ||
NearX.handleVerify.selector, | ||
DEFAULT_GAS_LIMIT | ||
); | ||
|
||
emit VerifyRequested(latestHeader, ids); | ||
} | ||
|
||
function handleVerify(bytes memory _output, bytes memory _context) | ||
external | ||
{ | ||
if (msg.sender != gateway || !ISuccinctGateway(gateway).isCallback()) { | ||
revert NotFromSuccinctGateway(msg.sender); | ||
} | ||
TransactionOrReceiptId[] memory ids = decodePackedIds(_output); | ||
emit VerifyResult(ids); | ||
} | ||
} |
Oops, something went wrong.