Skip to content

Commit

Permalink
Added Recaptcha V2
Browse files Browse the repository at this point in the history
  • Loading branch information
praveen-kaia committed Dec 16, 2024
1 parent 8600ffe commit 106a53a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ KAIACHAIN_GITHUB_API_KEY=
KAIROS_RPC_URL=https://public-en-kairos.node.kaia.io
FAUCET_PRIVATE_KEY=
FAUCET_PUBLIC_KEY=
FAUCET_TRANSFER_VALUE=50
FAUCET_TRANSFER_VALUE=50
FAUCET_RECAPTCHA_SECRET=
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ app.get("/faucet/balance", async function (req, res) {
app.post("/faucet/run", async function (req, res) {
try {
let address = req.query.address || "";
let _gReCaptchaToken = req.body.recaptcha || "";
if(!address) {
throw new Error("Address is required")
}
let results = await faucetService.runFaucet(address);
let results = await faucetService.runFaucet(address, _gReCaptchaToken);
return res.status(200).json({ success: true, data: results})
} catch(err) {
console.log(err.message);
Expand Down
20 changes: 19 additions & 1 deletion services/faucetService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ let service = {};
const provider = new Web3.providers.HttpProvider(process.env.KAIROS_RPC_URL);
const web3 = new KlaytnWeb3(provider);

const validateRecaptchaV2 = async (_gReCaptchaToken) => {
let reCaptchaRes = await fetch("https://www.google.com/recaptcha/api/siteverify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `secret=${process.env.FAUCET_RECAPTCHA_SECRET}&response=${_gReCaptchaToken}`,
})
.then((reCaptchaRes) => reCaptchaRes.json());

logger.info(reCaptchaRes);
if(!(reCaptchaRes && reCaptchaRes.success)) {
throw new Error("Invalid Recaptcha");
}
}

async function clearOldCache() {
let entries = Object.entries(global.faucetCache);
for (let i = 0; i < entries.length; i++) {
Expand Down Expand Up @@ -37,8 +53,10 @@ service.getBalance = async (_address) => {
}
};

service.runFaucet = async (_address) => {
service.runFaucet = async (_address, _gReCaptchaToken) => {
try {
await validateRecaptchaV2(_gReCaptchaToken);

let isAddress = web3.utils.isAddress(_address);
if (isAddress) {
_address = _address.toLowerCase();
Expand Down

0 comments on commit 106a53a

Please sign in to comment.