From 970c130e7325c3a60844da476a83138170a2de23 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:36:28 -0700 Subject: [PATCH 1/3] add conf request helper for forge --- 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..fb8091f --- /dev/null +++ b/src/forge/ConfidentialRequest.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.8; + +// 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..1f085a1 --- /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 *; + + 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); + } +} From b419e7f5aa1f2547b91ef67f8f800f899023db0f Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:42:15 -0700 Subject: [PATCH 2/3] don't use * --- test/forge/ConfidentialRequest.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/forge/ConfidentialRequest.t.sol b/test/forge/ConfidentialRequest.t.sol index 1f085a1..fd57408 100644 --- a/test/forge/ConfidentialRequest.t.sol +++ b/test/forge/ConfidentialRequest.t.sol @@ -7,7 +7,7 @@ import {ConfRequest, Status} from "src/forge/ConfidentialRequest.sol"; import {NumberSuapp} from "../Forge.t.sol"; contract ConfRequestTest is Test, SuaveEnabled { - using ConfRequest for *; + using ConfRequest for address; NumberSuapp numberSuapp = new NumberSuapp(); From 6e2b7b1d11fd20742cc84ee3ac0dffc8a31d2c67 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:43:44 -0700 Subject: [PATCH 3/3] loosen solidity version restriction on ConfRequest lib --- src/forge/ConfidentialRequest.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/forge/ConfidentialRequest.sol b/src/forge/ConfidentialRequest.sol index fb8091f..a00b816 100644 --- a/src/forge/ConfidentialRequest.sol +++ b/src/forge/ConfidentialRequest.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8.8; +pragma solidity ^0.8.0; // Send confidential requests in Forge. library ConfRequest {