Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
add secret in HTLC struct EVM(native/ERC20)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerses-asaturyan committed Dec 2, 2024
1 parent 0575ea4 commit 54fa373
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
25 changes: 15 additions & 10 deletions chains/evm/solidity/contracts/HashedTimeLockERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ contract LayerswapV8ERC20 {
uint256 amount;
/// @notice The hash of the secret required for redemption.
bytes32 hashlock;
/// @notice The secret required to redeem.
uint256 secret;
/// @notice The ERC20 token contract address.
address tokenContract;
/// @notice The timestamp after which the funds can be refunded.
uint48 timelock;
/// @notice Indicates whether the funds were claimed (redeemed or refunded).
bool claimed;
/// @notice Indicates whether the funds were claimed (redeemed(3) or refunded(2)).
uint8 claimed;
/// @notice The creator of the HTLC.
address payable sender;
/// @notice The recipient of the funds if conditions are met.
Expand Down Expand Up @@ -207,9 +209,10 @@ contract LayerswapV8ERC20 {
contracts[Id] = HTLC(
amount,
bytes32(bytes1(0x01)),
uint256(1),
tokenContract,
timelock,
false,
uint8(1),
payable(msg.sender),
payable(srcReceiver)
);
Expand Down Expand Up @@ -244,7 +247,7 @@ contract LayerswapV8ERC20 {
uint48 timelock
) external _exists(Id) returns (bytes32) {
HTLC storage htlc = contracts[Id];
if (htlc.claimed) revert AlreadyClaimed();
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed();
if (timelock < block.timestamp) revert NotFutureTimelock();
if (msg.sender == htlc.sender) {
if (htlc.hashlock == bytes32(bytes1(0x01))) {
Expand Down Expand Up @@ -275,7 +278,7 @@ contract LayerswapV8ERC20 {
) external _exists(message.Id) returns (bytes32) {
if (verifyMessage(message, r, s, v)) {
HTLC storage htlc = contracts[message.Id];
if (htlc.claimed) revert AlreadyClaimed();
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed();
if (message.timelock < block.timestamp) revert NotFutureTimelock();
if (htlc.hashlock == bytes32(bytes1(0x01))) {
htlc.hashlock = message.hashlock;
Expand Down Expand Up @@ -328,9 +331,10 @@ contract LayerswapV8ERC20 {
contracts[Id] = HTLC(
amount,
hashlock,
uint256(1),
tokenContract,
timelock,
false,
uint8(1),
payable(msg.sender),
payable(srcReceiver)
);
Expand Down Expand Up @@ -364,9 +368,10 @@ contract LayerswapV8ERC20 {

if (htlc.hashlock != sha256(abi.encodePacked(secret)))
revert HashlockNotMatch(); // Ensure secret matches hashlock.
if (htlc.claimed) revert AlreadyClaimed();
if (htlc.claimed == 3 || htlc.claimed == 2) revert AlreadyClaimed();

htlc.claimed = true;
htlc.claimed = 3;
htlc.secret = secret;
IERC20(htlc.tokenContract).safeTransfer(htlc.srcReceiver, htlc.amount);
emit TokenRedeemed(Id, msg.sender, secret, htlc.hashlock);
return true;
Expand All @@ -378,10 +383,10 @@ contract LayerswapV8ERC20 {
/// @return bool Returns `true` if the refund is successful.
function refund(bytes32 Id) external _exists(Id) returns (bool) {
HTLC storage htlc = contracts[Id];
if (htlc.claimed) revert AlreadyClaimed(); // Prevent refund if already redeemed.
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed(); // Prevent refund if already redeemed or refunded.
if (htlc.timelock > block.timestamp) revert NotPassedTimelock(); // Ensure timelock has passed.

htlc.claimed = true;
htlc.claimed = 2;
IERC20(htlc.tokenContract).safeTransfer(htlc.sender, htlc.amount);
emit TokenRefunded(Id);
return true;
Expand Down
25 changes: 15 additions & 10 deletions chains/evm/solidity/contracts/HashedTimeLockEther.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ contract LayerswapV8 {
uint256 amount;
/// @notice The hash of the secret required for redeem.
bytes32 hashlock;
/// @notice The secret required to redeem.
uint256 secret;
/// @notice The creator of the HTLC.
address payable sender;
/// @notice The recipient of the funds if conditions are met.
address payable srcReceiver;
/// @notice The timestamp after which the funds can be refunded.
uint48 timelock;
/// @notice Indicates whether the funds were claimed (redeemed or refunded).
bool claimed;
/// @notice Indicates whether the funds were claimed (redeemed(3) or refunded (2)).
uint8 claimed;
}

/// @dev Represents the details required to add a lock, used as part of the `addLockSig` parameters.
Expand Down Expand Up @@ -186,10 +188,11 @@ contract LayerswapV8 {
contracts[Id] = HTLC(
msg.value,
bytes32(bytes1(0x01)),
uint256(1),
payable(msg.sender),
payable(srcReceiver),
timelock,
false
uint8(1)
);

// Emit the commit event.
Expand All @@ -215,10 +218,10 @@ contract LayerswapV8 {
/// @return bool Returns `true` if the refund is successful.
function refund(bytes32 Id) external _exists(Id) returns (bool) {
HTLC storage htlc = contracts[Id];
if (htlc.claimed) revert AlreadyClaimed(); // Prevent refund if already redeemed or refunded.
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed(); // Prevent refund if already redeemed or refunded.
if (htlc.timelock > block.timestamp) revert NotPassedTimelock(); // Ensure timelock has passed.

htlc.claimed = true;
htlc.claimed = 2;
(bool success, ) = htlc.sender.call{value: htlc.amount}("");
if (!success) revert TransferFailed(); // Ensure transfer succeeds.
emit TokenRefunded(Id);
Expand All @@ -237,7 +240,7 @@ contract LayerswapV8 {
uint48 timelock
) external _exists(Id) returns (bytes32) {
HTLC storage htlc = contracts[Id];
if (htlc.claimed) revert AlreadyClaimed();
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed();
if (timelock < block.timestamp) revert NotFutureTimelock();
if (msg.sender == htlc.sender) {
if (htlc.hashlock == bytes32(bytes1(0x01))) {
Expand Down Expand Up @@ -268,7 +271,7 @@ contract LayerswapV8 {
) external _exists(message.Id) returns (bytes32) {
if (verifyMessage(message, r, s, v)) {
HTLC storage htlc = contracts[message.Id];
if (htlc.claimed) revert AlreadyClaimed();
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed();
if (message.timelock < block.timestamp) revert NotFutureTimelock();
if (htlc.hashlock == bytes32(bytes1(0x01))) {
htlc.hashlock = message.hashlock;
Expand Down Expand Up @@ -310,10 +313,11 @@ contract LayerswapV8 {
contracts[Id] = HTLC(
msg.value,
hashlock,
uint256(1),
payable(msg.sender),
srcReceiver,
timelock,
false
uint8(1)
);
emit TokenLocked(
Id,
Expand Down Expand Up @@ -343,9 +347,10 @@ contract LayerswapV8 {

if (htlc.hashlock != sha256(abi.encodePacked(secret)))
revert HashlockNotMatch(); // Ensure secret matches hashlock.
if (htlc.claimed) revert AlreadyClaimed();
if (htlc.claimed == 3 || htlc.claimed == 2) revert AlreadyClaimed();

htlc.claimed = true;
htlc.claimed = 3;
htlc.secret = secret;
(bool success, ) = htlc.srcReceiver.call{value: htlc.amount}("");
if (!success) revert TransferFailed();
emit TokenRedeemed(Id, msg.sender, secret, htlc.hashlock);
Expand Down
4 changes: 2 additions & 2 deletions chains/evm/solidity/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
accounts: [process.env.PRIV_KEY],
},
arbitrumSepolia: {
url: 'https://arbitrum-sepolia.drpc.org',
url: 'https://arbitrum-sepolia.infura.io/v3/2d3e18b5f66f40df8d5df3d990d6d941',
accounts: [process.env.PRIV_KEY],
},
sepolia: {
Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports = {
},
},
{
network: "unicahinSepolia",
network: "unichainSepolia",
chainId: 1301,
urls: {
apiURL: "https://sepolia.uniscan.xyz/api",
Expand Down
6 changes: 3 additions & 3 deletions chains/evm/solidity/scripts/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function signHTLC() {
name: "LayerswapV8",
version: "1",
chainId: 11155111,
verifyingContract: "0x76fdbD22FD91059c8ff28197b4E30132EBd190b8",
verifyingContract: "0x89a21B772dD0Ec4a118d0823C5dD0C87dDbC5749",
salt: "0x2e4ff7169d640efc0d28f2e302a56f1cf54aff7e127eededda94b3df0946f5c0"
};

Expand Down Expand Up @@ -35,9 +35,9 @@ async function signHTLC() {
};

const message = {
Id: "0x2b8de560e0a112f53117d64ee7c28d14acaf8573e2796329878e006f7d778500",
Id: "0x3e958a2cdd5d2a505c622a0b16149cfc44e5526c7b89fd8d14850b3cb2366e56",
hashlock: "0x3b7674662e6569056cef73dab8b7809085a32beda0e8eb9e9b580cfc2af22a55",
timelock: 1732884938,
timelock: 1733139766,
};

const privateKey = process.env.PRIV_KEY;
Expand Down

0 comments on commit 54fa373

Please sign in to comment.