From 51993049f7bc13bf9c2af46f171cb1da16628998 Mon Sep 17 00:00:00 2001 From: brock smedley <2791467+zeroXbrock@users.noreply.github.com> Date: Mon, 5 Aug 2024 02:08:49 -0700 Subject: [PATCH] add conf request helper for forge (#98) * add conf request helper for forge * don't use * * loosen solidity version restriction on ConfRequest lib --- src/forge/ConfidentialRequest.sol | 27 +++++++++++++++++++++++++++ test/forge/ConfidentialRequest.t.sol | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/forge/ConfidentialRequest.sol create mode 100644 test/forge/ConfidentialRequest.t.sol diff --git a/src/forge/ConfidentialRequest.sol b/src/forge/ConfidentialRequest.sol new file mode 100644 index 0000000..a00b816 --- /dev/null +++ b/src/forge/ConfidentialRequest.sol @@ -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 +} diff --git a/test/forge/ConfidentialRequest.t.sol b/test/forge/ConfidentialRequest.t.sol new file mode 100644 index 0000000..fd57408 --- /dev/null +++ b/test/forge/ConfidentialRequest.t.sol @@ -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); + } +}