-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add conf request helper for forge (#98)
* add conf request helper for forge * don't use * * loosen solidity version restriction on ConfRequest lib
- Loading branch information
1 parent
2704332
commit 5199304
Showing
2 changed files
with
47 additions
and
0 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
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 | ||
} |
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,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); | ||
} | ||
} |