Skip to content
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

[SC-1023] Secret check function #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions contracts/EscrowRegistry.sol → contracts/Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import { Clone } from "clones-with-immutable-args/Clone.sol";

import { SafeERC20 } from "@1inch/solidity-utils/contracts/libraries/SafeERC20.sol";

import { IEscrowFactory } from "./interfaces/IEscrowFactory.sol";
import { IEscrowRegistry } from "./interfaces/IEscrowRegistry.sol";
import { IEscrow } from "./interfaces/IEscrow.sol";

contract EscrowRegistry is Clone, IEscrowRegistry {
contract Escrow is Clone, IEscrow {
using SafeERC20 for IERC20;

function withdrawSrc(bytes32 secret) external {
IEscrowFactory.SrcEscrowImmutables memory escrowImmutables = srcEscrowImmutables();
SrcEscrowImmutables calldata escrowImmutables = srcEscrowImmutables();
uint256 finalityTimestamp = escrowImmutables.deployedAt + escrowImmutables.extraDataParams.srcTimelocks.finality;
if (
block.timestamp < finalityTimestamp ||
Expand All @@ -23,19 +22,19 @@ contract EscrowRegistry is Clone, IEscrowRegistry {

_checkSecretAndTransfer(
secret,
escrowImmutables.extraDataParams.hashlock,
escrowImmutables.interactionParams.taker,
escrowImmutables.interactionParams.srcToken,
escrowImmutables.interactionParams.srcAmount
);
}

function cancelSrc() external {
IEscrowFactory.SrcEscrowImmutables memory escrowImmutables = srcEscrowImmutables();
SrcEscrowImmutables calldata escrowImmutables = srcEscrowImmutables();
uint256 finalityTimestamp = escrowImmutables.deployedAt + escrowImmutables.extraDataParams.srcTimelocks.finality;
if (
block.timestamp < finalityTimestamp ||
block.timestamp < finalityTimestamp + escrowImmutables.extraDataParams.srcTimelocks.publicUnlock
) revert InvalidCancellationTime();
if (block.timestamp < finalityTimestamp + escrowImmutables.extraDataParams.srcTimelocks.publicUnlock) {
revert InvalidCancellationTime();
}

IERC20(escrowImmutables.interactionParams.srcToken).safeTransfer(
escrowImmutables.interactionParams.maker,
Expand All @@ -44,7 +43,7 @@ contract EscrowRegistry is Clone, IEscrowRegistry {
}

function withdrawDst(bytes32 secret) external {
IEscrowFactory.DstEscrowImmutables memory escrowImmutables = dstEscrowImmutables();
DstEscrowImmutables calldata escrowImmutables = dstEscrowImmutables();
uint256 finalityTimestamp = escrowImmutables.deployedAt + escrowImmutables.timelocks.finality;
uint256 unlockTimestamp = finalityTimestamp + escrowImmutables.timelocks.unlock;
if (
Expand All @@ -57,6 +56,7 @@ contract EscrowRegistry is Clone, IEscrowRegistry {
}
_checkSecretAndTransfer(
secret,
escrowImmutables.hashlock,
escrowImmutables.maker,
escrowImmutables.token,
escrowImmutables.amount
Expand All @@ -68,12 +68,11 @@ contract EscrowRegistry is Clone, IEscrowRegistry {
}

function cancelDst() external {
IEscrowFactory.DstEscrowImmutables memory escrowImmutables = dstEscrowImmutables();
DstEscrowImmutables calldata escrowImmutables = dstEscrowImmutables();
uint256 finalityTimestamp = escrowImmutables.deployedAt + escrowImmutables.timelocks.finality;
if (
block.timestamp < finalityTimestamp ||
block.timestamp < finalityTimestamp + escrowImmutables.timelocks.unlock + escrowImmutables.timelocks.publicUnlock
) revert InvalidCancellationTime();
if (block.timestamp < finalityTimestamp + escrowImmutables.timelocks.unlock + escrowImmutables.timelocks.publicUnlock) {
revert InvalidCancellationTime();
}

IERC20(escrowImmutables.token).safeTransfer(
escrowImmutables.taker,
Expand All @@ -86,33 +85,28 @@ contract EscrowRegistry is Clone, IEscrowRegistry {
);
}

function srcEscrowImmutables() public pure returns (IEscrowFactory.SrcEscrowImmutables memory) {
IEscrowFactory.SrcEscrowImmutables calldata data;
function srcEscrowImmutables() public pure returns (SrcEscrowImmutables calldata) {
SrcEscrowImmutables calldata data;
uint256 offset = _getImmutableArgsOffset();
// solhint-disable-next-line no-inline-assembly
assembly {
data := offset
}
assembly { data := offset }
return data;
}

function dstEscrowImmutables() public pure returns (IEscrowFactory.DstEscrowImmutables memory) {
IEscrowFactory.DstEscrowImmutables calldata data;
function dstEscrowImmutables() public pure returns (DstEscrowImmutables calldata) {
DstEscrowImmutables calldata data;
uint256 offset = _getImmutableArgsOffset();
// solhint-disable-next-line no-inline-assembly
assembly {
data := offset
}
assembly { data := offset }
return data;
}

function _isValidSecret(bytes32 /* secret */) internal view returns (bool) {
// TODO: Validate secret
return true;
function _isValidSecret(bytes32 secret, uint256 hashlock) internal pure returns (bool) {
return uint256(keccak256(abi.encode(secret))) == hashlock;
}

function _checkSecretAndTransfer(bytes32 secret, address recipient, address token, uint256 amount) internal {
if (!_isValidSecret(secret)) revert InvalidSecret();
function _checkSecretAndTransfer(bytes32 secret, uint256 hashlock, address recipient, address token, uint256 amount) internal {
if (!_isValidSecret(secret, hashlock)) revert InvalidSecret();
IERC20(token).safeTransfer(recipient, amount);
}
}
12 changes: 6 additions & 6 deletions contracts/EscrowFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SafeERC20 } from "@1inch/solidity-utils/contracts/libraries/SafeERC20.s
import { ClonesWithImmutableArgs } from "clones-with-immutable-args/ClonesWithImmutableArgs.sol";

import { IEscrowFactory } from "./interfaces/IEscrowFactory.sol";
import { EscrowRegistry } from "./EscrowRegistry.sol";
import { Escrow } from "./Escrow.sol";

contract EscrowFactory is IEscrowFactory {
using AddressLib for Address;
Expand All @@ -22,7 +22,7 @@ contract EscrowFactory is IEscrowFactory {

/// @dev Modifier to check if the caller is the limit order protocol contract.
modifier onlyLimitOrderProtocol {
if (msg.sender != address(LIMIT_ORDER_PROTOCOL)) revert OnlyLimitOrderProtocol();
if (msg.sender != LIMIT_ORDER_PROTOCOL) revert OnlyLimitOrderProtocol();
_;
}

Expand Down Expand Up @@ -70,7 +70,7 @@ contract EscrowFactory is IEscrowFactory {
if (
block.timestamp +
dstEscrowImmutables.timelocks.finality +
dstEscrowImmutables.timelocks.unlock +
dstEscrowImmutables.timelocks.unlock +
dstEscrowImmutables.timelocks.publicUnlock >
dstEscrowImmutables.srcCancellationTimestamp
) revert InvalidCreationTime();
Expand All @@ -88,7 +88,7 @@ contract EscrowFactory is IEscrowFactory {
dstEscrowImmutables.timelocks.publicUnlock
);
bytes32 salt = keccak256(abi.encodePacked(data, msg.sender));
EscrowRegistry escrow = _createEscrow(data, salt);
Escrow escrow = _createEscrow(data, salt);
IERC20(dstEscrowImmutables.token).safeTransferFrom(
msg.sender, address(escrow), dstEscrowImmutables.amount + dstEscrowImmutables.safetyDeposit
);
Expand All @@ -101,7 +101,7 @@ contract EscrowFactory is IEscrowFactory {
function _createEscrow(
bytes memory data,
bytes32 salt
) private returns (EscrowRegistry clone) {
clone = EscrowRegistry(IMPLEMENTATION.clone3(data, salt));
) private returns (Escrow clone) {
clone = Escrow(IMPLEMENTATION.clone3(data, salt));
}
}
58 changes: 58 additions & 0 deletions contracts/interfaces/IEscrow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IEscrow {
// TODO: is it possible to optimise this?
// Timelocks represent the duration of each period, in seconds
struct SrcTimelocks {
uint256 finality;
uint256 publicUnlock;
}

struct DstTimelocks {
uint256 finality;
uint256 unlock;
uint256 publicUnlock;
}
struct InteractionParams {
address maker;
address taker;
uint256 srcChainId;
address srcToken;
uint256 srcAmount;
uint256 dstAmount;
}

struct ExtraDataParams {
uint256 hashlock;
uint256 dstChainId;
address dstToken;
uint256 safetyDeposit;
SrcTimelocks srcTimelocks;
DstTimelocks dstTimelocks;
}

struct SrcEscrowImmutables {
uint256 deployedAt;
InteractionParams interactionParams;
ExtraDataParams extraDataParams;
}

struct DstEscrowImmutables {
uint256 deployedAt;
uint256 hashlock;
address maker;
address taker;
uint256 chainId;
address token;
uint256 amount;
uint256 safetyDeposit;
DstTimelocks timelocks;
}

error InvalidCaller();
error InvalidCancellationTime();
error InvalidSecret();
error InvalidWithdrawalTime();
}
55 changes: 4 additions & 51 deletions contracts/interfaces/IEscrowFactory.sol
Original file line number Diff line number Diff line change
@@ -1,65 +1,18 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.23;
pragma solidity ^0.8.0;

interface IEscrowFactory {
// TODO: is it possible to optimise this?
// Timelocks represent the duration of each period, in seconds
struct SrcTimelocks {
uint256 finality;
uint256 publicUnlock;
}

struct DstTimelocks {
uint256 finality;
uint256 unlock;
uint256 publicUnlock;
}

struct InteractionParams {
address maker;
address taker;
uint256 srcChainId;
address srcToken;
uint256 srcAmount;
uint256 dstAmount;
}

struct ExtraDataParams {
uint256 hashlock;
uint256 dstChainId;
address dstToken;
uint256 safetyDeposit;
SrcTimelocks srcTimelocks;
DstTimelocks dstTimelocks;
}

struct SrcEscrowImmutables {
uint256 deployedAt;
InteractionParams interactionParams;
ExtraDataParams extraDataParams;
}

struct DstEscrowImmutables {
uint256 deployedAt;
uint256 hashlock;
address maker;
address taker;
uint256 chainId;
address token;
uint256 amount;
uint256 safetyDeposit;
DstTimelocks timelocks;
}
import { IEscrow } from "./IEscrow.sol";

interface IEscrowFactory {
struct DstEscrowImmutablesCreation {
uint256 hashlock;
address maker;
address taker;
address token;
uint256 amount;
uint256 safetyDeposit;
DstTimelocks timelocks;
IEscrow.DstTimelocks timelocks;
uint256 srcCancellationTimestamp;
}

Expand Down
10 changes: 0 additions & 10 deletions contracts/interfaces/IEscrowRegistry.sol

This file was deleted.

Loading