/src/qt/proofofimage.cpp

https://github.com/carsenk/denarius · C++ · 151 lines · 114 code · 26 blank · 11 comment · 9 complexity · 2d1f092e75a3743449bf00efd618463f MD5 · raw file

  1. #include "proofofimage.h"
  2. #include "ui_proofofimage.h"
  3. #include "bitcoinunits.h"
  4. #include "guiutil.h"
  5. #include "guiconstants.h"
  6. #include "hash.h"
  7. #include "base58.h"
  8. #include "key.h"
  9. #include "util.h"
  10. #include "init.h"
  11. #include "wallet.h"
  12. #include "walletdb.h"
  13. #include <QScrollArea>
  14. #include <QUrl>
  15. #include <QFileDialog>
  16. #include <QDesktopServices>
  17. #include <fstream>
  18. ProofOfImage::ProofOfImage(QWidget *parent) :
  19. QWidget(parent),
  20. ui(new Ui::ProofOfImage)
  21. {
  22. ui->setupUi(this);
  23. fileName = "";
  24. }
  25. ProofOfImage::~ProofOfImage()
  26. {
  27. delete ui;
  28. }
  29. void ProofOfImage::on_filePushButton_clicked()
  30. {
  31. fileName = QFileDialog::getOpenFileName(this,
  32. tr("Open File"), "./", tr("All Files (*.*)"));
  33. ui->labelFile->setText(fileName);
  34. }
  35. void ProofOfImage::on_createPushButton_clicked()
  36. {
  37. if(fileName == "")
  38. {
  39. noImageSelected();
  40. return;
  41. }
  42. //read whole file
  43. std::ifstream imageFile;
  44. imageFile.open(fileName.toStdString().c_str(), std::ios::binary);
  45. std::vector<char> imageContents((std::istreambuf_iterator<char>(imageFile)),
  46. std::istreambuf_iterator<char>());
  47. //hash file
  48. uint256 imagehash = SerializeHash(imageContents);
  49. CKeyID keyid(Hash160(imagehash.begin(), imagehash.end()));
  50. CBitcoinAddress baddr = CBitcoinAddress(keyid);
  51. std::string addr = baddr.ToString();
  52. ui->lineEdit->setText(QString::fromStdString(addr));
  53. CAmount nAmount = 0.001 * COIN; // 0.001 D Fee
  54. // Wallet comments
  55. CWalletTx wtx;
  56. wtx.mapValue["comment"] = fileName.toStdString();
  57. std::string sNarr = ui->edit->text().toStdString();
  58. wtx.mapValue["to"] = "Proof of Data";
  59. if (pwalletMain->IsLocked())
  60. {
  61. QMessageBox unlockbox;
  62. unlockbox.setText("Error, Your wallet is locked! Please unlock your wallet!");
  63. unlockbox.exec();
  64. ui->txLineEdit->setText("ERROR: Your wallet is locked! Cannot send proof of data. Unlock your wallet!");
  65. }
  66. else if(pwalletMain->GetBalance() < 0.001)
  67. {
  68. QMessageBox error2box;
  69. error2box.setText("Error, You need at least 0.001 D to send proof of data!");
  70. error2box.exec();
  71. ui->txLineEdit->setText("ERROR: You need at least a 0.001 D balance to send proof of data.");
  72. }
  73. else
  74. {
  75. //std::string sNarr;
  76. std::string strError = pwalletMain->SendMoneyToDestination(baddr.Get(), nAmount, sNarr, wtx);
  77. if(strError != "")
  78. {
  79. QMessageBox infobox;
  80. infobox.setText(QString::fromStdString(strError));
  81. infobox.exec();
  82. }
  83. QMessageBox successbox;
  84. successbox.setText("Proof of Data, Successful!");
  85. successbox.exec();
  86. ui->txLineEdit->setText(QString::fromStdString(wtx.GetHash().GetHex()));
  87. }
  88. }
  89. void ProofOfImage::on_checkButton_clicked()
  90. {
  91. if(fileName == "")
  92. {
  93. noImageSelected();
  94. return;
  95. }
  96. //read whole file
  97. std::ifstream imageFile;
  98. imageFile.open(fileName.toStdString().c_str(), std::ios::binary);
  99. std::vector<char> imageContents((std::istreambuf_iterator<char>(imageFile)),
  100. std::istreambuf_iterator<char>());
  101. //hash file
  102. uint256 imagehash = SerializeHash(imageContents);
  103. CKeyID keyid(Hash160(imagehash.begin(), imagehash.end()));
  104. std::string addr = CBitcoinAddress(keyid).ToString();
  105. //go to block explorer
  106. std::string bexp = "https://www.coinexplorer.net/D/address/";
  107. //open url
  108. QString link = QString::fromStdString(bexp + addr);
  109. QDesktopServices::openUrl(QUrl(link));
  110. }
  111. void ProofOfImage::on_checkTxButton_clicked()
  112. {
  113. //go to block explorer
  114. std::string bexp = "https://www.coinexplorer.net/D/transaction/";
  115. //open url
  116. QString link = QString::fromStdString(bexp + ui->txLineEdit->text().toStdString());
  117. QDesktopServices::openUrl(QUrl(link));
  118. }
  119. void ProofOfImage::noImageSelected()
  120. {
  121. //err message
  122. QMessageBox errorbox;
  123. errorbox.setText("No file selected!");
  124. errorbox.exec();
  125. }