Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "privatekey.h"
- #include "base58.h"
- #include "iostream"
- #include <sstream>
- PrivateKey::PrivateKey()
- {
- }
- QString PrivateKey::generatePrivateKey()
- {
- QString privKey { "0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d" };
- QString mainnetAddr { prependVersion() + privKey };
- QString checksum { appendChecksum(mainnetAddr) };
- QString wif { wifKey(checksum) };
- qDebug() << "privKey with checksum : " << checksum;
- qDebug() << "privKey final : " << wif;
- return wif;
- }
- QString PrivateKey::generateSeed()
- {
- std::string tmp_s;
- static const char alphanum[] =
- "0123456789"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- ",?;.:/!\(){}[@]";
- srand( (unsigned) time(NULL) * getpid());
- tmp_s.reserve(256);
- for (int i = 0; i < 256; ++i)
- tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
- return QCryptographicHash::hash(tmp_s.c_str(), QCryptographicHash::Sha256).toHex();
- }
- QString PrivateKey::prependVersion()
- {
- return "80";
- }
- QString PrivateKey::appendChecksum(QString addr)
- {
- QString hashPrivKey1 = QCryptographicHash::hash(hexToText(addr.toStdString()).c_str(), QCryptographicHash::Sha256).toHex();
- QString hashPrivKey2 = QCryptographicHash::hash(hexToText(hashPrivKey1.toStdString()).c_str(), QCryptographicHash::Sha256).toHex();
- return addr + hashPrivKey2.mid(0, 8);
- }
- QString PrivateKey::wifKey(QString addr)
- {
- base58 wif;
- return wif.encodeBase58(hexToText(addr.toStdString())).c_str();
- }
- std::string PrivateKey::hexToText(std::string const input) {
- if (input.empty() || input.size() % 2 != 0)
- throw std::invalid_argument("invalid argument");
- std::string result, piece;
- for (auto it = input.begin(); it != input.end(); it += 2) {
- piece = std::string(&(*it), 2);
- result += std::stoi(piece, nullptr, 16);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement