Skip to content

Commit

Permalink
feat: Mainnet faucet with Captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdt committed Sep 24, 2023
1 parent a871966 commit dfe2f7d
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 466 deletions.
3 changes: 3 additions & 0 deletions boba_community/hc-captcha-mainnetfaucet-2023/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Boba Mainnet Faucet
Using ImageCaptcha

### Foundry commands
Use `--no-commit --no-git` for all commands.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REDIS_URL=localhost
REDIS_PORT=6379
IS_LOCAL=False
Empty file.
23 changes: 15 additions & 8 deletions boba_community/hc-captcha-mainnetfaucet-2023/api/hc_getCAPTCHA.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
from web3 import Web3
import uuid
import redis
import yaml
import os
from dotenv import load_dotenv
load_dotenv()

authorized_contract = None # for open access
REDIS_URL = os.environ.get('REDIS_URL') # is None if not found
REDIS_PORT = os.environ.get('REDIS_PORT') # is None if not found
IS_LOCAL = os.environ.get('IS_LOCAL') == 'True'

def hc_getCAPTCHA(event, context):

Expand All @@ -26,20 +32,19 @@ def hc_getCAPTCHA(event, context):
# Get base64
imageBase64 = base64.b64encode(imageData.getvalue())

# Read YML
with open("env.yml", 'r') as ymlfile:
config = yaml.load(ymlfile)

# Connect to Redis Elasticache
db = redis.StrictRedis(host=config.get('REDIS_URL'),
port=config.get('REDIS_PORT'))
db = redis.StrictRedis(host=REDIS_URL,
port=REDIS_PORT)

# Store and set the expire time as 10 minutes
#print('keyBytes: ', keyBytes, 'imageStrBytes: ', imageStrBytes, 'imageStr: ', imageStr)
db.set(keyBytes, imageStrBytes, ex=600)

payload = {'uuid': keyBytes, 'imageBase64': imageBase64.decode('utf-8') }

if IS_LOCAL:
payload['imageStr'] = str(imageStr)

response = {
"statusCode": 201,
"headers": {
Expand All @@ -52,7 +57,9 @@ def hc_getCAPTCHA(event, context):
"Referrer-Policy": "same-origin",
"Permissions-Policy": "*",
},
"body": json.dumps(payload)
"body": json.dumps({
"result": payload
})
}
#print(response)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import json
from captcha.image import ImageCaptcha
import base64
from web3 import Web3
import uuid
import redis
import yaml
import os
from dotenv import load_dotenv
load_dotenv()

REDIS_URL = os.environ.get('REDIS_URL') # is None if not found
REDIS_PORT = os.environ.get('REDIS_PORT') # is None if not found

def hc_verifyCAPTCHA(event, context):

body = json.loads(event["body"])

#print('FROM Geth', body)

paramsHexString = body['params'][0]

uuid = '0x' + paramsHexString[66: 130]
key = '0x' + paramsHexString[130:]

# Read YML
with open("env.yml", 'r') as ymlfile:
config = yaml.load(ymlfile)
uuid = '0x' + paramsHexString[2: 66]
key = '0x' + paramsHexString[66:]

# Connect to Redis Elasticache
db = redis.StrictRedis(host=config.get('REDIS_URL'),
port=config.get('REDIS_PORT'))
db = redis.StrictRedis(host=REDIS_URL,
port=REDIS_PORT)

# Store and set the expire time as 10 minutes
keyInRedis = db.get(uuid)

if keyInRedis:
print('keyInRedis: ', keyInRedis.decode('utf-8'), 'key: ', key)
#print('keyInRedis: ', keyInRedis.decode('utf-8'), 'key: ', key)
isMatch = keyInRedis.decode('utf-8') == key
return returnPayload(isMatch)
else:
Expand All @@ -46,7 +41,9 @@ def returnPayload(status):

returnPayload = {
'statusCode': 200,
'body': json.dumps({ 'result': payload })
'body':json.dumps({
"result": payload
})
}

#print('Return payload: ', payload)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python-dotenv
redis
web3>=5.29.2
captcha
56 changes: 0 additions & 56 deletions boba_community/hc-captcha-mainnetfaucet-2023/api/serverless.yml

This file was deleted.

25 changes: 25 additions & 0 deletions boba_community/hc-captcha-mainnetfaucet-2023/api/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MainnetFaucetGetCaptcha:
Type: AWS::Serverless::Function
Properties:
Handler: hc_getCAPTCHA.lambda_handler
Runtime: python3.9
Events:
HttpGet:
Type: Api
Properties:
Path: '/'
Method: post
MainnetFaucetVerifyCaptcha:
Type: AWS::Serverless::Function
Properties:
Handler: hc_verifyCAPTCHA.lambda_handler
Runtime: python3.9
Events:
HttpGet:
Type: Api
Properties:
Path: '/'
Method: post
12 changes: 0 additions & 12 deletions boba_community/hc-captcha-mainnetfaucet-2023/script/Counter.s.sol

This file was deleted.

This file was deleted.

Loading

0 comments on commit dfe2f7d

Please sign in to comment.