Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UST-196] [Contract] Check if reputation is negative and the epoch last claimed proof < 1 day #579

Closed
chentihe opened this issue Nov 27, 2024 · 1 comment · Fixed by #601
Assignees
Labels
contracts feature new feature test test improvements

Comments

@chentihe
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
目前還沒有機制去防止用戶一天只能 check in 一次,也沒有檢查用戶 reputation 是否為 negative

Describe the solution you'd like

  • 將 epoch key proof 改為 daily claim proof,claimDailyLoginRep 裡,除了驗證 epoch key 以外,還需要驗證 reputation < 0 與 用戶當天是否有領取reputation
  • 新增一個 daily epoch data 用來記錄以天為單位的 epoch 作為防止用戶重複領取每日簽到獎勵
contract UnirepApp is Ownable {
	struct DailyEpochData {
		uint48 startTimestamp; // setup while deploying
		uint48 currentEpoch;
		uint48 epochLength; // 60 * 60 * 24
	}

	DailyEpochData dailyEpochData;

    function _updateDailyEpochIfNeeded(
        uint256 attesterId
    ) public returns (uint epoch) {
        // 可參考 https://github.com/Unirep/Unirep/blob/36e5adf8c1eaa7398716ff3653327d5bd98181b3/packages/contracts/contracts/Unirep.sol#L406
    }

    function dailyCurrentEpoch() public view returns (uint48) {
        uint48 timestamp = dailyEpochData.startTimestamp;
        uint48 epochLength = dailyEpochData.epochLength;
        return (uint48(block.timestamp) - timestamp) / epochLength;
    }

    function claimDailyLoginRep(
        uint256[] calldata publicSignals,
        uint256[8] calldata proof <-- daily claim proof
    ) public onlyOwner() {
        _updateDailyEpochIfNeeded();
        
        // TODO: 需要將publicSignals轉換成 DailyClaimSignals
        DailyClaimVerifierHelper.DailyClaimSignals
            memory signals = dailyClaimHelper.decodeEpochKeySignals(
                publicSignals
            );
		    // TODO: check signals.dailyEpoch == dailyEpochData.currentEpoch;
		    
        // check if proof is used before
        bytes32 nullifier = signals.dailyNullifier;
        if (proofNullifier[nullifier]) {
            revert ProofHasUsed();
        }

        proofNullifier[nullifier] = true;

        // check the epoch != current epoch (ppl can only post in current aepoch)
        uint48 epoch = unirep.attesterCurrentEpoch(signals.attesterId);
        if (signals.epoch > epoch) {
            revert InvalidEpoch();
        }

        dailyClaimHelper.verifyAndCheckCaller(publicSignals, proof);
        
        require(signals.minRep - signals.maxRep < 0, "bad boy");

        // attesting on Unirep contract:
        unirep.attest(
            signals.epochKey,
            epoch,
            posRepFieldIndex, // field index: posRep
            1
        );

        emit ClaimPosRep(
            signals.epochKey,
            epoch
        );
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts feature new feature test test improvements
Projects
None yet
2 participants