-
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.
- Loading branch information
1 parent
2704332
commit f413211
Showing
2 changed files
with
60 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,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); | ||
} | ||
} |
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,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))); | ||
} | ||
} |