Created
April 19, 2022 07:34
-
-
Save bitcoinwarrior1/953902cefc81c92b5b4c3541181242e2 to your computer and use it in GitHub Desktop.
A POC for a module that validates a user based on their crypto activity to prevent spam/scam accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Stop spammers/scammers with various accounts by providing a proof of balance and logging their address to prevent reuse. | |
* This is easy for normal users with crypto, but expensive for spammers/scammers as they will have to constantly credit their accounts once reported. | |
*/ | |
const request = require("superagent"); | |
module.exports = class CryptoAntiSpamValidator { | |
/* | |
* @param provider - the web3 provider | |
* @param message - the message that the user must sign to prove ownership of the account | |
* @param endpoint - where to send the payload | |
* @param address - the user's address | |
* */ | |
constructor(provider, message, endpoint, address) { | |
this.provider = provider; | |
this.message = message; | |
this.endpoint = endpoint; | |
this.address = address; | |
this.payload = { | |
address: address, | |
message: message, | |
signature: undefined | |
} | |
} | |
/* | |
* @dev check if the user has made a certain number of txs | |
* @param amount - the amount required | |
* @returns - true if they have enough txs else false | |
* */ | |
async checkHasMadeTransactions(amount) { | |
const txCount = await this.provider.getTransactionCount(this.address); | |
return txCount >= amount; | |
} | |
/* | |
* @dev check if the user has a certain amount or more of eth | |
* @param amount - the amount required | |
* @returns - true if the balance is >= the amount required | |
* */ | |
async getHasBalanceETH(amount) { | |
const balance = await this.provider.getBalance(this.address); | |
return balance.toNumber() >= amount; | |
} | |
/* | |
* @dev get the user to sign the payload to prove ownership of the account (assuming they meet the criteria) | |
* @returns - true if signature obtained else the error | |
* */ | |
async signMessageToProve() { | |
try { | |
this.payload.signature = await this.provider.sign(this.message); | |
return true; | |
} catch (e) { | |
return e; | |
} | |
} | |
/* | |
* @dev send the payload to the server for validation of the user | |
* @returns - the server response | |
* */ | |
sendPayload() { | |
if(this.payload.signature === undefined) throw "No signature in payload"; | |
return new Promise((res, reject) => { | |
request.post(this.endpoint, this.payload, (err, data) => { | |
if(err) reject(err); | |
res(data); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment