-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTrain.sol
More file actions
432 lines (394 loc) · 17.1 KB
/
Train.sol
File metadata and controls
432 lines (394 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// @@ @@@
// @@@
// @@@ @@ @@@@ @@@@@ @ @ @@@@@
// @@@@@@@@@ @@@@@@ @@@@ @@@@@ @@@ @@@@@@ @@@@
// @@@ @@@ @@@ @@@ @@@ @@@ @@@
// @@@ @@@ @@@ @@@ @@@ @@@ @@@
// @@@ @@@ @@@ @@@ @@@ @@@ @@@
// @@@ @@@ @@@@ @@@@@ @@@ @@@ @@@
// @@@@@ @@@ @@@@@@@@@ @@@ @@@ @@@ @@@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';
import '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
import '@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol';
import '@openzeppelin/contracts/utils/cryptography/EIP712.sol';
/// @title Train Contract
/// @notice Implements the Train protocol, enabling secure and atomic cross-chain swaps.
/// @dev Manages HTLCs for trustless cross-chain transactions with event-based updates.
contract Train is ReentrancyGuard, EIP712 {
using ECDSA for bytes32;
constructor() EIP712('Train', '1') {}
/// @dev Custom errors to simplify failure handling in the contract.
error FundsNotSent();
error NotPassedTimelock();
error HTLCAlreadyExists();
error HTLCNotExists();
error HashlockNotMatch();
error AlreadyClaimed();
error NoAllowance();
error InvalidSignature();
error HashlockAlreadySet();
error InvalidTimelock();
error InvaliRewardTimelock();
/// @dev Represents a hashed time-locked contract (HTLC) used in the Train protocol.
struct HTLC {
/// @notice The amount of funds locked in the HTLC.
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(3) or refunded (2)).
uint8 claimed;
}
/// @dev Represents the details required to add a lock, used as part of the `addLockSig` parameters.
struct addLockMsg {
/// @notice The identifier of the HTLC to which the hashlock should be added and the timelock updated.
bytes32 Id;
/// @notice The hashlock to be added to the HTLC.
bytes32 hashlock;
/// @notice The new timelock to be set for the HTLC.
uint48 timelock;
}
/// @dev Represents the reward details including the amount and the timelock for claiming the reward.
struct Reward {
/// @notice The amount of the reward in wei to be claimed.
uint256 amount;
/// @notice The timelock (timestamp) after which the reward can be claimed.
uint48 timelock;
}
/// @dev Emitted when an HTLC is created and funds are committed.
/// @param Id The unique identifier of the HTLC.
/// @param hopChains The sequence of chains forming the path from the source to the destination chain.
/// @param hopAssets The sequence of assets being swapped along the path.
/// @param hopAddresses The sequence of addresses involved along the path.
/// @param dstChain The destination blockchain.
/// @param dstAddress The recipient address on the destination chain.
/// @param dstAsset The asset on the destination chain.
/// @param sender The creator of the HTLC.
/// @param srcReceiver The recipient of the funds if conditions are met.
/// @param srcAsset The asset being locked.
/// @param amount The amount of funds locked in the HTLC.
/// @param timelock The timestamp after which the funds can be refunded.
event TokenCommitted(
bytes32 indexed Id,
string[] hopChains,
string[] hopAssets,
string[] hopAddresses,
string dstChain,
string dstAddress,
string dstAsset,
address indexed sender,
address indexed srcReceiver,
string srcAsset,
uint256 amount,
uint48 timelock
);
/// @dev Emitted when an HTLC is locked with a hashlock and timelock.
/// @param reward The reward amount (in wei) associated with the HTLC.
/// @param rewardTimelock The timelock (timestamp) after which the reward can be claimed.
event TokenLocked(
bytes32 indexed Id,
bytes32 hashlock,
string dstChain,
string dstAddress,
string dstAsset,
address indexed sender,
address indexed srcReceiver,
string srcAsset,
uint256 amount,
uint256 reward,
uint48 rewardTimelock,
uint48 timelock
);
/// @dev Emitted when a hashlock and timelock are added to an existing HTLC.
event TokenLockAdded(bytes32 indexed Id, bytes32 hashlock, uint48 timelock);
/// @dev Emitted when funds are refunded from an HTLC after the timelock expires.
event TokenRefunded(bytes32 indexed Id);
/// @dev Emitted when funds are redeemed from an HTLC using the correct secret.
event TokenRedeemed(bytes32 indexed Id, address redeemAddress, uint256 secret, bytes32 hashlock);
/// @dev Modifier to ensure HTLC exists before proceeding.
modifier _exists(bytes32 Id) {
if (!hasHTLC(Id)) revert HTLCNotExists();
_;
}
/// @dev Modifier to ensure the provided timelock is at least 15 minutes in the future.
modifier _validTimelock(uint48 timelock) {
if (block.timestamp + 900 > timelock) revert InvalidTimelock();
_;
}
/// @dev Storage for HTLCs
mapping(bytes32 => HTLC) private contracts;
/// @dev Storage for rewards on unclaimed HTLCs
mapping(bytes32 => Reward) private rewards;
/// @notice Creates and commits a new hashed time-locked contract (HTLC).
/// @dev Locks funds in the contract and emits a `TokenCommitted` event.
/// @param hopChains The sequence of chains forming the path from the source to the destination chain.
/// @param hopAssets The sequence of assets being swapped along the path.
/// @param hopAddresses The sequence of addresses involved along the path.
/// @param dstChain The destination blockchain.
/// @param dstAsset The asset on the destination chain.
/// @param dstAddress The recipient address on the destination chain.
/// @param srcAsset The asset being locked.
/// @param Id The unique identifier of the created HTLC.
/// @param srcReceiver The recipient of the funds if conditions are met.
/// @param timelock The timestamp after which the funds can be refunded.
/// @return bytes32 The unique identifier of the created HTLC.
function commit(
string[] calldata hopChains,
string[] calldata hopAssets,
string[] calldata hopAddresses,
string calldata dstChain,
string calldata dstAsset,
string calldata dstAddress,
string calldata srcAsset,
bytes32 Id,
address srcReceiver,
uint48 timelock
) external payable _validTimelock(timelock) nonReentrant returns (bytes32) {
// Ensure the generated ID does not already exist to prevent overwriting.
if (hasHTLC(Id)) revert HTLCAlreadyExists();
if (msg.value == 0) revert FundsNotSent(); // Ensure funds are sent.
// Store HTLC details.
contracts[Id] = HTLC(
msg.value,
bytes32(bytes1(0x01)),
uint256(1),
payable(msg.sender),
payable(srcReceiver),
timelock,
uint8(1)
);
// Emit the commit event.
emit TokenCommitted(
Id,
hopChains,
hopAssets,
hopAddresses,
dstChain,
dstAddress,
dstAsset,
msg.sender,
srcReceiver,
srcAsset,
msg.value,
timelock
);
return Id;
}
/// @notice Refunds the locked funds from an HTLC after the timelock expires.
/// @dev Can only be called if the HTLC exists and the timelock has passed. Emits a `TokenRefunded` event.
/// @param Id The unique identifier of the HTLC to be refunded.
/// @return bool Returns `true` if the refund is successful.
function refund(bytes32 Id) external _exists(Id) nonReentrant returns (bool) {
HTLC storage htlc = contracts[Id];
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 = 2;
if (rewards[Id].amount != 0) {
htlc.sender.call{ value: htlc.amount + rewards[Id].amount, gas: 10000 }('');
} else {
htlc.sender.call{ value: htlc.amount, gas: 10000 }('');
}
emit TokenRefunded(Id);
return true;
}
/// @notice Adds a hashlock and updates the timelock for an existing HTLC.
/// @dev Can only be called by the HTLC's creator if the HTLC exists and has not been claimed. Emits a `TokenLockAdded` event.
/// @param Id The unique identifier of the HTLC to update.
/// @param hashlock The hashlock to be added.
/// @param timelock The new timelock to be set.
/// @return bytes32 The updated HTLC identifier.
function addLock(
bytes32 Id,
bytes32 hashlock,
uint48 timelock
) external _exists(Id) _validTimelock(timelock) nonReentrant returns (bytes32) {
HTLC storage htlc = contracts[Id];
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed();
if (msg.sender == htlc.sender) {
if (htlc.hashlock == bytes32(bytes1(0x01))) {
htlc.hashlock = hashlock;
htlc.timelock = timelock;
} else {
revert HashlockAlreadySet(); // Prevent overwriting hashlock.
}
emit TokenLockAdded(Id, hashlock, timelock);
return Id;
} else {
revert NoAllowance(); // Ensure only allowed accounts can add a lock.
}
}
/// @notice Adds a hashlock and updates the timelock for an existing HTLC using a signed message.
/// @dev Verifies the provided signature and updates the HTLC if valid. Emits a `TokenLockAdded` event.
/// @param message The details of the lock to be added, including the HTLC ID, hashlock, and timelock.
/// @param r The `r` value of the ECDSA signature.
/// @param s The `s` value of the ECDSA signature.
/// @param v The `v` value of the ECDSA signature.
/// @return bytes32 The updated HTLC identifier.
function addLockSig(
addLockMsg calldata message,
bytes32 r,
bytes32 s,
uint8 v
) external _exists(message.Id) _validTimelock(message.timelock) nonReentrant returns (bytes32) {
HTLC storage htlc = contracts[message.Id];
bool verified = false;
if (htlc.sender.code.length == 0) {
verified = verifyMessage(message, r, s, v);
} else {
bytes memory signature = abi.encodePacked(r, s, v);
bytes32 digest = keccak256(abi.encodePacked('\x19\x01', _domainSeparatorV4(), hashMessage(message)));
verified = SignatureChecker.isValidERC1271SignatureNow(htlc.sender, digest, signature);
}
if (!verified) revert InvalidSignature();
if (htlc.claimed == 2 || htlc.claimed == 3) revert AlreadyClaimed();
if (htlc.hashlock == bytes32(bytes1(0x01))) {
htlc.hashlock = message.hashlock;
htlc.timelock = message.timelock;
} else {
revert HashlockAlreadySet();
}
emit TokenLockAdded(message.Id, message.hashlock, message.timelock);
return message.Id;
}
/// @notice Locks funds in a new hashed time-locked contract (HTLC).
/// @dev Creates an HTLC with the specified details and emits a `TokenLocked` event.
/// @param Id The unique identifier for the new HTLC.
/// @param hashlock The hash of the secret required for redeeming the HTLC.
/// @param reward The reward amount in wei granted to the caller of redeem.
/// @param rewardTimelock The timelock (timestamp) after which the reward can be claimed.
/// @param timelock The timestamp after which the funds can be refunded if not claimed.
/// @param srcReceiver The recipient of the funds if the HTLC is successfully redeemed.
/// @param srcAsset The asset being locked in the HTLC.
/// @param dstChain The destination blockchain for the swap.
/// @param dstAddress The recipient address on the destination chain.
/// @param dstAsset The asset on the destination chain.
/// @return bytes32 The unique identifier of the created HTLC.
function lock(
bytes32 Id,
bytes32 hashlock,
uint256 reward,
uint48 rewardTimelock,
uint48 timelock,
address payable srcReceiver,
string calldata srcAsset,
string calldata dstChain,
string calldata dstAddress,
string calldata dstAsset
) external payable nonReentrant returns (bytes32) {
if (hasHTLC(Id)) revert HTLCAlreadyExists();
if (msg.value <= reward || msg.value == 0) revert FundsNotSent();
if (block.timestamp + 1800 > timelock) revert InvalidTimelock();
if (rewardTimelock > timelock || rewardTimelock <= block.timestamp) revert InvaliRewardTimelock();
contracts[Id] = HTLC(
msg.value - reward,
hashlock,
uint256(1),
payable(msg.sender),
srcReceiver,
timelock,
uint8(1)
);
if (reward != 0) {
rewards[Id] = Reward(reward, rewardTimelock);
}
emit TokenLocked(
Id,
hashlock,
dstChain,
dstAddress,
dstAsset,
msg.sender,
srcReceiver,
srcAsset,
msg.value - reward,
reward,
rewardTimelock,
timelock
);
return Id;
}
/// @notice Redeems funds from an HTLC using the correct secret.
/// @dev Verifies the provided secret against the hashlock and transfers the funds to the recipient. Emits a `TokenRedeemed` event.
/// @param Id The unique identifier of the HTLC to be redeemed.
/// @param secret The secret value used to unlock the HTLC.
/// @return bool Returns `true` if the redemption is successful.
function redeem(bytes32 Id, uint256 secret) external _exists(Id) nonReentrant returns (bool) {
HTLC storage htlc = contracts[Id];
if (htlc.hashlock != sha256(abi.encodePacked(secret))) revert HashlockNotMatch(); // Ensure secret matches hashlock.
if (htlc.claimed == 3 || htlc.claimed == 2) revert AlreadyClaimed();
htlc.claimed = 3;
htlc.secret = secret;
Reward storage reward = rewards[Id];
if (reward.amount == 0) {
htlc.srcReceiver.call{ value: htlc.amount, gas: 10000 }('');
} else if (reward.timelock > block.timestamp) {
htlc.srcReceiver.call{ value: htlc.amount, gas: 10000 }('');
htlc.sender.call{ value: reward.amount, gas: 10000 }('');
} else {
if (msg.sender == htlc.srcReceiver) {
htlc.srcReceiver.call{ value: htlc.amount + reward.amount, gas: 10000 }('');
} else {
htlc.srcReceiver.call{ value: htlc.amount, gas: 10000 }('');
msg.sender.call{ value: reward.amount, gas: 10000 }('');
}
}
emit TokenRedeemed(Id, msg.sender, secret, htlc.hashlock);
return true;
}
/// @notice Retrieves the details of a specific HTLC.
/// @dev Returns the HTLC structure associated with the given identifier.
/// @param Id The unique identifier of the HTLC.
/// @return HTLC The details of the specified HTLC.
function getHTLCDetails(bytes32 Id) public view returns (HTLC memory) {
return contracts[Id];
}
/// @notice Fetches the reward details for a specific HTLC.
/// @dev Returns the reward amount (in wei) and the timelock after which it can be claimed.
/// @param Id The unique identifier of the HTLC.
/// @return Reward A struct with the reward amount and claimable timelock.
function getRewardDetails(bytes32 Id) public view returns (Reward memory) {
return rewards[Id];
}
/// @notice Generates a hash of the `addLockMsg` structure.
/// @dev Encodes and hashes the `addLockMsg` fields for use in EIP-712 signature verification.
/// @param message The `addLockMsg` structure containing the HTLC details to be hashed.
/// @return bytes32 The hashed representation of the `addLockMsg` structure.
function hashMessage(addLockMsg calldata message) private pure returns (bytes32) {
return
keccak256(
abi.encode(
keccak256('addLockMsg(bytes32 Id,bytes32 hashlock,uint48 timelock)'),
message.Id,
message.hashlock,
message.timelock
)
);
}
/// @notice Verifies that an EIP-712 message signature matches the sender of the specified HTLC.
/// @dev Combines the domain separator and the hashed message to create the digest, then verifies the signature.
/// @param message The `addLockMsg` structure containing the HTLC details.
/// @param r The `r` value of the ECDSA signature.
/// @param s The `s` value of the ECDSA signature.
/// @param v The `v` value of the ECDSA signature.
/// @return bool Returns `true` if the signature is valid and matches the sender of the HTLC.
function verifyMessage(addLockMsg calldata message, bytes32 r, bytes32 s, uint8 v) private view returns (bool) {
bytes32 digest = keccak256(abi.encodePacked('\x19\x01', _domainSeparatorV4(), hashMessage(message)));
return (ECDSA.recover(digest, v, r, s) == contracts[message.Id].sender);
}
/// @notice Checks whether an HTLC with the given Id exists.
/// @dev An HTLC exists if the sender address in its details is non-zero.
/// @param Id The unique identifier of the HTLC to check.
/// @return bool Returns `true` if the HTLC exists, otherwise `false`.
function hasHTLC(bytes32 Id) private view returns (bool) {
return (contracts[Id].sender != address(0));
}
}