From 16904359993ccc3353f0c70118246473768919fa Mon Sep 17 00:00:00 2001 From: wsdt Date: Wed, 5 Jun 2024 11:53:35 +0200 Subject: [PATCH 1/3] feat: add captcha contract --- contracts/test/TestCaptcha.sol | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 contracts/test/TestCaptcha.sol diff --git a/contracts/test/TestCaptcha.sol b/contracts/test/TestCaptcha.sol new file mode 100644 index 0000000..f99db63 --- /dev/null +++ b/contracts/test/TestCaptcha.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity ^0.8.12; + +import "../samples/HybridAccount.sol"; + +contract TestCaptcha { + address payable immutable helperAddr; + + constructor(address payable _helperAddr) { + helperAddr = _helperAddr; + } + + function verifycaptcha(string calldata userAddress, string calldata uuid, string calldata captchaInput) public returns (bool) { + HybridAccount ha = HybridAccount(helperAddr); + + bytes memory req = abi.encodeWithSignature( + "verifyCaptcha(string,string,string)", + userAddress, + uuid, + captchaInput + ); + 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; + } +} From bbb3a6be7a6af907600ea14eb3b38a857ec8845b Mon Sep 17 00:00:00 2001 From: wsdt Date: Thu, 6 Jun 2024 11:17:46 +0200 Subject: [PATCH 2/3] feat: add withdraw, receive and getBalance to contract --- contracts/test/TestCaptcha.sol | 35 ++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/contracts/test/TestCaptcha.sol b/contracts/test/TestCaptcha.sol index f99db63..2819091 100644 --- a/contracts/test/TestCaptcha.sol +++ b/contracts/test/TestCaptcha.sol @@ -1,16 +1,39 @@ // 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 { +contract TestCaptcha is Ownable { address payable immutable helperAddr; + IERC20 public token; + + uint256 private constant SAFE_GAS_STIPEND = 6000; constructor(address payable _helperAddr) { helperAddr = _helperAddr; } - function verifycaptcha(string calldata userAddress, string calldata uuid, string calldata captchaInput) public returns (bool) { + 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( + string calldata userAddress, + string calldata uuid, + string calldata captchaInput + ) public returns (bool) { HybridAccount ha = HybridAccount(helperAddr); bytes memory req = abi.encodeWithSignature( @@ -30,4 +53,12 @@ contract TestCaptcha { (isVerified) = abi.decode(ret, (bool)); return isVerified; } + + function getBalances() + public + view + returns (uint256 nativeBalance) + { + nativeBalance = address(this).balance; + } } From aaa2663870f9c98184c4a4a03920e2fc2880341c Mon Sep 17 00:00:00 2001 From: "Riedl Kevin, Bsc" Date: Thu, 6 Jun 2024 15:34:45 +0200 Subject: [PATCH 3/3] Update TestCaptcha.sol --- contracts/test/TestCaptcha.sol | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/contracts/test/TestCaptcha.sol b/contracts/test/TestCaptcha.sol index 2819091..03b5e30 100644 --- a/contracts/test/TestCaptcha.sol +++ b/contracts/test/TestCaptcha.sol @@ -7,8 +7,12 @@ 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) { @@ -30,17 +34,17 @@ contract TestCaptcha is Ownable { } function verifycaptcha( - string calldata userAddress, - string calldata uuid, - string calldata captchaInput - ) public returns (bool) { + address _to, + bytes32 _uuid, + string memory _key + ) private returns (bool) { HybridAccount ha = HybridAccount(helperAddr); bytes memory req = abi.encodeWithSignature( "verifyCaptcha(string,string,string)", - userAddress, - uuid, - captchaInput + _to, + _uuid, + _key ); bytes32 userKey = bytes32(abi.encode(msg.sender)); (uint32 error, bytes memory ret) = ha.CallOffchain(userKey, req); @@ -54,11 +58,15 @@ contract TestCaptcha is Ownable { return isVerified; } - function getBalances() - public - view - returns (uint256 nativeBalance) - { - nativeBalance = address(this).balance; + 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"); } }