From f41321193fb5e0ad0dcd5d0d6ea4c99c65270995 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Wed, 7 Aug 2024 19:26:08 -0700 Subject: [PATCH] add ConfKVS --- src/ConfKVS.sol | 34 ++++++++++++++++++++++++++++++++++ test/ConfKVS.t.sol | 26 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/ConfKVS.sol create mode 100644 test/ConfKVS.t.sol diff --git a/src/ConfKVS.sol b/src/ConfKVS.sol new file mode 100644 index 0000000..33a6baa --- /dev/null +++ b/src/ConfKVS.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.13; + +import {Suave} from "src/suavelib/Suave.sol"; + +struct ConfStore { + address[] allowedPeekers; + address[] allowedStores; + string namespace; +} + +struct ConfRecord { + Suave.DataId id; + string key; +} + +library ConfKVS { + /// Create a new data record and store the value. Data is available to consumers immediately. + function set(ConfStore memory cs, string memory key, bytes memory value) + internal + returns (ConfRecord memory confRecord) + { + Suave.DataRecord memory rec = Suave.newDataRecord( + 0, cs.allowedPeekers, cs.allowedStores, string(abi.encodePacked(cs.namespace, "::", key)) + ); + Suave.confidentialStore(rec.id, key, value); + confRecord = ConfRecord(rec.id, key); + } + + /// Retrieve the value from the data record. + function get(ConfRecord memory confRecord) internal returns (bytes memory) { + return Suave.confidentialRetrieve(confRecord.id, confRecord.key); + } +} diff --git a/test/ConfKVS.t.sol b/test/ConfKVS.t.sol new file mode 100644 index 0000000..d3880ef --- /dev/null +++ b/test/ConfKVS.t.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.9; + +import "forge-std/Test.sol"; +import "src/forge/ConfidentialStore.sol"; +import {SuaveEnabled} from "src/Test.sol"; +import "forge-std/console2.sol"; +import {ConfStore, ConfRecord, ConfKVS} from "src/ConfKVS.sol"; + +contract TestConfKVS is Test, SuaveEnabled { + using ConfKVS for ConfStore; + using ConfKVS for ConfRecord; + + // example initialization of ConfStore; allows any address to peek and store + address[] public addressList = [Suave.ANYALLOWED]; + ConfStore cs = ConfStore(addressList, addressList, "my_app_name"); + + /// set a confidential value and retrieve it, make sure the retrieved value matches what we set + function testSimpleConfStore() public { + string memory secretValue = "hello, suave!"; + ConfRecord memory cr = cs.set("secretMessage", abi.encodePacked(secretValue)); + + bytes memory value = cr.get(); + assertEq(keccak256(value), keccak256(abi.encodePacked(secretValue))); + } +}