Skip to content

Commit

Permalink
Merge pull request #2 from bobanetwork/wsdt/captcha-example
Browse files Browse the repository at this point in the history
feat: add captcha contract
  • Loading branch information
mmontour1306 authored Jun 7, 2024
2 parents 3a8690e + aaa2663 commit e0e17e9
Showing 1 changed file with 72 additions and 0 deletions.
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");
}
}

0 comments on commit e0e17e9

Please sign in to comment.