-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnonUbi.sol
60 lines (52 loc) · 1.96 KB
/
AnonUbi.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "../interfaces/IProofOfHumanitySemaphore.sol";
contract AnonUbi {
error AnonUbi__WrongService();
// sha256("AnonUbi") % SNARK_FIELD_SIZE
uint256 constant SERVICE_NULLIFIER =
2599749192553526744101485407855232264006280871422148661884192113239190147728;
IProofOfHumanitySemaphore proofOfHumanitySemaphore;
mapping(uint256 => uint256) public lastClaimDateByUser;
mapping(address => uint256) public balanceByAddress;
constructor(IProofOfHumanitySemaphore _proofOfHumanitySemaphore) {
proofOfHumanitySemaphore = _proofOfHumanitySemaphore;
}
function claimUbiVerifiably(
bytes32 signal,
uint256 nullifierHash,
uint256 serviceNullifier,
uint256 externalNullifier,
uint256 identityProxy,
uint256[8] calldata nullifierConsistencyProof,
uint256[8] calldata semaphoreProof
) external {
proofOfHumanitySemaphore.verifyProof(
signal,
nullifierHash,
serviceNullifier,
externalNullifier,
identityProxy,
nullifierConsistencyProof,
semaphoreProof
);
if (serviceNullifier != SERVICE_NULLIFIER) {
revert AnonUbi__WrongService();
}
address recipient = address(bytes20(signal));
claimUbi(identityProxy, recipient);
}
function claimAllowance(uint256 userId) public view returns (uint256) {
uint256 lastClaimDate = lastClaimDateByUser[userId];
if (lastClaimDate == 0) {
// Never claimed before - grant one token
return 1e18;
}
// Otherwise allow one token per day
return (1e18 * (block.timestamp - lastClaimDate)) / (60 * 60 * 24);
}
function claimUbi(uint256 userId, address recipient) private {
balanceByAddress[recipient] += claimAllowance(userId);
lastClaimDateByUser[userId] = block.timestamp;
}
}