Skip to content

feat: add captcha contract #2

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

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions contracts/test/TestCaptcha.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../samples/HybridAccount.sol";

contract TestCaptcha is Ownable {
address payable immutable helperAddr;
uint256 constant public nativeFaucetAmount = 0.01 ether;
uint256 constant public waitingPeriod = 1 days;
IERC20 public token;

mapping(address => uint256) public claimRecords;

uint256 private constant SAFE_GAS_STIPEND = 6000;

constructor(address payable _helperAddr) {
helperAddr = _helperAddr;
}

event Withdraw(address receiver, uint256 nativeAmount);

receive() external payable {}

function withdraw(uint256 _nativeAmount) public onlyOwner {
(bool sent, ) = msg.sender.call{
gas: SAFE_GAS_STIPEND,
value: _nativeAmount
}("");
require(sent, "Failed to send native Ether");

emit Withdraw(msg.sender, _nativeAmount);
}

function verifycaptcha(
address _to,
bytes32 _uuid,
string memory _key
) private returns (bool) {
HybridAccount ha = HybridAccount(helperAddr);

bytes memory req = abi.encodeWithSignature(
"verifyCaptcha(string,string,string)",
_to,
_uuid,
_key
);
bytes32 userKey = bytes32(abi.encode(msg.sender));
(uint32 error, bytes memory ret) = ha.CallOffchain(userKey, req);

if (error != 0) {
revert(string(ret));
}

bool isVerified;
(isVerified) = abi.decode(ret, (bool));
return isVerified;
}

function getTestnetETH(
bytes32 _uuid,
string memory _key,
address _to) external {
require(claimRecords[_to] + waitingPeriod <= block.timestamp, 'Invalid request');
require(verifyCaptcha(_to, _uuid, _key), "Invalid captcha");
claimRecords[_to] = block.timestamp;

(bool sent,) = (_to).call{gas: SAFE_GAS_STIPEND, value: nativeFaucetAmount}("");
require(sent, "Failed to send native");
}
}
Loading