-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-deposit.sol
61 lines (49 loc) · 1.94 KB
/
simple-deposit.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract PrivateBalanceProxy {
mapping(bytes32 => uint256) private balances;
event Deposit(
address indexed sender,
bytes32 indexed recipientHash,
uint256 amount
);
event Withdrawal(address indexed recipient, uint256 amount);
/**
* @dev Allows a sender to deposit funds for a recipient using a hash.
* @param recipientHash The hashed recipient identifier.
*/
function deposit(bytes32 recipientHash) external payable {
require(msg.value > 0, "Deposit amount must be greater than zero");
balances[recipientHash] += msg.value;
emit Deposit(msg.sender, recipientHash, msg.value);
}
/**
* @dev Generates a hash for the recipient's address.
* Use this function to compute the recipientHash off-chain as well.
* @param recipient The recipient's address.
* @return The hash of the recipient's address.
*/
function hashAddress(address recipient) public pure returns (bytes32) {
return keccak256(abi.encodePacked(recipient));
}
/**
* @dev Allows a recipient to withdraw their funds using their address.
* @param recipient The address of the recipient.
*/
function withdraw(address recipient) external {
bytes32 recipientHash = keccak256(abi.encodePacked(recipient));
uint256 localAmount = balances[recipientHash];
require(localAmount > 0, "No balance to withdraw");
balances[recipientHash] = 0;
payable(recipient).transfer(localAmount);
emit Withdrawal(recipient, localAmount);
}
/**
* @dev Allows checking the balance associated with a recipient hash (debugging)
* @param recipientHash The hash of the recipient.
* @return The balance associated with the hash.
*/
function getBalance(bytes32 recipientHash) external view returns (uint256) {
return balances[recipientHash];
}
}