Skip to content

Commit

Permalink
feat: use new DecryptionOracle with requestID generated by dApp
Browse files Browse the repository at this point in the history
  • Loading branch information
jatZama committed Dec 27, 2024
1 parent edb5d3d commit 1275b6d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
9 changes: 6 additions & 3 deletions decryption/DecryptionOracleCaller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IKMSVerifier {
}

interface IDecryptionOracle {
function requestDecryption(uint256[] calldata ctsHandles, bytes4 callbackSelector) external returns (uint256);
function requestDecryption(uint256 requestID, uint256[] calldata ctsHandles, bytes4 callbackSelector) external;
}

struct DecryptionOracleConfigStruct {
Expand All @@ -28,6 +28,8 @@ abstract contract DecryptionOracleCaller {
error InvalidKMSSignatures();
error UnsupportedHandleType();

uint256 internal counterRequest;
mapping(uint256 => uint256[]) private requestedHandles;
mapping(uint256 => ebool[]) private paramsEBool;
mapping(uint256 => euint4[]) private paramsEUint4;
mapping(uint256 => euint8[]) private paramsEUint8;
Expand All @@ -37,7 +39,6 @@ abstract contract DecryptionOracleCaller {
mapping(uint256 => eaddress[]) private paramsEAddress;
mapping(uint256 => address[]) private paramsAddress;
mapping(uint256 => uint256[]) private paramsUint256;
mapping(uint256 => uint256[]) private requestedHandles;

constructor() {}

Expand Down Expand Up @@ -199,11 +200,13 @@ abstract contract DecryptionOracleCaller {
uint256[] memory ctsHandles,
bytes4 callbackSelector
) internal returns (uint256 requestID) {
requestID = counterRequest;
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
IACL($.ACLAddress).allowForDecryption(ctsHandles);
DecryptionOracleConfigStruct storage $$ = getDecryptionOracleConfig();
requestID = IDecryptionOracle($$.DecryptionOracleAddress).requestDecryption(ctsHandles, callbackSelector);
IDecryptionOracle($$.DecryptionOracleAddress).requestDecryption(requestID, ctsHandles, callbackSelector);
saveRequestedHandles(requestID, ctsHandles);
counterRequest++;
}

/// @dev this function should be called inside the callback function the dApp contract to verify the signatures
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"ethers": "^6.8.0",
"fhevm-core-contracts": "0.7.0-1",
"fhevm-core-contracts": "0.7.0-2",
"fhevmjs": "^0.6.0-8",
"hardhat": "^2.22.10",
"hardhat-deploy": "^0.11.29",
Expand Down
25 changes: 15 additions & 10 deletions test/asyncDecrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ if (networkName === 'hardhat') {
const privKeyRelayer = process.env.PRIVATE_KEY_DECRYPTION_ORACLE_RELAYER;
relayer = new ethers.Wallet(privKeyRelayer!, ethers.provider);
}

const argEvents = '(uint256 indexed requestID, uint256[] cts, address contractCaller, bytes4 callbackSelector)';
const argEvents =
'(uint256 indexed counter, uint256 requestID, uint256[] cts, address contractCaller, bytes4 callbackSelector)';
const ifaceEventDecryption = new ethers.Interface(['event DecryptionRequest' + argEvents]);

let decryptionOracle: DecryptionOracle;
Expand All @@ -55,10 +55,15 @@ export const initDecryptionOracle = async (): Promise<void> => {
}
// this function will emit logs for every request and fulfilment of a decryption
decryptionOracle = await ethers.getContractAt('DecryptionOracle', parsedEnv.DECRYPTION_ORACLE_ADDRESS);
decryptionOracle.on('DecryptionRequest', async (requestID, cts, contractCaller, callbackSelector, eventData) => {
const blockNumber = eventData.log.blockNumber;
console.log(`${await currentTime()} - Requested decrypt on block ${blockNumber} (requestID ${requestID})`);
});
decryptionOracle.on(
'DecryptionRequest',
async (counter, requestID, cts, contractCaller, callbackSelector, eventData) => {
const blockNumber = eventData.log.blockNumber;
console.log(
`${await currentTime()} - Requested decrypt on block ${blockNumber} (counter ${counter} - requestID ${requestID})`,
);
},
);
};

export const awaitAllDecryptionResults = async (): Promise<void> => {
Expand Down Expand Up @@ -92,10 +97,10 @@ const fulfillAllPastRequestsIds = async (mocked: boolean) => {
const pastRequests = await ethers.provider.getLogs(filterDecryption);
for (const request of pastRequests) {
const event = ifaceEventDecryption.parseLog(request);
const requestID = event.args[0];
const handles = event.args[1];
const contractCaller = event.args[2];
const callbackSelector = event.args[3];
const requestID = event.args[1];
const handles = event.args[2];
const contractCaller = event.args[3];
const callbackSelector = event.args[4];
const typesList = handles.map((handle) => parseInt(handle.toString(16).slice(-4, -2), 16));
// if request is not already fulfilled
if (mocked && !toSkip.includes(requestID)) {
Expand Down

0 comments on commit 1275b6d

Please sign in to comment.