Advertisement
Guest User

PrivateKey.cpp

a guest
Jul 16th, 2021
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include "privatekey.h"
  2. #include "base58.h"
  3. #include "iostream"
  4. #include <sstream>
  5.  
  6. PrivateKey::PrivateKey()
  7. {
  8.  
  9. }
  10.  
  11. QString PrivateKey::generatePrivateKey()
  12. {
  13.     QString privKey { "0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d" };
  14.     QString mainnetAddr { prependVersion() + privKey };
  15.     QString checksum { appendChecksum(mainnetAddr) };
  16.     QString wif { wifKey(checksum) };
  17.  
  18.     qDebug() << "privKey with checksum : " << checksum;
  19.     qDebug() << "privKey final : " << wif;
  20.  
  21.     return wif;
  22. }
  23.  
  24. QString PrivateKey::generateSeed()
  25. {
  26.     std::string tmp_s;
  27.     static const char alphanum[] =
  28.         "0123456789"
  29.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  30.         "abcdefghijklmnopqrstuvwxyz"
  31.         ",?;.:/!\(){}[@]";
  32.  
  33.     srand( (unsigned) time(NULL) * getpid());
  34.  
  35.     tmp_s.reserve(256);
  36.  
  37.     for (int i = 0; i < 256; ++i)
  38.         tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
  39.  
  40.     return QCryptographicHash::hash(tmp_s.c_str(), QCryptographicHash::Sha256).toHex();
  41. }
  42.  
  43. QString PrivateKey::prependVersion()
  44. {
  45.     return "80";
  46. }
  47.  
  48. QString PrivateKey::appendChecksum(QString addr)
  49. {
  50.     QString hashPrivKey1 = QCryptographicHash::hash(hexToText(addr.toStdString()).c_str(), QCryptographicHash::Sha256).toHex();
  51.     QString hashPrivKey2 = QCryptographicHash::hash(hexToText(hashPrivKey1.toStdString()).c_str(), QCryptographicHash::Sha256).toHex();
  52.  
  53.     return addr + hashPrivKey2.mid(0, 8);
  54. }
  55.  
  56. QString PrivateKey::wifKey(QString addr)
  57. {
  58.     base58 wif;
  59.     return wif.encodeBase58(hexToText(addr.toStdString())).c_str();
  60. }
  61.  
  62. std::string PrivateKey::hexToText(std::string const input) {
  63.     if (input.empty() || input.size() % 2 != 0)
  64.         throw std::invalid_argument("invalid argument");
  65.  
  66.     std::string result, piece;
  67.  
  68.     for (auto it = input.begin(); it != input.end(); it += 2) {
  69.         piece = std::string(&(*it), 2);
  70.         result += std::stoi(piece, nullptr, 16);
  71.     }
  72.     return result;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement