Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add conf request helper for forge #98

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/forge/ConfidentialRequest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Send confidential requests in Forge.
library ConfRequest {
/// Sends a confidential request; calls offchain function and onchain callback.
function sendConfRequest(address to, bytes memory data) internal returns (Status, bytes memory callbackResult) {
// offchain execution
(bool success, bytes memory suaveCalldata) = to.call(data);
if (!success) {
return (Status.FAILURE_OFFCHAIN, suaveCalldata);
}
suaveCalldata = abi.decode(suaveCalldata, (bytes));
// onchain callback
(success, callbackResult) = to.call(suaveCalldata);
if (!success) {
return (Status.FAILURE_ONCHAIN, callbackResult);
}
return (Status.SUCCESS, callbackResult);
}
}

enum Status {
SUCCESS,
FAILURE_OFFCHAIN,
FAILURE_ONCHAIN
}
20 changes: 20 additions & 0 deletions test/forge/ConfidentialRequest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../../src/Test.sol";
import {ConfRequest, Status} from "src/forge/ConfidentialRequest.sol";
import {NumberSuapp} from "../Forge.t.sol";

contract ConfRequestTest is Test, SuaveEnabled {
using ConfRequest for address;

NumberSuapp numberSuapp = new NumberSuapp();

function testConfRequest() public {
ctx.setConfidentialInputs(abi.encode(0x42));
(Status s,) = address(numberSuapp).sendConfRequest(abi.encodeWithSelector(NumberSuapp.setNumber.selector));
assertEq(uint256(s), uint256(Status.SUCCESS));
assertEq(numberSuapp.number(), 0x42);
}
}
Loading