Skip to content

Commit

Permalink
Merge pull request #188 from wqking/upstream-airdrop
Browse files Browse the repository at this point in the history
makeairdropfile: use simple algorithm to validate BSC address
  • Loading branch information
tohsnoom authored Aug 16, 2021
2 parents a9a3fd6 + e39b4a1 commit 468948e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,30 @@ UniValue bip38decrypt(const UniValue& params, bool fHelp)
return result;
}

bool isValidBscAddress(const std::string & bscAddress)
{
if(bscAddress.size() != 42) {
return false;
}
if(bscAddress[0] != '0' || bscAddress[1] != 'x') {
return false;
}
for(size_t i = 2; i < bscAddress.size(); ++i) {
const char c = bscAddress[i];
if(c >= '0' && c <= '9') {
continue;
}
if(c >= 'a' && c <= 'f') {
continue;
}
if(c >= 'A' && c <= 'F') {
continue;
}
return false;
}
return true;
}

UniValue makeairdropfile(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 3)
Expand All @@ -757,12 +781,17 @@ UniValue makeairdropfile(const UniValue& params, bool fHelp)

const std::string bscAddress = params[0].get_str();
const std::string airDropNumber = params[1].get_str();


/* seems regex causes regex_error on some compilers or OS, let's check it manually
std::regex re("^0x[0-9a-fA-F]{40}$");
std::smatch matchResult;
if(! std::regex_match(bscAddress, matchResult, re)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid BSC address format.");
}
*/
if(! isValidBscAddress(bscAddress)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid BSC address format.");
}

ofstream file;
file.open(params[2].get_str().c_str());
Expand Down

0 comments on commit 468948e

Please sign in to comment.