Skip to content
7 changes: 5 additions & 2 deletions AddressUtil/AddressUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Base58 {

void toHash160(const std::string &s, unsigned int hash[5]);

bool isBase58(std::string s);
bool isBase58(const std::string &s);
};


Expand All @@ -32,4 +32,7 @@ namespace Hash {
};


#endif
#endif

EOF
)
34 changes: 22 additions & 12 deletions AddressUtil/Base58.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <map>
#include <array>
#include <algorithm>
#include "CryptoUtil.h"

#include "AddressUtil.h"
Expand All @@ -7,20 +8,21 @@
static const std::string BASE58_STRING = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

struct Base58Map {
static std::map<char, int> createBase58Map()
static std::array<int, 128> createBase58Map()
{
std::map<char, int> m;
std::array<int, 128> m{};
m.fill(-1);
for(int i = 0; i < 58; i++) {
m[BASE58_STRING[i]] = i;
m[(unsigned char)BASE58_STRING[i]] = i;
}

return m;
}

static std::map<char, int> myMap;
static std::array<int, 128> myMap;
};

std::map<char, int> Base58Map::myMap = Base58Map::createBase58Map();
std::array<int, 128> Base58Map::myMap = Base58Map::createBase58Map();



Expand All @@ -34,8 +36,12 @@ secp256k1::uint256 Base58::toBigInt(const std::string &s)
for(unsigned int i = 0; i < s.length(); i++) {
value = value.mul(58);

int c = Base58Map::myMap[s[i]];
value = value.add(c);
const unsigned char c = (unsigned char)s[i];
if(c >= Base58Map::myMap.size() || Base58Map::myMap[c] < 0) {
throw std::string("Invalid base58 character");
}
int valueChar = Base58Map::myMap[c];
value = value.add(valueChar);
}

return value;
Expand All @@ -54,10 +60,11 @@ void Base58::toHash160(const std::string &s, unsigned int hash[5])
}
}

bool Base58::isBase58(std::string s)
bool Base58::isBase58(const std::string &s)
{
for(unsigned int i = 0; i < s.length(); i++) {
if(BASE58_STRING.find(s[i]) < 0) {
const unsigned char c = (unsigned char)s[i];
if(c >= Base58Map::myMap.size() || Base58Map::myMap[c] < 0) {
return false;
}
}
Expand All @@ -68,18 +75,21 @@ bool Base58::isBase58(std::string s)
std::string Base58::toBase58(const secp256k1::uint256 &x)
{
std::string s;
s.reserve(50);

secp256k1::uint256 value = x;

while(!value.isZero()) {
secp256k1::uint256 digit = value.mod(58);
int digitInt = digit.toInt32();

s = BASE58_STRING[digitInt] + s;
s.push_back(BASE58_STRING[digitInt]);

value = value.div(58);
}

std::reverse(s.begin(), s.end());

return s;
}

Expand Down Expand Up @@ -114,4 +124,4 @@ void Base58::getMinMaxFromPrefix(const std::string &prefix, secp256k1::uint256 &

minValueOut = minValue;
maxValueOut = maxValue;
}
}
4 changes: 2 additions & 2 deletions AddressUtil/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool Address::verifyAddress(std::string address)
{
// Check length
if(address.length() > 34) {
false;
return false;
}

// Check encoding
Expand Down Expand Up @@ -185,4 +185,4 @@ void Hash::hashPublicKeyCompressed(const unsigned int *x, const unsigned int *y,
msg[15] = 0;

crypto::ripemd160(msg, digest);
}
}
23 changes: 14 additions & 9 deletions util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include<vector>
#include<set>
#include<algorithm>
#include<cinttypes>

#include"util.h"

Expand Down Expand Up @@ -57,7 +58,7 @@ namespace util {
{
char buf[32] = "";

sprintf(buf, "%lld", x);
sprintf(buf, "%" PRIu64, x);

std::string s(buf);

Expand Down Expand Up @@ -95,22 +96,26 @@ namespace util {
uint64_t val = 0;
bool isHex = false;

if(s[0] == '0' && s[1] == 'x') {
if(s.length() > 2 && s[0] == '0' && s[1] == 'x') {
isHex = true;
s = s.substr(2);
}

if(s[s.length() - 1] == 'h') {
if(s.length() > 0 && s[s.length() - 1] == 'h') {
isHex = true;
s = s.substr(0, s.length() - 1);
}

if(s.length() == 0) {
throw std::string("Expected an integer");
}

if(isHex) {
if(sscanf(s.c_str(), "%llx", &val) != 1) {
if(sscanf(s.c_str(), "%" SCNx64, &val) != 1) {
throw std::string("Expected an integer");
}
} else {
if(sscanf(s.c_str(), "%lld", &val) != 1) {
if(sscanf(s.c_str(), "%" SCNu64, &val) != 1) {
throw std::string("Expected an integer");
}
}
Expand All @@ -120,7 +125,7 @@ namespace util {

bool isHex(const std::string &s)
{
int len = 0;
int len = (int)s.length();

for(int i = 0; i < len; i++) {
char c = s[i];
Expand Down Expand Up @@ -235,11 +240,11 @@ namespace util {
return std::string(buf);
}

std::string format(uint64_t value)
std::string format(uint64_t value)
{
char buf[100] = { 0 };

sprintf(buf, "%lld", (uint64_t)value);
sprintf(buf, "%" PRIu64, value);

return std::string(buf);
}
Expand Down Expand Up @@ -295,4 +300,4 @@ namespace util {

return s.substr(left, right - left + 1);
}
}
}