Skip to content

Commit

Permalink
add ConfKVS
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Aug 8, 2024
1 parent 2704332 commit f413211
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/ConfKVS.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions test/ConfKVS.t.sol
Original file line number Diff line number Diff line change
@@ -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)));
}
}

0 comments on commit f413211

Please sign in to comment.