11// SPDX-License-Identifier: MIT
2- pragma solidity ^ 0.8.24 ;
2+ pragma solidity ^ 0.8.27 ;
33
44import {FHE, externalEuint64, euint64} from "@fhevm/solidity/lib/FHE.sol " ;
55import {IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol " ;
66import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol " ;
77import {IERC7984 } from "../../interfaces/IERC7984.sol " ;
88
9- contract SwapConfidentialToERC20 {
10- error SwapConfidentialToERC20InvalidGatewayRequest ( uint256 requestId );
9+ contract SwapERC7984ToERC20 {
10+ error SwapERC7984ToERC20InvalidFinalization (euint64 amount );
1111
12- mapping (uint256 requestId = > address ) private _receivers;
12+ mapping (euint64 amount => address ) private _receivers;
1313 IERC7984 private _fromToken;
1414 IERC20 private _toToken;
1515
@@ -23,23 +23,21 @@ contract SwapConfidentialToERC20 {
2323 FHE.allowTransient (amount, address (_fromToken));
2424 euint64 amountTransferred = _fromToken.confidentialTransferFrom (msg .sender , address (this ), amount);
2525
26- bytes32 [] memory cts = new bytes32 [](1 );
27- cts[0 ] = euint64.unwrap (amountTransferred);
28- uint256 requestID = FHE.requestDecryption (cts, this .finalizeSwap.selector );
29-
30- // register who is getting the tokens
31- _receivers[requestID] = msg .sender ;
26+ FHE.makePubliclyDecryptable (amountTransferred);
27+ _receivers[amountTransferred] = msg .sender ;
3228 }
3329
34- function finalizeSwap (uint256 requestID , bytes calldata cleartexts , bytes calldata decryptionProof ) public virtual {
35- FHE.checkSignatures (requestID, cleartexts, decryptionProof);
36- uint64 amount = abi.decode (cleartexts, (uint64 ));
37- address to = _receivers[requestID];
38- require (to != address (0 ), SwapConfidentialToERC20InvalidGatewayRequest (requestID));
39- delete _receivers[requestID];
30+ function finalizeSwap (euint64 amount , uint64 cleartextAmount , bytes calldata decryptionProof ) public virtual {
31+ bytes32 [] memory handles = new bytes32 [](1 );
32+ handles[0 ] = euint64.unwrap (amount);
33+
34+ FHE.verifySignatures (handles, abi.encode (cleartextAmount), decryptionProof);
35+ address to = _receivers[amount];
36+ require (to != address (0 ), SwapERC7984ToERC20InvalidFinalization (amount));
37+ delete _receivers[amount];
4038
41- if (amount != 0 ) {
42- SafeERC20.safeTransfer (_toToken, to, amount );
39+ if (cleartextAmount != 0 ) {
40+ SafeERC20.safeTransfer (_toToken, to, cleartextAmount );
4341 }
4442 }
4543}
0 commit comments