-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create proof of liveness smart contract
- Loading branch information
1 parent
9e35108
commit d653425
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
packages/zevm-app-contracts/contracts/proof-of-liveness/ProofOfLiveness.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
contract ProofOfLiveness { | ||
// Mapping to track the last time the user proved liveness | ||
mapping(address => uint256) public lastProofTimestamp; | ||
|
||
// Custom error for when a user has already proved liveness within the last 24 hours | ||
error ProofWithinLast24Hours(uint256 lastProofTime); | ||
|
||
// Event to log when liveness is proved | ||
event LivenessProved(address indexed user, uint256 proofTimestamp); | ||
|
||
// The function to prove liveness, can only be called once every 24 hours | ||
function proveLiveness() external { | ||
uint256 currentTime = block.timestamp; | ||
uint256 lastProofTime = lastProofTimestamp[msg.sender]; | ||
|
||
// Check if the user has proved liveness within the last 24 hours | ||
if (currentTime < lastProofTime + 24 hours) { | ||
revert ProofWithinLast24Hours(lastProofTime); | ||
} | ||
|
||
// Update the last proof timestamp for the user | ||
lastProofTimestamp[msg.sender] = currentTime; | ||
|
||
// Emit an event to track the liveness proof | ||
emit LivenessProved(msg.sender, currentTime); | ||
} | ||
Check notice Code scanning / Slither Block timestamp Low
ProofOfLiveness.proveLiveness() uses timestamp for comparisons
Dangerous comparisons: - currentTime < lastProofTime + PROOF_PERIOD |
||
|
||
// Helper function to check if a user can prove liveness (returns true if 24 hours have passed) | ||
function canProveLiveness(address user) external view returns (bool) { | ||
uint256 currentTime = block.timestamp; | ||
return currentTime >= lastProofTimestamp[user] + 24 hours; | ||
} | ||
Check notice Code scanning / Slither Block timestamp Low
ProofOfLiveness.canProveLiveness(address) uses timestamp for comparisons
Dangerous comparisons: - currentTime >= proofHistory[user][0] + PROOF_PERIOD |
||
|
||
// View function to return the liveness proof status for the last 5 periods (each 24 hours long) | ||
function getLastFivePeriodsStatus(address user) external view returns (bool[5] memory) { | ||
uint256 currentTime = block.timestamp; | ||
uint256 lastProofTime = lastProofTimestamp[user]; | ||
|
||
bool[5] memory proofStatus; | ||
Check warning Code scanning / Slither Uninitialized local variables Medium
ProofOfLiveness.getLastFivePeriodsStatus(address).proofStatus is a local variable never initialized
|
||
uint256 periodDuration = 24 hours; | ||
|
||
for (uint256 i = 0; i < 5; i++) { | ||
// Calculate the start of the period (going back i * 24 hours) | ||
uint256 periodStart = currentTime - (i * periodDuration); | ||
uint256 periodEnd = periodStart - periodDuration; | ||
|
||
// If the last proof timestamp falls within this period, mark it as true | ||
if (lastProofTime >= periodEnd && lastProofTime < periodStart) { | ||
proofStatus[i] = true; | ||
} else { | ||
proofStatus[i] = false; | ||
} | ||
} | ||
|
||
return proofStatus; | ||
} | ||
Check notice Code scanning / Slither Block timestamp Low
ProofOfLiveness.getLastFivePeriodsStatus(address) uses timestamp for comparisons
Dangerous comparisons: - lastProofTime >= periodEnd && lastProofTime < periodStart |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/zevm-app-contracts/scripts/proof-of-liveness/deploy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { isProtocolNetworkName } from "@zetachain/protocol-contracts"; | ||
import { ethers, network } from "hardhat"; | ||
|
||
import { ProofOfLiveness__factory } from "../../typechain-types"; | ||
import { saveAddress } from "../address.helpers"; | ||
import { verifyContract } from "../explorer.helpers"; | ||
|
||
const networkName = network.name; | ||
|
||
const deployProofOfLiveness = async () => { | ||
if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); | ||
|
||
const ProofOfLivenessFactory = (await ethers.getContractFactory("ProofOfLiveness")) as ProofOfLiveness__factory; | ||
const ProofOfLiveness = await ProofOfLivenessFactory.deploy(); | ||
|
||
await ProofOfLiveness.deployed(); | ||
|
||
console.log("ProofOfLiveness deployed to:", ProofOfLiveness.address); | ||
|
||
saveAddress("ProofOfLiveness", ProofOfLiveness.address, networkName); | ||
|
||
await verifyContract(ProofOfLiveness.address, []); | ||
}; | ||
|
||
const main = async () => { | ||
if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); | ||
await deployProofOfLiveness(); | ||
}; | ||
|
||
main().catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |