-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-commit.sol
67 lines (55 loc) · 2.27 KB
/
simple-commit.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract PrivateTransfer {
mapping(bytes32 => uint256) private commitments;
event Commit(address indexed sender, bytes32 indexed commitmentHash); // for demonstration purposes
event Withdraw(address indexed recipient, uint256 amount); // for demonstration purposes
/**
* @dev Commit EUROe for a recipient using a hashed commitment.
* @param commitmentHash The hash representing the recipient, amount, and salt.
*/
function commitTransfer(bytes32 commitmentHash) external payable {
require(msg.value > 0, "Must send some EUROe");
require(commitments[commitmentHash] == 0, "Commitment already exists");
commitments[commitmentHash] = msg.value;
emit Commit(msg.sender, commitmentHash);
}
/**
* @dev Withdraw EUROe using the provided amount and salt.
* @param amount The amount of EUROe to withdraw.
* @param salt The salt used to generate the commitment hash.
*/
function withdraw(uint256 amount, uint256 salt) external {
bytes32 hash = keccak256(abi.encodePacked(msg.sender, amount, salt));
uint256 committedAmount = commitments[hash];
require(committedAmount > 0, "Invalid commitment or already withdrawn");
require(committedAmount == amount, "Amount mismatch");
commitments[hash] = 0;
payable(msg.sender).transfer(amount);
emit Withdraw(msg.sender, amount);
}
/**
* @dev Check the EUROe balance associated with a commitment hash (for debugging).
* @param commitmentHash The hash of the commitment.
* @return The amount of EUROe associated with the hash.
*/
function checkCommitment(
bytes32 commitmentHash
) external view returns (uint256) {
return commitments[commitmentHash];
}
/**
* @dev Utility function to calculate a commitment hash.
* @param to The recipient's address.
* @param amount The amount to commit.
* @param salt The unique salt for the commitment.
* @return The calculated commitment hash.
*/
function calculateHash(
address to,
uint256 amount,
uint256 salt
) public pure returns (bytes32) {
return keccak256(abi.encodePacked(to, amount, salt));
}
}