2019-04-24 19:22:47 +03:00
|
|
|
#include <QByteArray>
|
2019-11-18 00:21:34 +02:00
|
|
|
#include <string>
|
2019-04-24 19:22:47 +03:00
|
|
|
#include <string.h>
|
2019-11-18 00:21:34 +02:00
|
|
|
#include "base58.h"
|
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
|
#include <iostream> // testing
|
|
|
|
|
|
2019-04-24 19:22:47 +03:00
|
|
|
namespace bitcoin {
|
2019-11-18 00:21:34 +02:00
|
|
|
bool TestBase58(bool silent, bool throws)
|
2019-04-24 19:22:47 +03:00
|
|
|
{
|
2019-11-18 00:21:34 +02:00
|
|
|
using Print = Debug;
|
2019-04-24 19:22:47 +03:00
|
|
|
std::vector<unsigned char> result;
|
|
|
|
|
const char *anAddress = "1C3SoftYBC2bbDzCadZxDrfbnobEXLBLQZ";
|
|
|
|
|
if (!bitcoin::DecodeBase58Check(anAddress, result)) {
|
2019-11-18 00:21:34 +02:00
|
|
|
static constexpr auto err = "Base58 decode check fail!";
|
|
|
|
|
if (throws) throw InternalError(err);
|
|
|
|
|
if (!silent) Print() << err;
|
2019-04-24 19:22:47 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
QByteArray ba;
|
|
|
|
|
ba.insert(0, reinterpret_cast<char *>(&result[0]), int(result.size()));
|
|
|
|
|
auto hexDecoded = ba.toHex();
|
2019-11-18 00:21:34 +02:00
|
|
|
if (!silent) Print() << anAddress << " -> " << hexDecoded << " (decoded)";
|
2019-04-24 19:22:47 +03:00
|
|
|
ba = QByteArray::fromHex("00791fc195e712c142df4c4e14fd4ec5b302733832");
|
|
|
|
|
result.resize(size_t(ba.length()));
|
|
|
|
|
memcpy(&result[0], ba.constData(), size_t(ba.length()));
|
|
|
|
|
auto str = bitcoin::EncodeBase58Check(result);
|
|
|
|
|
std::vector<unsigned char> result2;
|
|
|
|
|
if (!bitcoin::DecodeBase58Check(str, result2) || result2 != result) {
|
2019-11-18 00:21:34 +02:00
|
|
|
static constexpr auto err = "Base58 Decode -> Encode results differ! Fail!";
|
|
|
|
|
if (throws) throw InternalError(err);
|
|
|
|
|
if (!silent) Print() << err;
|
2019-04-24 19:22:47 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-11-18 00:21:34 +02:00
|
|
|
if (!silent) Print() << ba.toHex() << " -> " << str << " (encoded)";
|
2019-04-24 19:22:47 +03:00
|
|
|
bool ret = ba.toHex() == hexDecoded && str == std::string(anAddress);
|
2019-11-18 00:21:34 +02:00
|
|
|
if (!silent) Print() << (ret ? "Compare ok, success." : "Values differ -- ERROR!!");
|
2019-04-24 19:22:47 +03:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|