-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock the confidential datastore for Foundry #40
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bfaf2ff
Scaffold the solution+
ferranbt c875e32
Use mock confidential store
ferranbt 81a6cf3
Merge branch 'main' into feature/mock-conf-store
ferranbt bf2db7c
Remove view funcç
ferranbt 9e438e7
Clean PR
ferranbt e3b080b
Fix example
ferranbt bf8ccc9
Add signatures in a better way·
ferranbt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,106 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.8; | ||
|
||
import "../suavelib/Suave.sol"; | ||
import "forge-std/Test.sol"; | ||
|
||
contract ConfidentialStore is Test { | ||
mapping(bytes32 => Suave.DataRecord[]) private dataRecordsByConditionAndNamespace; | ||
mapping(Suave.DataId => mapping(string => bytes)) private dataRecordsContent; | ||
uint64 private numRecords; | ||
|
||
type DataId is bytes16; | ||
|
||
constructor() { | ||
vm.record(); | ||
} | ||
|
||
function newDataRecord( | ||
uint64 decryptionCondition, | ||
address[] memory allowedPeekers, | ||
address[] memory allowedStores, | ||
string memory dataType | ||
) public returns (Suave.DataRecord memory) { | ||
numRecords++; | ||
|
||
// Use a counter of the records to create a unique key | ||
Suave.DataId id = Suave.DataId.wrap(bytes16(keccak256(abi.encodePacked(numRecords)))); | ||
numRecords++; | ||
|
||
Suave.DataRecord memory newRecord; | ||
newRecord.id = id; | ||
newRecord.decryptionCondition = decryptionCondition; | ||
newRecord.allowedPeekers = allowedPeekers; | ||
newRecord.allowedStores = allowedStores; | ||
newRecord.version = dataType; | ||
|
||
// Use a composite index to store the records for the 'fetchDataRecords' function | ||
bytes32 key = keccak256(abi.encodePacked(decryptionCondition, dataType)); | ||
dataRecordsByConditionAndNamespace[key].push(newRecord); | ||
|
||
return newRecord; | ||
} | ||
|
||
function fetchDataRecords(uint64 cond, string memory namespace) public view returns (Suave.DataRecord[] memory) { | ||
bytes32 key = keccak256(abi.encodePacked(cond, namespace)); | ||
return dataRecordsByConditionAndNamespace[key]; | ||
} | ||
|
||
function confidentialStore(Suave.DataId dataId, string memory key, bytes memory value) public { | ||
dataRecordsContent[dataId][key] = value; | ||
} | ||
|
||
function confidentialRetrieve(Suave.DataId dataId, string memory key) public view returns (bytes memory) { | ||
return dataRecordsContent[dataId][key]; | ||
} | ||
|
||
function reset() public { | ||
(, bytes32[] memory writes) = vm.accesses(address(this)); | ||
for (uint256 i = 0; i < writes.length; i++) { | ||
vm.store(address(this), writes[i], 0); | ||
} | ||
} | ||
} | ||
|
||
contract ConfidentialStoreWrapper is Test { | ||
fallback() external { | ||
address confidentialStoreAddr = 0x0101010101010101010101010101010101010101; | ||
|
||
address addr = address(this); | ||
bytes4 sig; | ||
|
||
// You can use 'forge selectors list' to retrieve the function signatures | ||
if (addr == Suave.CONFIDENTIAL_STORE) { | ||
sig = 0xd22a3b0b; | ||
} else if (addr == Suave.CONFIDENTIAL_RETRIEVE) { | ||
sig = 0xe3b417bc; | ||
} else if (addr == Suave.FETCH_DATA_RECORDS) { | ||
sig = 0xccb885c4; | ||
} else if (addr == Suave.NEW_DATA_RECORD) { | ||
sig = 0xe3fbcfc3; | ||
} else { | ||
revert("function signature not found in the confidential store"); | ||
} | ||
|
||
bytes memory input = msg.data; | ||
|
||
// call 'confidentialStore' with the selector and the input data. | ||
(bool success, bytes memory output) = confidentialStoreAddr.call(abi.encodePacked(sig, input)); | ||
if (!success) { | ||
revert("Call to confidentialStore failed"); | ||
} | ||
|
||
if (addr == Suave.CONFIDENTIAL_RETRIEVE) { | ||
// special case we have to unloop the value from the abi | ||
// since it comes encoded as tuple() but we return the value normally | ||
// this was a special case that was not fixed yet in suave-geth. | ||
output = abi.decode(output, (bytes)); | ||
} | ||
|
||
assembly { | ||
let location := output | ||
let length := mload(output) | ||
return(add(location, 0x20), length) | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to add a license?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing is licensed for now. I will bring this up during weekly tomorrow.